vfs_default.c revision 75858
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 75858 2001-04-23 09:05:15Z 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 <net/radix.h>
51#include <sys/socket.h>
52#include <sys/mount.h>
53#include <sys/mutex.h>
54#include <sys/unistd.h>
55#include <sys/vnode.h>
56#include <sys/poll.h>
57
58#include <machine/limits.h>
59
60#include <vm/vm.h>
61#include <vm/vm_object.h>
62#include <vm/vm_extern.h>
63#include <vm/pmap.h>
64#include <vm/vm_map.h>
65#include <vm/vm_page.h>
66#include <vm/vm_pager.h>
67#include <vm/vnode_pager.h>
68#include <vm/vm_zone.h>
69
70static int	vop_nolookup __P((struct vop_lookup_args *));
71static int	vop_nostrategy __P((struct vop_strategy_args *));
72
73/*
74 * This vnode table stores what we want to do if the filesystem doesn't
75 * implement a particular VOP.
76 *
77 * If there is no specific entry here, we will return EOPNOTSUPP.
78 *
79 */
80
81vop_t **default_vnodeop_p;
82static struct vnodeopv_entry_desc default_vnodeop_entries[] = {
83	{ &vop_default_desc,		(vop_t *) vop_eopnotsupp },
84	{ &vop_advlock_desc,		(vop_t *) vop_einval },
85	{ &vop_close_desc,		(vop_t *) vop_null },
86	{ &vop_createvobject_desc,	(vop_t *) vop_stdcreatevobject },
87	{ &vop_destroyvobject_desc,	(vop_t *) vop_stddestroyvobject },
88	{ &vop_fsync_desc,		(vop_t *) vop_null },
89	{ &vop_getvobject_desc,		(vop_t *) vop_stdgetvobject },
90	{ &vop_inactive_desc,		(vop_t *) vop_stdinactive },
91	{ &vop_ioctl_desc,		(vop_t *) vop_enotty },
92	{ &vop_islocked_desc,		(vop_t *) vop_noislocked },
93	{ &vop_lease_desc,		(vop_t *) vop_null },
94	{ &vop_lock_desc,		(vop_t *) vop_nolock },
95	{ &vop_lookup_desc,		(vop_t *) vop_nolookup },
96	{ &vop_open_desc,		(vop_t *) vop_null },
97	{ &vop_pathconf_desc,		(vop_t *) vop_einval },
98	{ &vop_poll_desc,		(vop_t *) vop_nopoll },
99	{ &vop_readlink_desc,		(vop_t *) vop_einval },
100	{ &vop_revoke_desc,		(vop_t *) vop_revoke },
101	{ &vop_strategy_desc,		(vop_t *) vop_nostrategy },
102	{ &vop_unlock_desc,		(vop_t *) vop_nounlock },
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 *	BIO_ERROR and B_INVAL must be cleared prior to calling any strategy
182 *	routine.  Typically this is done for a BIO_READ strategy call.
183 *	Typically B_INVAL is assumed to already be clear prior to a write
184 *	and should not be cleared manually unless you just made the buffer
185 *	invalid.  BIO_ERROR 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_ioflags |= BIO_ERROR;
195	ap->a_bp->b_error = EOPNOTSUPP;
196	bufdone(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 vnode *vp = ap->a_vp;
249
250#ifndef	DEBUG_LOCKS
251	return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock, ap->a_p));
252#else
253	return (debuglockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock,
254	    ap->a_p, "vop_stdlock", vp->filename, vp->line));
255#endif
256}
257
258int
259vop_stdunlock(ap)
260	struct vop_unlock_args /* {
261		struct vnode *a_vp;
262		int a_flags;
263		struct proc *a_p;
264	} */ *ap;
265{
266	struct vnode *vp = ap->a_vp;
267
268	return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE, &vp->v_interlock,
269	    ap->a_p));
270}
271
272int
273vop_stdislocked(ap)
274	struct vop_islocked_args /* {
275		struct vnode *a_vp;
276		struct proc *a_p;
277	} */ *ap;
278{
279
280	return (lockstatus(&ap->a_vp->v_lock, ap->a_p));
281}
282
283int
284vop_stdinactive(ap)
285	struct vop_inactive_args /* {
286		struct vnode *a_vp;
287		struct proc *a_p;
288	} */ *ap;
289{
290
291	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
292	return (0);
293}
294
295/*
296 * Return true for select/poll.
297 */
298int
299vop_nopoll(ap)
300	struct vop_poll_args /* {
301		struct vnode *a_vp;
302		int  a_events;
303		struct ucred *a_cred;
304		struct proc *a_p;
305	} */ *ap;
306{
307	/*
308	 * Return true for read/write.  If the user asked for something
309	 * special, return POLLNVAL, so that clients have a way of
310	 * determining reliably whether or not the extended
311	 * functionality is present without hard-coding knowledge
312	 * of specific filesystem implementations.
313	 */
314	if (ap->a_events & ~POLLSTANDARD)
315		return (POLLNVAL);
316
317	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
318}
319
320/*
321 * Implement poll for local filesystems that support it.
322 */
323int
324vop_stdpoll(ap)
325	struct vop_poll_args /* {
326		struct vnode *a_vp;
327		int  a_events;
328		struct ucred *a_cred;
329		struct proc *a_p;
330	} */ *ap;
331{
332	if ((ap->a_events & ~POLLSTANDARD) == 0)
333		return (ap->a_events & (POLLRDNORM|POLLWRNORM));
334	return (vn_pollrecord(ap->a_vp, ap->a_p, ap->a_events));
335}
336
337/*
338 * Stubs to use when there is no locking to be done on the underlying object.
339 * A minimal shared lock is necessary to ensure that the underlying object
340 * is not revoked while an operation is in progress. So, an active shared
341 * count is maintained in an auxillary vnode lock structure.
342 */
343int
344vop_sharedlock(ap)
345	struct vop_lock_args /* {
346		struct vnode *a_vp;
347		int a_flags;
348		struct proc *a_p;
349	} */ *ap;
350{
351	/*
352	 * This code cannot be used until all the non-locking filesystems
353	 * (notably NFS) are converted to properly lock and release nodes.
354	 * Also, certain vnode operations change the locking state within
355	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
356	 * and symlink). Ideally these operations should not change the
357	 * lock state, but should be changed to let the caller of the
358	 * function unlock them. Otherwise all intermediate vnode layers
359	 * (such as union, umapfs, etc) must catch these functions to do
360	 * the necessary locking at their layer. Note that the inactive
361	 * and lookup operations also change their lock state, but this
362	 * cannot be avoided, so these two operations will always need
363	 * to be handled in intermediate layers.
364	 */
365	struct vnode *vp = ap->a_vp;
366	int vnflags, flags = ap->a_flags;
367
368	switch (flags & LK_TYPE_MASK) {
369	case LK_DRAIN:
370		vnflags = LK_DRAIN;
371		break;
372	case LK_EXCLUSIVE:
373#ifdef DEBUG_VFS_LOCKS
374		/*
375		 * Normally, we use shared locks here, but that confuses
376		 * the locking assertions.
377		 */
378		vnflags = LK_EXCLUSIVE;
379		break;
380#endif
381	case LK_SHARED:
382		vnflags = LK_SHARED;
383		break;
384	case LK_UPGRADE:
385	case LK_EXCLUPGRADE:
386	case LK_DOWNGRADE:
387		return (0);
388	case LK_RELEASE:
389	default:
390		panic("vop_sharedlock: bad operation %d", flags & LK_TYPE_MASK);
391	}
392	if (flags & LK_INTERLOCK)
393		vnflags |= LK_INTERLOCK;
394#ifndef	DEBUG_LOCKS
395	return (lockmgr(&vp->v_lock, vnflags, &vp->v_interlock, ap->a_p));
396#else
397	return (debuglockmgr(&vp->v_lock, vnflags, &vp->v_interlock, ap->a_p,
398	    "vop_sharedlock", vp->filename, vp->line));
399#endif
400}
401
402/*
403 * Stubs to use when there is no locking to be done on the underlying object.
404 * A minimal shared lock is necessary to ensure that the underlying object
405 * is not revoked while an operation is in progress. So, an active shared
406 * count is maintained in an auxillary vnode lock structure.
407 */
408int
409vop_nolock(ap)
410	struct vop_lock_args /* {
411		struct vnode *a_vp;
412		int a_flags;
413		struct proc *a_p;
414	} */ *ap;
415{
416#ifdef notyet
417	/*
418	 * This code cannot be used until all the non-locking filesystems
419	 * (notably NFS) are converted to properly lock and release nodes.
420	 * Also, certain vnode operations change the locking state within
421	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
422	 * and symlink). Ideally these operations should not change the
423	 * lock state, but should be changed to let the caller of the
424	 * function unlock them. Otherwise all intermediate vnode layers
425	 * (such as union, umapfs, etc) must catch these functions to do
426	 * the necessary locking at their layer. Note that the inactive
427	 * and lookup operations also change their lock state, but this
428	 * cannot be avoided, so these two operations will always need
429	 * to be handled in intermediate layers.
430	 */
431	struct vnode *vp = ap->a_vp;
432	int vnflags, flags = ap->a_flags;
433
434	switch (flags & LK_TYPE_MASK) {
435	case LK_DRAIN:
436		vnflags = LK_DRAIN;
437		break;
438	case LK_EXCLUSIVE:
439	case LK_SHARED:
440		vnflags = LK_SHARED;
441		break;
442	case LK_UPGRADE:
443	case LK_EXCLUPGRADE:
444	case LK_DOWNGRADE:
445		return (0);
446	case LK_RELEASE:
447	default:
448		panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
449	}
450	if (flags & LK_INTERLOCK)
451		vnflags |= LK_INTERLOCK;
452	return(lockmgr(&vp->v_lock, vnflags, &vp->v_interlock, ap->a_p));
453#else /* for now */
454	/*
455	 * Since we are not using the lock manager, we must clear
456	 * the interlock here.
457	 */
458	if (ap->a_flags & LK_INTERLOCK)
459		mtx_unlock(&ap->a_vp->v_interlock);
460	return (0);
461#endif
462}
463
464/*
465 * Do the inverse of vop_nolock, handling the interlock in a compatible way.
466 */
467int
468vop_nounlock(ap)
469	struct vop_unlock_args /* {
470		struct vnode *a_vp;
471		int a_flags;
472		struct proc *a_p;
473	} */ *ap;
474{
475
476	/*
477	 * Since we are not using the lock manager, we must clear
478	 * the interlock here.
479	 */
480	if (ap->a_flags & LK_INTERLOCK)
481		mtx_unlock(&ap->a_vp->v_interlock);
482	return (0);
483}
484
485/*
486 * Return whether or not the node is in use.
487 */
488int
489vop_noislocked(ap)
490	struct vop_islocked_args /* {
491		struct vnode *a_vp;
492		struct proc *a_p;
493	} */ *ap;
494{
495
496	return (0);
497}
498
499/*
500 * Return our mount point, as we will take charge of the writes.
501 */
502int
503vop_stdgetwritemount(ap)
504	struct vop_getwritemount_args /* {
505		struct vnode *a_vp;
506		struct mount **a_mpp;
507	} */ *ap;
508{
509
510	*(ap->a_mpp) = ap->a_vp->v_mount;
511	return (0);
512}
513
514int
515vop_stdcreatevobject(ap)
516	struct vop_createvobject_args /* {
517		struct vnode *vp;
518		struct ucred *cred;
519		struct proc *p;
520	} */ *ap;
521{
522	struct vnode *vp = ap->a_vp;
523	struct ucred *cred = ap->a_cred;
524	struct proc *p = ap->a_p;
525	struct vattr vat;
526	vm_object_t object;
527	int error = 0;
528
529	if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
530		return (0);
531
532retry:
533	if ((object = vp->v_object) == NULL) {
534		if (vp->v_type == VREG || vp->v_type == VDIR) {
535			if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
536				goto retn;
537			object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
538		} else if (devsw(vp->v_rdev) != NULL) {
539			/*
540			 * This simply allocates the biggest object possible
541			 * for a disk vnode.  This should be fixed, but doesn't
542			 * cause any problems (yet).
543			 */
544			object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
545		} else {
546			goto retn;
547		}
548		/*
549		 * Dereference the reference we just created.  This assumes
550		 * that the object is associated with the vp.
551		 */
552		object->ref_count--;
553		vp->v_usecount--;
554	} else {
555		if (object->flags & OBJ_DEAD) {
556			VOP_UNLOCK(vp, 0, p);
557			tsleep(object, PVM, "vodead", 0);
558			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
559			goto retry;
560		}
561	}
562
563	KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
564	vp->v_flag |= VOBJBUF;
565
566retn:
567	return (error);
568}
569
570int
571vop_stddestroyvobject(ap)
572	struct vop_destroyvobject_args /* {
573		struct vnode *vp;
574	} */ *ap;
575{
576	struct vnode *vp = ap->a_vp;
577	vm_object_t obj = vp->v_object;
578
579	if (vp->v_object == NULL)
580		return (0);
581
582	if (obj->ref_count == 0) {
583		/*
584		 * vclean() may be called twice. The first time
585		 * removes the primary reference to the object,
586		 * the second time goes one further and is a
587		 * special-case to terminate the object.
588		 */
589		vm_object_terminate(obj);
590	} else {
591		/*
592		 * Woe to the process that tries to page now :-).
593		 */
594		vm_pager_deallocate(obj);
595	}
596	return (0);
597}
598
599int
600vop_stdgetvobject(ap)
601	struct vop_getvobject_args /* {
602		struct vnode *vp;
603		struct vm_object **objpp;
604	} */ *ap;
605{
606	struct vnode *vp = ap->a_vp;
607	struct vm_object **objpp = ap->a_objpp;
608
609	if (objpp)
610		*objpp = vp->v_object;
611	return (vp->v_object ? 0 : EINVAL);
612}
613
614/*
615 * vfs default ops
616 * used to fill the vfs fucntion table to get reasonable default return values.
617 */
618int
619vfs_stdmount (mp, path, data, ndp, p)
620	struct mount *mp;
621	char *path;
622	caddr_t data;
623	struct nameidata *ndp;
624	struct proc *p;
625{
626	return (0);
627}
628
629int
630vfs_stdunmount (mp, mntflags, p)
631	struct mount *mp;
632	int mntflags;
633	struct proc *p;
634{
635	return (0);
636}
637
638int
639vfs_stdroot (mp, vpp)
640	struct mount *mp;
641	struct vnode **vpp;
642{
643	return (EOPNOTSUPP);
644}
645
646int
647vfs_stdstatfs (mp, sbp, p)
648	struct mount *mp;
649	struct statfs *sbp;
650	struct proc *p;
651{
652	return (EOPNOTSUPP);
653}
654
655int
656vfs_stdvptofh (vp, fhp)
657	struct vnode *vp;
658	struct fid *fhp;
659{
660	return (EOPNOTSUPP);
661}
662
663int
664vfs_stdstart (mp, flags, p)
665	struct mount *mp;
666	int flags;
667	struct proc *p;
668{
669	return (0);
670}
671
672int
673vfs_stdquotactl (mp, cmds, uid, arg, p)
674	struct mount *mp;
675	int cmds;
676	uid_t uid;
677	caddr_t arg;
678	struct proc *p;
679{
680	return (EOPNOTSUPP);
681}
682
683int
684vfs_stdsync (mp, waitfor, cred, p)
685	struct mount *mp;
686	int waitfor;
687	struct ucred *cred;
688	struct proc *p;
689{
690	return (0);
691}
692
693int
694vfs_stdvget (mp, ino, vpp)
695	struct mount *mp;
696	ino_t ino;
697	struct vnode **vpp;
698{
699	return (EOPNOTSUPP);
700}
701
702int
703vfs_stdfhtovp (mp, fhp, vpp)
704	struct mount *mp;
705	struct fid *fhp;
706	struct vnode **vpp;
707{
708	return (EOPNOTSUPP);
709}
710
711int
712vfs_stdcheckexp (mp, nam, extflagsp, credanonp)
713	struct mount *mp;
714	struct sockaddr *nam;
715	int *extflagsp;
716	struct ucred **credanonp;
717{
718	return (EOPNOTSUPP);
719}
720
721int
722vfs_stdinit (vfsp)
723	struct vfsconf *vfsp;
724{
725	return (0);
726}
727
728int
729vfs_stduninit (vfsp)
730	struct vfsconf *vfsp;
731{
732	return(0);
733}
734
735int
736vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname, p)
737	struct mount *mp;
738	int cmd;
739	struct vnode *filename_vp;
740	int attrnamespace;
741	const char *attrname;
742	struct proc *p;
743{
744	return(EOPNOTSUPP);
745}
746
747/* end of vfs default ops */
748