Merge from vendor branch BSDTAR:
[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.21 2004/10/12 19:20:46 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/namei.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_page.h>
61 #include <vm/vm_pager.h>
62 #include <vm/vnode_pager.h>
63
64 static int      vop_nolookup (struct vop_lookup_args *);
65 static int      vop_nostrategy (struct vop_strategy_args *);
66
67 /*
68  * This vnode table stores what we want to do if the filesystem doesn't
69  * implement a particular VOP.
70  *
71  * If there is no specific entry here, we will return EOPNOTSUPP.
72  */
73 struct vop_ops *default_vnode_vops;
74 static struct vnodeopv_entry_desc default_vnodeop_entries[] = {
75         { &vop_default_desc,            vop_eopnotsupp },
76         { &vop_advlock_desc,            vop_einval },
77         { &vop_bwrite_desc,             (void *) vop_stdbwrite },
78         { &vop_close_desc,              vop_null },
79         { &vop_createvobject_desc,      (void *) vop_stdcreatevobject },
80         { &vop_destroyvobject_desc,     (void *) vop_stddestroyvobject },
81         { &vop_fsync_desc,              vop_null },
82         { &vop_getvobject_desc,         (void *) vop_stdgetvobject },
83         { &vop_ioctl_desc,              vop_enotty },
84         { &vop_islocked_desc,           (void *) vop_stdislocked },
85         { &vop_lease_desc,              vop_null },
86         { &vop_lock_desc,               (void *) vop_stdlock },
87         { &vop_mmap_desc,               vop_einval },
88         { &vop_resolve_desc,            (void *) vop_noresolve },
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_stdunlock },
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         return (EOPNOTSUPP);
115 }
116
117 int
118 vop_ebadf(struct vop_generic_args *ap)
119 {
120         return (EBADF);
121 }
122
123 int
124 vop_enotty(struct vop_generic_args *ap)
125 {
126         return (ENOTTY);
127 }
128
129 int
130 vop_einval(struct vop_generic_args *ap)
131 {
132         return (EINVAL);
133 }
134
135 int
136 vop_null(struct vop_generic_args *ap)
137 {
138         return (0);
139 }
140
141 int
142 vop_defaultop(struct vop_generic_args *ap)
143 {
144         return (VOCALL(default_vnode_vops, ap));
145 }
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_noresolve { struct namecache *a_ncp }    XXX STOPGAP FUNCTION
156  *
157  * XXX OLD API ROUTINE!  WHEN ALL VFSs HAVE BEEN CLEANED UP THIS PROCEDURE
158  * WILL BE REMOVED.  This procedure exists for all VFSs which have not
159  * yet implemented vop_resolve().  It converts vop_resolve() into a 
160  * vop_lookup() and does appropriate translations.
161  *
162  * Resolve a ncp for VFSs which do not support the VOP.  Eventually all
163  * VFSs will support this VOP and this routine can be removed, since
164  * vop_resolve() is far less complex then the older LOOKUP/CACHEDLOOKUP
165  * API.
166  *
167  * A locked ncp is passed in to be resolved.  The NCP is resolved by
168  * figuring out the vnode (if any) and calling cache_setvp() to attach the
169  * vnode to the entry.  If the entry represents a non-existant node then
170  * cache_setvp() is called with a NULL vnode to resolve the entry into a
171  * negative cache entry.  No vnode locks are retained and the
172  * ncp is left locked on return.
173  *
174  * There is a potential directory and vnode interlock.   The lock order
175  * requirement is: namecache, governing directory, resolved vnode.
176  */
177 int
178 vop_noresolve(struct vop_resolve_args *ap)
179 {
180         int error;
181         struct vnode *dvp;
182         struct vnode *vp;
183         struct namecache *ncp;
184         struct componentname cnp;
185
186         ncp = ap->a_ncp;        /* locked namecache node */
187         if (ncp->nc_flag & NCF_MOUNTPT) /* can't cross a mount point! */
188                 return(EPERM);
189         if (ncp->nc_parent == NULL)
190                 return(EPERM);
191         if ((dvp = ncp->nc_parent->nc_vp) == NULL)
192                 return(EPERM);
193
194         if ((error = vget(dvp, LK_EXCLUSIVE, curthread)) != 0) {
195                 printf("[diagnostic] vop_noresolve: EAGAIN on ncp %p %*.*s\n",
196                         ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
197                 return(EAGAIN);
198         }
199
200         bzero(&cnp, sizeof(cnp));
201         cnp.cn_nameiop = NAMEI_LOOKUP;
202         cnp.cn_flags = CNP_ISLASTCN;
203         cnp.cn_nameptr = ncp->nc_name;
204         cnp.cn_namelen = ncp->nc_nlen;
205         cnp.cn_cred = ap->a_cred;
206         cnp.cn_td = curthread; /* XXX */
207
208         /*
209          * vop_lookup() always returns vp locked.  dvp may or may not be
210          * left locked depending on CNP_PDIRUNLOCK.
211          */
212         error = vop_lookup(ap->a_head.a_ops, dvp, &vp, &cnp);
213         if (error == 0)
214                 VOP_UNLOCK(vp, 0, curthread);
215         if ((cnp.cn_flags & CNP_PDIRUNLOCK) == 0)
216                 VOP_UNLOCK(dvp, 0, curthread);
217         if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) {
218                 /* was resolved by another process while we were unlocked */
219                 if (error == 0)
220                         vrele(vp);
221         } else if (error == 0) {
222                 KKASSERT(vp != NULL);
223                 cache_setvp(ncp, vp);
224                 vrele(vp);
225         } else if (error == ENOENT) {
226                 KKASSERT(vp == NULL);
227                 if (cnp.cn_flags & CNP_ISWHITEOUT)
228                         ncp->nc_flag |= NCF_WHITEOUT;
229                 cache_setvp(ncp, NULL);
230         }
231         vrele(dvp);
232         return (error);
233 }
234
235 #if 0
236
237 /*
238  * vop_noremove { struct namecache *a_ncp }     XXX STOPGAP FUNCTION
239  *
240  * Remove the file/dir represented by a_ncp.
241  *
242  * XXX ultra difficult.  A number of existing filesystems, including UFS,
243  *     assume that the directory will remain locked and the lookup will
244  *     store the directory offset and other things in the directory inode
245  *     for the later VOP_REMOVE to use.  We have to move all that
246  *     functionality into e.g. UFS's VOP_REMOVE itself.
247  */
248 static int
249 vop_nonremove(struct vop_nremove_args *ap)
250 {
251         struct namecache *ncfile;
252         struct namecache *ncdir;
253         struct componentname cnd;
254         struct vnode *vp;
255         struct vnode *vpd;
256         thread_t td;
257         int error;
258
259         td = curthread;
260         ncfile = ap->a_ncp;
261         ncdir = ncfile->nc_parent;
262
263         if ((error = cache_vget(ncdir, ap->a_cred, LK_EXCLUSIVE, &vpd)) != 0)
264                 return (error);
265         if ((error = cache_vget(ncfile, ap->a_cred, LK_EXCLUSIVE, &vp)) != 0) {
266                 vput(vpd);
267                 return (error);
268         }
269         bzero(&cnd, sizeof(cnd));
270         cnd.cn_nameiop = NAMEI_DELETE;
271         cnd.cn_td = td;
272         cnd.cn_cred = ap->a_cred;
273         cnd.cn_nameptr = ncfile->nc_name;
274         cnd.cn_namelen = ncfile->nc_nlen;
275         error = VOP_REMOVE(vpd, NCPNULL, vp, &cnd);
276         if (error == 0)
277                 cache_purge(vp);
278         vput(vp);
279         vput(vpd);
280
281         /*
282          * Re-resolve the ncp to match the fact that the file has been
283          * deleted from the namespace.  If an error occured leave the ncp
284          * unresolved (meaning that we have no idea what the correct state
285          * is).
286          */
287         if (error == 0) {
288                 cache_setunresolved(ncfile);
289                 cache_setvp(ncfile, NULL);
290         }
291         return (error);
292 }
293
294 #endif
295
296
297 static int
298 vop_nolookup(ap)
299         struct vop_lookup_args /* {
300                 struct vnode *a_dvp;
301                 struct vnode **a_vpp;
302                 struct componentname *a_cnp;
303         } */ *ap;
304 {
305
306         *ap->a_vpp = NULL;
307         return (ENOTDIR);
308 }
309
310 /*
311  *      vop_nostrategy:
312  *
313  *      Strategy routine for VFS devices that have none.
314  *
315  *      B_ERROR and B_INVAL must be cleared prior to calling any strategy
316  *      routine.  Typically this is done for a B_READ strategy call.  Typically
317  *      B_INVAL is assumed to already be clear prior to a write and should not
318  *      be cleared manually unless you just made the buffer invalid.  B_ERROR
319  *      should be cleared either way.
320  */
321
322 static int
323 vop_nostrategy (struct vop_strategy_args *ap)
324 {
325         printf("No strategy for buffer at %p\n", ap->a_bp);
326         vprint("", ap->a_vp);
327         vprint("", ap->a_bp->b_vp);
328         ap->a_bp->b_flags |= B_ERROR;
329         ap->a_bp->b_error = EOPNOTSUPP;
330         biodone(ap->a_bp);
331         return (EOPNOTSUPP);
332 }
333
334 int
335 vop_stdpathconf(ap)
336         struct vop_pathconf_args /* {
337         struct vnode *a_vp;
338         int a_name;
339         int *a_retval;
340         } */ *ap;
341 {
342
343         switch (ap->a_name) {
344                 case _PC_LINK_MAX:
345                         *ap->a_retval = LINK_MAX;
346                         return (0);
347                 case _PC_MAX_CANON:
348                         *ap->a_retval = MAX_CANON;
349                         return (0);
350                 case _PC_MAX_INPUT:
351                         *ap->a_retval = MAX_INPUT;
352                         return (0);
353                 case _PC_PIPE_BUF:
354                         *ap->a_retval = PIPE_BUF;
355                         return (0);
356                 case _PC_CHOWN_RESTRICTED:
357                         *ap->a_retval = 1;
358                         return (0);
359                 case _PC_VDISABLE:
360                         *ap->a_retval = _POSIX_VDISABLE;
361                         return (0);
362                 default:
363                         return (EINVAL);
364         }
365         /* NOTREACHED */
366 }
367
368 /*
369  * Standard lock.  The lock is recursive-capable only if the lock was
370  * initialized with LK_CANRECURSE or that flag is passed in a_flags.
371  */
372 int
373 vop_stdlock(ap)
374         struct vop_lock_args /* {
375                 struct vnode *a_vp;
376                 int a_flags;
377                 struct proc *a_p;
378         } */ *ap;
379 {               
380         int error;
381
382 #ifndef DEBUG_LOCKS
383         error = lockmgr(&ap->a_vp->v_lock, ap->a_flags, NULL, ap->a_td);
384 #else
385         error = debuglockmgr(&ap->a_vp->v_lock, ap->a_flags,
386                         NULL, ap->a_td,
387                         "vop_stdlock", ap->a_vp->filename, ap->a_vp->line);
388 #endif
389         return(error);
390 }
391
392 int
393 vop_stdunlock(ap)
394         struct vop_unlock_args /* {
395                 struct vnode *a_vp;
396                 int a_flags;
397                 struct thread *a_td;
398         } */ *ap;
399 {
400         int error;
401
402         error = lockmgr(&ap->a_vp->v_lock, ap->a_flags | LK_RELEASE,
403                         NULL, ap->a_td);
404         return(error);
405 }
406
407 int
408 vop_stdislocked(ap)
409         struct vop_islocked_args /* {
410                 struct vnode *a_vp;
411                 struct thread *a_td;
412         } */ *ap;
413 {
414         return (lockstatus(&ap->a_vp->v_lock, ap->a_td));
415 }
416
417 /*
418  * Return true for select/poll.
419  */
420 int
421 vop_nopoll(ap)
422         struct vop_poll_args /* {
423                 struct vnode *a_vp;
424                 int  a_events;
425                 struct ucred *a_cred;
426                 struct proc *a_p;
427         } */ *ap;
428 {
429         /*
430          * Return true for read/write.  If the user asked for something
431          * special, return POLLNVAL, so that clients have a way of
432          * determining reliably whether or not the extended
433          * functionality is present without hard-coding knowledge
434          * of specific filesystem implementations.
435          */
436         if (ap->a_events & ~POLLSTANDARD)
437                 return (POLLNVAL);
438
439         return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
440 }
441
442 /*
443  * Implement poll for local filesystems that support it.
444  */
445 int
446 vop_stdpoll(ap)
447         struct vop_poll_args /* {
448                 struct vnode *a_vp;
449                 int  a_events;
450                 struct ucred *a_cred;
451                 struct thread *a_td;
452         } */ *ap;
453 {
454         if (ap->a_events & ~POLLSTANDARD)
455                 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
456         return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
457 }
458
459 int
460 vop_stdbwrite(ap)
461         struct vop_bwrite_args *ap;
462 {
463         return (bwrite(ap->a_bp));
464 }
465
466 int
467 vop_stdcreatevobject(ap)
468         struct vop_createvobject_args /* {
469                 struct vnode *a_vp;
470                 struct proc *a_td;
471         } */ *ap;
472 {
473         struct vnode *vp = ap->a_vp;
474         struct thread *td = ap->a_td;
475         struct vattr vat;
476         vm_object_t object;
477         int error = 0;
478
479         if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
480                 return (0);
481
482 retry:
483         if ((object = vp->v_object) == NULL) {
484                 if (vp->v_type == VREG || vp->v_type == VDIR) {
485                         if ((error = VOP_GETATTR(vp, &vat, td)) != 0)
486                                 goto retn;
487                         object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
488                 } else if (vp->v_rdev && dev_is_good(vp->v_rdev)) {
489                         /*
490                          * XXX v_rdev uses NULL/non-NULL instead of NODEV
491                          *
492                          * This simply allocates the biggest object possible
493                          * for a disk vnode.  This should be fixed, but doesn't
494                          * cause any problems (yet).
495                          */
496                         object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
497                 } else {
498                         goto retn;
499                 }
500                 /*
501                  * Dereference the reference we just created.  This assumes
502                  * that the object is associated with the vp.
503                  */
504                 object->ref_count--;
505                 vp->v_usecount--;
506         } else {
507                 if (object->flags & OBJ_DEAD) {
508                         VOP_UNLOCK(vp, 0, td);
509                         tsleep(object, 0, "vodead", 0);
510                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
511                         goto retry;
512                 }
513         }
514
515         KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
516         vp->v_flag |= VOBJBUF;
517
518 retn:
519         return (error);
520 }
521
522 int
523 vop_stddestroyvobject(ap)
524         struct vop_destroyvobject_args /* {
525                 struct vnode *vp;
526         } */ *ap;
527 {
528         struct vnode *vp = ap->a_vp;
529         vm_object_t obj = vp->v_object;
530
531         if (vp->v_object == NULL)
532                 return (0);
533
534         if (obj->ref_count == 0) {
535                 /*
536                  * vclean() may be called twice. The first time
537                  * removes the primary reference to the object,
538                  * the second time goes one further and is a
539                  * special-case to terminate the object.
540                  *
541                  * don't double-terminate the object.
542                  */
543                 if ((obj->flags & OBJ_DEAD) == 0)
544                         vm_object_terminate(obj);
545         } else {
546                 /*
547                  * Woe to the process that tries to page now :-).
548                  */
549                 vm_pager_deallocate(obj);
550         }
551         return (0);
552 }
553
554 /*
555  * Return the underlying VM object.  This routine may be called with or
556  * without the vnode interlock held.  If called without, the returned
557  * object is not guarenteed to be valid.  The syncer typically gets the
558  * object without holding the interlock in order to quickly test whether
559  * it might be dirty before going heavy-weight.  vm_object's use zalloc
560  * and thus stable-storage, so this is safe.
561  */
562 int
563 vop_stdgetvobject(ap)
564         struct vop_getvobject_args /* {
565                 struct vnode *vp;
566                 struct vm_object **objpp;
567         } */ *ap;
568 {
569         struct vnode *vp = ap->a_vp;
570         struct vm_object **objpp = ap->a_objpp;
571
572         if (objpp)
573                 *objpp = vp->v_object;
574         return (vp->v_object ? 0 : EINVAL);
575 }
576
577 /* 
578  * vfs default ops
579  * used to fill the vfs fucntion table to get reasonable default return values.
580  */
581 int 
582 vfs_stdmount(struct mount *mp, char *path, caddr_t data,
583         struct nameidata *ndp, struct thread *td)
584 {
585         return (0);
586 }
587
588 int     
589 vfs_stdunmount(struct mount *mp, int mntflags, struct thread *td)
590 {
591         return (0);
592 }
593
594 int     
595 vfs_stdroot(struct mount *mp, struct vnode **vpp)
596 {
597         return (EOPNOTSUPP);
598 }
599
600 int     
601 vfs_stdstatfs(struct mount *mp, struct statfs *sbp, struct thread *td)
602 {
603         return (EOPNOTSUPP);
604 }
605
606 int
607 vfs_stdvptofh(struct vnode *vp, struct fid *fhp)
608 {
609         return (EOPNOTSUPP);
610 }
611
612 int     
613 vfs_stdstart(struct mount *mp, int flags, struct thread *td)
614 {
615         return (0);
616 }
617
618 int     
619 vfs_stdquotactl(struct mount *mp, int cmds, uid_t uid,
620         caddr_t arg, struct thread *td)
621 {
622         return (EOPNOTSUPP);
623 }
624
625 int     
626 vfs_stdsync(struct mount *mp, int waitfor, struct thread *td)
627 {
628         return (0);
629 }
630
631 int     
632 vfs_stdvget(struct mount *mp, ino_t ino, struct vnode **vpp)
633 {
634         return (EOPNOTSUPP);
635 }
636
637 int     
638 vfs_stdfhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
639 {
640         return (EOPNOTSUPP);
641 }
642
643 int 
644 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
645         struct ucred **credanonp)
646 {
647         return (EOPNOTSUPP);
648 }
649
650 int
651 vfs_stdinit(struct vfsconf *vfsp)
652 {
653         return (0);
654 }
655
656 int
657 vfs_stduninit(struct vfsconf *vfsp)
658 {
659         return(0);
660 }
661
662 int
663 vfs_stdextattrctl(struct mount *mp, int cmd, const char *attrname,
664         caddr_t arg, struct thread *td)
665 {
666         return(EOPNOTSUPP);
667 }
668
669 /* end of vfs default ops */