xref: /freebsd/sys/kern/vfs_default.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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$");
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/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/unistd.h>
50 #include <sys/vnode.h>
51 #include <sys/poll.h>
52 
53 #include <vm/vm.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_extern.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pager.h>
60 #include <vm/vnode_pager.h>
61 
62 static int	vop_nolookup(struct vop_lookup_args *);
63 static int	vop_nostrategy(struct vop_strategy_args *);
64 
65 /*
66  * This vnode table stores what we want to do if the filesystem doesn't
67  * implement a particular VOP.
68  *
69  * If there is no specific entry here, we will return EOPNOTSUPP.
70  *
71  */
72 
73 struct vop_vector default_vnodeops = {
74 	.vop_default =		NULL,
75 	.vop_bypass =		VOP_EOPNOTSUPP,
76 
77 	.vop_advlock =		VOP_EINVAL,
78 	.vop_bmap =		vop_stdbmap,
79 	.vop_close =		VOP_NULL,
80 	.vop_fsync =		VOP_NULL,
81 	.vop_getpages =		vop_stdgetpages,
82 	.vop_getwritemount = 	vop_stdgetwritemount,
83 	.vop_inactive =		vop_stdinactive,
84 	.vop_ioctl =		VOP_ENOTTY,
85 	.vop_islocked =		vop_stdislocked,
86 	.vop_lease =		VOP_NULL,
87 	.vop_lock =		vop_stdlock,
88 	.vop_lookup =		vop_nolookup,
89 	.vop_open =		VOP_NULL,
90 	.vop_pathconf =		VOP_EINVAL,
91 	.vop_poll =		vop_nopoll,
92 	.vop_putpages =		vop_stdputpages,
93 	.vop_readlink =		VOP_EINVAL,
94 	.vop_revoke =		VOP_PANIC,
95 	.vop_strategy =		vop_nostrategy,
96 	.vop_unlock =		vop_stdunlock,
97 };
98 
99 /*
100  * Series of placeholder functions for various error returns for
101  * VOPs.
102  */
103 
104 int
105 vop_eopnotsupp(struct vop_generic_args *ap)
106 {
107 	/*
108 	printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
109 	*/
110 
111 	return (EOPNOTSUPP);
112 }
113 
114 int
115 vop_ebadf(struct vop_generic_args *ap)
116 {
117 
118 	return (EBADF);
119 }
120 
121 int
122 vop_enotty(struct vop_generic_args *ap)
123 {
124 
125 	return (ENOTTY);
126 }
127 
128 int
129 vop_einval(struct vop_generic_args *ap)
130 {
131 
132 	return (EINVAL);
133 }
134 
135 int
136 vop_null(struct vop_generic_args *ap)
137 {
138 
139 	return (0);
140 }
141 
142 /*
143  * Helper function to panic on some bad VOPs in some filesystems.
144  */
145 int
146 vop_panic(struct vop_generic_args *ap)
147 {
148 
149 	panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
150 }
151 
152 /*
153  * vop_std<something> and vop_no<something> are default functions for use by
154  * filesystems that need the "default reasonable" implementation for a
155  * particular operation.
156  *
157  * The documentation for the operations they implement exists (if it exists)
158  * in the VOP_<SOMETHING>(9) manpage (all uppercase).
159  */
160 
161 /*
162  * Default vop for filesystems that do not support name lookup
163  */
164 static int
165 vop_nolookup(ap)
166 	struct vop_lookup_args /* {
167 		struct vnode *a_dvp;
168 		struct vnode **a_vpp;
169 		struct componentname *a_cnp;
170 	} */ *ap;
171 {
172 
173 	*ap->a_vpp = NULL;
174 	return (ENOTDIR);
175 }
176 
177 /*
178  *	vop_nostrategy:
179  *
180  *	Strategy routine for VFS devices that have none.
181  *
182  *	BIO_ERROR and B_INVAL must be cleared prior to calling any strategy
183  *	routine.  Typically this is done for a BIO_READ strategy call.
184  *	Typically B_INVAL is assumed to already be clear prior to a write
185  *	and should not be cleared manually unless you just made the buffer
186  *	invalid.  BIO_ERROR should be cleared either way.
187  */
188 
189 static int
190 vop_nostrategy (struct vop_strategy_args *ap)
191 {
192 	printf("No strategy for buffer at %p\n", ap->a_bp);
193 	vprint("vnode", ap->a_vp);
194 	ap->a_bp->b_ioflags |= BIO_ERROR;
195 	ap->a_bp->b_error = EOPNOTSUPP;
196 	bufdone(ap->a_bp);
197 	return (EOPNOTSUPP);
198 }
199 
200 /*
201  * vop_stdpathconf:
202  *
203  * Standard implementation of POSIX pathconf, to get information about limits
204  * for a filesystem.
205  * Override per filesystem for the case where the filesystem has smaller
206  * limits.
207  */
208 int
209 vop_stdpathconf(ap)
210 	struct vop_pathconf_args /* {
211 	struct vnode *a_vp;
212 	int a_name;
213 	int *a_retval;
214 	} */ *ap;
215 {
216 
217 	switch (ap->a_name) {
218 		case _PC_LINK_MAX:
219 			*ap->a_retval = LINK_MAX;
220 			return (0);
221 		case _PC_MAX_CANON:
222 			*ap->a_retval = MAX_CANON;
223 			return (0);
224 		case _PC_MAX_INPUT:
225 			*ap->a_retval = MAX_INPUT;
226 			return (0);
227 		case _PC_PIPE_BUF:
228 			*ap->a_retval = PIPE_BUF;
229 			return (0);
230 		case _PC_CHOWN_RESTRICTED:
231 			*ap->a_retval = 1;
232 			return (0);
233 		case _PC_VDISABLE:
234 			*ap->a_retval = _POSIX_VDISABLE;
235 			return (0);
236 		default:
237 			return (EINVAL);
238 	}
239 	/* NOTREACHED */
240 }
241 
242 /*
243  * Standard lock, unlock and islocked functions.
244  */
245 int
246 vop_stdlock(ap)
247 	struct vop_lock_args /* {
248 		struct vnode *a_vp;
249 		int a_flags;
250 		struct thread *a_td;
251 	} */ *ap;
252 {
253 	struct vnode *vp = ap->a_vp;
254 
255 #ifndef	DEBUG_LOCKS
256 	return (lockmgr(vp->v_vnlock, ap->a_flags, VI_MTX(vp), ap->a_td));
257 #else
258 	return (debuglockmgr(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
259 	    ap->a_td, "vop_stdlock", vp->filename, vp->line));
260 #endif
261 }
262 
263 /* See above. */
264 int
265 vop_stdunlock(ap)
266 	struct vop_unlock_args /* {
267 		struct vnode *a_vp;
268 		int a_flags;
269 		struct thread *a_td;
270 	} */ *ap;
271 {
272 	struct vnode *vp = ap->a_vp;
273 
274 	return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE, VI_MTX(vp),
275 	    ap->a_td));
276 }
277 
278 /* See above. */
279 int
280 vop_stdislocked(ap)
281 	struct vop_islocked_args /* {
282 		struct vnode *a_vp;
283 		struct thread *a_td;
284 	} */ *ap;
285 {
286 
287 	return (lockstatus(ap->a_vp->v_vnlock, ap->a_td));
288 }
289 
290 /* Mark the vnode inactive */
291 int
292 vop_stdinactive(ap)
293 	struct vop_inactive_args /* {
294 		struct vnode *a_vp;
295 		struct thread *a_td;
296 	} */ *ap;
297 {
298 
299 	VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
300 	return (0);
301 }
302 
303 /*
304  * Return true for select/poll.
305  */
306 int
307 vop_nopoll(ap)
308 	struct vop_poll_args /* {
309 		struct vnode *a_vp;
310 		int  a_events;
311 		struct ucred *a_cred;
312 		struct thread *a_td;
313 	} */ *ap;
314 {
315 	/*
316 	 * Return true for read/write.  If the user asked for something
317 	 * special, return POLLNVAL, so that clients have a way of
318 	 * determining reliably whether or not the extended
319 	 * functionality is present without hard-coding knowledge
320 	 * of specific filesystem implementations.
321 	 * Stay in sync with kern_conf.c::no_poll().
322 	 */
323 	if (ap->a_events & ~POLLSTANDARD)
324 		return (POLLNVAL);
325 
326 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
327 }
328 
329 /*
330  * Implement poll for local filesystems that support it.
331  */
332 int
333 vop_stdpoll(ap)
334 	struct vop_poll_args /* {
335 		struct vnode *a_vp;
336 		int  a_events;
337 		struct ucred *a_cred;
338 		struct thread *a_td;
339 	} */ *ap;
340 {
341 	if (ap->a_events & ~POLLSTANDARD)
342 		return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
343 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
344 }
345 
346 /*
347  * Return our mount point, as we will take charge of the writes.
348  */
349 int
350 vop_stdgetwritemount(ap)
351 	struct vop_getwritemount_args /* {
352 		struct vnode *a_vp;
353 		struct mount **a_mpp;
354 	} */ *ap;
355 {
356 
357 	*(ap->a_mpp) = ap->a_vp->v_mount;
358 	return (0);
359 }
360 
361 /* XXX Needs good comment and VOP_BMAP(9) manpage */
362 int
363 vop_stdbmap(ap)
364 	struct vop_bmap_args /* {
365 		struct vnode *a_vp;
366 		daddr_t  a_bn;
367 		struct bufobj **a_bop;
368 		daddr_t *a_bnp;
369 		int *a_runp;
370 		int *a_runb;
371 	} */ *ap;
372 {
373 
374 	if (ap->a_bop != NULL)
375 		*ap->a_bop = &ap->a_vp->v_bufobj;
376 	if (ap->a_bnp != NULL)
377 		*ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
378 	if (ap->a_runp != NULL)
379 		*ap->a_runp = 0;
380 	if (ap->a_runb != NULL)
381 		*ap->a_runb = 0;
382 	return (0);
383 }
384 
385 int
386 vop_stdfsync(ap)
387 	struct vop_fsync_args /* {
388 		struct vnode *a_vp;
389 		struct ucred *a_cred;
390 		int a_waitfor;
391 		struct thread *a_td;
392 	} */ *ap;
393 {
394 	struct vnode *vp = ap->a_vp;
395 	struct buf *bp;
396 	struct bufobj *bo;
397 	struct buf *nbp;
398 	int s, error = 0;
399 	int maxretry = 100;     /* large, arbitrarily chosen */
400 
401 	VI_LOCK(vp);
402 loop1:
403 	/*
404 	 * MARK/SCAN initialization to avoid infinite loops.
405 	 */
406 	s = splbio();
407         TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
408                 bp->b_vflags &= ~BV_SCANNED;
409 		bp->b_error = 0;
410 	}
411 	splx(s);
412 
413 	/*
414 	 * Flush all dirty buffers associated with a block device.
415 	 */
416 loop2:
417 	s = splbio();
418 	TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
419 		if ((bp->b_vflags & BV_SCANNED) != 0)
420 			continue;
421 		bp->b_vflags |= BV_SCANNED;
422 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
423 			continue;
424 		VI_UNLOCK(vp);
425 		if ((bp->b_flags & B_DELWRI) == 0)
426 			panic("fsync: not dirty");
427 		if ((vp->v_object != NULL) && (bp->b_flags & B_CLUSTEROK)) {
428 			vfs_bio_awrite(bp);
429 			splx(s);
430 		} else {
431 			bremfree(bp);
432 			splx(s);
433 			bawrite(bp);
434 		}
435 		VI_LOCK(vp);
436 		goto loop2;
437 	}
438 
439 	/*
440 	 * If synchronous the caller expects us to completely resolve all
441 	 * dirty buffers in the system.  Wait for in-progress I/O to
442 	 * complete (which could include background bitmap writes), then
443 	 * retry if dirty blocks still exist.
444 	 */
445 	if (ap->a_waitfor == MNT_WAIT) {
446 		bo = &vp->v_bufobj;
447 		bufobj_wwait(bo, 0, 0);
448 		if (bo->bo_dirty.bv_cnt > 0) {
449 			/*
450 			 * If we are unable to write any of these buffers
451 			 * then we fail now rather than trying endlessly
452 			 * to write them out.
453 			 */
454 			TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
455 				if ((error = bp->b_error) == 0)
456 					continue;
457 			if (error == 0 && --maxretry >= 0) {
458 				splx(s);
459 				goto loop1;
460 			}
461 			vprint("fsync: giving up on dirty", vp);
462 			error = EAGAIN;
463 		}
464 	}
465 	VI_UNLOCK(vp);
466 	splx(s);
467 
468 	return (error);
469 }
470 
471 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
472 int
473 vop_stdgetpages(ap)
474 	struct vop_getpages_args /* {
475 		struct vnode *a_vp;
476 		vm_page_t *a_m;
477 		int a_count;
478 		int a_reqpage;
479 		vm_ooffset_t a_offset;
480 	} */ *ap;
481 {
482 
483 	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
484 	    ap->a_count, ap->a_reqpage);
485 }
486 
487 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
488 int
489 vop_stdputpages(ap)
490 	struct vop_putpages_args /* {
491 		struct vnode *a_vp;
492 		vm_page_t *a_m;
493 		int a_count;
494 		int a_sync;
495 		int *a_rtvals;
496 		vm_ooffset_t a_offset;
497 	} */ *ap;
498 {
499 
500 	return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
501 	     ap->a_sync, ap->a_rtvals);
502 }
503 
504 /*
505  * vfs default ops
506  * used to fill the vfs function table to get reasonable default return values.
507  */
508 int
509 vfs_stdroot (mp, vpp, td)
510 	struct mount *mp;
511 	struct vnode **vpp;
512 	struct thread *td;
513 {
514 
515 	return (EOPNOTSUPP);
516 }
517 
518 int
519 vfs_stdstatfs (mp, sbp, td)
520 	struct mount *mp;
521 	struct statfs *sbp;
522 	struct thread *td;
523 {
524 
525 	return (EOPNOTSUPP);
526 }
527 
528 int
529 vfs_stdvptofh (vp, fhp)
530 	struct vnode *vp;
531 	struct fid *fhp;
532 {
533 
534 	return (EOPNOTSUPP);
535 }
536 
537 int
538 vfs_stdstart (mp, flags, td)
539 	struct mount *mp;
540 	int flags;
541 	struct thread *td;
542 {
543 
544 	return (0);
545 }
546 
547 int
548 vfs_stdquotactl (mp, cmds, uid, arg, td)
549 	struct mount *mp;
550 	int cmds;
551 	uid_t uid;
552 	caddr_t arg;
553 	struct thread *td;
554 {
555 
556 	return (EOPNOTSUPP);
557 }
558 
559 int
560 vfs_stdsync(mp, waitfor, td)
561 	struct mount *mp;
562 	int waitfor;
563 	struct thread *td;
564 {
565 	struct vnode *vp, *nvp;
566 	int error, lockreq, allerror = 0;
567 
568 	lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
569 	if (waitfor != MNT_WAIT)
570 		lockreq |= LK_NOWAIT;
571 	/*
572 	 * Force stale buffer cache information to be flushed.
573 	 */
574 	MNT_ILOCK(mp);
575 loop:
576 	MNT_VNODE_FOREACH(vp, mp, nvp) {
577 
578 		VI_LOCK(vp);
579 		if (vp->v_bufobj.bo_dirty.bv_cnt == 0) {
580 			VI_UNLOCK(vp);
581 			continue;
582 		}
583 		MNT_IUNLOCK(mp);
584 
585 		if ((error = vget(vp, lockreq, td)) != 0) {
586 			MNT_ILOCK(mp);
587 			if (error == ENOENT)
588 				goto loop;
589 			continue;
590 		}
591 		error = VOP_FSYNC(vp, waitfor, td);
592 		if (error)
593 			allerror = error;
594 
595 		VOP_UNLOCK(vp, 0, td);
596 		vrele(vp);
597 		MNT_ILOCK(mp);
598 	}
599 	MNT_IUNLOCK(mp);
600 	return (allerror);
601 }
602 
603 int
604 vfs_stdnosync (mp, waitfor, td)
605 	struct mount *mp;
606 	int waitfor;
607 	struct thread *td;
608 {
609 
610 	return (0);
611 }
612 
613 int
614 vfs_stdvget (mp, ino, flags, vpp)
615 	struct mount *mp;
616 	ino_t ino;
617 	int flags;
618 	struct vnode **vpp;
619 {
620 
621 	return (EOPNOTSUPP);
622 }
623 
624 int
625 vfs_stdfhtovp (mp, fhp, vpp)
626 	struct mount *mp;
627 	struct fid *fhp;
628 	struct vnode **vpp;
629 {
630 
631 	return (EOPNOTSUPP);
632 }
633 
634 int
635 vfs_stdinit (vfsp)
636 	struct vfsconf *vfsp;
637 {
638 
639 	return (0);
640 }
641 
642 int
643 vfs_stduninit (vfsp)
644 	struct vfsconf *vfsp;
645 {
646 
647 	return(0);
648 }
649 
650 int
651 vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname, td)
652 	struct mount *mp;
653 	int cmd;
654 	struct vnode *filename_vp;
655 	int attrnamespace;
656 	const char *attrname;
657 	struct thread *td;
658 {
659 
660 	if (filename_vp != NULL)
661 		VOP_UNLOCK(filename_vp, 0, td);
662 	return (EOPNOTSUPP);
663 }
664 
665 int
666 vfs_stdsysctl(mp, op, req)
667 	struct mount *mp;
668 	fsctlop_t op;
669 	struct sysctl_req *req;
670 {
671 
672 	return (EOPNOTSUPP);
673 }
674 
675 /* end of vfs default ops */
676