autofs_vnops.c revision 297236
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 297236 2016-03-24 13:34:39Z 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>
47297236Strasz#include <sys/tree.h>
48270096Strasz#include <sys/vnode.h>
49270096Strasz#include <machine/atomic.h>
50270096Strasz#include <vm/uma.h>
51270096Strasz
52270281Strasz#include <fs/autofs/autofs.h>
53270096Strasz
54270096Straszstatic int	autofs_trigger_vn(struct vnode *vp, const char *path,
55270096Strasz		    int pathlen, struct vnode **newvp);
56270096Strasz
57270402Straszextern struct autofs_softc	*autofs_softc;
58270402Strasz
59270096Straszstatic int
60270096Straszautofs_access(struct vop_access_args *ap)
61270096Strasz{
62270096Strasz
63270096Strasz	/*
64270096Strasz	 * Nothing to do here; the only kind of access control
65270096Strasz	 * needed is in autofs_mkdir().
66270096Strasz	 */
67270096Strasz
68270096Strasz	return (0);
69270096Strasz}
70270096Strasz
71270096Straszstatic int
72270096Straszautofs_getattr(struct vop_getattr_args *ap)
73270096Strasz{
74270096Strasz	struct vnode *vp, *newvp;
75270096Strasz	struct autofs_node *anp;
76270096Strasz	struct mount *mp;
77270096Strasz	struct vattr *vap;
78270096Strasz	int error;
79270096Strasz
80270096Strasz	vp = ap->a_vp;
81270096Strasz	anp = vp->v_data;
82270096Strasz	mp = vp->v_mount;
83270096Strasz	vap = ap->a_vap;
84270096Strasz
85270096Strasz	KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR"));
86270096Strasz
87270096Strasz	/*
88270096Strasz	 * The reason we must do this is that some tree-walking software,
89270096Strasz	 * namely fts(3), assumes that stat(".") results will not change
90270096Strasz	 * between chdir("subdir") and chdir(".."), and fails with ENOENT
91270096Strasz	 * otherwise.
92270096Strasz	 */
93270096Strasz	if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false &&
94270096Strasz	    autofs_ignore_thread(curthread) == false) {
95270096Strasz		error = autofs_trigger_vn(vp, "", 0, &newvp);
96270096Strasz		if (error != 0)
97270096Strasz			return (error);
98270096Strasz
99270096Strasz		if (newvp != NULL) {
100270096Strasz			error = VOP_GETATTR(newvp, ap->a_vap,
101270096Strasz			    ap->a_cred);
102270096Strasz			vput(newvp);
103270096Strasz			return (error);
104270096Strasz		}
105270096Strasz	}
106270096Strasz
107270096Strasz	vap->va_type = VDIR;
108270096Strasz	vap->va_mode = 0755;
109270096Strasz	vap->va_nlink = 3; /* XXX */
110270096Strasz	vap->va_uid = 0;
111270096Strasz	vap->va_gid = 0;
112270096Strasz	vap->va_rdev = NODEV;
113270096Strasz	vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
114270096Strasz	vap->va_fileid = anp->an_fileno;
115296718Strasz	vap->va_size = S_BLKSIZE;
116296718Strasz	vap->va_blocksize = S_BLKSIZE;
117270096Strasz	vap->va_mtime = anp->an_ctime;
118270096Strasz	vap->va_atime = anp->an_ctime;
119270096Strasz	vap->va_ctime = anp->an_ctime;
120270096Strasz	vap->va_birthtime = anp->an_ctime;
121270096Strasz	vap->va_gen = 0;
122270096Strasz	vap->va_flags = 0;
123270096Strasz	vap->va_rdev = 0;
124296718Strasz	vap->va_bytes = S_BLKSIZE;
125270096Strasz	vap->va_filerev = 0;
126270096Strasz	vap->va_spare = 0;
127270096Strasz
128270096Strasz	return (0);
129270096Strasz}
130270096Strasz
131270096Strasz/*
132270096Strasz * Unlock the vnode, request automountd(8) action, and then lock it back.
133270096Strasz * If anything got mounted on top of the vnode, return the new filesystem's
134270096Strasz * root vnode in 'newvp', locked.
135270096Strasz */
136270096Straszstatic int
137270096Straszautofs_trigger_vn(struct vnode *vp, const char *path, int pathlen,
138270096Strasz    struct vnode **newvp)
139270096Strasz{
140270096Strasz	struct autofs_node *anp;
141270096Strasz	struct autofs_mount *amp;
142270096Strasz	int error, lock_flags;
143270096Strasz
144270096Strasz	anp = vp->v_data;
145270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
146270096Strasz
147270096Strasz	/*
148270096Strasz	 * Release the vnode lock, so that other operations, in partcular
149270096Strasz	 * mounting a filesystem on top of it, can proceed.  Increase use
150270096Strasz	 * count, to prevent the vnode from being deallocated and to prevent
151270096Strasz	 * filesystem from being unmounted.
152270096Strasz	 */
153270096Strasz	lock_flags = VOP_ISLOCKED(vp);
154270096Strasz	vref(vp);
155270096Strasz	VOP_UNLOCK(vp, 0);
156270096Strasz
157270402Strasz	sx_xlock(&autofs_softc->sc_lock);
158270096Strasz
159270096Strasz	/*
160270096Strasz	 * XXX: Workaround for mounting the same thing multiple times; revisit.
161270096Strasz	 */
162270096Strasz	if (vp->v_mountedhere != NULL) {
163270096Strasz		error = 0;
164270096Strasz		goto mounted;
165270096Strasz	}
166270096Strasz
167270096Strasz	error = autofs_trigger(anp, path, pathlen);
168270096Straszmounted:
169270402Strasz	sx_xunlock(&autofs_softc->sc_lock);
170270096Strasz	vn_lock(vp, lock_flags | LK_RETRY);
171270096Strasz	vunref(vp);
172270096Strasz	if ((vp->v_iflag & VI_DOOMED) != 0) {
173270096Strasz		AUTOFS_DEBUG("VI_DOOMED");
174270096Strasz		return (ENOENT);
175270096Strasz	}
176270096Strasz
177270096Strasz	if (error != 0)
178270096Strasz		return (error);
179270096Strasz
180270096Strasz	if (vp->v_mountedhere == NULL) {
181270096Strasz		*newvp = NULL;
182270096Strasz		return (0);
183270096Strasz	} else {
184270096Strasz		/*
185270096Strasz		 * If the operation that succeeded was mount, then mark
186270096Strasz		 * the node as non-cached.  Otherwise, if someone unmounts
187270096Strasz		 * the filesystem before the cache times out, we will fail
188270096Strasz		 * to trigger.
189270096Strasz		 */
190270096Strasz		anp->an_cached = false;
191270096Strasz	}
192270096Strasz
193270096Strasz	error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp);
194270096Strasz	if (error != 0) {
195270096Strasz		AUTOFS_WARN("VFS_ROOT() failed with error %d", error);
196270096Strasz		return (error);
197270096Strasz	}
198270096Strasz
199270096Strasz	return (0);
200270096Strasz}
201270096Strasz
202270096Straszstatic int
203272512Straszautofs_vget_callback(struct mount *mp, void *arg, int flags,
204270207Strasz    struct vnode **vpp)
205270207Strasz{
206270207Strasz
207270207Strasz
208272512Strasz	return (autofs_node_vn(arg, mp, flags, vpp));
209270207Strasz}
210270207Strasz
211270207Straszstatic int
212270096Straszautofs_lookup(struct vop_lookup_args *ap)
213270096Strasz{
214270096Strasz	struct vnode *dvp, *newvp, **vpp;
215270096Strasz	struct mount *mp;
216270096Strasz	struct autofs_mount *amp;
217270096Strasz	struct autofs_node *anp, *child;
218270096Strasz	struct componentname *cnp;
219296715Strasz	int error;
220270096Strasz
221270096Strasz	dvp = ap->a_dvp;
222270096Strasz	vpp = ap->a_vpp;
223270096Strasz	mp = dvp->v_mount;
224270096Strasz	amp = VFSTOAUTOFS(mp);
225270096Strasz	anp = dvp->v_data;
226270096Strasz	cnp = ap->a_cnp;
227270096Strasz
228270096Strasz	if (cnp->cn_flags & ISDOTDOT) {
229270096Strasz		KASSERT(anp->an_parent != NULL, ("NULL parent"));
230270096Strasz		/*
231270207Strasz		 * Note that in this case, dvp is the child vnode, and we
232270207Strasz		 * are looking up the parent vnode - exactly reverse from
233270207Strasz		 * normal operation.  Unlocking dvp requires some rather
234270207Strasz		 * tricky unlock/relock dance to prevent mp from being freed;
235270207Strasz		 * use vn_vget_ino_gen() which takes care of all that.
236270096Strasz		 */
237270207Strasz		error = vn_vget_ino_gen(dvp, autofs_vget_callback,
238272512Strasz		    anp->an_parent, cnp->cn_lkflags, vpp);
239270096Strasz		if (error != 0) {
240270207Strasz			AUTOFS_WARN("vn_vget_ino_gen() failed with error %d",
241270096Strasz			    error);
242270207Strasz			return (error);
243270096Strasz		}
244270096Strasz		return (error);
245270096Strasz	}
246270096Strasz
247270096Strasz	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
248270096Strasz		vref(dvp);
249270096Strasz		*vpp = dvp;
250270096Strasz
251270096Strasz		return (0);
252270096Strasz	}
253270096Strasz
254270096Strasz	if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false &&
255270096Strasz	    autofs_ignore_thread(cnp->cn_thread) == false) {
256270096Strasz		error = autofs_trigger_vn(dvp,
257270096Strasz		    cnp->cn_nameptr, cnp->cn_namelen, &newvp);
258270096Strasz		if (error != 0)
259270096Strasz			return (error);
260270096Strasz
261270096Strasz		if (newvp != NULL) {
262270096Strasz			/*
263296715Strasz			 * The target filesystem got automounted.
264296715Strasz			 * Let the lookup(9) go around with the same
265296715Strasz			 * path component.
266270096Strasz			 */
267296715Strasz			vput(newvp);
268296715Strasz			return (ERELOOKUP);
269270096Strasz		}
270270096Strasz	}
271270096Strasz
272272470Strasz	AUTOFS_SLOCK(amp);
273270096Strasz	error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child);
274270096Strasz	if (error != 0) {
275270096Strasz		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
276272470Strasz			AUTOFS_SUNLOCK(amp);
277270096Strasz			return (EJUSTRETURN);
278270096Strasz		}
279270096Strasz
280272470Strasz		AUTOFS_SUNLOCK(amp);
281270096Strasz		return (ENOENT);
282270096Strasz	}
283270096Strasz
284270096Strasz	/*
285270096Strasz	 * XXX: Dropping the node here is ok, because we never remove nodes.
286270096Strasz	 */
287272470Strasz	AUTOFS_SUNLOCK(amp);
288270096Strasz
289272512Strasz	error = autofs_node_vn(child, mp, cnp->cn_lkflags, vpp);
290270096Strasz	if (error != 0) {
291270096Strasz		if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE)
292270096Strasz			return (EJUSTRETURN);
293270096Strasz
294270096Strasz		return (error);
295270096Strasz	}
296270096Strasz
297270096Strasz	return (0);
298270096Strasz}
299270096Strasz
300270096Straszstatic int
301270096Straszautofs_mkdir(struct vop_mkdir_args *ap)
302270096Strasz{
303270096Strasz	struct vnode *vp;
304270096Strasz	struct autofs_node *anp;
305270096Strasz	struct autofs_mount *amp;
306270096Strasz	struct autofs_node *child;
307270096Strasz	int error;
308270096Strasz
309270096Strasz	vp = ap->a_dvp;
310270096Strasz	anp = vp->v_data;
311270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
312270096Strasz
313270096Strasz	/*
314270096Strasz	 * Do not allow mkdir() if the calling thread is not
315270096Strasz	 * automountd(8) descendant.
316270096Strasz	 */
317270096Strasz	if (autofs_ignore_thread(curthread) == false)
318270096Strasz		return (EPERM);
319270096Strasz
320272470Strasz	AUTOFS_XLOCK(amp);
321270096Strasz	error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr,
322270096Strasz	    ap->a_cnp->cn_namelen, &child);
323270096Strasz	if (error != 0) {
324272470Strasz		AUTOFS_XUNLOCK(amp);
325270096Strasz		return (error);
326270096Strasz	}
327272470Strasz	AUTOFS_XUNLOCK(amp);
328270096Strasz
329272512Strasz	error = autofs_node_vn(child, vp->v_mount, LK_EXCLUSIVE, ap->a_vpp);
330270096Strasz
331270096Strasz	return (error);
332270096Strasz}
333270096Strasz
334296798Strasz/*
335296798Strasz * Write out a single 'struct dirent', based on 'name' and 'fileno' arguments.
336296798Strasz */
337270096Straszstatic int
338296798Straszautofs_readdir_one(struct uio *uio, const char *name, int fileno,
339296798Strasz    size_t *reclenp)
340270096Strasz{
341270096Strasz	struct dirent dirent;
342296798Strasz	size_t namlen, padded_namlen, reclen;
343296798Strasz	int error;
344270096Strasz
345296798Strasz	namlen = strlen(name);
346296798Strasz	padded_namlen = roundup2(namlen + 1, __alignof(struct dirent));
347296798Strasz	KASSERT(padded_namlen <= MAXNAMLEN, ("%zd > MAXNAMLEN", padded_namlen));
348296798Strasz	reclen = offsetof(struct dirent, d_name) + padded_namlen;
349296798Strasz
350296798Strasz	if (reclenp != NULL)
351296798Strasz		*reclenp = reclen;
352296798Strasz
353296798Strasz	if (uio == NULL)
354296798Strasz		return (0);
355296798Strasz
356296798Strasz	if (uio->uio_resid < reclen)
357296798Strasz		return (EINVAL);
358296798Strasz
359296798Strasz	dirent.d_fileno = fileno;
360296798Strasz	dirent.d_reclen = reclen;
361270096Strasz	dirent.d_type = DT_DIR;
362296798Strasz	dirent.d_namlen = namlen;
363296798Strasz	memcpy(dirent.d_name, name, namlen);
364296798Strasz	memset(dirent.d_name + namlen, 0, padded_namlen - namlen);
365296798Strasz	error = uiomove(&dirent, reclen, uio);
366270096Strasz
367270096Strasz	return (error);
368270096Strasz}
369270096Strasz
370296798Straszstatic size_t
371296798Straszautofs_dirent_reclen(const char *name)
372296798Strasz{
373296798Strasz	size_t reclen;
374296798Strasz
375296937Strasz	(void)autofs_readdir_one(NULL, name, -1, &reclen);
376296798Strasz
377296798Strasz	return (reclen);
378296798Strasz}
379296798Strasz
380270096Straszstatic int
381270096Straszautofs_readdir(struct vop_readdir_args *ap)
382270096Strasz{
383270096Strasz	struct vnode *vp, *newvp;
384270096Strasz	struct autofs_mount *amp;
385270096Strasz	struct autofs_node *anp, *child;
386270096Strasz	struct uio *uio;
387296798Strasz	size_t reclen, reclens;
388296798Strasz	ssize_t initial_resid;
389296798Strasz	int error;
390270096Strasz
391270096Strasz	vp = ap->a_vp;
392270096Strasz	amp = VFSTOAUTOFS(vp->v_mount);
393270096Strasz	anp = vp->v_data;
394270096Strasz	uio = ap->a_uio;
395296798Strasz	initial_resid = ap->a_uio->uio_resid;
396270096Strasz
397270096Strasz	KASSERT(vp->v_type == VDIR, ("!VDIR"));
398270096Strasz
399270096Strasz	if (autofs_cached(anp, NULL, 0) == false &&
400270096Strasz	    autofs_ignore_thread(curthread) == false) {
401270096Strasz		error = autofs_trigger_vn(vp, "", 0, &newvp);
402270096Strasz		if (error != 0)
403270096Strasz			return (error);
404270096Strasz
405270096Strasz		if (newvp != NULL) {
406270096Strasz			error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred,
407270096Strasz			    ap->a_eofflag, ap->a_ncookies, ap->a_cookies);
408270096Strasz			vput(newvp);
409270096Strasz			return (error);
410270096Strasz		}
411270096Strasz	}
412270096Strasz
413296798Strasz	if (uio->uio_offset < 0)
414270096Strasz		return (EINVAL);
415270096Strasz
416270096Strasz	if (ap->a_eofflag != NULL)
417296798Strasz		*ap->a_eofflag = FALSE;
418270096Strasz
419296798Strasz	/*
420296798Strasz	 * Write out the directory entry for ".".  This is conditional
421296798Strasz	 * on the current offset into the directory; same applies to the
422296798Strasz	 * other two cases below.
423296798Strasz	 */
424296798Strasz	if (uio->uio_offset == 0) {
425296798Strasz		error = autofs_readdir_one(uio, ".", anp->an_fileno, &reclen);
426270096Strasz		if (error != 0)
427296798Strasz			goto out;
428270096Strasz	}
429296798Strasz	reclens = autofs_dirent_reclen(".");
430270096Strasz
431296798Strasz	/*
432296798Strasz	 * Write out the directory entry for "..".
433296798Strasz	 */
434296798Strasz	if (uio->uio_offset <= reclens) {
435296798Strasz		if (uio->uio_offset != reclens)
436296798Strasz			return (EINVAL);
437270096Strasz		if (anp->an_parent == NULL) {
438296798Strasz			error = autofs_readdir_one(uio, "..",
439296798Strasz			    anp->an_fileno, &reclen);
440270096Strasz		} else {
441270096Strasz			error = autofs_readdir_one(uio, "..",
442296798Strasz			    anp->an_parent->an_fileno, &reclen);
443270096Strasz		}
444270096Strasz		if (error != 0)
445296798Strasz			goto out;
446270096Strasz	}
447270096Strasz
448296798Strasz	reclens += autofs_dirent_reclen("..");
449296798Strasz
450296798Strasz	/*
451296798Strasz	 * Write out the directory entries for subdirectories.
452296798Strasz	 */
453272470Strasz	AUTOFS_SLOCK(amp);
454297236Strasz	RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
455296798Strasz		/*
456296798Strasz		 * Check the offset to skip entries returned by previous
457296798Strasz		 * calls to getdents().
458296798Strasz		 */
459296798Strasz		if (uio->uio_offset > reclens) {
460296798Strasz			reclens += autofs_dirent_reclen(child->an_name);
461296798Strasz			continue;
462270096Strasz		}
463270096Strasz
464270096Strasz		/*
465296798Strasz		 * Prevent seeking into the middle of dirent.
466270096Strasz		 */
467296798Strasz		if (uio->uio_offset != reclens) {
468296798Strasz			AUTOFS_SUNLOCK(amp);
469296798Strasz			return (EINVAL);
470296798Strasz		}
471270096Strasz
472270096Strasz		error = autofs_readdir_one(uio, child->an_name,
473296798Strasz		    child->an_fileno, &reclen);
474296798Strasz		reclens += reclen;
475270096Strasz		if (error != 0) {
476272470Strasz			AUTOFS_SUNLOCK(amp);
477296798Strasz			goto out;
478270096Strasz		}
479270096Strasz	}
480296798Strasz	AUTOFS_SUNLOCK(amp);
481270096Strasz
482296798Strasz	if (ap->a_eofflag != NULL)
483296798Strasz		*ap->a_eofflag = TRUE;
484296798Strasz
485270096Strasz	return (0);
486296798Strasz
487296798Straszout:
488296798Strasz	/*
489296798Strasz	 * Return error if the initial buffer was too small to do anything.
490296798Strasz	 */
491296798Strasz	if (uio->uio_resid == initial_resid)
492296798Strasz		return (error);
493296798Strasz
494296798Strasz	/*
495296798Strasz	 * Don't return an error if we managed to copy out some entries.
496296798Strasz	 */
497296798Strasz	if (uio->uio_resid < reclen)
498296798Strasz		return (0);
499296798Strasz
500296798Strasz	return (error);
501270096Strasz}
502270096Strasz
503270096Straszstatic int
504270096Straszautofs_reclaim(struct vop_reclaim_args *ap)
505270096Strasz{
506272836Strasz	struct vnode *vp;
507272836Strasz	struct autofs_node *anp;
508270096Strasz
509270096Strasz	vp = ap->a_vp;
510270096Strasz	anp = vp->v_data;
511270096Strasz
512270096Strasz	/*
513270096Strasz	 * We do not free autofs_node here; instead we are
514270096Strasz	 * destroying them in autofs_node_delete().
515270096Strasz	 */
516270096Strasz	sx_xlock(&anp->an_vnode_lock);
517270096Strasz	anp->an_vnode = NULL;
518270096Strasz	vp->v_data = NULL;
519270096Strasz	sx_xunlock(&anp->an_vnode_lock);
520270096Strasz
521270096Strasz	return (0);
522270096Strasz}
523270096Strasz
524270096Straszstruct vop_vector autofs_vnodeops = {
525270096Strasz	.vop_default =		&default_vnodeops,
526270096Strasz
527270096Strasz	.vop_access =		autofs_access,
528270096Strasz	.vop_lookup =		autofs_lookup,
529270096Strasz	.vop_create =		VOP_EOPNOTSUPP,
530270096Strasz	.vop_getattr =		autofs_getattr,
531270096Strasz	.vop_link =		VOP_EOPNOTSUPP,
532270096Strasz	.vop_mkdir =		autofs_mkdir,
533270096Strasz	.vop_mknod =		VOP_EOPNOTSUPP,
534270096Strasz	.vop_read =		VOP_EOPNOTSUPP,
535270096Strasz	.vop_readdir =		autofs_readdir,
536270096Strasz	.vop_remove =		VOP_EOPNOTSUPP,
537270096Strasz	.vop_rename =		VOP_EOPNOTSUPP,
538270096Strasz	.vop_rmdir =		VOP_EOPNOTSUPP,
539270096Strasz	.vop_setattr =		VOP_EOPNOTSUPP,
540270096Strasz	.vop_symlink =		VOP_EOPNOTSUPP,
541270096Strasz	.vop_write =		VOP_EOPNOTSUPP,
542270096Strasz	.vop_reclaim =		autofs_reclaim,
543270096Strasz};
544270096Strasz
545270096Straszint
546270096Straszautofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
547270096Strasz    const char *name, int namelen, struct autofs_node **anpp)
548270096Strasz{
549270096Strasz	struct autofs_node *anp;
550270096Strasz
551272931Strasz	if (parent != NULL) {
552272470Strasz		AUTOFS_ASSERT_XLOCKED(parent->an_mount);
553270096Strasz
554272931Strasz		KASSERT(autofs_node_find(parent, name, namelen, NULL) == ENOENT,
555272931Strasz		    ("node \"%s\" already exists", name));
556272931Strasz	}
557272931Strasz
558270096Strasz	anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO);
559270096Strasz	if (namelen >= 0)
560270096Strasz		anp->an_name = strndup(name, namelen, M_AUTOFS);
561270096Strasz	else
562270096Strasz		anp->an_name = strdup(name, M_AUTOFS);
563270096Strasz	anp->an_fileno = atomic_fetchadd_int(&amp->am_last_fileno, 1);
564270096Strasz	callout_init(&anp->an_callout, 1);
565270096Strasz	/*
566270096Strasz	 * The reason for SX_NOWITNESS here is that witness(4)
567270096Strasz	 * cannot tell vnodes apart, so the following perfectly
568270096Strasz	 * valid lock order...
569270096Strasz	 *
570270096Strasz	 * vnode lock A -> autofsvlk B -> vnode lock B
571270096Strasz	 *
572270096Strasz	 * ... gets reported as a LOR.
573270096Strasz	 */
574270096Strasz	sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS);
575270096Strasz	getnanotime(&anp->an_ctime);
576270096Strasz	anp->an_parent = parent;
577270096Strasz	anp->an_mount = amp;
578270096Strasz	if (parent != NULL)
579297236Strasz		RB_INSERT(autofs_node_tree, &parent->an_children, anp);
580297236Strasz	RB_INIT(&anp->an_children);
581270096Strasz
582270096Strasz	*anpp = anp;
583270096Strasz	return (0);
584270096Strasz}
585270096Strasz
586270096Straszint
587270096Straszautofs_node_find(struct autofs_node *parent, const char *name,
588270096Strasz    int namelen, struct autofs_node **anpp)
589270096Strasz{
590297236Strasz	struct autofs_node *anp, find;
591297236Strasz	int error;
592270096Strasz
593270096Strasz	AUTOFS_ASSERT_LOCKED(parent->an_mount);
594270096Strasz
595297236Strasz	if (namelen >= 0)
596297236Strasz		find.an_name = strndup(name, namelen, M_AUTOFS);
597297236Strasz	else
598297236Strasz		find.an_name = strdup(name, M_AUTOFS);
599270096Strasz
600297236Strasz	anp = RB_FIND(autofs_node_tree, &parent->an_children, &find);
601297236Strasz	if (anp != NULL) {
602297236Strasz		error = 0;
603270096Strasz		if (anpp != NULL)
604270096Strasz			*anpp = anp;
605297236Strasz	} else {
606297236Strasz		error = ENOENT;
607270096Strasz	}
608270096Strasz
609297236Strasz	free(find.an_name, M_AUTOFS);
610297236Strasz
611297236Strasz	return (error);
612270096Strasz}
613270096Strasz
614270096Straszvoid
615270096Straszautofs_node_delete(struct autofs_node *anp)
616270096Strasz{
617270096Strasz	struct autofs_node *parent;
618270096Strasz
619272470Strasz	AUTOFS_ASSERT_XLOCKED(anp->an_mount);
620297236Strasz	KASSERT(RB_EMPTY(&anp->an_children), ("have children"));
621270096Strasz
622270096Strasz	callout_drain(&anp->an_callout);
623270096Strasz
624270096Strasz	parent = anp->an_parent;
625270096Strasz	if (parent != NULL)
626297236Strasz		RB_REMOVE(autofs_node_tree, &parent->an_children, anp);
627270096Strasz	sx_destroy(&anp->an_vnode_lock);
628270096Strasz	free(anp->an_name, M_AUTOFS);
629270096Strasz	uma_zfree(autofs_node_zone, anp);
630270096Strasz}
631270096Strasz
632270096Straszint
633272512Straszautofs_node_vn(struct autofs_node *anp, struct mount *mp, int flags,
634272512Strasz    struct vnode **vpp)
635270096Strasz{
636270096Strasz	struct vnode *vp;
637270096Strasz	int error;
638270096Strasz
639270096Strasz	AUTOFS_ASSERT_UNLOCKED(anp->an_mount);
640270096Strasz
641270096Strasz	sx_xlock(&anp->an_vnode_lock);
642270096Strasz
643270096Strasz	vp = anp->an_vnode;
644270096Strasz	if (vp != NULL) {
645272512Strasz		error = vget(vp, flags | LK_RETRY, curthread);
646270096Strasz		if (error != 0) {
647270096Strasz			AUTOFS_WARN("vget failed with error %d", error);
648270096Strasz			sx_xunlock(&anp->an_vnode_lock);
649270096Strasz			return (error);
650270096Strasz		}
651270096Strasz		if (vp->v_iflag & VI_DOOMED) {
652270096Strasz			/*
653270096Strasz			 * We got forcibly unmounted.
654270096Strasz			 */
655270096Strasz			AUTOFS_DEBUG("doomed vnode");
656270096Strasz			sx_xunlock(&anp->an_vnode_lock);
657270096Strasz			vput(vp);
658270096Strasz
659270096Strasz			return (ENOENT);
660270096Strasz		}
661270096Strasz
662270096Strasz		*vpp = vp;
663270096Strasz		sx_xunlock(&anp->an_vnode_lock);
664270096Strasz		return (0);
665270096Strasz	}
666270096Strasz
667270096Strasz	error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp);
668270096Strasz	if (error != 0) {
669270096Strasz		sx_xunlock(&anp->an_vnode_lock);
670270096Strasz		return (error);
671270096Strasz	}
672270096Strasz
673270096Strasz	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
674270096Strasz	if (error != 0) {
675270096Strasz		sx_xunlock(&anp->an_vnode_lock);
676270096Strasz		vdrop(vp);
677270096Strasz		return (error);
678270096Strasz	}
679270096Strasz
680270096Strasz	vp->v_type = VDIR;
681270096Strasz	if (anp->an_parent == NULL)
682270096Strasz		vp->v_vflag |= VV_ROOT;
683270096Strasz	vp->v_data = anp;
684270096Strasz
685272512Strasz	VN_LOCK_ASHARE(vp);
686272512Strasz
687270096Strasz	error = insmntque(vp, mp);
688270096Strasz	if (error != 0) {
689270096Strasz		AUTOFS_WARN("insmntque() failed with error %d", error);
690270096Strasz		sx_xunlock(&anp->an_vnode_lock);
691270096Strasz		return (error);
692270096Strasz	}
693270096Strasz
694270096Strasz	KASSERT(anp->an_vnode == NULL, ("lost race"));
695270096Strasz	anp->an_vnode = vp;
696270096Strasz
697270096Strasz	sx_xunlock(&anp->an_vnode_lock);
698270096Strasz
699270096Strasz	*vpp = vp;
700270096Strasz	return (0);
701270096Strasz}
702