vfs_default.c revision 31561
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
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/lock.h>
44#include <sys/malloc.h>
45#include <sys/unistd.h>
46#include <sys/vnode.h>
47#include <sys/poll.h>
48
49static int vop_nostrategy __P((struct vop_strategy_args *));
50
51/*
52 * This vnode table stores what we want to do if the filesystem doesn't
53 * implement a particular VOP.
54 *
55 * If there is no specific entry here, we will return EOPNOTSUPP.
56 *
57 */
58
59vop_t **default_vnodeop_p;
60static struct vnodeopv_entry_desc default_vnodeop_entries[] = {
61	{ &vop_default_desc,		(vop_t *) vop_eopnotsupp },
62	{ &vop_abortop_desc,		(vop_t *) vop_null },
63	{ &vop_advlock_desc,		(vop_t *) vop_einval },
64	{ &vop_bwrite_desc,		(vop_t *) vop_stdbwrite },
65	{ &vop_close_desc,		(vop_t *) vop_null },
66	{ &vop_fsync_desc,		(vop_t *) vop_null },
67	{ &vop_ioctl_desc,		(vop_t *) vop_enotty },
68	{ &vop_islocked_desc,		(vop_t *) vop_noislocked },
69	{ &vop_lease_desc,		(vop_t *) vop_null },
70	{ &vop_lock_desc,		(vop_t *) vop_nolock },
71	{ &vop_mmap_desc,		(vop_t *) vop_einval },
72	{ &vop_open_desc,		(vop_t *) vop_null },
73	{ &vop_pathconf_desc,		(vop_t *) vop_einval },
74	{ &vop_poll_desc,		(vop_t *) vop_nopoll },
75	{ &vop_readlink_desc,		(vop_t *) vop_einval },
76	{ &vop_reallocblks_desc,	(vop_t *) vop_eopnotsupp },
77	{ &vop_revoke_desc,		(vop_t *) vop_revoke },
78	{ &vop_strategy_desc,		(vop_t *) vop_nostrategy },
79	{ &vop_unlock_desc,		(vop_t *) vop_nounlock },
80	{ NULL, NULL }
81};
82
83static struct vnodeopv_desc default_vnodeop_opv_desc =
84        { &default_vnodeop_p, default_vnodeop_entries };
85
86VNODEOP_SET(default_vnodeop_opv_desc);
87
88int
89vop_eopnotsupp(struct vop_generic_args *ap)
90{
91	/*
92	printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
93	*/
94
95	return (EOPNOTSUPP);
96}
97
98int
99vop_ebadf(struct vop_generic_args *ap)
100{
101
102	return (EBADF);
103}
104
105int
106vop_enotty(struct vop_generic_args *ap)
107{
108
109	return (ENOTTY);
110}
111
112int
113vop_einval(struct vop_generic_args *ap)
114{
115
116	return (EINVAL);
117}
118
119int
120vop_null(struct vop_generic_args *ap)
121{
122
123	return (0);
124}
125
126int
127vop_defaultop(struct vop_generic_args *ap)
128{
129
130	return (VOCALL(default_vnodeop_p, ap->a_desc->vdesc_offset, ap));
131}
132
133static int
134vop_nostrategy (struct vop_strategy_args *ap)
135{
136	printf("No strategy for buffer at %p\n", ap->a_bp);
137	vprint("", ap->a_bp->b_vp);
138	ap->a_bp->b_flags |= B_ERROR;
139	ap->a_bp->b_error = EOPNOTSUPP;
140	biodone(ap->a_bp);
141	return (EOPNOTSUPP);
142}
143
144int
145vop_stdpathconf(ap)
146	struct vop_pathconf_args /* {
147	struct vnode *a_vp;
148	int a_name;
149	int *a_retval;
150	} */ *ap;
151{
152
153	switch (ap->a_name) {
154		case _PC_LINK_MAX:
155			*ap->a_retval = LINK_MAX;
156			return (0);
157		case _PC_MAX_CANON:
158			*ap->a_retval = MAX_CANON;
159			return (0);
160		case _PC_MAX_INPUT:
161			*ap->a_retval = MAX_INPUT;
162			return (0);
163		case _PC_PIPE_BUF:
164			*ap->a_retval = PIPE_BUF;
165			return (0);
166		case _PC_CHOWN_RESTRICTED:
167			*ap->a_retval = 1;
168			return (0);
169		case _PC_VDISABLE:
170			*ap->a_retval = _POSIX_VDISABLE;
171			return (0);
172		default:
173			return (EINVAL);
174	}
175	/* NOTREACHED */
176}
177
178/*
179 * Standard lock, unlock and islocked functions.
180 *
181 * These depend on the lock structure being the first element in the
182 * inode, ie: vp->v_data points to the the lock!
183 */
184int
185vop_stdlock(ap)
186	struct vop_lock_args /* {
187		struct vnode *a_vp;
188		int a_flags;
189		struct proc *a_p;
190	} */ *ap;
191{
192	struct lock *l = (struct lock*)ap->a_vp->v_data;
193
194	return (lockmgr(l, ap->a_flags, &ap->a_vp->v_interlock, ap->a_p));
195}
196
197int
198vop_stdunlock(ap)
199	struct vop_unlock_args /* {
200		struct vnode *a_vp;
201		int a_flags;
202		struct proc *a_p;
203	} */ *ap;
204{
205	struct lock *l = (struct lock*)ap->a_vp->v_data;
206
207	return (lockmgr(l, ap->a_flags | LK_RELEASE, &ap->a_vp->v_interlock,
208	    ap->a_p));
209}
210
211int
212vop_stdislocked(ap)
213	struct vop_islocked_args /* {
214		struct vnode *a_vp;
215	} */ *ap;
216{
217	struct lock *l = (struct lock*)ap->a_vp->v_data;
218
219	return (lockstatus(l));
220}
221
222/*
223 * Return true for select/poll.
224 */
225int
226vop_nopoll(ap)
227	struct vop_poll_args /* {
228		struct vnode *a_vp;
229		int  a_events;
230		struct ucred *a_cred;
231		struct proc *a_p;
232	} */ *ap;
233{
234
235	/*
236	 * Just return what we were asked for.
237	 */
238	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
239}
240
241int
242vop_stdbwrite(ap)
243	struct vop_bwrite_args *ap;
244{
245	return (bwrite(ap->a_bp));
246}
247
248/*
249 * Stubs to use when there is no locking to be done on the underlying object.
250 * A minimal shared lock is necessary to ensure that the underlying object
251 * is not revoked while an operation is in progress. So, an active shared
252 * count is maintained in an auxillary vnode lock structure.
253 */
254int
255vop_sharedlock(ap)
256	struct vop_lock_args /* {
257		struct vnode *a_vp;
258		int a_flags;
259		struct proc *a_p;
260	} */ *ap;
261{
262	/*
263	 * This code cannot be used until all the non-locking filesystems
264	 * (notably NFS) are converted to properly lock and release nodes.
265	 * Also, certain vnode operations change the locking state within
266	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
267	 * and symlink). Ideally these operations should not change the
268	 * lock state, but should be changed to let the caller of the
269	 * function unlock them. Otherwise all intermediate vnode layers
270	 * (such as union, umapfs, etc) must catch these functions to do
271	 * the necessary locking at their layer. Note that the inactive
272	 * and lookup operations also change their lock state, but this
273	 * cannot be avoided, so these two operations will always need
274	 * to be handled in intermediate layers.
275	 */
276	struct vnode *vp = ap->a_vp;
277	int vnflags, flags = ap->a_flags;
278
279	if (vp->v_vnlock == NULL) {
280		if ((flags & LK_TYPE_MASK) == LK_DRAIN)
281			return (0);
282		MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
283		    M_VNODE, M_WAITOK);
284		lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
285	}
286	switch (flags & LK_TYPE_MASK) {
287	case LK_DRAIN:
288		vnflags = LK_DRAIN;
289		break;
290	case LK_EXCLUSIVE:
291#ifdef DEBUG_VFS_LOCKS
292		/*
293		 * Normally, we use shared locks here, but that confuses
294		 * the locking assertions.
295		 */
296		vnflags = LK_EXCLUSIVE;
297		break;
298#endif
299	case LK_SHARED:
300		vnflags = LK_SHARED;
301		break;
302	case LK_UPGRADE:
303	case LK_EXCLUPGRADE:
304	case LK_DOWNGRADE:
305		return (0);
306	case LK_RELEASE:
307	default:
308		panic("vop_sharedlock: bad operation %d", flags & LK_TYPE_MASK);
309	}
310	if (flags & LK_INTERLOCK)
311		vnflags |= LK_INTERLOCK;
312	return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock, ap->a_p));
313}
314
315/*
316 * Stubs to use when there is no locking to be done on the underlying object.
317 * A minimal shared lock is necessary to ensure that the underlying object
318 * is not revoked while an operation is in progress. So, an active shared
319 * count is maintained in an auxillary vnode lock structure.
320 */
321int
322vop_nolock(ap)
323	struct vop_lock_args /* {
324		struct vnode *a_vp;
325		int a_flags;
326		struct proc *a_p;
327	} */ *ap;
328{
329#ifdef notyet
330	/*
331	 * This code cannot be used until all the non-locking filesystems
332	 * (notably NFS) are converted to properly lock and release nodes.
333	 * Also, certain vnode operations change the locking state within
334	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
335	 * and symlink). Ideally these operations should not change the
336	 * lock state, but should be changed to let the caller of the
337	 * function unlock them. Otherwise all intermediate vnode layers
338	 * (such as union, umapfs, etc) must catch these functions to do
339	 * the necessary locking at their layer. Note that the inactive
340	 * and lookup operations also change their lock state, but this
341	 * cannot be avoided, so these two operations will always need
342	 * to be handled in intermediate layers.
343	 */
344	struct vnode *vp = ap->a_vp;
345	int vnflags, flags = ap->a_flags;
346
347	if (vp->v_vnlock == NULL) {
348		if ((flags & LK_TYPE_MASK) == LK_DRAIN)
349			return (0);
350		MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
351		    M_VNODE, M_WAITOK);
352		lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
353	}
354	switch (flags & LK_TYPE_MASK) {
355	case LK_DRAIN:
356		vnflags = LK_DRAIN;
357		break;
358	case LK_EXCLUSIVE:
359	case LK_SHARED:
360		vnflags = LK_SHARED;
361		break;
362	case LK_UPGRADE:
363	case LK_EXCLUPGRADE:
364	case LK_DOWNGRADE:
365		return (0);
366	case LK_RELEASE:
367	default:
368		panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
369	}
370	if (flags & LK_INTERLOCK)
371		vnflags |= LK_INTERLOCK;
372	return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock, ap->a_p));
373#else /* for now */
374	/*
375	 * Since we are not using the lock manager, we must clear
376	 * the interlock here.
377	 */
378	if (ap->a_flags & LK_INTERLOCK)
379		simple_unlock(&ap->a_vp->v_interlock);
380	return (0);
381#endif
382}
383
384/*
385 * Do the inverse of vop_nolock, handling the interlock in a compatible way.
386 */
387int
388vop_nounlock(ap)
389	struct vop_unlock_args /* {
390		struct vnode *a_vp;
391		int a_flags;
392		struct proc *a_p;
393	} */ *ap;
394{
395	struct vnode *vp = ap->a_vp;
396
397	if (vp->v_vnlock == NULL) {
398		if (ap->a_flags & LK_INTERLOCK)
399			simple_unlock(&ap->a_vp->v_interlock);
400		return (0);
401	}
402	return (lockmgr(vp->v_vnlock, LK_RELEASE | ap->a_flags,
403		&ap->a_vp->v_interlock, ap->a_p));
404}
405
406/*
407 * Return whether or not the node is in use.
408 */
409int
410vop_noislocked(ap)
411	struct vop_islocked_args /* {
412		struct vnode *a_vp;
413	} */ *ap;
414{
415	struct vnode *vp = ap->a_vp;
416
417	if (vp->v_vnlock == NULL)
418		return (0);
419	return (lockstatus(vp->v_vnlock));
420}
421
422