autofs_vfsops.c revision 1.8 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.8 2019/11/26 16:17:31 tkusumi 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
78 KASSERT(autofs_softc);
79 KASSERT(!autofs_softc->sc_dev_opened);
80
81 workqueue_destroy(autofs_tmo_wq);
82
83 struct autofs_softc *sc = autofs_softc;
84 autofs_softc = NULL;
85
86 cv_destroy(&sc->sc_cv);
87 mutex_destroy(&sc->sc_lock);
88
89 pool_destroy(&autofs_request_pool);
90 pool_destroy(&autofs_node_pool);
91
92 kmem_free(sc, sizeof(*sc));
93 }
94
95 static int
96 autofs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
97 {
98 struct autofs_args *args = data;
99 struct autofs_mount *amp = VFSTOAUTOFS(mp);
100 struct statvfs *sbp = &mp->mnt_stat;
101 int error;
102
103 if (mp->mnt_flag & MNT_UPDATE) {
104 if (amp == NULL)
105 return EIO;
106 autofs_flush(amp);
107 return 0;
108 }
109
110 if (!args)
111 return EINVAL;
112
113 if (mp->mnt_flag & MNT_GETARGS) {
114 if (amp == NULL)
115 return EIO;
116 error = copyoutstr(amp->am_from, args->from,
117 sizeof(amp->am_from), NULL);
118 if (error)
119 return error;
120 error = copyoutstr(amp->am_options, args->master_options,
121 sizeof(amp->am_options), NULL);
122 if (error)
123 return error;
124 error = copyoutstr(amp->am_prefix, args->master_prefix,
125 sizeof(amp->am_prefix), NULL);
126 return error;
127 }
128
129 if (amp != NULL)
130 return EBUSY;
131
132 /*
133 * Allocate the autofs mount.
134 */
135 amp = kmem_zalloc(sizeof(*amp), KM_SLEEP);
136 mp->mnt_data = amp;
137 amp->am_mp = mp;
138
139 /*
140 * Copy-in master_options string.
141 */
142 error = copyinstr(args->master_options, amp->am_options,
143 sizeof(amp->am_options), NULL);
144 if (error)
145 goto fail;
146
147 /*
148 * Copy-in master_prefix string.
149 */
150 error = copyinstr(args->master_prefix, amp->am_prefix,
151 sizeof(amp->am_prefix), NULL);
152 if (error)
153 goto fail;
154
155 /*
156 * Initialize the autofs mount.
157 */
158 mutex_init(&->am_lock, MUTEX_DEFAULT, IPL_NONE);
159 amp->am_last_ino = AUTOFS_ROOTINO;
160
161 mutex_enter(&->am_lock);
162 error = autofs_node_new(NULL, amp, ".", -1, &->am_root);
163 mutex_exit(&->am_lock);
164 if (error)
165 goto fail1;
166 KASSERT(amp->am_root->an_ino == AUTOFS_ROOTINO);
167
168 autofs_statvfs(mp, sbp);
169 vfs_getnewfsid(mp);
170
171 error = set_statvfs_info(path, UIO_USERSPACE, args->from, UIO_USERSPACE,
172 mp->mnt_op->vfs_name, mp, curlwp);
173 if (error)
174 goto fail1;
175 strlcpy(amp->am_from, sbp->f_mntfromname, sizeof(amp->am_from));
176 strlcpy(amp->am_on, sbp->f_mntonname, sizeof(amp->am_on));
177
178 return 0;
179
180 fail1:
181 mutex_destroy(&->am_lock);
182 fail:
183 mp->mnt_data = NULL;
184 kmem_free(amp, sizeof(*amp));
185 return error;
186 }
187
188 static int
189 autofs_unmount(struct mount *mp, int mntflags)
190 {
191 struct autofs_mount *amp = VFSTOAUTOFS(mp);
192 int error, flags;
193
194 flags = 0;
195 if (mntflags & MNT_FORCE)
196 flags |= FORCECLOSE;
197 error = vflush(mp, NULL, flags);
198 if (error) {
199 AUTOFS_WARN("vflush failed with error %d", error);
200 return error;
201 }
202
203 /*
204 * All vnodes are gone, and new one will not appear - so,
205 * no new triggerings.
206 */
207 for (;;) {
208 struct autofs_request *ar;
209 int dummy;
210 bool found;
211
212 found = false;
213 mutex_enter(&autofs_softc->sc_lock);
214 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
215 if (ar->ar_mount != amp)
216 continue;
217 ar->ar_error = ENXIO;
218 ar->ar_done = true;
219 ar->ar_in_progress = false;
220 found = true;
221 }
222 if (found == false) {
223 mutex_exit(&autofs_softc->sc_lock);
224 break;
225 }
226
227 cv_broadcast(&autofs_softc->sc_cv);
228 mutex_exit(&autofs_softc->sc_lock);
229
230 tsleep(&dummy, 0, "autofs_umount", hz);
231 }
232
233 mutex_enter(&->am_lock);
234 while (!RB_EMPTY(&->am_root->an_children)) {
235 struct autofs_node *anp;
236 /*
237 * Force delete all nodes when more than one level of
238 * directories are created via indirect map. Autofs doesn't
239 * support rmdir(2), thus this is the only way to get out.
240 */
241 anp = RB_MIN(autofs_node_tree, &->am_root->an_children);
242 while (!RB_EMPTY(&anp->an_children))
243 anp = RB_MIN(autofs_node_tree, &anp->an_children);
244 autofs_node_delete(anp);
245 }
246 autofs_node_delete(amp->am_root);
247 mp->mnt_data = NULL;
248 mutex_exit(&->am_lock);
249
250 mutex_destroy(&->am_lock);
251
252 kmem_free(amp, sizeof(*amp));
253
254 return 0;
255 }
256
257 static int
258 autofs_start(struct mount *mp, int flags)
259 {
260
261 return 0;
262 }
263
264 static int
265 autofs_root(struct mount *mp, struct vnode **vpp)
266 {
267 struct autofs_node *anp = VFSTOAUTOFS(mp)->am_root;
268 int error;
269
270 error = vcache_get(mp, &anp, sizeof(anp), vpp);
271 if (error)
272 return error;
273 error = vn_lock(*vpp, LK_EXCLUSIVE);
274 if (error) {
275 vrele(*vpp);
276 *vpp = NULLVP;
277 return error;
278 }
279
280 return 0;
281 }
282
283 static int
284 autofs_statvfs(struct mount *mp, struct statvfs *sbp)
285 {
286
287 sbp->f_bsize = S_BLKSIZE;
288 sbp->f_frsize = S_BLKSIZE;
289 sbp->f_iosize = 0;
290 sbp->f_blocks = 0;
291 sbp->f_bfree = 0;
292 sbp->f_bavail = 0;
293 sbp->f_bresvd = 0;
294 sbp->f_files = 0;
295 sbp->f_ffree = 0;
296 sbp->f_favail = 0;
297 sbp->f_fresvd = 0;
298
299 copy_statvfs_info(sbp, mp);
300
301 return 0;
302 }
303
304 static int
305 autofs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
306 {
307
308 return 0;
309 }
310
311 static int
312 autofs_loadvnode(struct mount *mp, struct vnode *vp,
313 const void *key, size_t key_len, const void **new_key)
314 {
315 struct autofs_node *anp;
316
317 KASSERT(key_len == sizeof(anp));
318 memcpy(&anp, key, key_len);
319 KASSERT(!anp->an_vnode);
320
321 vp->v_tag = VT_AUTOFS;
322 vp->v_type = VDIR;
323 vp->v_op = autofs_vnodeop_p;
324 vp->v_data = anp;
325
326 if (anp->an_ino == AUTOFS_ROOTINO)
327 vp->v_vflag |= VV_ROOT;
328
329 anp->an_vnode = vp;
330 uvm_vnp_setsize(vp, 0);
331
332 *new_key = &vp->v_data;
333
334 return 0;
335 }
336
337 static const struct vnodeopv_desc * const autofs_vnodeopv_descs[] = {
338 &autofs_vnodeop_opv_desc,
339 NULL,
340 };
341
342 static struct vfsops autofs_vfsops = {
343 .vfs_name = MOUNT_AUTOFS,
344 .vfs_min_mount_data = sizeof(struct autofs_args),
345 .vfs_mount = autofs_mount,
346 .vfs_start = autofs_start,
347 .vfs_unmount = autofs_unmount,
348 .vfs_root = autofs_root,
349 .vfs_quotactl = (void *)eopnotsupp,
350 .vfs_statvfs = autofs_statvfs,
351 .vfs_sync = autofs_sync,
352 .vfs_vget = (void *)eopnotsupp,
353 .vfs_loadvnode = (void *)autofs_loadvnode,
354 .vfs_newvnode = (void *)eopnotsupp,
355 .vfs_fhtovp = (void *)eopnotsupp,
356 .vfs_vptofh = (void *)eopnotsupp,
357 .vfs_init = autofs_init,
358 .vfs_reinit = (void *)eopnotsupp,
359 .vfs_done = autofs_done,
360 .vfs_mountroot = (void *)eopnotsupp,
361 .vfs_snapshot = (void *)eopnotsupp,
362 .vfs_extattrctl = (void *)eopnotsupp,
363 .vfs_suspendctl = (void *)genfs_suspendctl,
364 .vfs_renamelock_enter = (void *)eopnotsupp,
365 .vfs_renamelock_exit = (void *)eopnotsupp,
366 .vfs_fsync = (void *)eopnotsupp,
367 .vfs_opv_descs = autofs_vnodeopv_descs
368 };
369
370 #define AUTOFS_SYSCTL_DEBUG 1
371 #define AUTOFS_SYSCTL_MOUNT_ON_STAT 2
372 #define AUTOFS_SYSCTL_TIMEOUT 3
373 #define AUTOFS_SYSCTL_CACHE 4
374 #define AUTOFS_SYSCTL_RETRY_ATTEMPTS 5
375 #define AUTOFS_SYSCTL_RETRY_DELAY 6
376 #define AUTOFS_SYSCTL_INTERRUPTIBLE 7
377
378 static struct sysctllog *autofs_sysctl_log;
379
380 static int
381 autofs_sysctl_create(void)
382 {
383 int error;
384
385 /*
386 * XXX the "33" below could be dynamic, thereby eliminating one
387 * more instance of the "number to vfs" mapping problem, but
388 * "33" is the order as taken from sys/mount.h
389 */
390 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
391 CTLFLAG_PERMANENT,
392 CTLTYPE_NODE, "autofs",
393 SYSCTL_DESCR("Automounter filesystem"),
394 NULL, 0, NULL, 0,
395 CTL_VFS, 33, CTL_EOL);
396 if (error)
397 goto fail;
398
399 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
400 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
401 CTLTYPE_INT, "autofs_debug",
402 SYSCTL_DESCR("Enable debug messages"),
403 NULL, 0, &autofs_debug, 0,
404 CTL_VFS, 33, AUTOFS_SYSCTL_DEBUG, CTL_EOL);
405 if (error)
406 goto fail;
407
408 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
409 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
410 CTLTYPE_INT, "autofs_mount_on_stat",
411 SYSCTL_DESCR("Trigger mount on stat(2) on mountpoint"),
412 NULL, 0, &autofs_mount_on_stat, 0,
413 CTL_VFS, 33, AUTOFS_SYSCTL_MOUNT_ON_STAT, CTL_EOL);
414 if (error)
415 goto fail;
416
417 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
418 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
419 CTLTYPE_INT, "autofs_timeout",
420 SYSCTL_DESCR("Number of seconds to wait for automountd(8)"),
421 NULL, 0, &autofs_timeout, 0,
422 CTL_VFS, 33, AUTOFS_SYSCTL_TIMEOUT, CTL_EOL);
423 if (error)
424 goto fail;
425
426 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
427 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
428 CTLTYPE_INT, "autofs_cache",
429 SYSCTL_DESCR("Number of seconds to wait before reinvoking"),
430 NULL, 0, &autofs_cache, 0,
431 CTL_VFS, 33, AUTOFS_SYSCTL_CACHE, CTL_EOL);
432 if (error)
433 goto fail;
434
435 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
436 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
437 CTLTYPE_INT, "autofs_retry_attempts",
438 SYSCTL_DESCR("Number of attempts before failing mount"),
439 NULL, 0, &autofs_retry_attempts, 0,
440 CTL_VFS, 33, AUTOFS_SYSCTL_RETRY_ATTEMPTS, CTL_EOL);
441 if (error)
442 goto fail;
443
444 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
445 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
446 CTLTYPE_INT, "autofs_retry_delay",
447 SYSCTL_DESCR("Number of seconds before retrying"),
448 NULL, 0, &autofs_retry_delay, 0,
449 CTL_VFS, 33, AUTOFS_SYSCTL_RETRY_DELAY, CTL_EOL);
450 if (error)
451 goto fail;
452
453 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
454 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
455 CTLTYPE_INT, "autofs_interruptible",
456 SYSCTL_DESCR("Allow requests to be interrupted by signal"),
457 NULL, 0, &autofs_interruptible, 0,
458 CTL_VFS, 33, AUTOFS_SYSCTL_INTERRUPTIBLE, CTL_EOL);
459 if (error)
460 goto fail;
461
462 return 0;
463 fail:
464 AUTOFS_WARN("sysctl_createv failed with error %d", error);
465
466 return error;
467 }
468
469 extern const struct cdevsw autofs_cdevsw;
470
471 static int
472 autofs_modcmd(modcmd_t cmd, void *arg)
473 {
474 #ifdef _MODULE
475 devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
476 #endif
477 int error = 0;
478
479 switch (cmd) {
480 case MODULE_CMD_INIT:
481 error = vfs_attach(&autofs_vfsops);
482 if (error)
483 break;
484 #ifdef _MODULE
485 error = devsw_attach("autofs", NULL, &bmajor, &autofs_cdevsw,
486 &cmajor);
487 if (error) {
488 vfs_detach(&autofs_vfsops);
489 break;
490 }
491 #endif
492 break;
493 case MODULE_CMD_FINI:
494 #ifdef _MODULE
495 KASSERT(autofs_softc);
496 mutex_enter(&autofs_softc->sc_lock);
497 if (autofs_softc->sc_dev_opened) {
498 mutex_exit(&autofs_softc->sc_lock);
499 error = EBUSY;
500 break;
501 }
502 mutex_exit(&autofs_softc->sc_lock);
503
504 error = devsw_detach(NULL, &autofs_cdevsw);
505 if (error)
506 break;
507 #endif
508 error = vfs_detach(&autofs_vfsops);
509 if (error)
510 break;
511 break;
512 default:
513 error = ENOTTY;
514 break;
515 }
516
517 return error;
518 }
519