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