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