vfs_default.c revision 112067
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 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *
39 * $FreeBSD: head/sys/kern/vfs_default.c 112067 2003-03-10 21:55:00Z kan $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/bio.h>
45#include <sys/buf.h>
46#include <sys/conf.h>
47#include <sys/kernel.h>
48#include <sys/lock.h>
49#include <sys/malloc.h>
50#include <sys/mount.h>
51#include <sys/mutex.h>
52#include <sys/unistd.h>
53#include <sys/vnode.h>
54#include <sys/poll.h>
55
56#include <machine/limits.h>
57
58#include <vm/vm.h>
59#include <vm/vm_object.h>
60#include <vm/vm_extern.h>
61#include <vm/pmap.h>
62#include <vm/vm_map.h>
63#include <vm/vm_page.h>
64#include <vm/vm_pager.h>
65#include <vm/vnode_pager.h>
66
67static int	vop_nolookup(struct vop_lookup_args *);
68static int	vop_nostrategy(struct vop_strategy_args *);
69static int	vop_nospecstrategy(struct vop_specstrategy_args *);
70
71/*
72 * This vnode table stores what we want to do if the filesystem doesn't
73 * implement a particular VOP.
74 *
75 * If there is no specific entry here, we will return EOPNOTSUPP.
76 *
77 */
78
79vop_t **default_vnodeop_p;
80static struct vnodeopv_entry_desc default_vnodeop_entries[] = {
81	{ &vop_default_desc,		(vop_t *) vop_eopnotsupp },
82	{ &vop_advlock_desc,		(vop_t *) vop_einval },
83	{ &vop_bmap_desc,		(vop_t *) vop_stdbmap },
84	{ &vop_close_desc,		(vop_t *) vop_null },
85	{ &vop_createvobject_desc,	(vop_t *) vop_stdcreatevobject },
86	{ &vop_destroyvobject_desc,	(vop_t *) vop_stddestroyvobject },
87	{ &vop_fsync_desc,		(vop_t *) vop_null },
88	{ &vop_getpages_desc,		(vop_t *) vop_stdgetpages },
89	{ &vop_getvobject_desc,		(vop_t *) vop_stdgetvobject },
90	{ &vop_inactive_desc,		(vop_t *) vop_stdinactive },
91	{ &vop_ioctl_desc,		(vop_t *) vop_enotty },
92	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
93	{ &vop_lease_desc,		(vop_t *) vop_null },
94	{ &vop_lock_desc,		(vop_t *) vop_stdlock },
95	{ &vop_lookup_desc,		(vop_t *) vop_nolookup },
96	{ &vop_open_desc,		(vop_t *) vop_null },
97	{ &vop_pathconf_desc,		(vop_t *) vop_einval },
98	{ &vop_poll_desc,		(vop_t *) vop_nopoll },
99	{ &vop_putpages_desc,		(vop_t *) vop_stdputpages },
100	{ &vop_readlink_desc,		(vop_t *) vop_einval },
101	{ &vop_revoke_desc,		(vop_t *) vop_revoke },
102	{ &vop_specstrategy_desc,	(vop_t *) vop_nospecstrategy },
103	{ &vop_strategy_desc,		(vop_t *) vop_nostrategy },
104	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
105	{ NULL, NULL }
106};
107
108static struct vnodeopv_desc default_vnodeop_opv_desc =
109        { &default_vnodeop_p, default_vnodeop_entries };
110
111VNODEOP_SET(default_vnodeop_opv_desc);
112
113/*
114 * Series of placeholder functions for various error returns for
115 * VOPs.
116 */
117
118int
119vop_eopnotsupp(struct vop_generic_args *ap)
120{
121	/*
122	printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
123	*/
124
125	return (EOPNOTSUPP);
126}
127
128int
129vop_ebadf(struct vop_generic_args *ap)
130{
131
132	return (EBADF);
133}
134
135int
136vop_enotty(struct vop_generic_args *ap)
137{
138
139	return (ENOTTY);
140}
141
142int
143vop_einval(struct vop_generic_args *ap)
144{
145
146	return (EINVAL);
147}
148
149int
150vop_null(struct vop_generic_args *ap)
151{
152
153	return (0);
154}
155
156/*
157 * Used to make a defined VOP fall back to the default VOP.
158 */
159int
160vop_defaultop(struct vop_generic_args *ap)
161{
162
163	return (VOCALL(default_vnodeop_p, ap->a_desc->vdesc_offset, ap));
164}
165
166/*
167 * Helper function to panic on some bad VOPs in some filesystems.
168 */
169int
170vop_panic(struct vop_generic_args *ap)
171{
172
173	panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
174}
175
176/*
177 * vop_std<something> and vop_no<something> are default functions for use by
178 * filesystems that need the "default reasonable" implementation for a
179 * particular operation.
180 *
181 * The documentation for the operations they implement exists (if it exists)
182 * in the VOP_<SOMETHING>(9) manpage (all uppercase).
183 */
184
185/*
186 * Default vop for filesystems that do not support name lookup
187 */
188static int
189vop_nolookup(ap)
190	struct vop_lookup_args /* {
191		struct vnode *a_dvp;
192		struct vnode **a_vpp;
193		struct componentname *a_cnp;
194	} */ *ap;
195{
196
197	*ap->a_vpp = NULL;
198	return (ENOTDIR);
199}
200
201/*
202 *	vop_nostrategy:
203 *
204 *	Strategy routine for VFS devices that have none.
205 *
206 *	BIO_ERROR and B_INVAL must be cleared prior to calling any strategy
207 *	routine.  Typically this is done for a BIO_READ strategy call.
208 *	Typically B_INVAL is assumed to already be clear prior to a write
209 *	and should not be cleared manually unless you just made the buffer
210 *	invalid.  BIO_ERROR should be cleared either way.
211 */
212
213static int
214vop_nostrategy (struct vop_strategy_args *ap)
215{
216	printf("No strategy for buffer at %p\n", ap->a_bp);
217	vprint("vnode", ap->a_vp);
218	vprint("device vnode", ap->a_bp->b_vp);
219	ap->a_bp->b_ioflags |= BIO_ERROR;
220	ap->a_bp->b_error = EOPNOTSUPP;
221	bufdone(ap->a_bp);
222	return (EOPNOTSUPP);
223}
224
225/*
226 *	vop_nospecstrategy:
227 *
228 *	This shouldn't happen.  VOP_SPECSTRATEGY should always have a VCHR
229 *	argument vnode, and thos have a method for specstrategy over in
230 *	specfs, so we only ever get here if somebody botched it.
231 *	Pass the call to VOP_STRATEGY() and get on with life.
232 *	The first time we print some info useful for debugging.
233 */
234
235static int
236vop_nospecstrategy (struct vop_specstrategy_args *ap)
237{
238	static int once;
239
240	if (!once) {
241		vprint("VOP_SPECSTRATEGY on non-VCHR", ap->a_vp);
242		backtrace();
243		once++;
244	}
245	return VOP_STRATEGY(ap->a_vp, ap->a_bp);
246}
247
248/*
249 * vop_stdpathconf:
250 *
251 * Standard implementation of POSIX pathconf, to get information about limits
252 * for a filesystem.
253 * Override per filesystem for the case where the filesystem has smaller
254 * limits.
255 */
256int
257vop_stdpathconf(ap)
258	struct vop_pathconf_args /* {
259	struct vnode *a_vp;
260	int a_name;
261	int *a_retval;
262	} */ *ap;
263{
264
265	switch (ap->a_name) {
266		case _PC_LINK_MAX:
267			*ap->a_retval = LINK_MAX;
268			return (0);
269		case _PC_MAX_CANON:
270			*ap->a_retval = MAX_CANON;
271			return (0);
272		case _PC_MAX_INPUT:
273			*ap->a_retval = MAX_INPUT;
274			return (0);
275		case _PC_PIPE_BUF:
276			*ap->a_retval = PIPE_BUF;
277			return (0);
278		case _PC_CHOWN_RESTRICTED:
279			*ap->a_retval = 1;
280			return (0);
281		case _PC_VDISABLE:
282			*ap->a_retval = _POSIX_VDISABLE;
283			return (0);
284		default:
285			return (EINVAL);
286	}
287	/* NOTREACHED */
288}
289
290/*
291 * Standard lock, unlock and islocked functions.
292 */
293int
294vop_stdlock(ap)
295	struct vop_lock_args /* {
296		struct vnode *a_vp;
297		int a_flags;
298		struct thread *a_td;
299	} */ *ap;
300{
301	struct vnode *vp = ap->a_vp;
302
303#ifndef	DEBUG_LOCKS
304	return (lockmgr(vp->v_vnlock, ap->a_flags, VI_MTX(vp), ap->a_td));
305#else
306	return (debuglockmgr(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
307	    ap->a_td, "vop_stdlock", vp->filename, vp->line));
308#endif
309}
310
311/* See above. */
312int
313vop_stdunlock(ap)
314	struct vop_unlock_args /* {
315		struct vnode *a_vp;
316		int a_flags;
317		struct thread *a_td;
318	} */ *ap;
319{
320	struct vnode *vp = ap->a_vp;
321
322	return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE, VI_MTX(vp),
323	    ap->a_td));
324}
325
326/* See above. */
327int
328vop_stdislocked(ap)
329	struct vop_islocked_args /* {
330		struct vnode *a_vp;
331		struct thread *a_td;
332	} */ *ap;
333{
334
335	return (lockstatus(ap->a_vp->v_vnlock, ap->a_td));
336}
337
338/* Mark the vnode inactive */
339int
340vop_stdinactive(ap)
341	struct vop_inactive_args /* {
342		struct vnode *a_vp;
343		struct thread *a_td;
344	} */ *ap;
345{
346
347	VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
348	return (0);
349}
350
351/*
352 * Return true for select/poll.
353 */
354int
355vop_nopoll(ap)
356	struct vop_poll_args /* {
357		struct vnode *a_vp;
358		int  a_events;
359		struct ucred *a_cred;
360		struct thread *a_td;
361	} */ *ap;
362{
363	/*
364	 * Return true for read/write.  If the user asked for something
365	 * special, return POLLNVAL, so that clients have a way of
366	 * determining reliably whether or not the extended
367	 * functionality is present without hard-coding knowledge
368	 * of specific filesystem implementations.
369	 */
370	if (ap->a_events & ~POLLSTANDARD)
371		return (POLLNVAL);
372
373	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
374}
375
376/*
377 * Implement poll for local filesystems that support it.
378 */
379int
380vop_stdpoll(ap)
381	struct vop_poll_args /* {
382		struct vnode *a_vp;
383		int  a_events;
384		struct ucred *a_cred;
385		struct thread *a_td;
386	} */ *ap;
387{
388	if (ap->a_events & ~POLLSTANDARD)
389		return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
390	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
391}
392
393/*
394 * Stubs to use when there is no locking to be done on the underlying object.
395 * A minimal shared lock is necessary to ensure that the underlying object
396 * is not revoked while an operation is in progress. So, an active shared
397 * count is maintained in an auxillary vnode lock structure.
398 */
399int
400vop_sharedlock(ap)
401	struct vop_lock_args /* {
402		struct vnode *a_vp;
403		int a_flags;
404		struct thread *a_td;
405	} */ *ap;
406{
407	/*
408	 * This code cannot be used until all the non-locking filesystems
409	 * (notably NFS) are converted to properly lock and release nodes.
410	 * Also, certain vnode operations change the locking state within
411	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
412	 * and symlink). Ideally these operations should not change the
413	 * lock state, but should be changed to let the caller of the
414	 * function unlock them. Otherwise all intermediate vnode layers
415	 * (such as union, umapfs, etc) must catch these functions to do
416	 * the necessary locking at their layer. Note that the inactive
417	 * and lookup operations also change their lock state, but this
418	 * cannot be avoided, so these two operations will always need
419	 * to be handled in intermediate layers.
420	 */
421	struct vnode *vp = ap->a_vp;
422	int vnflags, flags = ap->a_flags;
423
424	switch (flags & LK_TYPE_MASK) {
425	case LK_DRAIN:
426		vnflags = LK_DRAIN;
427		break;
428	case LK_EXCLUSIVE:
429#ifdef DEBUG_VFS_LOCKS
430		/*
431		 * Normally, we use shared locks here, but that confuses
432		 * the locking assertions.
433		 */
434		vnflags = LK_EXCLUSIVE;
435		break;
436#endif
437	case LK_SHARED:
438		vnflags = LK_SHARED;
439		break;
440	case LK_UPGRADE:
441	case LK_EXCLUPGRADE:
442	case LK_DOWNGRADE:
443		return (0);
444	case LK_RELEASE:
445	default:
446		panic("vop_sharedlock: bad operation %d", flags & LK_TYPE_MASK);
447	}
448	vnflags |= flags & (LK_INTERLOCK | LK_EXTFLG_MASK);
449#ifndef	DEBUG_LOCKS
450	return (lockmgr(vp->v_vnlock, vnflags, VI_MTX(vp), ap->a_td));
451#else
452	return (debuglockmgr(vp->v_vnlock, vnflags, VI_MTX(vp), ap->a_td,
453	    "vop_sharedlock", vp->filename, vp->line));
454#endif
455}
456
457/*
458 * Stubs to use when there is no locking to be done on the underlying object.
459 * A minimal shared lock is necessary to ensure that the underlying object
460 * is not revoked while an operation is in progress. So, an active shared
461 * count is maintained in an auxillary vnode lock structure.
462 */
463int
464vop_nolock(ap)
465	struct vop_lock_args /* {
466		struct vnode *a_vp;
467		int a_flags;
468		struct thread *a_td;
469	} */ *ap;
470{
471#ifdef notyet
472	/*
473	 * This code cannot be used until all the non-locking filesystems
474	 * (notably NFS) are converted to properly lock and release nodes.
475	 * Also, certain vnode operations change the locking state within
476	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
477	 * and symlink). Ideally these operations should not change the
478	 * lock state, but should be changed to let the caller of the
479	 * function unlock them. Otherwise all intermediate vnode layers
480	 * (such as union, umapfs, etc) must catch these functions to do
481	 * the necessary locking at their layer. Note that the inactive
482	 * and lookup operations also change their lock state, but this
483	 * cannot be avoided, so these two operations will always need
484	 * to be handled in intermediate layers.
485	 */
486	struct vnode *vp = ap->a_vp;
487	int vnflags, flags = ap->a_flags;
488
489	switch (flags & LK_TYPE_MASK) {
490	case LK_DRAIN:
491		vnflags = LK_DRAIN;
492		break;
493	case LK_EXCLUSIVE:
494	case LK_SHARED:
495		vnflags = LK_SHARED;
496		break;
497	case LK_UPGRADE:
498	case LK_EXCLUPGRADE:
499	case LK_DOWNGRADE:
500		return (0);
501	case LK_RELEASE:
502	default:
503		panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
504	}
505	vnflags |= flags & (LK_INTERLOCK | LK_EXTFLG_MASK);
506	return(lockmgr(vp->v_vnlock, vnflags, VI_MTX(vp), ap->a_td));
507#else /* for now */
508	/*
509	 * Since we are not using the lock manager, we must clear
510	 * the interlock here.
511	 */
512	if (ap->a_flags & LK_INTERLOCK)
513		VI_UNLOCK(ap->a_vp);
514	return (0);
515#endif
516}
517
518/*
519 * Do the inverse of vop_nolock, handling the interlock in a compatible way.
520 */
521int
522vop_nounlock(ap)
523	struct vop_unlock_args /* {
524		struct vnode *a_vp;
525		int a_flags;
526		struct thread *a_td;
527	} */ *ap;
528{
529
530	/*
531	 * Since we are not using the lock manager, we must clear
532	 * the interlock here.
533	 */
534	if (ap->a_flags & LK_INTERLOCK)
535		VI_UNLOCK(ap->a_vp);
536	return (0);
537}
538
539/*
540 * Return whether or not the node is in use.
541 */
542int
543vop_noislocked(ap)
544	struct vop_islocked_args /* {
545		struct vnode *a_vp;
546		struct thread *a_td;
547	} */ *ap;
548{
549
550	return (0);
551}
552
553/*
554 * Return our mount point, as we will take charge of the writes.
555 */
556int
557vop_stdgetwritemount(ap)
558	struct vop_getwritemount_args /* {
559		struct vnode *a_vp;
560		struct mount **a_mpp;
561	} */ *ap;
562{
563
564	*(ap->a_mpp) = ap->a_vp->v_mount;
565	return (0);
566}
567
568/* Create the VM system backing object for this vnode */
569int
570vop_stdcreatevobject(ap)
571	struct vop_createvobject_args /* {
572		struct vnode *vp;
573		struct ucred *cred;
574		struct thread *td;
575	} */ *ap;
576{
577	struct vnode *vp = ap->a_vp;
578	struct ucred *cred = ap->a_cred;
579	struct thread *td = ap->a_td;
580	struct vattr vat;
581	vm_object_t object;
582	int error = 0;
583
584	GIANT_REQUIRED;
585
586	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
587		return (0);
588
589retry:
590	if ((object = vp->v_object) == NULL) {
591		if (vp->v_type == VREG || vp->v_type == VDIR) {
592			if ((error = VOP_GETATTR(vp, &vat, cred, td)) != 0)
593				goto retn;
594			object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
595		} else if (devsw(vp->v_rdev) != NULL) {
596			/*
597			 * This simply allocates the biggest object possible
598			 * for a disk vnode.  This should be fixed, but doesn't
599			 * cause any problems (yet).
600			 */
601			object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
602		} else {
603			goto retn;
604		}
605		/*
606		 * Dereference the reference we just created.  This assumes
607		 * that the object is associated with the vp.
608		 */
609		object->ref_count--;
610		vrele(vp);
611	} else {
612		if (object->flags & OBJ_DEAD) {
613			VOP_UNLOCK(vp, 0, td);
614			tsleep(object, PVM, "vodead", 0);
615			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
616			goto retry;
617		}
618	}
619
620	KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
621	vp->v_vflag |= VV_OBJBUF;
622
623retn:
624	return (error);
625}
626
627/* Destroy the VM system object associated with this vnode */
628int
629vop_stddestroyvobject(ap)
630	struct vop_destroyvobject_args /* {
631		struct vnode *vp;
632	} */ *ap;
633{
634	struct vnode *vp = ap->a_vp;
635	vm_object_t obj = vp->v_object;
636
637	GIANT_REQUIRED;
638
639	if (vp->v_object == NULL)
640		return (0);
641
642	if (obj->ref_count == 0) {
643		/*
644		 * vclean() may be called twice. The first time
645		 * removes the primary reference to the object,
646		 * the second time goes one further and is a
647		 * special-case to terminate the object.
648		 *
649		 * don't double-terminate the object
650		 */
651		if ((obj->flags & OBJ_DEAD) == 0)
652			vm_object_terminate(obj);
653	} else {
654		/*
655		 * Woe to the process that tries to page now :-).
656		 */
657		vm_pager_deallocate(obj);
658	}
659	return (0);
660}
661
662/*
663 * Return the underlying VM object.  This routine may be called with or
664 * without the vnode interlock held.  If called without, the returned
665 * object is not guarenteed to be valid.  The syncer typically gets the
666 * object without holding the interlock in order to quickly test whether
667 * it might be dirty before going heavy-weight.  vm_object's use zalloc
668 * and thus stable-storage, so this is safe.
669 */
670int
671vop_stdgetvobject(ap)
672	struct vop_getvobject_args /* {
673		struct vnode *vp;
674		struct vm_object **objpp;
675	} */ *ap;
676{
677	struct vnode *vp = ap->a_vp;
678	struct vm_object **objpp = ap->a_objpp;
679
680	if (objpp)
681		*objpp = vp->v_object;
682	return (vp->v_object ? 0 : EINVAL);
683}
684
685/* XXX Needs good comment and VOP_BMAP(9) manpage */
686int
687vop_stdbmap(ap)
688	struct vop_bmap_args /* {
689		struct vnode *a_vp;
690		daddr_t  a_bn;
691		struct vnode **a_vpp;
692		daddr_t *a_bnp;
693		int *a_runp;
694		int *a_runb;
695	} */ *ap;
696{
697
698	if (ap->a_vpp != NULL)
699		*ap->a_vpp = ap->a_vp;
700	if (ap->a_bnp != NULL)
701		*ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
702	if (ap->a_runp != NULL)
703		*ap->a_runp = 0;
704	if (ap->a_runb != NULL)
705		*ap->a_runb = 0;
706	return (0);
707}
708
709int
710vop_stdfsync(ap)
711	struct vop_fsync_args /* {
712		struct vnode *a_vp;
713		struct ucred *a_cred;
714		int a_waitfor;
715		struct thread *a_td;
716	} */ *ap;
717{
718	struct vnode *vp = ap->a_vp;
719	struct buf *bp;
720	struct buf *nbp;
721	int s, error = 0;
722	int maxretry = 100;     /* large, arbitrarily chosen */
723
724	VI_LOCK(vp);
725loop1:
726	/*
727	 * MARK/SCAN initialization to avoid infinite loops.
728	 */
729	s = splbio();
730        TAILQ_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
731                bp->b_vflags &= ~BV_SCANNED;
732		bp->b_error = 0;
733	}
734	splx(s);
735
736	/*
737	 * Flush all dirty buffers associated with a block device.
738	 */
739loop2:
740	s = splbio();
741	for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp != NULL; bp = nbp) {
742		nbp = TAILQ_NEXT(bp, b_vnbufs);
743		if ((bp->b_vflags & BV_SCANNED) != 0)
744			continue;
745		bp->b_vflags |= BV_SCANNED;
746		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
747			continue;
748		VI_UNLOCK(vp);
749		if ((bp->b_flags & B_DELWRI) == 0)
750			panic("fsync: not dirty");
751		if ((vp->v_vflag & VV_OBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
752			BUF_UNLOCK(bp);
753			vfs_bio_awrite(bp);
754			splx(s);
755		} else {
756			bremfree(bp);
757			splx(s);
758			bawrite(bp);
759		}
760		VI_LOCK(vp);
761		goto loop2;
762	}
763
764	/*
765	 * If synchronous the caller expects us to completely resolve all
766	 * dirty buffers in the system.  Wait for in-progress I/O to
767	 * complete (which could include background bitmap writes), then
768	 * retry if dirty blocks still exist.
769	 */
770	if (ap->a_waitfor == MNT_WAIT) {
771		while (vp->v_numoutput) {
772			vp->v_iflag |= VI_BWAIT;
773			msleep((caddr_t)&vp->v_numoutput, VI_MTX(vp),
774			    PRIBIO + 1, "fsync", 0);
775		}
776		if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
777			/*
778			 * If we are unable to write any of these buffers
779			 * then we fail now rather than trying endlessly
780			 * to write them out.
781			 */
782			TAILQ_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs)
783				if ((error = bp->b_error) == 0)
784					continue;
785			if (error == 0 && --maxretry >= 0) {
786				splx(s);
787				goto loop1;
788			}
789			vprint("fsync: giving up on dirty", vp);
790			error = EAGAIN;
791		}
792	}
793	VI_UNLOCK(vp);
794	splx(s);
795
796	return (error);
797}
798
799/* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
800int
801vop_stdgetpages(ap)
802	struct vop_getpages_args /* {
803		struct vnode *a_vp;
804		vm_page_t *a_m;
805		int a_count;
806		int a_reqpage;
807		vm_ooffset_t a_offset;
808	} */ *ap;
809{
810
811	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
812	    ap->a_count, ap->a_reqpage);
813}
814
815/* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
816int
817vop_stdputpages(ap)
818	struct vop_putpages_args /* {
819		struct vnode *a_vp;
820		vm_page_t *a_m;
821		int a_count;
822		int a_sync;
823		int *a_rtvals;
824		vm_ooffset_t a_offset;
825	} */ *ap;
826{
827
828	return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
829	     ap->a_sync, ap->a_rtvals);
830}
831
832/*
833 * vfs default ops
834 * used to fill the vfs function table to get reasonable default return values.
835 */
836int
837vfs_stdroot (mp, vpp)
838	struct mount *mp;
839	struct vnode **vpp;
840{
841	return (EOPNOTSUPP);
842}
843
844int
845vfs_stdstatfs (mp, sbp, td)
846	struct mount *mp;
847	struct statfs *sbp;
848	struct thread *td;
849{
850	return (EOPNOTSUPP);
851}
852
853int
854vfs_stdvptofh (vp, fhp)
855	struct vnode *vp;
856	struct fid *fhp;
857{
858	return (EOPNOTSUPP);
859}
860
861int
862vfs_stdstart (mp, flags, td)
863	struct mount *mp;
864	int flags;
865	struct thread *td;
866{
867	return (0);
868}
869
870int
871vfs_stdquotactl (mp, cmds, uid, arg, td)
872	struct mount *mp;
873	int cmds;
874	uid_t uid;
875	caddr_t arg;
876	struct thread *td;
877{
878	return (EOPNOTSUPP);
879}
880
881int
882vfs_stdsync (mp, waitfor, cred, td)
883	struct mount *mp;
884	int waitfor;
885	struct ucred *cred;
886	struct thread *td;
887{
888	return (0);
889}
890
891int
892vfs_stdvget (mp, ino, flags, vpp)
893	struct mount *mp;
894	ino_t ino;
895	int flags;
896	struct vnode **vpp;
897{
898	return (EOPNOTSUPP);
899}
900
901int
902vfs_stdfhtovp (mp, fhp, vpp)
903	struct mount *mp;
904	struct fid *fhp;
905	struct vnode **vpp;
906{
907	return (EOPNOTSUPP);
908}
909
910int
911vfs_stdinit (vfsp)
912	struct vfsconf *vfsp;
913{
914	return (0);
915}
916
917int
918vfs_stduninit (vfsp)
919	struct vfsconf *vfsp;
920{
921	return(0);
922}
923
924int
925vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname, td)
926	struct mount *mp;
927	int cmd;
928	struct vnode *filename_vp;
929	int attrnamespace;
930	const char *attrname;
931	struct thread *td;
932{
933	if (filename_vp != NULL)
934		VOP_UNLOCK(filename_vp, 0, td);
935	return(EOPNOTSUPP);
936}
937
938/* end of vfs default ops */
939