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