autofs_vnops.c revision 296798
1270096Strasz/*-
2270096Strasz * Copyright (c) 2014 The FreeBSD Foundation
3270096Strasz * All rights reserved.
4270096Strasz *
5270096Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6270096Strasz * from the FreeBSD Foundation.
7270096Strasz *
8270096Strasz * Redistribution and use in source and binary forms, with or without
9270096Strasz * modification, are permitted provided that the following conditions
10270096Strasz * are met:
11270096Strasz * 1. Redistributions of source code must retain the above copyright
12270096Strasz *    notice, this list of conditions and the following disclaimer.
13270096Strasz * 2. Redistributions in binary form must reproduce the above copyright
14270096Strasz *    notice, this list of conditions and the following disclaimer in the
15270096Strasz *    documentation and/or other materials provided with the distribution.
16270096Strasz *
17270096Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18270096Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19270096Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20270096Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21270096Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22270096Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23270096Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24270096Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25270096Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26270096Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27270096Strasz * SUCH DAMAGE.
28270096Strasz *
29270096Strasz */
30270096Strasz
31270096Strasz#include <sys/cdefs.h>
32270096Strasz__FBSDID("$FreeBSD: head/sys/fs/autofs/autofs_vnops.c 296798 2016-03-13 14:17:23Z trasz $");
33270096Strasz
34270096Strasz#include <sys/param.h>
35270096Strasz#include <sys/kernel.h>
36270096Strasz#include <sys/condvar.h>
37270096Strasz#include <sys/dirent.h>
38270096Strasz#include <sys/fcntl.h>
39270096Strasz#include <sys/lock.h>
40270096Strasz#include <sys/mount.h>
41270096Strasz#include <sys/mutex.h>
42270096Strasz#include <sys/namei.h>
43270096Strasz#include <sys/signalvar.h>
44296718Strasz#include <sys/stat.h>
45270096Strasz#include <sys/systm.h>
46272403Strasz#include <sys/taskqueue.h>
47270096Strasz#include <sys/vnode.h>
48270096Strasz#include <machine/atomic.h>
49270096Strasz#include <vm/uma.h>
50270096Strasz
51270281Strasz#include <fs/autofs/autofs.h>
52270096Strasz
53270096Straszstatic int	autofs_trigger_vn(struct vnode *vp, const char *path,
54270096Strasz		    int pathlen, struct vnode **newvp);
55270096Strasz
56270402Straszextern struct autofs_softc	*autofs_softc;
57270402Strasz
58270096Straszstatic int
59270096Straszautofs_access(struct vop_access_args *ap)
60270096Strasz{
61270096Strasz
62270096Strasz	/*
63270096Strasz	 * Nothing to do here; the only kind of access control
64270096Strasz	 * needed is in autofs_mkdir().
65270096Strasz	 */
66270096Strasz
67270096Strasz	return (0);
68270096Strasz}
69270096Strasz
70270096Straszstatic int
71270096Straszautofs_getattr(struct vop_getattr_args *ap)
72270096Strasz{
73270096Strasz	struct vnode *vp, *newvp;
74270096Strasz	struct autofs_node *anp;
75270096Strasz	struct mount *mp;
76270096Strasz	struct vattr *vap;
77270096Strasz	int error;
78270096Strasz
79270096Strasz	vp = ap->a_vp;
80270096Strasz	anp = vp->v_data;
81270096Strasz	mp = vp->v_mount;
82270096Strasz	vap = ap->a_vap;
83270096Strasz
84270096Strasz	KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR"));
85270096Strasz
86270096Strasz	/*
87270096Strasz	 * The reason we must do this is that some tree-walking software,
88270096Strasz	 * namely fts(3), assumes that stat(".") results will not change
89270096Strasz	 * between chdir("subdir") and chdir(".."), and fails with ENOENT
90270096Strasz	 * otherwise.
91270096Strasz	 */
92270096Strasz	if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false &&
93270096Strasz	    autofs_ignore_thread(curthread) == false) {
94270096Strasz		error = autofs_trigger_vn(vp, "", 0, &newvp);
95270096Strasz		if (error != 0)
96270096Strasz			return (error);
97270096Strasz
98270096Strasz		if (newvp != NULL) {
99270096Strasz			error = VOP_GETATTR(newvp, ap->a_vap,
100270096Strasz			    ap->a_cred);
101270096Strasz			vput(newvp);
102270096Strasz			return (error);
103270096Strasz		}
104270096Strasz	}
105270096Strasz
106270096Strasz	vap->va_type = VDIR;
107270096Strasz	vap->va_mode = 0755;
108270096Strasz	vap->va_nlink = 3; /* XXX */
109270096Strasz	vap->va_uid = 0;
110270096Strasz	vap->va_gid = 0;
111270096Strasz	vap->va_rdev = NODEV;
112270096Strasz	vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
113270096Strasz	vap->va_fileid = anp->an_fileno;
114296718Strasz	vap->va_size = S_BLKSIZE;
115296718Strasz	vap->va_blocksize = S_BLKSIZE;
116270096Strasz	vap->va_mtime = anp->an_ctime;
117270096Strasz	vap->va_atime = anp->an_ctime;
118270096Strasz	vap->va_ctime = anp->an_ctime;
119270096Strasz	vap->va_birthtime = anp->an_ctime;
120270096Strasz	vap->va_gen = 0;
121270096Strasz	vap->va_flags = 0;
122270096Strasz	vap->va_rdev = 0;
123296718Strasz	vap->va_bytes = S_BLKSIZE;
124270096Strasz	vap->va_filerev = 0;
125270096Strasz	vap->va_spare = 0;
126270096Strasz
127270096Strasz	return (0);
128270096Strasz}
129270096Strasz
130270096Strasz/*
131270096Strasz * Unlock the vnode, request automountd(8) action, and then lock it back.
132270096Strasz * If anything got mounted on top of the vnode, return the new filesystem's
133270096Strasz * root vnode in 'newvp', locked.
134270096Strasz */
135270096Straszstatic int
136270096Straszautofs_trigger_vn(struct vnode *vp, const char *path, int pathlen,
137270096Strasz    struct vnode **newvp)
138270096Strasz{
139270096Strasz	struct autofs_node *anp;
140270096Strasz	struct autofs_mount *amp;
141270096Strasz	int error, lock_flags;
142270096Strasz
143270096Strasz	anp = vp->v_data;
144270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
145270096Strasz
146270096Strasz	/*
147270096Strasz	 * Release the vnode lock, so that other operations, in partcular
148270096Strasz	 * mounting a filesystem on top of it, can proceed.  Increase use
149270096Strasz	 * count, to prevent the vnode from being deallocated and to prevent
150270096Strasz	 * filesystem from being unmounted.
151270096Strasz	 */
152270096Strasz	lock_flags = VOP_ISLOCKED(vp);
153270096Strasz	vref(vp);
154270096Strasz	VOP_UNLOCK(vp, 0);
155270096Strasz
156270402Strasz	sx_xlock(&autofs_softc->sc_lock);
157270096Strasz
158270096Strasz	/*
159270096Strasz	 * XXX: Workaround for mounting the same thing multiple times; revisit.
160270096Strasz	 */
161270096Strasz	if (vp->v_mountedhere != NULL) {
162270096Strasz		error = 0;
163270096Strasz		goto mounted;
164270096Strasz	}
165270096Strasz
166270096Strasz	error = autofs_trigger(anp, path, pathlen);
167270096Straszmounted:
168270402Strasz	sx_xunlock(&autofs_softc->sc_lock);
169270096Strasz	vn_lock(vp, lock_flags | LK_RETRY);
170270096Strasz	vunref(vp);
171270096Strasz	if ((vp->v_iflag & VI_DOOMED) != 0) {
172270096Strasz		AUTOFS_DEBUG("VI_DOOMED");
173270096Strasz		return (ENOENT);
174270096Strasz	}
175270096Strasz
176270096Strasz	if (error != 0)
177270096Strasz		return (error);
178270096Strasz
179270096Strasz	if (vp->v_mountedhere == NULL) {
180270096Strasz		*newvp = NULL;
181270096Strasz		return (0);
182270096Strasz	} else {
183270096Strasz		/*
184270096Strasz		 * If the operation that succeeded was mount, then mark
185270096Strasz		 * the node as non-cached.  Otherwise, if someone unmounts
186270096Strasz		 * the filesystem before the cache times out, we will fail
187270096Strasz		 * to trigger.
188270096Strasz		 */
189270096Strasz		anp->an_cached = false;
190270096Strasz	}
191270096Strasz
192270096Strasz	error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp);
193270096Strasz	if (error != 0) {
194270096Strasz		AUTOFS_WARN("VFS_ROOT() failed with error %d", error);
195270096Strasz		return (error);
196270096Strasz	}
197270096Strasz
198270096Strasz	return (0);
199270096Strasz}
200270096Strasz
201270096Straszstatic int
202272512Straszautofs_vget_callback(struct mount *mp, void *arg, int flags,
203270207Strasz    struct vnode **vpp)
204270207Strasz{
205270207Strasz
206270207Strasz
207272512Strasz	return (autofs_node_vn(arg, mp, flags, vpp));
208270207Strasz}
209270207Strasz
210270207Straszstatic int
211270096Straszautofs_lookup(struct vop_lookup_args *ap)
212270096Strasz{
213270096Strasz	struct vnode *dvp, *newvp, **vpp;
214270096Strasz	struct mount *mp;
215270096Strasz	struct autofs_mount *amp;
216270096Strasz	struct autofs_node *anp, *child;
217270096Strasz	struct componentname *cnp;
218296715Strasz	int error;
219270096Strasz
220270096Strasz	dvp = ap->a_dvp;
221270096Strasz	vpp = ap->a_vpp;
222270096Strasz	mp = dvp->v_mount;
223270096Strasz	amp = VFSTOAUTOFS(mp);
224270096Strasz	anp = dvp->v_data;
225270096Strasz	cnp = ap->a_cnp;
226270096Strasz
227270096Strasz	if (cnp->cn_flags & ISDOTDOT) {
228270096Strasz		KASSERT(anp->an_parent != NULL, ("NULL parent"));
229270096Strasz		/*
230270207Strasz		 * Note that in this case, dvp is the child vnode, and we
231270207Strasz		 * are looking up the parent vnode - exactly reverse from
232270207Strasz		 * normal operation.  Unlocking dvp requires some rather
233270207Strasz		 * tricky unlock/relock dance to prevent mp from being freed;
234270207Strasz		 * use vn_vget_ino_gen() which takes care of all that.
235270096Strasz		 */
236270207Strasz		error = vn_vget_ino_gen(dvp, autofs_vget_callback,
237272512Strasz		    anp->an_parent, cnp->cn_lkflags, vpp);
238270096Strasz		if (error != 0) {
239270207Strasz			AUTOFS_WARN("vn_vget_ino_gen() failed with error %d",
240270096Strasz			    error);
241270207Strasz			return (error);
242270096Strasz		}
243270096Strasz		return (error);
244270096Strasz	}
245270096Strasz
246270096Strasz	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
247270096Strasz		vref(dvp);
248270096Strasz		*vpp = dvp;
249270096Strasz
250270096Strasz		return (0);
251270096Strasz	}
252270096Strasz
253270096Strasz	if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false &&
254270096Strasz	    autofs_ignore_thread(cnp->cn_thread) == false) {
255270096Strasz		error = autofs_trigger_vn(dvp,
256270096Strasz		    cnp->cn_nameptr, cnp->cn_namelen, &newvp);
257270096Strasz		if (error != 0)
258270096Strasz			return (error);
259270096Strasz
260270096Strasz		if (newvp != NULL) {
261270096Strasz			/*
262296715Strasz			 * The target filesystem got automounted.
263296715Strasz			 * Let the lookup(9) go around with the same
264296715Strasz			 * path component.
265270096Strasz			 */
266296715Strasz			vput(newvp);
267296715Strasz			return (ERELOOKUP);
268270096Strasz		}
269270096Strasz	}
270270096Strasz
271272470Strasz	AUTOFS_SLOCK(amp);
272270096Strasz	error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child);
273270096Strasz	if (error != 0) {
274270096Strasz		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
275272470Strasz			AUTOFS_SUNLOCK(amp);
276270096Strasz			return (EJUSTRETURN);
277270096Strasz		}
278270096Strasz
279272470Strasz		AUTOFS_SUNLOCK(amp);
280270096Strasz		return (ENOENT);
281270096Strasz	}
282270096Strasz
283270096Strasz	/*
284270096Strasz	 * XXX: Dropping the node here is ok, because we never remove nodes.
285270096Strasz	 */
286272470Strasz	AUTOFS_SUNLOCK(amp);
287270096Strasz
288272512Strasz	error = autofs_node_vn(child, mp, cnp->cn_lkflags, vpp);
289270096Strasz	if (error != 0) {
290270096Strasz		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE)
291270096Strasz			return (EJUSTRETURN);
292270096Strasz
293270096Strasz		return (error);
294270096Strasz	}
295270096Strasz
296270096Strasz	return (0);
297270096Strasz}
298270096Strasz
299270096Straszstatic int
300270096Straszautofs_mkdir(struct vop_mkdir_args *ap)
301270096Strasz{
302270096Strasz	struct vnode *vp;
303270096Strasz	struct autofs_node *anp;
304270096Strasz	struct autofs_mount *amp;
305270096Strasz	struct autofs_node *child;
306270096Strasz	int error;
307270096Strasz
308270096Strasz	vp = ap->a_dvp;
309270096Strasz	anp = vp->v_data;
310270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
311270096Strasz
312270096Strasz	/*
313270096Strasz	 * Do not allow mkdir() if the calling thread is not
314270096Strasz	 * automountd(8) descendant.
315270096Strasz	 */
316270096Strasz	if (autofs_ignore_thread(curthread) == false)
317270096Strasz		return (EPERM);
318270096Strasz
319272470Strasz	AUTOFS_XLOCK(amp);
320270096Strasz	error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr,
321270096Strasz	    ap->a_cnp->cn_namelen, &child);
322270096Strasz	if (error != 0) {
323272470Strasz		AUTOFS_XUNLOCK(amp);
324270096Strasz		return (error);
325270096Strasz	}
326272470Strasz	AUTOFS_XUNLOCK(amp);
327270096Strasz
328272512Strasz	error = autofs_node_vn(child, vp->v_mount, LK_EXCLUSIVE, ap->a_vpp);
329270096Strasz
330270096Strasz	return (error);
331270096Strasz}
332270096Strasz
333296798Strasz/*
334296798Strasz * Write out a single 'struct dirent', based on 'name' and 'fileno' arguments.
335296798Strasz */
336270096Straszstatic int
337296798Straszautofs_readdir_one(struct uio *uio, const char *name, int fileno,
338296798Strasz    size_t *reclenp)
339270096Strasz{
340270096Strasz	struct dirent dirent;
341296798Strasz	size_t namlen, padded_namlen, reclen;
342296798Strasz	int error;
343270096Strasz
344296798Strasz	namlen = strlen(name);
345296798Strasz	padded_namlen = roundup2(namlen + 1, __alignof(struct dirent));
346296798Strasz	KASSERT(padded_namlen <= MAXNAMLEN, ("%zd > MAXNAMLEN", padded_namlen));
347296798Strasz	reclen = offsetof(struct dirent, d_name) + padded_namlen;
348296798Strasz
349296798Strasz	if (reclenp != NULL)
350296798Strasz		*reclenp = reclen;
351296798Strasz
352296798Strasz	if (uio == NULL)
353296798Strasz		return (0);
354296798Strasz
355296798Strasz	if (uio->uio_resid < reclen)
356296798Strasz		return (EINVAL);
357296798Strasz
358296798Strasz	dirent.d_fileno = fileno;
359296798Strasz	dirent.d_reclen = reclen;
360270096Strasz	dirent.d_type = DT_DIR;
361296798Strasz	dirent.d_namlen = namlen;
362296798Strasz	memcpy(dirent.d_name, name, namlen);
363296798Strasz	memset(dirent.d_name + namlen, 0, padded_namlen - namlen);
364296798Strasz	error = uiomove(&dirent, reclen, uio);
365270096Strasz
366270096Strasz	return (error);
367270096Strasz}
368270096Strasz
369296798Straszstatic size_t
370296798Straszautofs_dirent_reclen(const char *name)
371296798Strasz{
372296798Strasz	size_t reclen;
373296798Strasz
374296798Strasz	autofs_readdir_one(NULL, name, -1, &reclen);
375296798Strasz
376296798Strasz	return (reclen);
377296798Strasz}
378296798Strasz
379270096Straszstatic int
380270096Straszautofs_readdir(struct vop_readdir_args *ap)
381270096Strasz{
382270096Strasz	struct vnode *vp, *newvp;
383270096Strasz	struct autofs_mount *amp;
384270096Strasz	struct autofs_node *anp, *child;
385270096Strasz	struct uio *uio;
386296798Strasz	size_t reclen, reclens;
387296798Strasz	ssize_t initial_resid;
388296798Strasz	int error;
389270096Strasz
390270096Strasz	vp = ap->a_vp;
391270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
392270096Strasz	anp = vp->v_data;
393270096Strasz	uio = ap->a_uio;
394296798Strasz	initial_resid = ap->a_uio->uio_resid;
395270096Strasz
396270096Strasz	KASSERT(vp->v_type == VDIR, ("!VDIR"));
397270096Strasz
398270096Strasz	if (autofs_cached(anp, NULL, 0) == false &&
399270096Strasz	    autofs_ignore_thread(curthread) == false) {
400270096Strasz		error = autofs_trigger_vn(vp, "", 0, &newvp);
401270096Strasz		if (error != 0)
402270096Strasz			return (error);
403270096Strasz
404270096Strasz		if (newvp != NULL) {
405270096Strasz			error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred,
406270096Strasz			    ap->a_eofflag, ap->a_ncookies, ap->a_cookies);
407270096Strasz			vput(newvp);
408270096Strasz			return (error);
409270096Strasz		}
410270096Strasz	}
411270096Strasz
412296798Strasz	if (uio->uio_offset < 0)
413270096Strasz		return (EINVAL);
414270096Strasz
415270096Strasz	if (ap->a_eofflag != NULL)
416296798Strasz		*ap->a_eofflag = FALSE;
417270096Strasz
418296798Strasz	/*
419296798Strasz	 * Write out the directory entry for ".".  This is conditional
420296798Strasz	 * on the current offset into the directory; same applies to the
421296798Strasz	 * other two cases below.
422296798Strasz	 */
423296798Strasz	if (uio->uio_offset == 0) {
424296798Strasz		error = autofs_readdir_one(uio, ".", anp->an_fileno, &reclen);
425270096Strasz		if (error != 0)
426296798Strasz			goto out;
427270096Strasz	}
428296798Strasz	reclens = autofs_dirent_reclen(".");
429270096Strasz
430296798Strasz	/*
431296798Strasz	 * Write out the directory entry for "..".
432296798Strasz	 */
433296798Strasz	if (uio->uio_offset <= reclens) {
434296798Strasz		if (uio->uio_offset != reclens)
435296798Strasz			return (EINVAL);
436270096Strasz		if (anp->an_parent == NULL) {
437296798Strasz			error = autofs_readdir_one(uio, "..",
438296798Strasz			    anp->an_fileno, &reclen);
439270096Strasz		} else {
440270096Strasz			error = autofs_readdir_one(uio, "..",
441296798Strasz			    anp->an_parent->an_fileno, &reclen);
442270096Strasz		}
443270096Strasz		if (error != 0)
444296798Strasz			goto out;
445270096Strasz	}
446270096Strasz
447296798Strasz	reclens += autofs_dirent_reclen("..");
448296798Strasz
449296798Strasz	/*
450296798Strasz	 * Write out the directory entries for subdirectories.
451296798Strasz	 */
452272470Strasz	AUTOFS_SLOCK(amp);
453270096Strasz	TAILQ_FOREACH(child, &anp->an_children, an_next) {
454296798Strasz		/*
455296798Strasz		 * Check the offset to skip entries returned by previous
456296798Strasz		 * calls to getdents().
457296798Strasz		 */
458296798Strasz		if (uio->uio_offset > reclens) {
459296798Strasz			reclens += autofs_dirent_reclen(child->an_name);
460296798Strasz			continue;
461270096Strasz		}
462270096Strasz
463270096Strasz		/*
464296798Strasz		 * Prevent seeking into the middle of dirent.
465270096Strasz		 */
466296798Strasz		if (uio->uio_offset != reclens) {
467296798Strasz			AUTOFS_SUNLOCK(amp);
468296798Strasz			return (EINVAL);
469296798Strasz		}
470270096Strasz
471270096Strasz		error = autofs_readdir_one(uio, child->an_name,
472296798Strasz		    child->an_fileno, &reclen);
473296798Strasz		reclens += reclen;
474270096Strasz		if (error != 0) {
475272470Strasz			AUTOFS_SUNLOCK(amp);
476296798Strasz			goto out;
477270096Strasz		}
478270096Strasz	}
479296798Strasz	AUTOFS_SUNLOCK(amp);
480270096Strasz
481296798Strasz	if (ap->a_eofflag != NULL)
482296798Strasz		*ap->a_eofflag = TRUE;
483296798Strasz
484270096Strasz	return (0);
485296798Strasz
486296798Straszout:
487296798Strasz	/*
488296798Strasz	 * Return error if the initial buffer was too small to do anything.
489296798Strasz	 */
490296798Strasz	if (uio->uio_resid == initial_resid)
491296798Strasz		return (error);
492296798Strasz
493296798Strasz	/*
494296798Strasz	 * Don't return an error if we managed to copy out some entries.
495296798Strasz	 */
496296798Strasz	if (uio->uio_resid < reclen)
497296798Strasz		return (0);
498296798Strasz
499296798Strasz	return (error);
500270096Strasz}
501270096Strasz
502270096Straszstatic int
503270096Straszautofs_reclaim(struct vop_reclaim_args *ap)
504270096Strasz{
505272836Strasz	struct vnode *vp;
506272836Strasz	struct autofs_node *anp;
507270096Strasz
508270096Strasz	vp = ap->a_vp;
509270096Strasz	anp = vp->v_data;
510270096Strasz
511270096Strasz	/*
512270096Strasz	 * We do not free autofs_node here; instead we are
513270096Strasz	 * destroying them in autofs_node_delete().
514270096Strasz	 */
515270096Strasz	sx_xlock(&anp->an_vnode_lock);
516270096Strasz	anp->an_vnode = NULL;
517270096Strasz	vp->v_data = NULL;
518270096Strasz	sx_xunlock(&anp->an_vnode_lock);
519270096Strasz
520270096Strasz	return (0);
521270096Strasz}
522270096Strasz
523270096Straszstruct vop_vector autofs_vnodeops = {
524270096Strasz	.vop_default =		&default_vnodeops,
525270096Strasz
526270096Strasz	.vop_access =		autofs_access,
527270096Strasz	.vop_lookup =		autofs_lookup,
528270096Strasz	.vop_create =		VOP_EOPNOTSUPP,
529270096Strasz	.vop_getattr =		autofs_getattr,
530270096Strasz	.vop_link =		VOP_EOPNOTSUPP,
531270096Strasz	.vop_mkdir =		autofs_mkdir,
532270096Strasz	.vop_mknod =		VOP_EOPNOTSUPP,
533270096Strasz	.vop_read =		VOP_EOPNOTSUPP,
534270096Strasz	.vop_readdir =		autofs_readdir,
535270096Strasz	.vop_remove =		VOP_EOPNOTSUPP,
536270096Strasz	.vop_rename =		VOP_EOPNOTSUPP,
537270096Strasz	.vop_rmdir =		VOP_EOPNOTSUPP,
538270096Strasz	.vop_setattr =		VOP_EOPNOTSUPP,
539270096Strasz	.vop_symlink =		VOP_EOPNOTSUPP,
540270096Strasz	.vop_write =		VOP_EOPNOTSUPP,
541270096Strasz	.vop_reclaim =		autofs_reclaim,
542270096Strasz};
543270096Strasz
544270096Straszint
545270096Straszautofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
546270096Strasz    const char *name, int namelen, struct autofs_node **anpp)
547270096Strasz{
548270096Strasz	struct autofs_node *anp;
549270096Strasz
550272931Strasz	if (parent != NULL) {
551272470Strasz		AUTOFS_ASSERT_XLOCKED(parent->an_mount);
552270096Strasz
553272931Strasz		KASSERT(autofs_node_find(parent, name, namelen, NULL) == ENOENT,
554272931Strasz		    ("node \"%s\" already exists", name));
555272931Strasz	}
556272931Strasz
557270096Strasz	anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO);
558270096Strasz	if (namelen >= 0)
559270096Strasz		anp->an_name = strndup(name, namelen, M_AUTOFS);
560270096Strasz	else
561270096Strasz		anp->an_name = strdup(name, M_AUTOFS);
562270096Strasz	anp->an_fileno = atomic_fetchadd_int(&amp->am_last_fileno, 1);
563270096Strasz	callout_init(&anp->an_callout, 1);
564270096Strasz	/*
565270096Strasz	 * The reason for SX_NOWITNESS here is that witness(4)
566270096Strasz	 * cannot tell vnodes apart, so the following perfectly
567270096Strasz	 * valid lock order...
568270096Strasz	 *
569270096Strasz	 * vnode lock A -> autofsvlk B -> vnode lock B
570270096Strasz	 *
571270096Strasz	 * ... gets reported as a LOR.
572270096Strasz	 */
573270096Strasz	sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS);
574270096Strasz	getnanotime(&anp->an_ctime);
575270096Strasz	anp->an_parent = parent;
576270096Strasz	anp->an_mount = amp;
577270096Strasz	if (parent != NULL)
578270096Strasz		TAILQ_INSERT_TAIL(&parent->an_children, anp, an_next);
579270096Strasz	TAILQ_INIT(&anp->an_children);
580270096Strasz
581270096Strasz	*anpp = anp;
582270096Strasz	return (0);
583270096Strasz}
584270096Strasz
585270096Straszint
586270096Straszautofs_node_find(struct autofs_node *parent, const char *name,
587270096Strasz    int namelen, struct autofs_node **anpp)
588270096Strasz{
589270096Strasz	struct autofs_node *anp;
590270096Strasz
591270096Strasz	AUTOFS_ASSERT_LOCKED(parent->an_mount);
592270096Strasz
593270096Strasz	TAILQ_FOREACH(anp, &parent->an_children, an_next) {
594270096Strasz		if (namelen >= 0) {
595272025Strasz			if (strlen(anp->an_name) != namelen)
596272025Strasz				continue;
597270096Strasz			if (strncmp(anp->an_name, name, namelen) != 0)
598270096Strasz				continue;
599270096Strasz		} else {
600270096Strasz			if (strcmp(anp->an_name, name) != 0)
601270096Strasz				continue;
602270096Strasz		}
603270096Strasz
604270096Strasz		if (anpp != NULL)
605270096Strasz			*anpp = anp;
606270096Strasz		return (0);
607270096Strasz	}
608270096Strasz
609270096Strasz	return (ENOENT);
610270096Strasz}
611270096Strasz
612270096Straszvoid
613270096Straszautofs_node_delete(struct autofs_node *anp)
614270096Strasz{
615270096Strasz	struct autofs_node *parent;
616270096Strasz
617272470Strasz	AUTOFS_ASSERT_XLOCKED(anp->an_mount);
618270096Strasz	KASSERT(TAILQ_EMPTY(&anp->an_children), ("have children"));
619270096Strasz
620270096Strasz	callout_drain(&anp->an_callout);
621270096Strasz
622270096Strasz	parent = anp->an_parent;
623270096Strasz	if (parent != NULL)
624270096Strasz		TAILQ_REMOVE(&parent->an_children, anp, an_next);
625270096Strasz	sx_destroy(&anp->an_vnode_lock);
626270096Strasz	free(anp->an_name, M_AUTOFS);
627270096Strasz	uma_zfree(autofs_node_zone, anp);
628270096Strasz}
629270096Strasz
630270096Straszint
631272512Straszautofs_node_vn(struct autofs_node *anp, struct mount *mp, int flags,
632272512Strasz    struct vnode **vpp)
633270096Strasz{
634270096Strasz	struct vnode *vp;
635270096Strasz	int error;
636270096Strasz
637270096Strasz	AUTOFS_ASSERT_UNLOCKED(anp->an_mount);
638270096Strasz
639270096Strasz	sx_xlock(&anp->an_vnode_lock);
640270096Strasz
641270096Strasz	vp = anp->an_vnode;
642270096Strasz	if (vp != NULL) {
643272512Strasz		error = vget(vp, flags | LK_RETRY, curthread);
644270096Strasz		if (error != 0) {
645270096Strasz			AUTOFS_WARN("vget failed with error %d", error);
646270096Strasz			sx_xunlock(&anp->an_vnode_lock);
647270096Strasz			return (error);
648270096Strasz		}
649270096Strasz		if (vp->v_iflag & VI_DOOMED) {
650270096Strasz			/*
651270096Strasz			 * We got forcibly unmounted.
652270096Strasz			 */
653270096Strasz			AUTOFS_DEBUG("doomed vnode");
654270096Strasz			sx_xunlock(&anp->an_vnode_lock);
655270096Strasz			vput(vp);
656270096Strasz
657270096Strasz			return (ENOENT);
658270096Strasz		}
659270096Strasz
660270096Strasz		*vpp = vp;
661270096Strasz		sx_xunlock(&anp->an_vnode_lock);
662270096Strasz		return (0);
663270096Strasz	}
664270096Strasz
665270096Strasz	error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp);
666270096Strasz	if (error != 0) {
667270096Strasz		sx_xunlock(&anp->an_vnode_lock);
668270096Strasz		return (error);
669270096Strasz	}
670270096Strasz
671270096Strasz	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
672270096Strasz	if (error != 0) {
673270096Strasz		sx_xunlock(&anp->an_vnode_lock);
674270096Strasz		vdrop(vp);
675270096Strasz		return (error);
676270096Strasz	}
677270096Strasz
678270096Strasz	vp->v_type = VDIR;
679270096Strasz	if (anp->an_parent == NULL)
680270096Strasz		vp->v_vflag |= VV_ROOT;
681270096Strasz	vp->v_data = anp;
682270096Strasz
683272512Strasz	VN_LOCK_ASHARE(vp);
684272512Strasz
685270096Strasz	error = insmntque(vp, mp);
686270096Strasz	if (error != 0) {
687270096Strasz		AUTOFS_WARN("insmntque() failed with error %d", error);
688270096Strasz		sx_xunlock(&anp->an_vnode_lock);
689270096Strasz		return (error);
690270096Strasz	}
691270096Strasz
692270096Strasz	KASSERT(anp->an_vnode == NULL, ("lost race"));
693270096Strasz	anp->an_vnode = vp;
694270096Strasz
695270096Strasz	sx_xunlock(&anp->an_vnode_lock);
696270096Strasz
697270096Strasz	*vpp = vp;
698270096Strasz	return (0);
699270096Strasz}
700