autofs_vnops.c revision 270281
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 270281 2014-08-21 15:59:25Z 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>
44270096Strasz#include <sys/systm.h>
45270096Strasz#include <sys/vnode.h>
46270096Strasz#include <machine/atomic.h>
47270096Strasz#include <vm/uma.h>
48270096Strasz
49270281Strasz#include <fs/autofs/autofs.h>
50270096Strasz
51270096Straszstatic int	autofs_trigger_vn(struct vnode *vp, const char *path,
52270096Strasz		    int pathlen, struct vnode **newvp);
53270096Strasz
54270096Straszstatic int
55270096Straszautofs_access(struct vop_access_args *ap)
56270096Strasz{
57270096Strasz
58270096Strasz	/*
59270096Strasz	 * Nothing to do here; the only kind of access control
60270096Strasz	 * needed is in autofs_mkdir().
61270096Strasz	 */
62270096Strasz
63270096Strasz	return (0);
64270096Strasz}
65270096Strasz
66270096Straszstatic int
67270096Straszautofs_getattr(struct vop_getattr_args *ap)
68270096Strasz{
69270096Strasz	struct vnode *vp, *newvp;
70270096Strasz	struct autofs_node *anp;
71270096Strasz	struct mount *mp;
72270096Strasz	struct vattr *vap;
73270096Strasz	int error;
74270096Strasz
75270096Strasz	vp = ap->a_vp;
76270096Strasz	anp = vp->v_data;
77270096Strasz	mp = vp->v_mount;
78270096Strasz	vap = ap->a_vap;
79270096Strasz
80270096Strasz	KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR"));
81270096Strasz
82270096Strasz	/*
83270096Strasz	 * The reason we must do this is that some tree-walking software,
84270096Strasz	 * namely fts(3), assumes that stat(".") results will not change
85270096Strasz	 * between chdir("subdir") and chdir(".."), and fails with ENOENT
86270096Strasz	 * otherwise.
87270096Strasz	 */
88270096Strasz	if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false &&
89270096Strasz	    autofs_ignore_thread(curthread) == false) {
90270096Strasz		error = autofs_trigger_vn(vp, "", 0, &newvp);
91270096Strasz		if (error != 0)
92270096Strasz			return (error);
93270096Strasz
94270096Strasz		if (newvp != NULL) {
95270096Strasz			error = VOP_GETATTR(newvp, ap->a_vap,
96270096Strasz			    ap->a_cred);
97270096Strasz			vput(newvp);
98270096Strasz			return (error);
99270096Strasz		}
100270096Strasz	}
101270096Strasz
102270096Strasz	vap->va_type = VDIR;
103270096Strasz	vap->va_mode = 0755;
104270096Strasz	vap->va_nlink = 3; /* XXX */
105270096Strasz	vap->va_uid = 0;
106270096Strasz	vap->va_gid = 0;
107270096Strasz	vap->va_rdev = NODEV;
108270096Strasz	vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
109270096Strasz	vap->va_fileid = anp->an_fileno;
110270096Strasz	vap->va_size = 512; /* XXX */
111270096Strasz	vap->va_blocksize = 512;
112270096Strasz	vap->va_mtime = anp->an_ctime;
113270096Strasz	vap->va_atime = anp->an_ctime;
114270096Strasz	vap->va_ctime = anp->an_ctime;
115270096Strasz	vap->va_birthtime = anp->an_ctime;
116270096Strasz	vap->va_gen = 0;
117270096Strasz	vap->va_flags = 0;
118270096Strasz	vap->va_rdev = 0;
119270096Strasz	vap->va_bytes = 512; /* XXX */
120270096Strasz	vap->va_filerev = 0;
121270096Strasz	vap->va_spare = 0;
122270096Strasz
123270096Strasz	return (0);
124270096Strasz}
125270096Strasz
126270096Strasz/*
127270096Strasz * Unlock the vnode, request automountd(8) action, and then lock it back.
128270096Strasz * If anything got mounted on top of the vnode, return the new filesystem's
129270096Strasz * root vnode in 'newvp', locked.
130270096Strasz */
131270096Straszstatic int
132270096Straszautofs_trigger_vn(struct vnode *vp, const char *path, int pathlen,
133270096Strasz    struct vnode **newvp)
134270096Strasz{
135270096Strasz	struct autofs_node *anp;
136270096Strasz	struct autofs_mount *amp;
137270096Strasz	struct autofs_softc *sc;
138270096Strasz	int error, lock_flags;
139270096Strasz
140270096Strasz	anp = vp->v_data;
141270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
142270096Strasz	sc = amp->am_softc;
143270096Strasz
144270096Strasz	/*
145270096Strasz	 * Release the vnode lock, so that other operations, in partcular
146270096Strasz	 * mounting a filesystem on top of it, can proceed.  Increase use
147270096Strasz	 * count, to prevent the vnode from being deallocated and to prevent
148270096Strasz	 * filesystem from being unmounted.
149270096Strasz	 */
150270096Strasz	lock_flags = VOP_ISLOCKED(vp);
151270096Strasz	vref(vp);
152270096Strasz	VOP_UNLOCK(vp, 0);
153270096Strasz
154270096Strasz	sx_xlock(&sc->sc_lock);
155270096Strasz
156270096Strasz	/*
157270096Strasz	 * XXX: Workaround for mounting the same thing multiple times; revisit.
158270096Strasz	 */
159270096Strasz	if (vp->v_mountedhere != NULL) {
160270096Strasz		error = 0;
161270096Strasz		goto mounted;
162270096Strasz	}
163270096Strasz
164270096Strasz	error = autofs_trigger(anp, path, pathlen);
165270096Straszmounted:
166270096Strasz	sx_xunlock(&sc->sc_lock);
167270096Strasz	vn_lock(vp, lock_flags | LK_RETRY);
168270096Strasz	vunref(vp);
169270096Strasz	if ((vp->v_iflag & VI_DOOMED) != 0) {
170270096Strasz		AUTOFS_DEBUG("VI_DOOMED");
171270096Strasz		return (ENOENT);
172270096Strasz	}
173270096Strasz
174270096Strasz	if (error != 0)
175270096Strasz		return (error);
176270096Strasz
177270096Strasz	if (vp->v_mountedhere == NULL) {
178270096Strasz		*newvp = NULL;
179270096Strasz		return (0);
180270096Strasz	} else {
181270096Strasz		/*
182270096Strasz		 * If the operation that succeeded was mount, then mark
183270096Strasz		 * the node as non-cached.  Otherwise, if someone unmounts
184270096Strasz		 * the filesystem before the cache times out, we will fail
185270096Strasz		 * to trigger.
186270096Strasz		 */
187270096Strasz		anp->an_cached = false;
188270096Strasz	}
189270096Strasz
190270096Strasz	error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp);
191270096Strasz	if (error != 0) {
192270096Strasz		AUTOFS_WARN("VFS_ROOT() failed with error %d", error);
193270096Strasz		return (error);
194270096Strasz	}
195270096Strasz
196270096Strasz	return (0);
197270096Strasz}
198270096Strasz
199270096Straszstatic int
200270207Straszautofs_vget_callback(struct mount *mp, void *arg, int lkflags __unused,
201270207Strasz    struct vnode **vpp)
202270207Strasz{
203270207Strasz
204270207Strasz
205270207Strasz	return (autofs_node_vn(arg, mp, vpp));
206270207Strasz}
207270207Strasz
208270207Straszstatic int
209270096Straszautofs_lookup(struct vop_lookup_args *ap)
210270096Strasz{
211270096Strasz	struct vnode *dvp, *newvp, **vpp;
212270096Strasz	struct mount *mp;
213270096Strasz	struct autofs_mount *amp;
214270096Strasz	struct autofs_node *anp, *child;
215270096Strasz	struct componentname *cnp;
216270096Strasz	int error, lock_flags;
217270096Strasz
218270096Strasz	dvp = ap->a_dvp;
219270096Strasz	vpp = ap->a_vpp;
220270096Strasz	mp = dvp->v_mount;
221270096Strasz	amp = VFSTOAUTOFS(mp);
222270096Strasz	anp = dvp->v_data;
223270096Strasz	cnp = ap->a_cnp;
224270096Strasz
225270096Strasz	if (cnp->cn_flags & ISDOTDOT) {
226270096Strasz		KASSERT(anp->an_parent != NULL, ("NULL parent"));
227270096Strasz		/*
228270207Strasz		 * Note that in this case, dvp is the child vnode, and we
229270207Strasz		 * are looking up the parent vnode - exactly reverse from
230270207Strasz		 * normal operation.  Unlocking dvp requires some rather
231270207Strasz		 * tricky unlock/relock dance to prevent mp from being freed;
232270207Strasz		 * use vn_vget_ino_gen() which takes care of all that.
233270096Strasz		 */
234270207Strasz		error = vn_vget_ino_gen(dvp, autofs_vget_callback,
235270207Strasz		    anp->an_parent, 0, vpp);
236270096Strasz		if (error != 0) {
237270207Strasz			AUTOFS_WARN("vn_vget_ino_gen() failed with error %d",
238270096Strasz			    error);
239270207Strasz			return (error);
240270096Strasz		}
241270096Strasz		return (error);
242270096Strasz	}
243270096Strasz
244270096Strasz	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
245270096Strasz		vref(dvp);
246270096Strasz		*vpp = dvp;
247270096Strasz
248270096Strasz		return (0);
249270096Strasz	}
250270096Strasz
251270096Strasz	if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false &&
252270096Strasz	    autofs_ignore_thread(cnp->cn_thread) == false) {
253270096Strasz		error = autofs_trigger_vn(dvp,
254270096Strasz		    cnp->cn_nameptr, cnp->cn_namelen, &newvp);
255270096Strasz		if (error != 0)
256270096Strasz			return (error);
257270096Strasz
258270096Strasz		if (newvp != NULL) {
259270096Strasz			error = VOP_LOOKUP(newvp, ap->a_vpp, ap->a_cnp);
260270096Strasz
261270096Strasz			/*
262270096Strasz			 * Instead of figuring out whether our vnode should
263270096Strasz			 * be locked or not given the error and cnp flags,
264270096Strasz			 * just "copy" the lock status from vnode returned
265270096Strasz			 * by mounted filesystem's VOP_LOOKUP().  Get rid
266270096Strasz			 * of that new vnode afterwards.
267270096Strasz			 */
268270096Strasz			lock_flags = VOP_ISLOCKED(newvp);
269270096Strasz			if (lock_flags == 0) {
270270096Strasz				VOP_UNLOCK(dvp, 0);
271270096Strasz				vrele(newvp);
272270096Strasz			} else {
273270096Strasz				vput(newvp);
274270096Strasz			}
275270096Strasz			return (error);
276270096Strasz		}
277270096Strasz	}
278270096Strasz
279270096Strasz	if (cnp->cn_nameiop == RENAME)
280270096Strasz		return (EOPNOTSUPP);
281270096Strasz
282270096Strasz	AUTOFS_LOCK(amp);
283270096Strasz	error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child);
284270096Strasz	if (error != 0) {
285270096Strasz		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
286270096Strasz			AUTOFS_UNLOCK(amp);
287270096Strasz			return (EJUSTRETURN);
288270096Strasz		}
289270096Strasz
290270096Strasz		AUTOFS_UNLOCK(amp);
291270096Strasz		return (ENOENT);
292270096Strasz	}
293270096Strasz
294270096Strasz	/*
295270096Strasz	 * XXX: Dropping the node here is ok, because we never remove nodes.
296270096Strasz	 */
297270096Strasz	AUTOFS_UNLOCK(amp);
298270096Strasz
299270096Strasz	error = autofs_node_vn(child, mp, vpp);
300270096Strasz	if (error != 0) {
301270096Strasz		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE)
302270096Strasz			return (EJUSTRETURN);
303270096Strasz
304270096Strasz		return (error);
305270096Strasz	}
306270096Strasz
307270096Strasz	return (0);
308270096Strasz}
309270096Strasz
310270096Straszstatic int
311270096Straszautofs_mkdir(struct vop_mkdir_args *ap)
312270096Strasz{
313270096Strasz	struct vnode *vp;
314270096Strasz	struct autofs_node *anp;
315270096Strasz	struct autofs_mount *amp;
316270096Strasz	struct autofs_node *child;
317270096Strasz	int error;
318270096Strasz
319270096Strasz	vp = ap->a_dvp;
320270096Strasz	anp = vp->v_data;
321270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
322270096Strasz
323270096Strasz	/*
324270096Strasz	 * Do not allow mkdir() if the calling thread is not
325270096Strasz	 * automountd(8) descendant.
326270096Strasz	 */
327270096Strasz	if (autofs_ignore_thread(curthread) == false)
328270096Strasz		return (EPERM);
329270096Strasz
330270096Strasz	AUTOFS_LOCK(amp);
331270096Strasz	error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr,
332270096Strasz	    ap->a_cnp->cn_namelen, &child);
333270096Strasz	if (error != 0) {
334270096Strasz		AUTOFS_UNLOCK(amp);
335270096Strasz		return (error);
336270096Strasz	}
337270096Strasz	AUTOFS_UNLOCK(amp);
338270096Strasz
339270096Strasz	error = autofs_node_vn(child, vp->v_mount, ap->a_vpp);
340270096Strasz
341270096Strasz	return (error);
342270096Strasz}
343270096Strasz
344270096Straszstatic int
345270096Straszautofs_readdir_one(struct uio *uio, const char *name, int fileno)
346270096Strasz{
347270096Strasz	struct dirent dirent;
348270096Strasz	int error, i;
349270096Strasz
350270096Strasz	memset(&dirent, 0, sizeof(dirent));
351270096Strasz	dirent.d_type = DT_DIR;
352270096Strasz	dirent.d_reclen = AUTOFS_DELEN;
353270096Strasz	dirent.d_fileno = fileno;
354270096Strasz	/* PFS_DELEN was picked to fit PFS_NAMLEN */
355270096Strasz	for (i = 0; i < AUTOFS_NAMELEN - 1 && name[i] != '\0'; ++i)
356270096Strasz		dirent.d_name[i] = name[i];
357270096Strasz	dirent.d_name[i] = 0;
358270096Strasz	dirent.d_namlen = i;
359270096Strasz
360270096Strasz	error = uiomove(&dirent, AUTOFS_DELEN, uio);
361270096Strasz	return (error);
362270096Strasz}
363270096Strasz
364270096Straszstatic int
365270096Straszautofs_readdir(struct vop_readdir_args *ap)
366270096Strasz{
367270096Strasz	struct vnode *vp, *newvp;
368270096Strasz	struct autofs_mount *amp;
369270096Strasz	struct autofs_node *anp, *child;
370270096Strasz	struct uio *uio;
371270096Strasz	off_t offset;
372270096Strasz	int error, i, resid;
373270096Strasz
374270096Strasz	vp = ap->a_vp;
375270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
376270096Strasz	anp = vp->v_data;
377270096Strasz	uio = ap->a_uio;
378270096Strasz
379270096Strasz	KASSERT(vp->v_type == VDIR, ("!VDIR"));
380270096Strasz
381270096Strasz	if (autofs_cached(anp, NULL, 0) == false &&
382270096Strasz	    autofs_ignore_thread(curthread) == false) {
383270096Strasz		error = autofs_trigger_vn(vp, "", 0, &newvp);
384270096Strasz		if (error != 0)
385270096Strasz			return (error);
386270096Strasz
387270096Strasz		if (newvp != NULL) {
388270096Strasz			error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred,
389270096Strasz			    ap->a_eofflag, ap->a_ncookies, ap->a_cookies);
390270096Strasz			vput(newvp);
391270096Strasz			return (error);
392270096Strasz		}
393270096Strasz	}
394270096Strasz
395270096Strasz	/* only allow reading entire entries */
396270096Strasz	offset = uio->uio_offset;
397270096Strasz	resid = uio->uio_resid;
398270096Strasz	if (offset < 0 || offset % AUTOFS_DELEN != 0 ||
399270096Strasz	    (resid && resid < AUTOFS_DELEN))
400270096Strasz		return (EINVAL);
401270096Strasz	if (resid == 0)
402270096Strasz		return (0);
403270096Strasz
404270096Strasz	if (ap->a_eofflag != NULL)
405270096Strasz		*ap->a_eofflag = TRUE;
406270096Strasz
407270096Strasz	if (offset == 0 && resid >= AUTOFS_DELEN) {
408270096Strasz		error = autofs_readdir_one(uio, ".", anp->an_fileno);
409270096Strasz		if (error != 0)
410270096Strasz			return (error);
411270096Strasz		offset += AUTOFS_DELEN;
412270096Strasz		resid -= AUTOFS_DELEN;
413270096Strasz	}
414270096Strasz
415270096Strasz	if (offset == AUTOFS_DELEN && resid >= AUTOFS_DELEN) {
416270096Strasz		if (anp->an_parent == NULL) {
417270096Strasz			/*
418270096Strasz			 * XXX: Right?
419270096Strasz			 */
420270096Strasz			error = autofs_readdir_one(uio, "..", anp->an_fileno);
421270096Strasz		} else {
422270096Strasz			error = autofs_readdir_one(uio, "..",
423270096Strasz			    anp->an_parent->an_fileno);
424270096Strasz		}
425270096Strasz		if (error != 0)
426270096Strasz			return (error);
427270096Strasz		offset += AUTOFS_DELEN;
428270096Strasz		resid -= AUTOFS_DELEN;
429270096Strasz	}
430270096Strasz
431270096Strasz	i = 2; /* Account for "." and "..". */
432270096Strasz	AUTOFS_LOCK(amp);
433270096Strasz	TAILQ_FOREACH(child, &anp->an_children, an_next) {
434270096Strasz		if (resid < AUTOFS_DELEN) {
435270096Strasz			if (ap->a_eofflag != NULL)
436270096Strasz				*ap->a_eofflag = 0;
437270096Strasz			break;
438270096Strasz		}
439270096Strasz
440270096Strasz		/*
441270096Strasz		 * Skip entries returned by previous call to getdents().
442270096Strasz		 */
443270096Strasz		i++;
444270096Strasz		if (i * AUTOFS_DELEN <= offset)
445270096Strasz			continue;
446270096Strasz
447270096Strasz		error = autofs_readdir_one(uio, child->an_name,
448270096Strasz		    child->an_fileno);
449270096Strasz		if (error != 0) {
450270096Strasz			AUTOFS_UNLOCK(amp);
451270096Strasz			return (error);
452270096Strasz		}
453270096Strasz		offset += AUTOFS_DELEN;
454270096Strasz		resid -= AUTOFS_DELEN;
455270096Strasz	}
456270096Strasz
457270096Strasz	AUTOFS_UNLOCK(amp);
458270096Strasz	return (0);
459270096Strasz}
460270096Strasz
461270096Straszstatic int
462270096Straszautofs_reclaim(struct vop_reclaim_args *ap)
463270096Strasz{
464270096Strasz	struct vnode *vp = ap->a_vp;
465270096Strasz	struct autofs_node *anp = vp->v_data;
466270096Strasz
467270096Strasz	vp = ap->a_vp;
468270096Strasz	anp = vp->v_data;
469270096Strasz
470270096Strasz	/*
471270096Strasz	 * We do not free autofs_node here; instead we are
472270096Strasz	 * destroying them in autofs_node_delete().
473270096Strasz	 */
474270096Strasz	sx_xlock(&anp->an_vnode_lock);
475270096Strasz	anp->an_vnode = NULL;
476270096Strasz	vp->v_data = NULL;
477270096Strasz	sx_xunlock(&anp->an_vnode_lock);
478270096Strasz
479270096Strasz	return (0);
480270096Strasz}
481270096Strasz
482270096Straszstruct vop_vector autofs_vnodeops = {
483270096Strasz	.vop_default =		&default_vnodeops,
484270096Strasz
485270096Strasz	.vop_access =		autofs_access,
486270096Strasz	.vop_lookup =		autofs_lookup,
487270096Strasz	.vop_create =		VOP_EOPNOTSUPP,
488270096Strasz	.vop_getattr =		autofs_getattr,
489270096Strasz	.vop_link =		VOP_EOPNOTSUPP,
490270096Strasz	.vop_mkdir =		autofs_mkdir,
491270096Strasz	.vop_mknod =		VOP_EOPNOTSUPP,
492270096Strasz	.vop_read =		VOP_EOPNOTSUPP,
493270096Strasz	.vop_readdir =		autofs_readdir,
494270096Strasz	.vop_remove =		VOP_EOPNOTSUPP,
495270096Strasz	.vop_rename =		VOP_EOPNOTSUPP,
496270096Strasz	.vop_rmdir =		VOP_EOPNOTSUPP,
497270096Strasz	.vop_setattr =		VOP_EOPNOTSUPP,
498270096Strasz	.vop_symlink =		VOP_EOPNOTSUPP,
499270096Strasz	.vop_write =		VOP_EOPNOTSUPP,
500270096Strasz	.vop_reclaim =		autofs_reclaim,
501270096Strasz};
502270096Strasz
503270096Straszint
504270096Straszautofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
505270096Strasz    const char *name, int namelen, struct autofs_node **anpp)
506270096Strasz{
507270096Strasz	struct autofs_node *anp;
508270096Strasz
509270096Strasz	if (parent != NULL)
510270096Strasz		AUTOFS_ASSERT_LOCKED(parent->an_mount);
511270096Strasz
512270096Strasz	anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO);
513270096Strasz	if (namelen >= 0)
514270096Strasz		anp->an_name = strndup(name, namelen, M_AUTOFS);
515270096Strasz	else
516270096Strasz		anp->an_name = strdup(name, M_AUTOFS);
517270096Strasz	anp->an_fileno = atomic_fetchadd_int(&amp->am_last_fileno, 1);
518270096Strasz	callout_init(&anp->an_callout, 1);
519270096Strasz	/*
520270096Strasz	 * The reason for SX_NOWITNESS here is that witness(4)
521270096Strasz	 * cannot tell vnodes apart, so the following perfectly
522270096Strasz	 * valid lock order...
523270096Strasz	 *
524270096Strasz	 * vnode lock A -> autofsvlk B -> vnode lock B
525270096Strasz	 *
526270096Strasz	 * ... gets reported as a LOR.
527270096Strasz	 */
528270096Strasz	sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS);
529270096Strasz	getnanotime(&anp->an_ctime);
530270096Strasz	anp->an_parent = parent;
531270096Strasz	anp->an_mount = amp;
532270096Strasz	if (parent != NULL)
533270096Strasz		TAILQ_INSERT_TAIL(&parent->an_children, anp, an_next);
534270096Strasz	TAILQ_INIT(&anp->an_children);
535270096Strasz
536270096Strasz	*anpp = anp;
537270096Strasz	return (0);
538270096Strasz}
539270096Strasz
540270096Straszint
541270096Straszautofs_node_find(struct autofs_node *parent, const char *name,
542270096Strasz    int namelen, struct autofs_node **anpp)
543270096Strasz{
544270096Strasz	struct autofs_node *anp;
545270096Strasz
546270096Strasz	AUTOFS_ASSERT_LOCKED(parent->an_mount);
547270096Strasz
548270096Strasz	TAILQ_FOREACH(anp, &parent->an_children, an_next) {
549270096Strasz		if (namelen >= 0) {
550270096Strasz			if (strncmp(anp->an_name, name, namelen) != 0)
551270096Strasz				continue;
552270096Strasz		} else {
553270096Strasz			if (strcmp(anp->an_name, name) != 0)
554270096Strasz				continue;
555270096Strasz		}
556270096Strasz
557270096Strasz		if (anpp != NULL)
558270096Strasz			*anpp = anp;
559270096Strasz		return (0);
560270096Strasz	}
561270096Strasz
562270096Strasz	return (ENOENT);
563270096Strasz}
564270096Strasz
565270096Straszvoid
566270096Straszautofs_node_delete(struct autofs_node *anp)
567270096Strasz{
568270096Strasz	struct autofs_node *parent;
569270096Strasz
570270096Strasz	AUTOFS_ASSERT_LOCKED(anp->an_mount);
571270096Strasz	KASSERT(TAILQ_EMPTY(&anp->an_children), ("have children"));
572270096Strasz
573270096Strasz	callout_drain(&anp->an_callout);
574270096Strasz
575270096Strasz	parent = anp->an_parent;
576270096Strasz	if (parent != NULL)
577270096Strasz		TAILQ_REMOVE(&parent->an_children, anp, an_next);
578270096Strasz	sx_destroy(&anp->an_vnode_lock);
579270096Strasz	free(anp->an_name, M_AUTOFS);
580270096Strasz	uma_zfree(autofs_node_zone, anp);
581270096Strasz}
582270096Strasz
583270096Straszint
584270096Straszautofs_node_vn(struct autofs_node *anp, struct mount *mp, struct vnode **vpp)
585270096Strasz{
586270096Strasz	struct vnode *vp;
587270096Strasz	int error;
588270096Strasz
589270096Strasz	AUTOFS_ASSERT_UNLOCKED(anp->an_mount);
590270096Strasz
591270096Strasz	sx_xlock(&anp->an_vnode_lock);
592270096Strasz
593270096Strasz	vp = anp->an_vnode;
594270096Strasz	if (vp != NULL) {
595270096Strasz		error = vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
596270096Strasz		if (error != 0) {
597270096Strasz			AUTOFS_WARN("vget failed with error %d", error);
598270096Strasz			sx_xunlock(&anp->an_vnode_lock);
599270096Strasz			return (error);
600270096Strasz		}
601270096Strasz		if (vp->v_iflag & VI_DOOMED) {
602270096Strasz			/*
603270096Strasz			 * We got forcibly unmounted.
604270096Strasz			 */
605270096Strasz			AUTOFS_DEBUG("doomed vnode");
606270096Strasz			sx_xunlock(&anp->an_vnode_lock);
607270096Strasz			vput(vp);
608270096Strasz
609270096Strasz			return (ENOENT);
610270096Strasz		}
611270096Strasz
612270096Strasz		*vpp = vp;
613270096Strasz		sx_xunlock(&anp->an_vnode_lock);
614270096Strasz		return (0);
615270096Strasz	}
616270096Strasz
617270096Strasz	error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp);
618270096Strasz	if (error != 0) {
619270096Strasz		sx_xunlock(&anp->an_vnode_lock);
620270096Strasz		return (error);
621270096Strasz	}
622270096Strasz
623270096Strasz	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
624270096Strasz	if (error != 0) {
625270096Strasz		sx_xunlock(&anp->an_vnode_lock);
626270096Strasz		vdrop(vp);
627270096Strasz		return (error);
628270096Strasz	}
629270096Strasz
630270096Strasz	vp->v_type = VDIR;
631270096Strasz	if (anp->an_parent == NULL)
632270096Strasz		vp->v_vflag |= VV_ROOT;
633270096Strasz	vp->v_data = anp;
634270096Strasz
635270096Strasz	error = insmntque(vp, mp);
636270096Strasz	if (error != 0) {
637270096Strasz		AUTOFS_WARN("insmntque() failed with error %d", error);
638270096Strasz		sx_xunlock(&anp->an_vnode_lock);
639270096Strasz		return (error);
640270096Strasz	}
641270096Strasz
642270096Strasz	KASSERT(anp->an_vnode == NULL, ("lost race"));
643270096Strasz	anp->an_vnode = vp;
644270096Strasz
645270096Strasz	sx_xunlock(&anp->an_vnode_lock);
646270096Strasz
647270096Strasz	*vpp = vp;
648270096Strasz	return (0);
649270096Strasz}
650