proc->thread stage 5: BUF/VFS clearance! Remove the ucred argument from
[dragonfly.git] / sys / kern / vfs_default.c
... / ...
CommitLineData
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.4 2003/06/26 05:55:14 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
63static int vop_nolookup __P((struct vop_lookup_args *));
64static int vop_nostrategy __P((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
74vop_t **default_vnodeop_p;
75static struct vnodeopv_entry_desc default_vnodeop_entries[] = {
76 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
77 { &vop_advlock_desc, (vop_t *) vop_einval },
78 { &vop_bwrite_desc, (vop_t *) vop_stdbwrite },
79 { &vop_close_desc, (vop_t *) vop_null },
80 { &vop_createvobject_desc, (vop_t *) vop_stdcreatevobject },
81 { &vop_destroyvobject_desc, (vop_t *) vop_stddestroyvobject },
82 { &vop_fsync_desc, (vop_t *) vop_null },
83 { &vop_getvobject_desc, (vop_t *) vop_stdgetvobject },
84 { &vop_ioctl_desc, (vop_t *) vop_enotty },
85 { &vop_islocked_desc, (vop_t *) vop_noislocked },
86 { &vop_lease_desc, (vop_t *) vop_null },
87 { &vop_lock_desc, (vop_t *) vop_nolock },
88 { &vop_mmap_desc, (vop_t *) vop_einval },
89 { &vop_lookup_desc, (vop_t *) vop_nolookup },
90 { &vop_open_desc, (vop_t *) vop_null },
91 { &vop_pathconf_desc, (vop_t *) vop_einval },
92 { &vop_poll_desc, (vop_t *) vop_nopoll },
93 { &vop_readlink_desc, (vop_t *) vop_einval },
94 { &vop_reallocblks_desc, (vop_t *) vop_eopnotsupp },
95 { &vop_revoke_desc, (vop_t *) vop_revoke },
96 { &vop_strategy_desc, (vop_t *) vop_nostrategy },
97 { &vop_unlock_desc, (vop_t *) vop_nounlock },
98 { &vop_getacl_desc, (vop_t *) vop_eopnotsupp },
99 { &vop_setacl_desc, (vop_t *) vop_eopnotsupp },
100 { &vop_aclcheck_desc, (vop_t *) vop_eopnotsupp },
101 { &vop_getextattr_desc, (vop_t *) vop_eopnotsupp },
102 { &vop_setextattr_desc, (vop_t *) vop_eopnotsupp },
103 { NULL, NULL }
104};
105
106static struct vnodeopv_desc default_vnodeop_opv_desc =
107 { &default_vnodeop_p, default_vnodeop_entries };
108
109VNODEOP_SET(default_vnodeop_opv_desc);
110
111int
112vop_eopnotsupp(struct vop_generic_args *ap)
113{
114 /*
115 printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
116 */
117
118 return (EOPNOTSUPP);
119}
120
121int
122vop_ebadf(struct vop_generic_args *ap)
123{
124
125 return (EBADF);
126}
127
128int
129vop_enotty(struct vop_generic_args *ap)
130{
131
132 return (ENOTTY);
133}
134
135int
136vop_einval(struct vop_generic_args *ap)
137{
138
139 return (EINVAL);
140}
141
142int
143vop_null(struct vop_generic_args *ap)
144{
145
146 return (0);
147}
148
149int
150vop_defaultop(struct vop_generic_args *ap)
151{
152
153 return (VOCALL(default_vnodeop_p, ap->a_desc->vdesc_offset, ap));
154}
155
156int
157vop_panic(struct vop_generic_args *ap)
158{
159
160 panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
161}
162
163static int
164vop_nolookup(ap)
165 struct vop_lookup_args /* {
166 struct vnode *a_dvp;
167 struct vnode **a_vpp;
168 struct componentname *a_cnp;
169 } */ *ap;
170{
171
172 *ap->a_vpp = NULL;
173 return (ENOTDIR);
174}
175
176/*
177 * vop_nostrategy:
178 *
179 * Strategy routine for VFS devices that have none.
180 *
181 * B_ERROR and B_INVAL must be cleared prior to calling any strategy
182 * routine. Typically this is done for a B_READ strategy call. Typically
183 * B_INVAL is assumed to already be clear prior to a write and should not
184 * be cleared manually unless you just made the buffer invalid. B_ERROR
185 * should be cleared either way.
186 */
187
188static int
189vop_nostrategy (struct vop_strategy_args *ap)
190{
191 printf("No strategy for buffer at %p\n", ap->a_bp);
192 vprint("", ap->a_vp);
193 vprint("", ap->a_bp->b_vp);
194 ap->a_bp->b_flags |= B_ERROR;
195 ap->a_bp->b_error = EOPNOTSUPP;
196 biodone(ap->a_bp);
197 return (EOPNOTSUPP);
198}
199
200int
201vop_stdpathconf(ap)
202 struct vop_pathconf_args /* {
203 struct vnode *a_vp;
204 int a_name;
205 int *a_retval;
206 } */ *ap;
207{
208
209 switch (ap->a_name) {
210 case _PC_LINK_MAX:
211 *ap->a_retval = LINK_MAX;
212 return (0);
213 case _PC_MAX_CANON:
214 *ap->a_retval = MAX_CANON;
215 return (0);
216 case _PC_MAX_INPUT:
217 *ap->a_retval = MAX_INPUT;
218 return (0);
219 case _PC_PIPE_BUF:
220 *ap->a_retval = PIPE_BUF;
221 return (0);
222 case _PC_CHOWN_RESTRICTED:
223 *ap->a_retval = 1;
224 return (0);
225 case _PC_VDISABLE:
226 *ap->a_retval = _POSIX_VDISABLE;
227 return (0);
228 default:
229 return (EINVAL);
230 }
231 /* NOTREACHED */
232}
233
234/*
235 * Standard lock, unlock and islocked functions.
236 *
237 * These depend on the lock structure being the first element in the
238 * inode, ie: vp->v_data points to the the lock!
239 */
240int
241vop_stdlock(ap)
242 struct vop_lock_args /* {
243 struct vnode *a_vp;
244 int a_flags;
245 struct proc *a_p;
246 } */ *ap;
247{
248 struct lock *l;
249
250 if ((l = (struct lock *)ap->a_vp->v_data) == NULL) {
251 if (ap->a_flags & LK_INTERLOCK)
252 simple_unlock(&ap->a_vp->v_interlock);
253 return 0;
254 }
255
256#ifndef DEBUG_LOCKS
257 return (lockmgr(l, ap->a_flags, &ap->a_vp->v_interlock, ap->a_td));
258#else
259 return (debuglockmgr(l, ap->a_flags, &ap->a_vp->v_interlock, ap->a_td,
260 "vop_stdlock", ap->a_vp->filename, ap->a_vp->line));
261#endif
262}
263
264int
265vop_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 lock *l;
273
274 if ((l = (struct lock *)ap->a_vp->v_data) == NULL) {
275 if (ap->a_flags & LK_INTERLOCK)
276 simple_unlock(&ap->a_vp->v_interlock);
277 return 0;
278 }
279
280 return (lockmgr(l, ap->a_flags | LK_RELEASE,
281 &ap->a_vp->v_interlock, ap->a_td));
282}
283
284int
285vop_stdislocked(ap)
286 struct vop_islocked_args /* {
287 struct vnode *a_vp;
288 struct thread *a_td;
289 } */ *ap;
290{
291 struct lock *l;
292
293 if ((l = (struct lock *)ap->a_vp->v_data) == NULL)
294 return 0;
295
296 return (lockstatus(l, ap->a_td));
297}
298
299/*
300 * Return true for select/poll.
301 */
302int
303vop_nopoll(ap)
304 struct vop_poll_args /* {
305 struct vnode *a_vp;
306 int a_events;
307 struct ucred *a_cred;
308 struct proc *a_p;
309 } */ *ap;
310{
311 /*
312 * Return true for read/write. If the user asked for something
313 * special, return POLLNVAL, so that clients have a way of
314 * determining reliably whether or not the extended
315 * functionality is present without hard-coding knowledge
316 * of specific filesystem implementations.
317 */
318 if (ap->a_events & ~POLLSTANDARD)
319 return (POLLNVAL);
320
321 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
322}
323
324/*
325 * Implement poll for local filesystems that support it.
326 */
327int
328vop_stdpoll(ap)
329 struct vop_poll_args /* {
330 struct vnode *a_vp;
331 int a_events;
332 struct ucred *a_cred;
333 struct thread *a_td;
334 } */ *ap;
335{
336 if (ap->a_events & ~POLLSTANDARD)
337 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
338 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
339}
340
341int
342vop_stdbwrite(ap)
343 struct vop_bwrite_args *ap;
344{
345 return (bwrite(ap->a_bp));
346}
347
348/*
349 * Stubs to use when there is no locking to be done on the underlying object.
350 * A minimal shared lock is necessary to ensure that the underlying object
351 * is not revoked while an operation is in progress. So, an active shared
352 * count is maintained in an auxillary vnode lock structure.
353 */
354int
355vop_sharedlock(ap)
356 struct vop_lock_args /* {
357 struct vnode *a_vp;
358 int a_flags;
359 struct proc *a_p;
360 } */ *ap;
361{
362 /*
363 * This code cannot be used until all the non-locking filesystems
364 * (notably NFS) are converted to properly lock and release nodes.
365 * Also, certain vnode operations change the locking state within
366 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
367 * and symlink). Ideally these operations should not change the
368 * lock state, but should be changed to let the caller of the
369 * function unlock them. Otherwise all intermediate vnode layers
370 * (such as union, umapfs, etc) must catch these functions to do
371 * the necessary locking at their layer. Note that the inactive
372 * and lookup operations also change their lock state, but this
373 * cannot be avoided, so these two operations will always need
374 * to be handled in intermediate layers.
375 */
376 struct vnode *vp = ap->a_vp;
377 struct lock *l = (struct lock *)vp->v_data;
378 int vnflags, flags = ap->a_flags;
379
380 if (l == NULL) {
381 if (ap->a_flags & LK_INTERLOCK)
382 simple_unlock(&ap->a_vp->v_interlock);
383 return 0;
384 }
385 switch (flags & LK_TYPE_MASK) {
386 case LK_DRAIN:
387 vnflags = LK_DRAIN;
388 break;
389 case LK_EXCLUSIVE:
390#ifdef DEBUG_VFS_LOCKS
391 /*
392 * Normally, we use shared locks here, but that confuses
393 * the locking assertions.
394 */
395 vnflags = LK_EXCLUSIVE;
396 break;
397#endif
398 case LK_SHARED:
399 vnflags = LK_SHARED;
400 break;
401 case LK_UPGRADE:
402 case LK_EXCLUPGRADE:
403 case LK_DOWNGRADE:
404 return (0);
405 case LK_RELEASE:
406 default:
407 panic("vop_sharedlock: bad operation %d", flags & LK_TYPE_MASK);
408 }
409 if (flags & LK_INTERLOCK)
410 vnflags |= LK_INTERLOCK;
411#ifndef DEBUG_LOCKS
412 return (lockmgr(l, vnflags, &vp->v_interlock, ap->a_td));
413#else
414 return (debuglockmgr(l, vnflags, &vp->v_interlock, ap->a_td,
415 "vop_sharedlock", vp->filename, vp->line));
416#endif
417}
418
419/*
420 * Stubs to use when there is no locking to be done on the underlying object.
421 * A minimal shared lock is necessary to ensure that the underlying object
422 * is not revoked while an operation is in progress. So, an active shared
423 * count is maintained in an auxillary vnode lock structure.
424 */
425int
426vop_nolock(ap)
427 struct vop_lock_args /* {
428 struct vnode *a_vp;
429 int a_flags;
430 struct proc *a_p;
431 } */ *ap;
432{
433#ifdef notyet
434 /*
435 * This code cannot be used until all the non-locking filesystems
436 * (notably NFS) are converted to properly lock and release nodes.
437 * Also, certain vnode operations change the locking state within
438 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
439 * and symlink). Ideally these operations should not change the
440 * lock state, but should be changed to let the caller of the
441 * function unlock them. Otherwise all intermediate vnode layers
442 * (such as union, umapfs, etc) must catch these functions to do
443 * the necessary locking at their layer. Note that the inactive
444 * and lookup operations also change their lock state, but this
445 * cannot be avoided, so these two operations will always need
446 * to be handled in intermediate layers.
447 */
448 struct vnode *vp = ap->a_vp;
449 int vnflags, flags = ap->a_flags;
450
451 switch (flags & LK_TYPE_MASK) {
452 case LK_DRAIN:
453 vnflags = LK_DRAIN;
454 break;
455 case LK_EXCLUSIVE:
456 case LK_SHARED:
457 vnflags = LK_SHARED;
458 break;
459 case LK_UPGRADE:
460 case LK_EXCLUPGRADE:
461 case LK_DOWNGRADE:
462 return (0);
463 case LK_RELEASE:
464 default:
465 panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
466 }
467 if (flags & LK_INTERLOCK)
468 vnflags |= LK_INTERLOCK;
469 return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock, ap->a_p));
470#else /* for now */
471 /*
472 * Since we are not using the lock manager, we must clear
473 * the interlock here.
474 */
475 if (ap->a_flags & LK_INTERLOCK)
476 simple_unlock(&ap->a_vp->v_interlock);
477 return (0);
478#endif
479}
480
481/*
482 * Do the inverse of vop_nolock, handling the interlock in a compatible way.
483 */
484int
485vop_nounlock(ap)
486 struct vop_unlock_args /* {
487 struct vnode *a_vp;
488 int a_flags;
489 struct proc *a_p;
490 } */ *ap;
491{
492 if (ap->a_flags & LK_INTERLOCK)
493 simple_unlock(&ap->a_vp->v_interlock);
494 return (0);
495}
496
497/*
498 * Return whether or not the node is in use.
499 */
500int
501vop_noislocked(ap)
502 struct vop_islocked_args /* {
503 struct vnode *a_vp;
504 struct proc *a_p;
505 } */ *ap;
506{
507 return (0);
508}
509
510int
511vop_stdcreatevobject(ap)
512 struct vop_createvobject_args /* {
513 struct vnode *a_vp;
514 struct proc *a_td;
515 } */ *ap;
516{
517 struct vnode *vp = ap->a_vp;
518 struct thread *td = ap->a_td;
519 struct vattr vat;
520 vm_object_t object;
521 int error = 0;
522
523 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
524 return (0);
525
526retry:
527 if ((object = vp->v_object) == NULL) {
528 if (vp->v_type == VREG || vp->v_type == VDIR) {
529 if ((error = VOP_GETATTR(vp, &vat, td)) != 0)
530 goto retn;
531 object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
532 } else if (devsw(vp->v_rdev) != NULL) {
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, 0, td);
551 tsleep(object, PVM, "vodead", 0);
552 vn_lock(vp, 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
560retn:
561 return (error);
562}
563
564int
565vop_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 */
604int
605vop_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 */
623int
624vfs_stdmount(struct mount *mp, char *path, caddr_t data,
625 struct nameidata *ndp, struct thread *td)
626{
627 return (0);
628}
629
630int
631vfs_stdunmount(struct mount *mp, int mntflags, struct thread *td)
632{
633 return (0);
634}
635
636int
637vfs_stdroot(struct mount *mp, struct vnode **vpp)
638{
639 return (EOPNOTSUPP);
640}
641
642int
643vfs_stdstatfs(struct mount *mp, struct statfs *sbp, struct thread *td)
644{
645 return (EOPNOTSUPP);
646}
647
648int
649vfs_stdvptofh(struct vnode *vp, struct fid *fhp)
650{
651 return (EOPNOTSUPP);
652}
653
654int
655vfs_stdstart(struct mount *mp, int flags, struct thread *td)
656{
657 return (0);
658}
659
660int
661vfs_stdquotactl(struct mount *mp, int cmds, uid_t uid,
662 caddr_t arg, struct thread *td)
663{
664 return (EOPNOTSUPP);
665}
666
667int
668vfs_stdsync(struct mount *mp, int waitfor, struct thread *td)
669{
670 return (0);
671}
672
673int
674vfs_stdvget(struct mount *mp, ino_t ino, struct vnode **vpp)
675{
676 return (EOPNOTSUPP);
677}
678
679int
680vfs_stdfhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
681{
682 return (EOPNOTSUPP);
683}
684
685int
686vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
687 struct ucred **credanonp)
688{
689 return (EOPNOTSUPP);
690}
691
692int
693vfs_stdinit(struct vfsconf *vfsp)
694{
695 return (0);
696}
697
698int
699vfs_stduninit(struct vfsconf *vfsp)
700{
701 return(0);
702}
703
704int
705vfs_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 */