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: stable/11/sys/fs/autofs/autofs_vnops.c 341074 2018-11-27 16:51:18Z markj $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/condvar.h>
40#include <sys/dirent.h>
41#include <sys/fcntl.h>
42#include <sys/lock.h>
43#include <sys/mount.h>
44#include <sys/mutex.h>
45#include <sys/namei.h>
46#include <sys/signalvar.h>
47#include <sys/stat.h>
48#include <sys/taskqueue.h>
49#include <sys/tree.h>
50#include <sys/vnode.h>
51#include <machine/atomic.h>
52#include <vm/uma.h>
53
54#include <fs/autofs/autofs.h>
55
56static int	autofs_trigger_vn(struct vnode *vp, const char *path,
57		    int pathlen, struct vnode **newvp);
58
59extern struct autofs_softc	*autofs_softc;
60
61static int
62autofs_access(struct vop_access_args *ap)
63{
64
65	/*
66	 * Nothing to do here; the only kind of access control
67	 * needed is in autofs_mkdir().
68	 */
69
70	return (0);
71}
72
73static int
74autofs_getattr(struct vop_getattr_args *ap)
75{
76	struct vnode *vp, *newvp;
77	struct autofs_node *anp;
78	struct mount *mp;
79	struct vattr *vap;
80	int error;
81
82	vp = ap->a_vp;
83	anp = vp->v_data;
84	mp = vp->v_mount;
85	vap = ap->a_vap;
86
87	KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR"));
88
89	/*
90	 * The reason we must do this is that some tree-walking software,
91	 * namely fts(3), assumes that stat(".") results will not change
92	 * between chdir("subdir") and chdir(".."), and fails with ENOENT
93	 * otherwise.
94	 */
95	if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false &&
96	    autofs_ignore_thread(curthread) == false) {
97		error = autofs_trigger_vn(vp, "", 0, &newvp);
98		if (error != 0)
99			return (error);
100
101		if (newvp != NULL) {
102			error = VOP_GETATTR(newvp, ap->a_vap,
103			    ap->a_cred);
104			vput(newvp);
105			return (error);
106		}
107	}
108
109	vap->va_type = VDIR;
110	vap->va_mode = 0755;
111	vap->va_nlink = 3; /* XXX */
112	vap->va_uid = 0;
113	vap->va_gid = 0;
114	vap->va_rdev = NODEV;
115	vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
116	vap->va_fileid = anp->an_fileno;
117	vap->va_size = S_BLKSIZE;
118	vap->va_blocksize = S_BLKSIZE;
119	vap->va_mtime = anp->an_ctime;
120	vap->va_atime = anp->an_ctime;
121	vap->va_ctime = anp->an_ctime;
122	vap->va_birthtime = anp->an_ctime;
123	vap->va_gen = 0;
124	vap->va_flags = 0;
125	vap->va_rdev = 0;
126	vap->va_bytes = S_BLKSIZE;
127	vap->va_filerev = 0;
128	vap->va_spare = 0;
129
130	return (0);
131}
132
133/*
134 * Unlock the vnode, request automountd(8) action, and then lock it back.
135 * If anything got mounted on top of the vnode, return the new filesystem's
136 * root vnode in 'newvp', locked.
137 */
138static int
139autofs_trigger_vn(struct vnode *vp, const char *path, int pathlen,
140    struct vnode **newvp)
141{
142	struct autofs_node *anp;
143	int error, lock_flags;
144
145	anp = vp->v_data;
146
147	/*
148	 * Release the vnode lock, so that other operations, in partcular
149	 * mounting a filesystem on top of it, can proceed.  Increase use
150	 * count, to prevent the vnode from being deallocated and to prevent
151	 * filesystem from being unmounted.
152	 */
153	lock_flags = VOP_ISLOCKED(vp);
154	vref(vp);
155	VOP_UNLOCK(vp, 0);
156
157	sx_xlock(&autofs_softc->sc_lock);
158
159	/*
160	 * XXX: Workaround for mounting the same thing multiple times; revisit.
161	 */
162	if (vp->v_mountedhere != NULL) {
163		error = 0;
164		goto mounted;
165	}
166
167	error = autofs_trigger(anp, path, pathlen);
168mounted:
169	sx_xunlock(&autofs_softc->sc_lock);
170	vn_lock(vp, lock_flags | LK_RETRY);
171	vunref(vp);
172	if ((vp->v_iflag & VI_DOOMED) != 0) {
173		AUTOFS_DEBUG("VI_DOOMED");
174		return (ENOENT);
175	}
176
177	if (error != 0)
178		return (error);
179
180	if (vp->v_mountedhere == NULL) {
181		*newvp = NULL;
182		return (0);
183	} else {
184		/*
185		 * If the operation that succeeded was mount, then mark
186		 * the node as non-cached.  Otherwise, if someone unmounts
187		 * the filesystem before the cache times out, we will fail
188		 * to trigger.
189		 */
190		anp->an_cached = false;
191	}
192
193	error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp);
194	if (error != 0) {
195		AUTOFS_WARN("VFS_ROOT() failed with error %d", error);
196		return (error);
197	}
198
199	return (0);
200}
201
202static int
203autofs_vget_callback(struct mount *mp, void *arg, int flags,
204    struct vnode **vpp)
205{
206
207
208	return (autofs_node_vn(arg, mp, flags, vpp));
209}
210
211static int
212autofs_lookup(struct vop_lookup_args *ap)
213{
214	struct vnode *dvp, *newvp, **vpp;
215	struct mount *mp;
216	struct autofs_mount *amp;
217	struct autofs_node *anp, *child;
218	struct componentname *cnp;
219	int error;
220
221	dvp = ap->a_dvp;
222	vpp = ap->a_vpp;
223	mp = dvp->v_mount;
224	amp = VFSTOAUTOFS(mp);
225	anp = dvp->v_data;
226	cnp = ap->a_cnp;
227
228	if (cnp->cn_flags & ISDOTDOT) {
229		KASSERT(anp->an_parent != NULL, ("NULL parent"));
230		/*
231		 * Note that in this case, dvp is the child vnode, and we
232		 * are looking up the parent vnode - exactly reverse from
233		 * normal operation.  Unlocking dvp requires some rather
234		 * tricky unlock/relock dance to prevent mp from being freed;
235		 * use vn_vget_ino_gen() which takes care of all that.
236		 */
237		error = vn_vget_ino_gen(dvp, autofs_vget_callback,
238		    anp->an_parent, cnp->cn_lkflags, vpp);
239		if (error != 0) {
240			AUTOFS_WARN("vn_vget_ino_gen() failed with error %d",
241			    error);
242			return (error);
243		}
244		return (error);
245	}
246
247	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
248		vref(dvp);
249		*vpp = dvp;
250
251		return (0);
252	}
253
254	if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false &&
255	    autofs_ignore_thread(cnp->cn_thread) == false) {
256		error = autofs_trigger_vn(dvp,
257		    cnp->cn_nameptr, cnp->cn_namelen, &newvp);
258		if (error != 0)
259			return (error);
260
261		if (newvp != NULL) {
262			/*
263			 * The target filesystem got automounted.
264			 * Let the lookup(9) go around with the same
265			 * path component.
266			 */
267			vput(newvp);
268			return (ERELOOKUP);
269		}
270	}
271
272	AUTOFS_SLOCK(amp);
273	error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child);
274	if (error != 0) {
275		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
276			AUTOFS_SUNLOCK(amp);
277			return (EJUSTRETURN);
278		}
279
280		AUTOFS_SUNLOCK(amp);
281		return (ENOENT);
282	}
283
284	/*
285	 * XXX: Dropping the node here is ok, because we never remove nodes.
286	 */
287	AUTOFS_SUNLOCK(amp);
288
289	error = autofs_node_vn(child, mp, cnp->cn_lkflags, vpp);
290	if (error != 0) {
291		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE)
292			return (EJUSTRETURN);
293
294		return (error);
295	}
296
297	return (0);
298}
299
300static int
301autofs_mkdir(struct vop_mkdir_args *ap)
302{
303	struct vnode *vp;
304	struct autofs_node *anp;
305	struct autofs_mount *amp;
306	struct autofs_node *child;
307	int error;
308
309	vp = ap->a_dvp;
310	anp = vp->v_data;
311	amp = VFSTOAUTOFS(vp->v_mount);
312
313	/*
314	 * Do not allow mkdir() if the calling thread is not
315	 * automountd(8) descendant.
316	 */
317	if (autofs_ignore_thread(curthread) == false)
318		return (EPERM);
319
320	AUTOFS_XLOCK(amp);
321	error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr,
322	    ap->a_cnp->cn_namelen, &child);
323	if (error != 0) {
324		AUTOFS_XUNLOCK(amp);
325		return (error);
326	}
327	AUTOFS_XUNLOCK(amp);
328
329	error = autofs_node_vn(child, vp->v_mount, LK_EXCLUSIVE, ap->a_vpp);
330
331	return (error);
332}
333
334static int
335autofs_print(struct vop_print_args *ap)
336{
337	struct vnode *vp;
338	struct autofs_node *anp;
339
340	vp = ap->a_vp;
341	anp = vp->v_data;
342
343	printf("    name \"%s\", fileno %d, cached %d, wildcards %d\n",
344	    anp->an_name, anp->an_fileno, anp->an_cached, anp->an_wildcards);
345
346	return (0);
347}
348
349/*
350 * Write out a single 'struct dirent', based on 'name' and 'fileno' arguments.
351 */
352static int
353autofs_readdir_one(struct uio *uio, const char *name, int fileno,
354    size_t *reclenp)
355{
356	struct dirent dirent;
357	size_t namlen, padded_namlen, reclen;
358	int error;
359
360	namlen = strlen(name);
361	padded_namlen = roundup2(namlen + 1, __alignof(struct dirent));
362	KASSERT(padded_namlen <= MAXNAMLEN, ("%zd > MAXNAMLEN", padded_namlen));
363	reclen = offsetof(struct dirent, d_name) + padded_namlen;
364	if (reclenp != NULL)
365		*reclenp = reclen;
366
367	if (uio == NULL)
368		return (0);
369
370	if (uio->uio_resid < reclen)
371		return (EINVAL);
372
373	dirent.d_fileno = fileno;
374	dirent.d_reclen = reclen;
375	dirent.d_type = DT_DIR;
376	dirent.d_namlen = namlen;
377	memcpy(dirent.d_name, name, namlen);
378	dirent_terminate(&dirent);
379	error = uiomove(&dirent, reclen, uio);
380
381	return (error);
382}
383
384static size_t
385autofs_dirent_reclen(const char *name)
386{
387	size_t reclen;
388
389	(void)autofs_readdir_one(NULL, name, -1, &reclen);
390
391	return (reclen);
392}
393
394static int
395autofs_readdir(struct vop_readdir_args *ap)
396{
397	struct vnode *vp, *newvp;
398	struct autofs_mount *amp;
399	struct autofs_node *anp, *child;
400	struct uio *uio;
401	size_t reclen, reclens;
402	ssize_t initial_resid;
403	int error;
404
405	vp = ap->a_vp;
406	amp = VFSTOAUTOFS(vp->v_mount);
407	anp = vp->v_data;
408	uio = ap->a_uio;
409	initial_resid = ap->a_uio->uio_resid;
410
411	KASSERT(vp->v_type == VDIR, ("!VDIR"));
412
413	if (autofs_cached(anp, NULL, 0) == false &&
414	    autofs_ignore_thread(curthread) == false) {
415		error = autofs_trigger_vn(vp, "", 0, &newvp);
416		if (error != 0)
417			return (error);
418
419		if (newvp != NULL) {
420			error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred,
421			    ap->a_eofflag, ap->a_ncookies, ap->a_cookies);
422			vput(newvp);
423			return (error);
424		}
425	}
426
427	if (uio->uio_offset < 0)
428		return (EINVAL);
429
430	if (ap->a_eofflag != NULL)
431		*ap->a_eofflag = FALSE;
432
433	/*
434	 * Write out the directory entry for ".".  This is conditional
435	 * on the current offset into the directory; same applies to the
436	 * other two cases below.
437	 */
438	if (uio->uio_offset == 0) {
439		error = autofs_readdir_one(uio, ".", anp->an_fileno, &reclen);
440		if (error != 0)
441			goto out;
442	}
443	reclens = autofs_dirent_reclen(".");
444
445	/*
446	 * Write out the directory entry for "..".
447	 */
448	if (uio->uio_offset <= reclens) {
449		if (uio->uio_offset != reclens)
450			return (EINVAL);
451		if (anp->an_parent == NULL) {
452			error = autofs_readdir_one(uio, "..",
453			    anp->an_fileno, &reclen);
454		} else {
455			error = autofs_readdir_one(uio, "..",
456			    anp->an_parent->an_fileno, &reclen);
457		}
458		if (error != 0)
459			goto out;
460	}
461
462	reclens += autofs_dirent_reclen("..");
463
464	/*
465	 * Write out the directory entries for subdirectories.
466	 */
467	AUTOFS_SLOCK(amp);
468	RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
469		/*
470		 * Check the offset to skip entries returned by previous
471		 * calls to getdents().
472		 */
473		if (uio->uio_offset > reclens) {
474			reclens += autofs_dirent_reclen(child->an_name);
475			continue;
476		}
477
478		/*
479		 * Prevent seeking into the middle of dirent.
480		 */
481		if (uio->uio_offset != reclens) {
482			AUTOFS_SUNLOCK(amp);
483			return (EINVAL);
484		}
485
486		error = autofs_readdir_one(uio, child->an_name,
487		    child->an_fileno, &reclen);
488		reclens += reclen;
489		if (error != 0) {
490			AUTOFS_SUNLOCK(amp);
491			goto out;
492		}
493	}
494	AUTOFS_SUNLOCK(amp);
495
496	if (ap->a_eofflag != NULL)
497		*ap->a_eofflag = TRUE;
498
499	return (0);
500
501out:
502	/*
503	 * Return error if the initial buffer was too small to do anything.
504	 */
505	if (uio->uio_resid == initial_resid)
506		return (error);
507
508	/*
509	 * Don't return an error if we managed to copy out some entries.
510	 */
511	if (uio->uio_resid < reclen)
512		return (0);
513
514	return (error);
515}
516
517static int
518autofs_reclaim(struct vop_reclaim_args *ap)
519{
520	struct vnode *vp;
521	struct autofs_node *anp;
522
523	vp = ap->a_vp;
524	anp = vp->v_data;
525
526	/*
527	 * We do not free autofs_node here; instead we are
528	 * destroying them in autofs_node_delete().
529	 */
530	sx_xlock(&anp->an_vnode_lock);
531	anp->an_vnode = NULL;
532	vp->v_data = NULL;
533	sx_xunlock(&anp->an_vnode_lock);
534
535	return (0);
536}
537
538struct vop_vector autofs_vnodeops = {
539	.vop_default =		&default_vnodeops,
540
541	.vop_access =		autofs_access,
542	.vop_lookup =		autofs_lookup,
543	.vop_create =		VOP_EOPNOTSUPP,
544	.vop_getattr =		autofs_getattr,
545	.vop_link =		VOP_EOPNOTSUPP,
546	.vop_mkdir =		autofs_mkdir,
547	.vop_mknod =		VOP_EOPNOTSUPP,
548	.vop_print =		autofs_print,
549	.vop_read =		VOP_EOPNOTSUPP,
550	.vop_readdir =		autofs_readdir,
551	.vop_remove =		VOP_EOPNOTSUPP,
552	.vop_rename =		VOP_EOPNOTSUPP,
553	.vop_rmdir =		VOP_EOPNOTSUPP,
554	.vop_setattr =		VOP_EOPNOTSUPP,
555	.vop_symlink =		VOP_EOPNOTSUPP,
556	.vop_write =		VOP_EOPNOTSUPP,
557	.vop_reclaim =		autofs_reclaim,
558};
559
560int
561autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
562    const char *name, int namelen, struct autofs_node **anpp)
563{
564	struct autofs_node *anp;
565
566	if (parent != NULL) {
567		AUTOFS_ASSERT_XLOCKED(parent->an_mount);
568
569		KASSERT(autofs_node_find(parent, name, namelen, NULL) == ENOENT,
570		    ("node \"%s\" already exists", name));
571	}
572
573	anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO);
574	if (namelen >= 0)
575		anp->an_name = strndup(name, namelen, M_AUTOFS);
576	else
577		anp->an_name = strdup(name, M_AUTOFS);
578	anp->an_fileno = atomic_fetchadd_int(&amp->am_last_fileno, 1);
579	callout_init(&anp->an_callout, 1);
580	/*
581	 * The reason for SX_NOWITNESS here is that witness(4)
582	 * cannot tell vnodes apart, so the following perfectly
583	 * valid lock order...
584	 *
585	 * vnode lock A -> autofsvlk B -> vnode lock B
586	 *
587	 * ... gets reported as a LOR.
588	 */
589	sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS);
590	getnanotime(&anp->an_ctime);
591	anp->an_parent = parent;
592	anp->an_mount = amp;
593	if (parent != NULL)
594		RB_INSERT(autofs_node_tree, &parent->an_children, anp);
595	RB_INIT(&anp->an_children);
596
597	*anpp = anp;
598	return (0);
599}
600
601int
602autofs_node_find(struct autofs_node *parent, const char *name,
603    int namelen, struct autofs_node **anpp)
604{
605	struct autofs_node *anp, find;
606	int error;
607
608	AUTOFS_ASSERT_LOCKED(parent->an_mount);
609
610	if (namelen >= 0)
611		find.an_name = strndup(name, namelen, M_AUTOFS);
612	else
613		find.an_name = strdup(name, M_AUTOFS);
614
615	anp = RB_FIND(autofs_node_tree, &parent->an_children, &find);
616	if (anp != NULL) {
617		error = 0;
618		if (anpp != NULL)
619			*anpp = anp;
620	} else {
621		error = ENOENT;
622	}
623
624	free(find.an_name, M_AUTOFS);
625
626	return (error);
627}
628
629void
630autofs_node_delete(struct autofs_node *anp)
631{
632	struct autofs_node *parent;
633
634	AUTOFS_ASSERT_XLOCKED(anp->an_mount);
635	KASSERT(RB_EMPTY(&anp->an_children), ("have children"));
636
637	callout_drain(&anp->an_callout);
638
639	parent = anp->an_parent;
640	if (parent != NULL)
641		RB_REMOVE(autofs_node_tree, &parent->an_children, anp);
642	sx_destroy(&anp->an_vnode_lock);
643	free(anp->an_name, M_AUTOFS);
644	uma_zfree(autofs_node_zone, anp);
645}
646
647int
648autofs_node_vn(struct autofs_node *anp, struct mount *mp, int flags,
649    struct vnode **vpp)
650{
651	struct vnode *vp;
652	int error;
653
654	AUTOFS_ASSERT_UNLOCKED(anp->an_mount);
655
656	sx_xlock(&anp->an_vnode_lock);
657
658	vp = anp->an_vnode;
659	if (vp != NULL) {
660		error = vget(vp, flags | LK_RETRY, curthread);
661		if (error != 0) {
662			AUTOFS_WARN("vget failed with error %d", error);
663			sx_xunlock(&anp->an_vnode_lock);
664			return (error);
665		}
666		if (vp->v_iflag & VI_DOOMED) {
667			/*
668			 * We got forcibly unmounted.
669			 */
670			AUTOFS_DEBUG("doomed vnode");
671			sx_xunlock(&anp->an_vnode_lock);
672			vput(vp);
673
674			return (ENOENT);
675		}
676
677		*vpp = vp;
678		sx_xunlock(&anp->an_vnode_lock);
679		return (0);
680	}
681
682	error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp);
683	if (error != 0) {
684		sx_xunlock(&anp->an_vnode_lock);
685		return (error);
686	}
687
688	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
689	if (error != 0) {
690		sx_xunlock(&anp->an_vnode_lock);
691		vdrop(vp);
692		return (error);
693	}
694
695	vp->v_type = VDIR;
696	if (anp->an_parent == NULL)
697		vp->v_vflag |= VV_ROOT;
698	vp->v_data = anp;
699
700	VN_LOCK_ASHARE(vp);
701
702	error = insmntque(vp, mp);
703	if (error != 0) {
704		AUTOFS_DEBUG("insmntque() failed with error %d", error);
705		sx_xunlock(&anp->an_vnode_lock);
706		return (error);
707	}
708
709	KASSERT(anp->an_vnode == NULL, ("lost race"));
710	anp->an_vnode = vp;
711
712	sx_xunlock(&anp->an_vnode_lock);
713
714	*vpp = vp;
715	return (0);
716}
717