vfs_default.c revision 208003
1/*-
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed
6 * to Berkeley by John Heidemann of the UCLA Ficus project.
7 *
8 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/kern/vfs_default.c 208003 2010-05-12 21:24:46Z zml $");
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/bio.h>
41#include <sys/buf.h>
42#include <sys/conf.h>
43#include <sys/event.h>
44#include <sys/kernel.h>
45#include <sys/limits.h>
46#include <sys/lock.h>
47#include <sys/lockf.h>
48#include <sys/malloc.h>
49#include <sys/mount.h>
50#include <sys/mutex.h>
51#include <sys/namei.h>
52#include <sys/fcntl.h>
53#include <sys/unistd.h>
54#include <sys/vnode.h>
55#include <sys/dirent.h>
56#include <sys/poll.h>
57
58#include <security/mac/mac_framework.h>
59
60#include <vm/vm.h>
61#include <vm/vm_object.h>
62#include <vm/vm_extern.h>
63#include <vm/pmap.h>
64#include <vm/vm_map.h>
65#include <vm/vm_page.h>
66#include <vm/vm_pager.h>
67#include <vm/vnode_pager.h>
68
69static int	vop_nolookup(struct vop_lookup_args *);
70static int	vop_norename(struct vop_rename_args *);
71static int	vop_nostrategy(struct vop_strategy_args *);
72static int	get_next_dirent(struct vnode *vp, struct dirent **dpp,
73				char *dirbuf, int dirbuflen, off_t *off,
74				char **cpos, int *len, int *eofflag,
75				struct thread *td);
76static int	dirent_exists(struct vnode *vp, const char *dirname,
77			      struct thread *td);
78
79#define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
80
81/*
82 * This vnode table stores what we want to do if the filesystem doesn't
83 * implement a particular VOP.
84 *
85 * If there is no specific entry here, we will return EOPNOTSUPP.
86 *
87 * Note that every filesystem has to implement either vop_access
88 * or vop_accessx; failing to do so will result in immediate crash
89 * due to stack overflow, as vop_stdaccess() calls vop_stdaccessx(),
90 * which calls vop_stdaccess() etc.
91 */
92
93struct vop_vector default_vnodeops = {
94	.vop_default =		NULL,
95	.vop_bypass =		VOP_EOPNOTSUPP,
96
97	.vop_access =		vop_stdaccess,
98	.vop_accessx =		vop_stdaccessx,
99	.vop_advlock =		vop_stdadvlock,
100	.vop_advlockasync =	vop_stdadvlockasync,
101	.vop_advlockpurge =	vop_stdadvlockpurge,
102	.vop_bmap =		vop_stdbmap,
103	.vop_close =		VOP_NULL,
104	.vop_fsync =		VOP_NULL,
105	.vop_getpages =		vop_stdgetpages,
106	.vop_getwritemount = 	vop_stdgetwritemount,
107	.vop_inactive =		VOP_NULL,
108	.vop_ioctl =		VOP_ENOTTY,
109	.vop_kqfilter =		vop_stdkqfilter,
110	.vop_islocked =		vop_stdislocked,
111	.vop_lock1 =		vop_stdlock,
112	.vop_lookup =		vop_nolookup,
113	.vop_open =		VOP_NULL,
114	.vop_pathconf =		VOP_EINVAL,
115	.vop_poll =		vop_nopoll,
116	.vop_putpages =		vop_stdputpages,
117	.vop_readlink =		VOP_EINVAL,
118	.vop_rename =		vop_norename,
119	.vop_revoke =		VOP_PANIC,
120	.vop_strategy =		vop_nostrategy,
121	.vop_unlock =		vop_stdunlock,
122	.vop_vptocnp =		vop_stdvptocnp,
123	.vop_vptofh =		vop_stdvptofh,
124};
125
126/*
127 * Series of placeholder functions for various error returns for
128 * VOPs.
129 */
130
131int
132vop_eopnotsupp(struct vop_generic_args *ap)
133{
134	/*
135	printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
136	*/
137
138	return (EOPNOTSUPP);
139}
140
141int
142vop_ebadf(struct vop_generic_args *ap)
143{
144
145	return (EBADF);
146}
147
148int
149vop_enotty(struct vop_generic_args *ap)
150{
151
152	return (ENOTTY);
153}
154
155int
156vop_einval(struct vop_generic_args *ap)
157{
158
159	return (EINVAL);
160}
161
162int
163vop_enoent(struct vop_generic_args *ap)
164{
165
166	return (ENOENT);
167}
168
169int
170vop_null(struct vop_generic_args *ap)
171{
172
173	return (0);
174}
175
176/*
177 * Helper function to panic on some bad VOPs in some filesystems.
178 */
179int
180vop_panic(struct vop_generic_args *ap)
181{
182
183	panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
184}
185
186/*
187 * vop_std<something> and vop_no<something> are default functions for use by
188 * filesystems that need the "default reasonable" implementation for a
189 * particular operation.
190 *
191 * The documentation for the operations they implement exists (if it exists)
192 * in the VOP_<SOMETHING>(9) manpage (all uppercase).
193 */
194
195/*
196 * Default vop for filesystems that do not support name lookup
197 */
198static int
199vop_nolookup(ap)
200	struct vop_lookup_args /* {
201		struct vnode *a_dvp;
202		struct vnode **a_vpp;
203		struct componentname *a_cnp;
204	} */ *ap;
205{
206
207	*ap->a_vpp = NULL;
208	return (ENOTDIR);
209}
210
211/*
212 * vop_norename:
213 *
214 * Handle unlock and reference counting for arguments of vop_rename
215 * for filesystems that do not implement rename operation.
216 */
217static int
218vop_norename(struct vop_rename_args *ap)
219{
220
221	vop_rename_fail(ap);
222	return (EOPNOTSUPP);
223}
224
225/*
226 *	vop_nostrategy:
227 *
228 *	Strategy routine for VFS devices that have none.
229 *
230 *	BIO_ERROR and B_INVAL must be cleared prior to calling any strategy
231 *	routine.  Typically this is done for a BIO_READ strategy call.
232 *	Typically B_INVAL is assumed to already be clear prior to a write
233 *	and should not be cleared manually unless you just made the buffer
234 *	invalid.  BIO_ERROR should be cleared either way.
235 */
236
237static int
238vop_nostrategy (struct vop_strategy_args *ap)
239{
240	printf("No strategy for buffer at %p\n", ap->a_bp);
241	vprint("vnode", ap->a_vp);
242	ap->a_bp->b_ioflags |= BIO_ERROR;
243	ap->a_bp->b_error = EOPNOTSUPP;
244	bufdone(ap->a_bp);
245	return (EOPNOTSUPP);
246}
247
248static int
249get_next_dirent(struct vnode *vp, struct dirent **dpp, char *dirbuf,
250		int dirbuflen, off_t *off, char **cpos, int *len,
251		int *eofflag, struct thread *td)
252{
253	int error, reclen;
254	struct uio uio;
255	struct iovec iov;
256	struct dirent *dp;
257
258	KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
259	KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
260
261	if (*len == 0) {
262		iov.iov_base = dirbuf;
263		iov.iov_len = dirbuflen;
264
265		uio.uio_iov = &iov;
266		uio.uio_iovcnt = 1;
267		uio.uio_offset = *off;
268		uio.uio_resid = dirbuflen;
269		uio.uio_segflg = UIO_SYSSPACE;
270		uio.uio_rw = UIO_READ;
271		uio.uio_td = td;
272
273		*eofflag = 0;
274
275#ifdef MAC
276		error = mac_vnode_check_readdir(td->td_ucred, vp);
277		if (error == 0)
278#endif
279			error = VOP_READDIR(vp, &uio, td->td_ucred, eofflag,
280		    		NULL, NULL);
281		if (error)
282			return (error);
283
284		*off = uio.uio_offset;
285
286		*cpos = dirbuf;
287		*len = (dirbuflen - uio.uio_resid);
288	}
289
290	dp = (struct dirent *)(*cpos);
291	reclen = dp->d_reclen;
292	*dpp = dp;
293
294	/* check for malformed directory.. */
295	if (reclen < DIRENT_MINSIZE)
296		return (EINVAL);
297
298	*cpos += reclen;
299	*len -= reclen;
300
301	return (0);
302}
303
304/*
305 * Check if a named file exists in a given directory vnode.
306 */
307static int
308dirent_exists(struct vnode *vp, const char *dirname, struct thread *td)
309{
310	char *dirbuf, *cpos;
311	int error, eofflag, dirbuflen, len, found;
312	off_t off;
313	struct dirent *dp;
314	struct vattr va;
315
316	KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
317	KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
318
319	found = 0;
320
321	error = VOP_GETATTR(vp, &va, td->td_ucred);
322	if (error)
323		return (found);
324
325	dirbuflen = DEV_BSIZE;
326	if (dirbuflen < va.va_blocksize)
327		dirbuflen = va.va_blocksize;
328	dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
329
330	off = 0;
331	len = 0;
332	do {
333		error = get_next_dirent(vp, &dp, dirbuf, dirbuflen, &off,
334					&cpos, &len, &eofflag, td);
335		if (error)
336			goto out;
337
338		if ((dp->d_type != DT_WHT) &&
339		    !strcmp(dp->d_name, dirname)) {
340			found = 1;
341			goto out;
342		}
343	} while (len > 0 || !eofflag);
344
345out:
346	free(dirbuf, M_TEMP);
347	return (found);
348}
349
350int
351vop_stdaccess(struct vop_access_args *ap)
352{
353
354	KASSERT((ap->a_accmode & ~(VEXEC | VWRITE | VREAD | VADMIN |
355	    VAPPEND)) == 0, ("invalid bit in accmode"));
356
357	return (VOP_ACCESSX(ap->a_vp, ap->a_accmode, ap->a_cred, ap->a_td));
358}
359
360int
361vop_stdaccessx(struct vop_accessx_args *ap)
362{
363	int error;
364	accmode_t accmode = ap->a_accmode;
365
366	error = vfs_unixify_accmode(&accmode);
367	if (error != 0)
368		return (error);
369
370	if (accmode == 0)
371		return (0);
372
373	return (VOP_ACCESS(ap->a_vp, accmode, ap->a_cred, ap->a_td));
374}
375
376/*
377 * Advisory record locking support
378 */
379int
380vop_stdadvlock(struct vop_advlock_args *ap)
381{
382	struct vnode *vp;
383	struct ucred *cred;
384	struct vattr vattr;
385	int error;
386
387	vp = ap->a_vp;
388	cred = curthread->td_ucred;
389	vn_lock(vp, LK_SHARED | LK_RETRY);
390	error = VOP_GETATTR(vp, &vattr, cred);
391	VOP_UNLOCK(vp, 0);
392	if (error)
393		return (error);
394
395	return (lf_advlock(ap, &(vp->v_lockf), vattr.va_size));
396}
397
398int
399vop_stdadvlockasync(struct vop_advlockasync_args *ap)
400{
401	struct vnode *vp;
402	struct ucred *cred;
403	struct vattr vattr;
404	int error;
405
406	vp = ap->a_vp;
407	cred = curthread->td_ucred;
408	vn_lock(vp, LK_SHARED | LK_RETRY);
409	error = VOP_GETATTR(vp, &vattr, cred);
410	VOP_UNLOCK(vp, 0);
411	if (error)
412		return (error);
413
414	return (lf_advlockasync(ap, &(vp->v_lockf), vattr.va_size));
415}
416
417int
418vop_stdadvlockpurge(struct vop_advlockpurge_args *ap)
419{
420	struct vnode *vp;
421
422	vp = ap->a_vp;
423	lf_purgelocks(vp, &vp->v_lockf);
424	return (0);
425}
426
427/*
428 * vop_stdpathconf:
429 *
430 * Standard implementation of POSIX pathconf, to get information about limits
431 * for a filesystem.
432 * Override per filesystem for the case where the filesystem has smaller
433 * limits.
434 */
435int
436vop_stdpathconf(ap)
437	struct vop_pathconf_args /* {
438	struct vnode *a_vp;
439	int a_name;
440	int *a_retval;
441	} */ *ap;
442{
443
444	switch (ap->a_name) {
445		case _PC_NAME_MAX:
446			*ap->a_retval = NAME_MAX;
447			return (0);
448		case _PC_PATH_MAX:
449			*ap->a_retval = PATH_MAX;
450			return (0);
451		case _PC_LINK_MAX:
452			*ap->a_retval = LINK_MAX;
453			return (0);
454		case _PC_MAX_CANON:
455			*ap->a_retval = MAX_CANON;
456			return (0);
457		case _PC_MAX_INPUT:
458			*ap->a_retval = MAX_INPUT;
459			return (0);
460		case _PC_PIPE_BUF:
461			*ap->a_retval = PIPE_BUF;
462			return (0);
463		case _PC_CHOWN_RESTRICTED:
464			*ap->a_retval = 1;
465			return (0);
466		case _PC_VDISABLE:
467			*ap->a_retval = _POSIX_VDISABLE;
468			return (0);
469		default:
470			return (EINVAL);
471	}
472	/* NOTREACHED */
473}
474
475/*
476 * Standard lock, unlock and islocked functions.
477 */
478int
479vop_stdlock(ap)
480	struct vop_lock1_args /* {
481		struct vnode *a_vp;
482		int a_flags;
483		char *file;
484		int line;
485	} */ *ap;
486{
487	struct vnode *vp = ap->a_vp;
488
489	return (_lockmgr_args(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
490	    LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, ap->a_file,
491	    ap->a_line));
492}
493
494/* See above. */
495int
496vop_stdunlock(ap)
497	struct vop_unlock_args /* {
498		struct vnode *a_vp;
499		int a_flags;
500	} */ *ap;
501{
502	struct vnode *vp = ap->a_vp;
503
504	return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE, VI_MTX(vp)));
505}
506
507/* See above. */
508int
509vop_stdislocked(ap)
510	struct vop_islocked_args /* {
511		struct vnode *a_vp;
512	} */ *ap;
513{
514
515	return (lockstatus(ap->a_vp->v_vnlock));
516}
517
518/*
519 * Return true for select/poll.
520 */
521int
522vop_nopoll(ap)
523	struct vop_poll_args /* {
524		struct vnode *a_vp;
525		int  a_events;
526		struct ucred *a_cred;
527		struct thread *a_td;
528	} */ *ap;
529{
530
531	return (poll_no_poll(ap->a_events));
532}
533
534/*
535 * Implement poll for local filesystems that support it.
536 */
537int
538vop_stdpoll(ap)
539	struct vop_poll_args /* {
540		struct vnode *a_vp;
541		int  a_events;
542		struct ucred *a_cred;
543		struct thread *a_td;
544	} */ *ap;
545{
546	if (ap->a_events & ~POLLSTANDARD)
547		return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
548	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
549}
550
551/*
552 * Return our mount point, as we will take charge of the writes.
553 */
554int
555vop_stdgetwritemount(ap)
556	struct vop_getwritemount_args /* {
557		struct vnode *a_vp;
558		struct mount **a_mpp;
559	} */ *ap;
560{
561	struct mount *mp;
562
563	/*
564	 * XXX Since this is called unlocked we may be recycled while
565	 * attempting to ref the mount.  If this is the case or mountpoint
566	 * will be set to NULL.  We only have to prevent this call from
567	 * returning with a ref to an incorrect mountpoint.  It is not
568	 * harmful to return with a ref to our previous mountpoint.
569	 */
570	mp = ap->a_vp->v_mount;
571	if (mp != NULL) {
572		vfs_ref(mp);
573		if (mp != ap->a_vp->v_mount) {
574			vfs_rel(mp);
575			mp = NULL;
576		}
577	}
578	*(ap->a_mpp) = mp;
579	return (0);
580}
581
582/* XXX Needs good comment and VOP_BMAP(9) manpage */
583int
584vop_stdbmap(ap)
585	struct vop_bmap_args /* {
586		struct vnode *a_vp;
587		daddr_t  a_bn;
588		struct bufobj **a_bop;
589		daddr_t *a_bnp;
590		int *a_runp;
591		int *a_runb;
592	} */ *ap;
593{
594
595	if (ap->a_bop != NULL)
596		*ap->a_bop = &ap->a_vp->v_bufobj;
597	if (ap->a_bnp != NULL)
598		*ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
599	if (ap->a_runp != NULL)
600		*ap->a_runp = 0;
601	if (ap->a_runb != NULL)
602		*ap->a_runb = 0;
603	return (0);
604}
605
606int
607vop_stdfsync(ap)
608	struct vop_fsync_args /* {
609		struct vnode *a_vp;
610		struct ucred *a_cred;
611		int a_waitfor;
612		struct thread *a_td;
613	} */ *ap;
614{
615	struct vnode *vp = ap->a_vp;
616	struct buf *bp;
617	struct bufobj *bo;
618	struct buf *nbp;
619	int error = 0;
620	int maxretry = 1000;     /* large, arbitrarily chosen */
621
622	bo = &vp->v_bufobj;
623	BO_LOCK(bo);
624loop1:
625	/*
626	 * MARK/SCAN initialization to avoid infinite loops.
627	 */
628        TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
629                bp->b_vflags &= ~BV_SCANNED;
630		bp->b_error = 0;
631	}
632
633	/*
634	 * Flush all dirty buffers associated with a vnode.
635	 */
636loop2:
637	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
638		if ((bp->b_vflags & BV_SCANNED) != 0)
639			continue;
640		bp->b_vflags |= BV_SCANNED;
641		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
642			continue;
643		BO_UNLOCK(bo);
644		KASSERT(bp->b_bufobj == bo,
645		    ("bp %p wrong b_bufobj %p should be %p",
646		    bp, bp->b_bufobj, bo));
647		if ((bp->b_flags & B_DELWRI) == 0)
648			panic("fsync: not dirty");
649		if ((vp->v_object != NULL) && (bp->b_flags & B_CLUSTEROK)) {
650			vfs_bio_awrite(bp);
651		} else {
652			bremfree(bp);
653			bawrite(bp);
654		}
655		BO_LOCK(bo);
656		goto loop2;
657	}
658
659	/*
660	 * If synchronous the caller expects us to completely resolve all
661	 * dirty buffers in the system.  Wait for in-progress I/O to
662	 * complete (which could include background bitmap writes), then
663	 * retry if dirty blocks still exist.
664	 */
665	if (ap->a_waitfor == MNT_WAIT) {
666		bufobj_wwait(bo, 0, 0);
667		if (bo->bo_dirty.bv_cnt > 0) {
668			/*
669			 * If we are unable to write any of these buffers
670			 * then we fail now rather than trying endlessly
671			 * to write them out.
672			 */
673			TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
674				if ((error = bp->b_error) == 0)
675					continue;
676			if (error == 0 && --maxretry >= 0)
677				goto loop1;
678			error = EAGAIN;
679		}
680	}
681	BO_UNLOCK(bo);
682	if (error == EAGAIN)
683		vprint("fsync: giving up on dirty", vp);
684
685	return (error);
686}
687
688/* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
689int
690vop_stdgetpages(ap)
691	struct vop_getpages_args /* {
692		struct vnode *a_vp;
693		vm_page_t *a_m;
694		int a_count;
695		int a_reqpage;
696		vm_ooffset_t a_offset;
697	} */ *ap;
698{
699
700	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
701	    ap->a_count, ap->a_reqpage);
702}
703
704int
705vop_stdkqfilter(struct vop_kqfilter_args *ap)
706{
707	return vfs_kqfilter(ap);
708}
709
710/* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
711int
712vop_stdputpages(ap)
713	struct vop_putpages_args /* {
714		struct vnode *a_vp;
715		vm_page_t *a_m;
716		int a_count;
717		int a_sync;
718		int *a_rtvals;
719		vm_ooffset_t a_offset;
720	} */ *ap;
721{
722
723	return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
724	     ap->a_sync, ap->a_rtvals);
725}
726
727int
728vop_stdvptofh(struct vop_vptofh_args *ap)
729{
730	return (EOPNOTSUPP);
731}
732
733int
734vop_stdvptocnp(struct vop_vptocnp_args *ap)
735{
736	struct vnode *vp = ap->a_vp;
737	struct vnode **dvp = ap->a_vpp;
738	struct ucred *cred = ap->a_cred;
739	char *buf = ap->a_buf;
740	int *buflen = ap->a_buflen;
741	char *dirbuf, *cpos;
742	int i, error, eofflag, dirbuflen, flags, locked, len, covered;
743	off_t off;
744	ino_t fileno;
745	struct vattr va;
746	struct nameidata nd;
747	struct thread *td;
748	struct dirent *dp;
749	struct vnode *mvp;
750
751	i = *buflen;
752	error = 0;
753	covered = 0;
754	td = curthread;
755
756	if (vp->v_type != VDIR)
757		return (ENOENT);
758
759	error = VOP_GETATTR(vp, &va, cred);
760	if (error)
761		return (error);
762
763	VREF(vp);
764	locked = VOP_ISLOCKED(vp);
765	VOP_UNLOCK(vp, 0);
766	NDINIT_ATVP(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
767	    "..", vp, td);
768	flags = FREAD;
769	error = vn_open_cred(&nd, &flags, 0, VN_OPEN_NOAUDIT, cred, NULL);
770	if (error) {
771		vn_lock(vp, locked | LK_RETRY);
772		return (error);
773	}
774	NDFREE(&nd, NDF_ONLY_PNBUF);
775
776	mvp = *dvp = nd.ni_vp;
777
778	if (vp->v_mount != (*dvp)->v_mount &&
779	    ((*dvp)->v_vflag & VV_ROOT) &&
780	    ((*dvp)->v_mount->mnt_flag & MNT_UNION)) {
781		*dvp = (*dvp)->v_mount->mnt_vnodecovered;
782		VREF(mvp);
783		VOP_UNLOCK(mvp, 0);
784		vn_close(mvp, FREAD, cred, td);
785		VREF(*dvp);
786		vn_lock(*dvp, LK_EXCLUSIVE | LK_RETRY);
787		covered = 1;
788	}
789
790	fileno = va.va_fileid;
791
792	dirbuflen = DEV_BSIZE;
793	if (dirbuflen < va.va_blocksize)
794		dirbuflen = va.va_blocksize;
795	dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
796
797	if ((*dvp)->v_type != VDIR) {
798		error = ENOENT;
799		goto out;
800	}
801
802	off = 0;
803	len = 0;
804	do {
805		/* call VOP_READDIR of parent */
806		error = get_next_dirent(*dvp, &dp, dirbuf, dirbuflen, &off,
807					&cpos, &len, &eofflag, td);
808		if (error)
809			goto out;
810
811		if ((dp->d_type != DT_WHT) &&
812		    (dp->d_fileno == fileno)) {
813			if (covered) {
814				VOP_UNLOCK(*dvp, 0);
815				vn_lock(mvp, LK_EXCLUSIVE | LK_RETRY);
816				if (dirent_exists(mvp, dp->d_name, td)) {
817					error = ENOENT;
818					VOP_UNLOCK(mvp, 0);
819					vn_lock(*dvp, LK_EXCLUSIVE | LK_RETRY);
820					goto out;
821				}
822				VOP_UNLOCK(mvp, 0);
823				vn_lock(*dvp, LK_EXCLUSIVE | LK_RETRY);
824			}
825			i -= dp->d_namlen;
826
827			if (i < 0) {
828				error = ENOMEM;
829				goto out;
830			}
831			bcopy(dp->d_name, buf + i, dp->d_namlen);
832			error = 0;
833			goto out;
834		}
835	} while (len > 0 || !eofflag);
836	error = ENOENT;
837
838out:
839	free(dirbuf, M_TEMP);
840	if (!error) {
841		*buflen = i;
842		vhold(*dvp);
843	}
844	if (covered) {
845		vput(*dvp);
846		vrele(mvp);
847	} else {
848		VOP_UNLOCK(mvp, 0);
849		vn_close(mvp, FREAD, cred, td);
850	}
851	vn_lock(vp, locked | LK_RETRY);
852	return (error);
853}
854
855/*
856 * vfs default ops
857 * used to fill the vfs function table to get reasonable default return values.
858 */
859int
860vfs_stdroot (mp, flags, vpp)
861	struct mount *mp;
862	int flags;
863	struct vnode **vpp;
864{
865
866	return (EOPNOTSUPP);
867}
868
869int
870vfs_stdstatfs (mp, sbp)
871	struct mount *mp;
872	struct statfs *sbp;
873{
874
875	return (EOPNOTSUPP);
876}
877
878int
879vfs_stdquotactl (mp, cmds, uid, arg)
880	struct mount *mp;
881	int cmds;
882	uid_t uid;
883	void *arg;
884{
885
886	return (EOPNOTSUPP);
887}
888
889int
890vfs_stdsync(mp, waitfor)
891	struct mount *mp;
892	int waitfor;
893{
894	struct vnode *vp, *mvp;
895	struct thread *td;
896	int error, lockreq, allerror = 0;
897
898	td = curthread;
899	lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
900	if (waitfor != MNT_WAIT)
901		lockreq |= LK_NOWAIT;
902	/*
903	 * Force stale buffer cache information to be flushed.
904	 */
905	MNT_ILOCK(mp);
906loop:
907	MNT_VNODE_FOREACH(vp, mp, mvp) {
908		/* bv_cnt is an acceptable race here. */
909		if (vp->v_bufobj.bo_dirty.bv_cnt == 0)
910			continue;
911		VI_LOCK(vp);
912		MNT_IUNLOCK(mp);
913		if ((error = vget(vp, lockreq, td)) != 0) {
914			MNT_ILOCK(mp);
915			if (error == ENOENT) {
916				MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
917				goto loop;
918			}
919			continue;
920		}
921		error = VOP_FSYNC(vp, waitfor, td);
922		if (error)
923			allerror = error;
924		vput(vp);
925		MNT_ILOCK(mp);
926	}
927	MNT_IUNLOCK(mp);
928	return (allerror);
929}
930
931int
932vfs_stdnosync (mp, waitfor)
933	struct mount *mp;
934	int waitfor;
935{
936
937	return (0);
938}
939
940int
941vfs_stdvget (mp, ino, flags, vpp)
942	struct mount *mp;
943	ino_t ino;
944	int flags;
945	struct vnode **vpp;
946{
947
948	return (EOPNOTSUPP);
949}
950
951int
952vfs_stdfhtovp (mp, fhp, vpp)
953	struct mount *mp;
954	struct fid *fhp;
955	struct vnode **vpp;
956{
957
958	return (EOPNOTSUPP);
959}
960
961int
962vfs_stdinit (vfsp)
963	struct vfsconf *vfsp;
964{
965
966	return (0);
967}
968
969int
970vfs_stduninit (vfsp)
971	struct vfsconf *vfsp;
972{
973
974	return(0);
975}
976
977int
978vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname)
979	struct mount *mp;
980	int cmd;
981	struct vnode *filename_vp;
982	int attrnamespace;
983	const char *attrname;
984{
985
986	if (filename_vp != NULL)
987		VOP_UNLOCK(filename_vp, 0);
988	return (EOPNOTSUPP);
989}
990
991int
992vfs_stdsysctl(mp, op, req)
993	struct mount *mp;
994	fsctlop_t op;
995	struct sysctl_req *req;
996{
997
998	return (EOPNOTSUPP);
999}
1000
1001/* end of vfs default ops */
1002