vfs_default.c revision 33964
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/mount.h>
48#include <sys/poll.h>
49
50/*
51 * VFS operations
52 */
53
54/*
55 * Complement to all vpp returning ops.
56 * XXX - initially only to get rid of WILLRELE.
57 */
58/* ARGSUSED */
59int
60vfs_vrele(mp, vp)
61	struct mount *mp;
62	struct vnode *vp;
63{
64	vrele(vp);
65	return (0);
66}
67
68/*
69 * vnode operations
70 */
71
72static int vop_nostrategy __P((struct vop_strategy_args *));
73
74/*
75 * This vnode table stores what we want to do if the filesystem doesn't
76 * implement a particular VOP.
77 *
78 * If there is no specific entry here, we will return EOPNOTSUPP.
79 *
80 */
81
82vop_t **default_vnodeop_p;
83static struct vnodeopv_entry_desc default_vnodeop_entries[] = {
84	{ &vop_default_desc,		(vop_t *) vop_eopnotsupp },
85	{ &vop_abortop_desc,		(vop_t *) vop_null },
86	{ &vop_advlock_desc,		(vop_t *) vop_einval },
87	{ &vop_bwrite_desc,		(vop_t *) vop_stdbwrite },
88	{ &vop_close_desc,		(vop_t *) vop_null },
89	{ &vop_fsync_desc,		(vop_t *) vop_null },
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_mmap_desc,		(vop_t *) vop_einval },
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_reallocblks_desc,	(vop_t *) vop_eopnotsupp },
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
156static int
157vop_nostrategy (struct vop_strategy_args *ap)
158{
159	printf("No strategy for buffer at %p\n", ap->a_bp);
160	vprint("", ap->a_bp->b_vp);
161	ap->a_bp->b_flags |= B_ERROR;
162	ap->a_bp->b_error = EOPNOTSUPP;
163	biodone(ap->a_bp);
164	return (EOPNOTSUPP);
165}
166
167int
168vop_stdpathconf(ap)
169	struct vop_pathconf_args /* {
170	struct vnode *a_vp;
171	int a_name;
172	int *a_retval;
173	} */ *ap;
174{
175
176	switch (ap->a_name) {
177		case _PC_LINK_MAX:
178			*ap->a_retval = LINK_MAX;
179			return (0);
180		case _PC_MAX_CANON:
181			*ap->a_retval = MAX_CANON;
182			return (0);
183		case _PC_MAX_INPUT:
184			*ap->a_retval = MAX_INPUT;
185			return (0);
186		case _PC_PIPE_BUF:
187			*ap->a_retval = PIPE_BUF;
188			return (0);
189		case _PC_CHOWN_RESTRICTED:
190			*ap->a_retval = 1;
191			return (0);
192		case _PC_VDISABLE:
193			*ap->a_retval = _POSIX_VDISABLE;
194			return (0);
195		default:
196			return (EINVAL);
197	}
198	/* NOTREACHED */
199}
200
201/*
202 * Standard lock, unlock and islocked functions.
203 *
204 * These depend on the lock structure being the first element in the
205 * inode, ie: vp->v_data points to the the lock!
206 */
207int
208vop_stdlock(ap)
209	struct vop_lock_args /* {
210		struct vnode *a_vp;
211		int a_flags;
212		struct proc *a_p;
213	} */ *ap;
214{
215	struct lock *l;
216
217	if ((l = (struct lock *)ap->a_vp->v_data) == NULL) {
218		if (ap->a_flags & LK_INTERLOCK)
219			simple_unlock(&ap->a_vp->v_interlock);
220		return 0;
221	}
222
223	return (lockmgr(l, ap->a_flags, &ap->a_vp->v_interlock, ap->a_p));
224}
225
226int
227vop_stdunlock(ap)
228	struct vop_unlock_args /* {
229		struct vnode *a_vp;
230		int a_flags;
231		struct proc *a_p;
232	} */ *ap;
233{
234	struct lock *l;
235
236	if ((l = (struct lock *)ap->a_vp->v_data) == NULL) {
237		if (ap->a_flags & LK_INTERLOCK)
238			simple_unlock(&ap->a_vp->v_interlock);
239		return 0;
240	}
241
242	return (lockmgr(l, ap->a_flags | LK_RELEASE, &ap->a_vp->v_interlock,
243	    ap->a_p));
244}
245
246int
247vop_stdislocked(ap)
248	struct vop_islocked_args /* {
249		struct vnode *a_vp;
250	} */ *ap;
251{
252	struct lock *l;
253
254	if ((l = (struct lock *)ap->a_vp->v_data) == NULL)
255		return 0;
256
257	return (lockstatus(l));
258}
259
260/*
261 * Return true for select/poll.
262 */
263int
264vop_nopoll(ap)
265	struct vop_poll_args /* {
266		struct vnode *a_vp;
267		int  a_events;
268		struct ucred *a_cred;
269		struct proc *a_p;
270	} */ *ap;
271{
272	/*
273	 * Return true for read/write.  If the user asked for something
274	 * special, return POLLNVAL, so that clients have a way of
275	 * determining reliably whether or not the extended
276	 * functionality is present without hard-coding knowledge
277	 * of specific filesystem implementations.
278	 */
279	if (ap->a_events & ~POLLSTANDARD)
280		return (POLLNVAL);
281
282	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
283}
284
285/*
286 * Implement poll for local filesystems that support it.
287 */
288int
289vop_stdpoll(ap)
290	struct vop_poll_args /* {
291		struct vnode *a_vp;
292		int  a_events;
293		struct ucred *a_cred;
294		struct proc *a_p;
295	} */ *ap;
296{
297	if ((ap->a_events & ~POLLSTANDARD) == 0)
298		return (ap->a_events & (POLLRDNORM|POLLWRNORM));
299	return (vn_pollrecord(ap->a_vp, ap->a_p, ap->a_events));
300}
301
302int
303vop_stdbwrite(ap)
304	struct vop_bwrite_args *ap;
305{
306	return (bwrite(ap->a_bp));
307}
308
309/*
310 * Stubs to use when there is no locking to be done on the underlying object.
311 * A minimal shared lock is necessary to ensure that the underlying object
312 * is not revoked while an operation is in progress. So, an active shared
313 * count is maintained in an auxillary vnode lock structure.
314 */
315int
316vop_sharedlock(ap)
317	struct vop_lock_args /* {
318		struct vnode *a_vp;
319		int a_flags;
320		struct proc *a_p;
321	} */ *ap;
322{
323	/*
324	 * This code cannot be used until all the non-locking filesystems
325	 * (notably NFS) are converted to properly lock and release nodes.
326	 * Also, certain vnode operations change the locking state within
327	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
328	 * and symlink). Ideally these operations should not change the
329	 * lock state, but should be changed to let the caller of the
330	 * function unlock them. Otherwise all intermediate vnode layers
331	 * (such as union, umapfs, etc) must catch these functions to do
332	 * the necessary locking at their layer. Note that the inactive
333	 * and lookup operations also change their lock state, but this
334	 * cannot be avoided, so these two operations will always need
335	 * to be handled in intermediate layers.
336	 */
337	struct vnode *vp = ap->a_vp;
338	int vnflags, flags = ap->a_flags;
339
340	if (vp->v_vnlock == NULL) {
341		if ((flags & LK_TYPE_MASK) == LK_DRAIN)
342			return (0);
343		MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
344		    M_VNODE, M_WAITOK);
345		lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
346	}
347	switch (flags & LK_TYPE_MASK) {
348	case LK_DRAIN:
349		vnflags = LK_DRAIN;
350		break;
351	case LK_EXCLUSIVE:
352#ifdef DEBUG_VFS_LOCKS
353		/*
354		 * Normally, we use shared locks here, but that confuses
355		 * the locking assertions.
356		 */
357		vnflags = LK_EXCLUSIVE;
358		break;
359#endif
360	case LK_SHARED:
361		vnflags = LK_SHARED;
362		break;
363	case LK_UPGRADE:
364	case LK_EXCLUPGRADE:
365	case LK_DOWNGRADE:
366		return (0);
367	case LK_RELEASE:
368	default:
369		panic("vop_sharedlock: bad operation %d", flags & LK_TYPE_MASK);
370	}
371	if (flags & LK_INTERLOCK)
372		vnflags |= LK_INTERLOCK;
373	return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock, ap->a_p));
374}
375
376/*
377 * Stubs to use when there is no locking to be done on the underlying object.
378 * A minimal shared lock is necessary to ensure that the underlying object
379 * is not revoked while an operation is in progress. So, an active shared
380 * count is maintained in an auxillary vnode lock structure.
381 */
382int
383vop_nolock(ap)
384	struct vop_lock_args /* {
385		struct vnode *a_vp;
386		int a_flags;
387		struct proc *a_p;
388	} */ *ap;
389{
390#ifdef notyet
391	/*
392	 * This code cannot be used until all the non-locking filesystems
393	 * (notably NFS) are converted to properly lock and release nodes.
394	 * Also, certain vnode operations change the locking state within
395	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
396	 * and symlink). Ideally these operations should not change the
397	 * lock state, but should be changed to let the caller of the
398	 * function unlock them. Otherwise all intermediate vnode layers
399	 * (such as union, umapfs, etc) must catch these functions to do
400	 * the necessary locking at their layer. Note that the inactive
401	 * and lookup operations also change their lock state, but this
402	 * cannot be avoided, so these two operations will always need
403	 * to be handled in intermediate layers.
404	 */
405	struct vnode *vp = ap->a_vp;
406	int vnflags, flags = ap->a_flags;
407
408	if (vp->v_vnlock == NULL) {
409		if ((flags & LK_TYPE_MASK) == LK_DRAIN)
410			return (0);
411		MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
412		    M_VNODE, M_WAITOK);
413		lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
414	}
415	switch (flags & LK_TYPE_MASK) {
416	case LK_DRAIN:
417		vnflags = LK_DRAIN;
418		break;
419	case LK_EXCLUSIVE:
420	case LK_SHARED:
421		vnflags = LK_SHARED;
422		break;
423	case LK_UPGRADE:
424	case LK_EXCLUPGRADE:
425	case LK_DOWNGRADE:
426		return (0);
427	case LK_RELEASE:
428	default:
429		panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
430	}
431	if (flags & LK_INTERLOCK)
432		vnflags |= LK_INTERLOCK;
433	return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock, ap->a_p));
434#else /* for now */
435	/*
436	 * Since we are not using the lock manager, we must clear
437	 * the interlock here.
438	 */
439	if (ap->a_flags & LK_INTERLOCK)
440		simple_unlock(&ap->a_vp->v_interlock);
441	return (0);
442#endif
443}
444
445/*
446 * Do the inverse of vop_nolock, handling the interlock in a compatible way.
447 */
448int
449vop_nounlock(ap)
450	struct vop_unlock_args /* {
451		struct vnode *a_vp;
452		int a_flags;
453		struct proc *a_p;
454	} */ *ap;
455{
456	struct vnode *vp = ap->a_vp;
457
458	if (vp->v_vnlock == NULL) {
459		if (ap->a_flags & LK_INTERLOCK)
460			simple_unlock(&ap->a_vp->v_interlock);
461		return (0);
462	}
463	return (lockmgr(vp->v_vnlock, LK_RELEASE | ap->a_flags,
464		&ap->a_vp->v_interlock, ap->a_p));
465}
466
467/*
468 * Return whether or not the node is in use.
469 */
470int
471vop_noislocked(ap)
472	struct vop_islocked_args /* {
473		struct vnode *a_vp;
474	} */ *ap;
475{
476	struct vnode *vp = ap->a_vp;
477
478	if (vp->v_vnlock == NULL)
479		return (0);
480	return (lockstatus(vp->v_vnlock));
481}
482
483