xref: /freebsd/sys/kern/vfs_default.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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$
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 
67 static int	vop_nolookup(struct vop_lookup_args *);
68 static int	vop_nostrategy(struct vop_strategy_args *);
69 static 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 
79 vop_t **default_vnodeop_p;
80 static 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 
108 static struct vnodeopv_desc default_vnodeop_opv_desc =
109         { &default_vnodeop_p, default_vnodeop_entries };
110 
111 VNODEOP_SET(default_vnodeop_opv_desc);
112 
113 /*
114  * Series of placeholder functions for various error returns for
115  * VOPs.
116  */
117 
118 int
119 vop_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 
128 int
129 vop_ebadf(struct vop_generic_args *ap)
130 {
131 
132 	return (EBADF);
133 }
134 
135 int
136 vop_enotty(struct vop_generic_args *ap)
137 {
138 
139 	return (ENOTTY);
140 }
141 
142 int
143 vop_einval(struct vop_generic_args *ap)
144 {
145 
146 	return (EINVAL);
147 }
148 
149 int
150 vop_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  */
159 int
160 vop_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  */
169 int
170 vop_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  */
188 static int
189 vop_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 
213 static int
214 vop_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 
235 static int
236 vop_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  */
256 int
257 vop_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  */
293 int
294 vop_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. */
312 int
313 vop_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. */
327 int
328 vop_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 */
339 int
340 vop_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  */
354 int
355 vop_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  */
379 int
380 vop_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  */
399 int
400 vop_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  */
463 int
464 vop_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  */
521 int
522 vop_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  */
542 int
543 vop_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  */
556 int
557 vop_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 */
569 int
570 vop_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 
589 retry:
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 
623 retn:
624 	return (error);
625 }
626 
627 /* Destroy the VM system object associated with this vnode */
628 int
629 vop_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  */
670 int
671 vop_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 */
686 int
687 vop_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 
709 int
710 vop_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);
725 loop1:
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 	 */
739 loop2:
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 			vfs_bio_awrite(bp);
753 			splx(s);
754 		} else {
755 			bremfree(bp);
756 			splx(s);
757 			bawrite(bp);
758 		}
759 		VI_LOCK(vp);
760 		goto loop2;
761 	}
762 
763 	/*
764 	 * If synchronous the caller expects us to completely resolve all
765 	 * dirty buffers in the system.  Wait for in-progress I/O to
766 	 * complete (which could include background bitmap writes), then
767 	 * retry if dirty blocks still exist.
768 	 */
769 	if (ap->a_waitfor == MNT_WAIT) {
770 		while (vp->v_numoutput) {
771 			vp->v_iflag |= VI_BWAIT;
772 			msleep((caddr_t)&vp->v_numoutput, VI_MTX(vp),
773 			    PRIBIO + 1, "fsync", 0);
774 		}
775 		if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
776 			/*
777 			 * If we are unable to write any of these buffers
778 			 * then we fail now rather than trying endlessly
779 			 * to write them out.
780 			 */
781 			TAILQ_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs)
782 				if ((error = bp->b_error) == 0)
783 					continue;
784 			if (error == 0 && --maxretry >= 0) {
785 				splx(s);
786 				goto loop1;
787 			}
788 			vprint("fsync: giving up on dirty", vp);
789 			error = EAGAIN;
790 		}
791 	}
792 	VI_UNLOCK(vp);
793 	splx(s);
794 
795 	return (error);
796 }
797 
798 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
799 int
800 vop_stdgetpages(ap)
801 	struct vop_getpages_args /* {
802 		struct vnode *a_vp;
803 		vm_page_t *a_m;
804 		int a_count;
805 		int a_reqpage;
806 		vm_ooffset_t a_offset;
807 	} */ *ap;
808 {
809 
810 	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
811 	    ap->a_count, ap->a_reqpage);
812 }
813 
814 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
815 int
816 vop_stdputpages(ap)
817 	struct vop_putpages_args /* {
818 		struct vnode *a_vp;
819 		vm_page_t *a_m;
820 		int a_count;
821 		int a_sync;
822 		int *a_rtvals;
823 		vm_ooffset_t a_offset;
824 	} */ *ap;
825 {
826 
827 	return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
828 	     ap->a_sync, ap->a_rtvals);
829 }
830 
831 /*
832  * vfs default ops
833  * used to fill the vfs function table to get reasonable default return values.
834  */
835 int
836 vfs_stdroot (mp, vpp)
837 	struct mount *mp;
838 	struct vnode **vpp;
839 {
840 	return (EOPNOTSUPP);
841 }
842 
843 int
844 vfs_stdstatfs (mp, sbp, td)
845 	struct mount *mp;
846 	struct statfs *sbp;
847 	struct thread *td;
848 {
849 	return (EOPNOTSUPP);
850 }
851 
852 int
853 vfs_stdvptofh (vp, fhp)
854 	struct vnode *vp;
855 	struct fid *fhp;
856 {
857 	return (EOPNOTSUPP);
858 }
859 
860 int
861 vfs_stdstart (mp, flags, td)
862 	struct mount *mp;
863 	int flags;
864 	struct thread *td;
865 {
866 	return (0);
867 }
868 
869 int
870 vfs_stdquotactl (mp, cmds, uid, arg, td)
871 	struct mount *mp;
872 	int cmds;
873 	uid_t uid;
874 	caddr_t arg;
875 	struct thread *td;
876 {
877 	return (EOPNOTSUPP);
878 }
879 
880 int
881 vfs_stdsync(mp, waitfor, cred, td)
882 	struct mount *mp;
883 	int waitfor;
884 	struct ucred *cred;
885 	struct thread *td;
886 {
887 	struct vnode *vp, *nvp;
888 	int error, lockreq, allerror = 0;
889 
890 	lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
891 	if (waitfor != MNT_WAIT)
892 		lockreq |= LK_NOWAIT;
893 	/*
894 	 * Force stale buffer cache information to be flushed.
895 	 */
896 	mtx_lock(&mntvnode_mtx);
897 loop:
898 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
899 		/*
900 		 * If the vnode that we are about to sync is no longer
901 		 * associated with this mount point, start over.
902 		 */
903 		if (vp->v_mount != mp)
904 			goto loop;
905 
906 		nvp = TAILQ_NEXT(vp, v_nmntvnodes);
907 
908 		VI_LOCK(vp);
909 		if (TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
910 			VI_UNLOCK(vp);
911 			continue;
912 		}
913 		mtx_unlock(&mntvnode_mtx);
914 
915 		if ((error = vget(vp, lockreq, td)) != 0) {
916 			if (error == ENOENT)
917 				goto loop;
918 			continue;
919 		}
920 		error = VOP_FSYNC(vp, cred, waitfor, td);
921 		if (error)
922 			allerror = error;
923 
924 		mtx_lock(&mntvnode_mtx);
925 		if (nvp != TAILQ_NEXT(vp, v_nmntvnodes)) {
926 			vput(vp);
927 			goto loop;
928 		}
929 		vput(vp);
930 	}
931 	mtx_unlock(&mntvnode_mtx);
932 	return (allerror);
933 }
934 
935 int
936 vfs_stdnosync (mp, waitfor, cred, td)
937 	struct mount *mp;
938 	int waitfor;
939 	struct ucred *cred;
940 	struct thread *td;
941 {
942 	return (0);
943 }
944 
945 int
946 vfs_stdvget (mp, ino, flags, vpp)
947 	struct mount *mp;
948 	ino_t ino;
949 	int flags;
950 	struct vnode **vpp;
951 {
952 	return (EOPNOTSUPP);
953 }
954 
955 int
956 vfs_stdfhtovp (mp, fhp, vpp)
957 	struct mount *mp;
958 	struct fid *fhp;
959 	struct vnode **vpp;
960 {
961 	return (EOPNOTSUPP);
962 }
963 
964 int
965 vfs_stdinit (vfsp)
966 	struct vfsconf *vfsp;
967 {
968 	return (0);
969 }
970 
971 int
972 vfs_stduninit (vfsp)
973 	struct vfsconf *vfsp;
974 {
975 	return(0);
976 }
977 
978 int
979 vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname, td)
980 	struct mount *mp;
981 	int cmd;
982 	struct vnode *filename_vp;
983 	int attrnamespace;
984 	const char *attrname;
985 	struct thread *td;
986 {
987 	if (filename_vp != NULL)
988 		VOP_UNLOCK(filename_vp, 0, td);
989 	return(EOPNOTSUPP);
990 }
991 
992 /* end of vfs default ops */
993