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