887ed925eb2eac4b0f92d33c927b25663ec70e63
[dragonfly.git] / sys / kern / vfs_default.c
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: src/sys/kern/vfs_default.c,v 1.28.2.7 2003/01/10 18:23:26 bde Exp $
40  * $DragonFly: src/sys/kern/vfs_default.c,v 1.11 2004/08/13 17:51:09 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.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/unistd.h>
52 #include <sys/vnode.h>
53 #include <sys/poll.h>
54
55 #include <machine/limits.h>
56
57 #include <vm/vm.h>
58 #include <vm/vm_object.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_ops *default_vnode_vops;
75 static struct vnodeopv_entry_desc default_vnodeop_entries[] = {
76         { &vop_default_desc,            vop_eopnotsupp },
77         { &vop_advlock_desc,            vop_einval },
78         { &vop_bwrite_desc,             (void *) vop_stdbwrite },
79         { &vop_close_desc,              vop_null },
80         { &vop_createvobject_desc,      (void *) vop_stdcreatevobject },
81         { &vop_destroyvobject_desc,     (void *) vop_stddestroyvobject },
82         { &vop_fsync_desc,              vop_null },
83         { &vop_getvobject_desc,         (void *) vop_stdgetvobject },
84         { &vop_ioctl_desc,              vop_enotty },
85         { &vop_islocked_desc,           (void *) vop_noislocked },
86         { &vop_lease_desc,              vop_null },
87         { &vop_lock_desc,               (void *) vop_nolock },
88         { &vop_mmap_desc,               vop_einval },
89         { &vop_lookup_desc,             (void *) vop_nolookup },
90         { &vop_open_desc,               vop_null },
91         { &vop_pathconf_desc,           vop_einval },
92         { &vop_poll_desc,               (void *) vop_nopoll },
93         { &vop_readlink_desc,           vop_einval },
94         { &vop_reallocblks_desc,        vop_eopnotsupp },
95         { &vop_revoke_desc,             (void *) vop_stdrevoke },
96         { &vop_strategy_desc,           (void *) vop_nostrategy },
97         { &vop_unlock_desc,             (void *) vop_nounlock },
98         { &vop_getacl_desc,             vop_eopnotsupp },
99         { &vop_setacl_desc,             vop_eopnotsupp },
100         { &vop_aclcheck_desc,           vop_eopnotsupp },
101         { &vop_getextattr_desc,         vop_eopnotsupp },
102         { &vop_setextattr_desc,         vop_eopnotsupp },
103         { NULL, NULL }
104 };
105
106 static struct vnodeopv_desc default_vnodeop_opv_desc =
107         { &default_vnode_vops, default_vnodeop_entries };
108
109 VNODEOP_SET(default_vnodeop_opv_desc);
110
111 int
112 vop_eopnotsupp(struct vop_generic_args *ap)
113 {
114         /*
115         printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
116         */
117         return (EOPNOTSUPP);
118 }
119
120 int
121 vop_ebadf(struct vop_generic_args *ap)
122 {
123         return (EBADF);
124 }
125
126 int
127 vop_enotty(struct vop_generic_args *ap)
128 {
129         return (ENOTTY);
130 }
131
132 int
133 vop_einval(struct vop_generic_args *ap)
134 {
135         return (EINVAL);
136 }
137
138 int
139 vop_null(struct vop_generic_args *ap)
140 {
141         return (0);
142 }
143
144 int
145 vop_defaultop(struct vop_generic_args *ap)
146 {
147         return (VOCALL(default_vnode_vops, ap->a_desc->vdesc_offset, ap));
148 }
149
150 int
151 vop_panic(struct vop_generic_args *ap)
152 {
153
154         panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
155 }
156
157 static int
158 vop_nolookup(ap)
159         struct vop_lookup_args /* {
160                 struct vnode *a_dvp;
161                 struct vnode **a_vpp;
162                 struct componentname *a_cnp;
163         } */ *ap;
164 {
165
166         *ap->a_vpp = NULL;
167         return (ENOTDIR);
168 }
169
170 /*
171  *      vop_nostrategy:
172  *
173  *      Strategy routine for VFS devices that have none.
174  *
175  *      B_ERROR and B_INVAL must be cleared prior to calling any strategy
176  *      routine.  Typically this is done for a B_READ strategy call.  Typically
177  *      B_INVAL is assumed to already be clear prior to a write and should not
178  *      be cleared manually unless you just made the buffer invalid.  B_ERROR
179  *      should be cleared either way.
180  */
181
182 static int
183 vop_nostrategy (struct vop_strategy_args *ap)
184 {
185         printf("No strategy for buffer at %p\n", ap->a_bp);
186         vprint("", ap->a_vp);
187         vprint("", ap->a_bp->b_vp);
188         ap->a_bp->b_flags |= B_ERROR;
189         ap->a_bp->b_error = EOPNOTSUPP;
190         biodone(ap->a_bp);
191         return (EOPNOTSUPP);
192 }
193
194 int
195 vop_stdpathconf(ap)
196         struct vop_pathconf_args /* {
197         struct vnode *a_vp;
198         int a_name;
199         int *a_retval;
200         } */ *ap;
201 {
202
203         switch (ap->a_name) {
204                 case _PC_LINK_MAX:
205                         *ap->a_retval = LINK_MAX;
206                         return (0);
207                 case _PC_MAX_CANON:
208                         *ap->a_retval = MAX_CANON;
209                         return (0);
210                 case _PC_MAX_INPUT:
211                         *ap->a_retval = MAX_INPUT;
212                         return (0);
213                 case _PC_PIPE_BUF:
214                         *ap->a_retval = PIPE_BUF;
215                         return (0);
216                 case _PC_CHOWN_RESTRICTED:
217                         *ap->a_retval = 1;
218                         return (0);
219                 case _PC_VDISABLE:
220                         *ap->a_retval = _POSIX_VDISABLE;
221                         return (0);
222                 default:
223                         return (EINVAL);
224         }
225         /* NOTREACHED */
226 }
227
228 /*
229  * Standard lock, unlock and islocked functions.
230  *
231  * These depend on the lock structure being the first element in the
232  * inode, ie: vp->v_data points to the the lock!
233  */
234 int
235 vop_stdlock(ap)
236         struct vop_lock_args /* {
237                 struct vnode *a_vp;
238                 lwkt_tokref_t a_vlock;
239                 int a_flags;
240                 struct proc *a_p;
241         } */ *ap;
242 {               
243         struct lock *l;
244
245         if ((l = (struct lock *)ap->a_vp->v_data) == NULL) {
246                 if (ap->a_flags & LK_INTERLOCK)
247                         lwkt_reltoken(ap->a_vlock);
248                 return 0;
249         }
250
251 #ifndef DEBUG_LOCKS
252         return (lockmgr(l, ap->a_flags, ap->a_vlock, ap->a_td));
253 #else
254         return (debuglockmgr(l, ap->a_flags, ap->a_vlock, ap->a_td,
255             "vop_stdlock", ap->a_vp->filename, ap->a_vp->line));
256 #endif
257 }
258
259 int
260 vop_stdunlock(ap)
261         struct vop_unlock_args /* {
262                 struct vnode *a_vp;
263                 lwkt_tokref_t a_vlock;
264                 int a_flags;
265                 struct thread *a_td;
266         } */ *ap;
267 {
268         struct lock *l;
269
270         if ((l = (struct lock *)ap->a_vp->v_data) == NULL) {
271                 if (ap->a_flags & LK_INTERLOCK)
272                         lwkt_reltoken(ap->a_vlock);
273                 return 0;
274         }
275
276         return (lockmgr(l, ap->a_flags | LK_RELEASE, ap->a_vlock, ap->a_td));
277 }
278
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         struct lock *l;
287
288         if ((l = (struct lock *)ap->a_vp->v_data) == NULL)
289                 return 0;
290
291         return (lockstatus(l, ap->a_td));
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 proc *a_p;
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          */
313         if (ap->a_events & ~POLLSTANDARD)
314                 return (POLLNVAL);
315
316         return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
317 }
318
319 /*
320  * Implement poll for local filesystems that support it.
321  */
322 int
323 vop_stdpoll(ap)
324         struct vop_poll_args /* {
325                 struct vnode *a_vp;
326                 int  a_events;
327                 struct ucred *a_cred;
328                 struct thread *a_td;
329         } */ *ap;
330 {
331         if (ap->a_events & ~POLLSTANDARD)
332                 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
333         return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
334 }
335
336 int
337 vop_stdbwrite(ap)
338         struct vop_bwrite_args *ap;
339 {
340         return (bwrite(ap->a_bp));
341 }
342
343 /*
344  * Stubs to use when there is no locking to be done on the underlying object.
345  * A minimal shared lock is necessary to ensure that the underlying object
346  * is not revoked while an operation is in progress. So, an active shared
347  * count is maintained in an auxillary vnode lock structure.
348  */
349 int
350 vop_sharedlock(ap)
351         struct vop_lock_args /* {
352                 struct vnode *a_vp;
353                 lwkt_tokref_t a_vlock;
354                 int a_flags;
355                 struct proc *a_p;
356         } */ *ap;
357 {
358         /*
359          * This code cannot be used until all the non-locking filesystems
360          * (notably NFS) are converted to properly lock and release nodes.
361          * Also, certain vnode operations change the locking state within
362          * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
363          * and symlink). Ideally these operations should not change the
364          * lock state, but should be changed to let the caller of the
365          * function unlock them. Otherwise all intermediate vnode layers
366          * (such as union, umapfs, etc) must catch these functions to do
367          * the necessary locking at their layer. Note that the inactive
368          * and lookup operations also change their lock state, but this 
369          * cannot be avoided, so these two operations will always need
370          * to be handled in intermediate layers.
371          */
372         struct vnode *vp = ap->a_vp;
373         struct lock *l = (struct lock *)vp->v_data;
374         int vnflags, flags = ap->a_flags;
375
376         if (l == NULL) {
377                 if (ap->a_flags & LK_INTERLOCK)
378                         lwkt_reltoken(ap->a_vlock);
379                 return 0;
380         }
381         switch (flags & LK_TYPE_MASK) {
382         case LK_DRAIN:
383                 vnflags = LK_DRAIN;
384                 break;
385         case LK_EXCLUSIVE:
386 #ifdef DEBUG_VFS_LOCKS
387                 /*
388                  * Normally, we use shared locks here, but that confuses
389                  * the locking assertions.
390                  */
391                 vnflags = LK_EXCLUSIVE;
392                 break;
393 #endif
394         case LK_SHARED:
395                 vnflags = LK_SHARED;
396                 break;
397         case LK_UPGRADE:
398         case LK_EXCLUPGRADE:
399         case LK_DOWNGRADE:
400                 return (0);
401         case LK_RELEASE:
402         default:
403                 panic("vop_sharedlock: bad operation %d", flags & LK_TYPE_MASK);
404         }
405         if (flags & LK_INTERLOCK)
406                 vnflags |= LK_INTERLOCK;
407 #ifndef DEBUG_LOCKS
408         return (lockmgr(l, vnflags, ap->a_vlock, ap->a_td));
409 #else
410         return (debuglockmgr(l, vnflags, ap->a_vlock, ap->a_td,
411             "vop_sharedlock", vp->filename, vp->line));
412 #endif
413 }
414
415 /*
416  * Stubs to use when there is no locking to be done on the underlying object.
417  * A minimal shared lock is necessary to ensure that the underlying object
418  * is not revoked while an operation is in progress. So, an active shared
419  * count is maintained in an auxillary vnode lock structure.
420  */
421 int
422 vop_nolock(ap)
423         struct vop_lock_args /* {
424                 struct vnode *a_vp;
425                 lwkt_tokref_t a_vlock;
426                 int a_flags;
427                 struct proc *a_p;
428         } */ *ap;
429 {
430 #ifdef notyet
431         /*
432          * This code cannot be used until all the non-locking filesystems
433          * (notably NFS) are converted to properly lock and release nodes.
434          * Also, certain vnode operations change the locking state within
435          * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
436          * and symlink). Ideally these operations should not change the
437          * lock state, but should be changed to let the caller of the
438          * function unlock them. Otherwise all intermediate vnode layers
439          * (such as union, umapfs, etc) must catch these functions to do
440          * the necessary locking at their layer. Note that the inactive
441          * and lookup operations also change their lock state, but this 
442          * cannot be avoided, so these two operations will always need
443          * to be handled in intermediate layers.
444          */
445         struct vnode *vp = ap->a_vp;
446         int vnflags, flags = ap->a_flags;
447
448         switch (flags & LK_TYPE_MASK) {
449         case LK_DRAIN:
450                 vnflags = LK_DRAIN;
451                 break;
452         case LK_EXCLUSIVE:
453         case LK_SHARED:
454                 vnflags = LK_SHARED;
455                 break;
456         case LK_UPGRADE:
457         case LK_EXCLUPGRADE:
458         case LK_DOWNGRADE:
459                 return (0);
460         case LK_RELEASE:
461         default:
462                 panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
463         }
464         if (flags & LK_INTERLOCK)
465                 vnflags |= LK_INTERLOCK;
466         return(lockmgr(vp->v_vnlock, vnflags, ap->a_vlock, ap->a_p));
467 #else /* for now */
468         /*
469          * Since we are not using the lock manager, we must clear
470          * the interlock here.
471          */
472         if (ap->a_flags & LK_INTERLOCK)
473                 lwkt_reltoken(ap->a_vlock);
474         return (0);
475 #endif
476 }
477
478 /*
479  * Do the inverse of vop_nolock, handling the interlock in a compatible way.
480  */
481 int
482 vop_nounlock(ap)
483         struct vop_unlock_args /* {
484                 struct vnode *a_vp;
485                 lwkt_tokref_t a_vlock;
486                 int a_flags;
487                 struct proc *a_p;
488         } */ *ap;
489 {
490         if (ap->a_flags & LK_INTERLOCK)
491                 lwkt_reltoken(ap->a_vlock);
492         return (0);
493 }
494
495 /*
496  * Return whether or not the node is in use.
497  */
498 int
499 vop_noislocked(ap)
500         struct vop_islocked_args /* {
501                 struct vnode *a_vp;
502                 struct proc *a_p;
503         } */ *ap;
504 {
505         return (0);
506 }
507
508 int
509 vop_stdcreatevobject(ap)
510         struct vop_createvobject_args /* {
511                 struct vnode *a_vp;
512                 struct proc *a_td;
513         } */ *ap;
514 {
515         struct vnode *vp = ap->a_vp;
516         struct thread *td = ap->a_td;
517         struct vattr vat;
518         vm_object_t object;
519         int error = 0;
520
521         if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
522                 return (0);
523
524 retry:
525         if ((object = vp->v_object) == NULL) {
526                 if (vp->v_type == VREG || vp->v_type == VDIR) {
527                         if ((error = VOP_GETATTR(vp, &vat, td)) != 0)
528                                 goto retn;
529                         object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
530                 } else if (vp->v_rdev && dev_is_good(vp->v_rdev)) {
531                         /*
532                          * XXX v_rdev uses NULL/non-NULL instead of NODEV
533                          *
534                          * This simply allocates the biggest object possible
535                          * for a disk vnode.  This should be fixed, but doesn't
536                          * cause any problems (yet).
537                          */
538                         object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
539                 } else {
540                         goto retn;
541                 }
542                 /*
543                  * Dereference the reference we just created.  This assumes
544                  * that the object is associated with the vp.
545                  */
546                 object->ref_count--;
547                 vp->v_usecount--;
548         } else {
549                 if (object->flags & OBJ_DEAD) {
550                         VOP_UNLOCK(vp, NULL, 0, td);
551                         tsleep(object, 0, "vodead", 0);
552                         vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
553                         goto retry;
554                 }
555         }
556
557         KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
558         vp->v_flag |= VOBJBUF;
559
560 retn:
561         return (error);
562 }
563
564 int
565 vop_stddestroyvobject(ap)
566         struct vop_destroyvobject_args /* {
567                 struct vnode *vp;
568         } */ *ap;
569 {
570         struct vnode *vp = ap->a_vp;
571         vm_object_t obj = vp->v_object;
572
573         if (vp->v_object == NULL)
574                 return (0);
575
576         if (obj->ref_count == 0) {
577                 /*
578                  * vclean() may be called twice. The first time
579                  * removes the primary reference to the object,
580                  * the second time goes one further and is a
581                  * special-case to terminate the object.
582                  *
583                  * don't double-terminate the object.
584                  */
585                 if ((obj->flags & OBJ_DEAD) == 0)
586                         vm_object_terminate(obj);
587         } else {
588                 /*
589                  * Woe to the process that tries to page now :-).
590                  */
591                 vm_pager_deallocate(obj);
592         }
593         return (0);
594 }
595
596 /*
597  * Return the underlying VM object.  This routine may be called with or
598  * without the vnode interlock held.  If called without, the returned
599  * object is not guarenteed to be valid.  The syncer typically gets the
600  * object without holding the interlock in order to quickly test whether
601  * it might be dirty before going heavy-weight.  vm_object's use zalloc
602  * and thus stable-storage, so this is safe.
603  */
604 int
605 vop_stdgetvobject(ap)
606         struct vop_getvobject_args /* {
607                 struct vnode *vp;
608                 struct vm_object **objpp;
609         } */ *ap;
610 {
611         struct vnode *vp = ap->a_vp;
612         struct vm_object **objpp = ap->a_objpp;
613
614         if (objpp)
615                 *objpp = vp->v_object;
616         return (vp->v_object ? 0 : EINVAL);
617 }
618
619 /* 
620  * vfs default ops
621  * used to fill the vfs fucntion table to get reasonable default return values.
622  */
623 int 
624 vfs_stdmount(struct mount *mp, char *path, caddr_t data,
625         struct nameidata *ndp, struct thread *td)
626 {
627         return (0);
628 }
629
630 int     
631 vfs_stdunmount(struct mount *mp, int mntflags, struct thread *td)
632 {
633         return (0);
634 }
635
636 int     
637 vfs_stdroot(struct mount *mp, struct vnode **vpp)
638 {
639         return (EOPNOTSUPP);
640 }
641
642 int     
643 vfs_stdstatfs(struct mount *mp, struct statfs *sbp, struct thread *td)
644 {
645         return (EOPNOTSUPP);
646 }
647
648 int
649 vfs_stdvptofh(struct vnode *vp, struct fid *fhp)
650 {
651         return (EOPNOTSUPP);
652 }
653
654 int     
655 vfs_stdstart(struct mount *mp, int flags, struct thread *td)
656 {
657         return (0);
658 }
659
660 int     
661 vfs_stdquotactl(struct mount *mp, int cmds, uid_t uid,
662         caddr_t arg, struct thread *td)
663 {
664         return (EOPNOTSUPP);
665 }
666
667 int     
668 vfs_stdsync(struct mount *mp, int waitfor, struct thread *td)
669 {
670         return (0);
671 }
672
673 int     
674 vfs_stdvget(struct mount *mp, ino_t ino, struct vnode **vpp)
675 {
676         return (EOPNOTSUPP);
677 }
678
679 int     
680 vfs_stdfhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
681 {
682         return (EOPNOTSUPP);
683 }
684
685 int 
686 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
687         struct ucred **credanonp)
688 {
689         return (EOPNOTSUPP);
690 }
691
692 int
693 vfs_stdinit(struct vfsconf *vfsp)
694 {
695         return (0);
696 }
697
698 int
699 vfs_stduninit(struct vfsconf *vfsp)
700 {
701         return(0);
702 }
703
704 int
705 vfs_stdextattrctl(struct mount *mp, int cmd, const char *attrname,
706         caddr_t arg, struct thread *td)
707 {
708         return(EOPNOTSUPP);
709 }
710
711 /* end of vfs default ops */