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