xref: /freebsd/sys/kern/vfs_default.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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 
49 static 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 
59 vop_t **default_vnodeop_p;
60 static 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 
83 static struct vnodeopv_desc default_vnodeop_opv_desc =
84         { &default_vnodeop_p, default_vnodeop_entries };
85 
86 VNODEOP_SET(default_vnodeop_opv_desc);
87 
88 int
89 vop_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 
98 int
99 vop_ebadf(struct vop_generic_args *ap)
100 {
101 
102 	return (EBADF);
103 }
104 
105 int
106 vop_enotty(struct vop_generic_args *ap)
107 {
108 
109 	return (ENOTTY);
110 }
111 
112 int
113 vop_einval(struct vop_generic_args *ap)
114 {
115 
116 	return (EINVAL);
117 }
118 
119 int
120 vop_null(struct vop_generic_args *ap)
121 {
122 
123 	return (0);
124 }
125 
126 int
127 vop_defaultop(struct vop_generic_args *ap)
128 {
129 
130 	return (VOCALL(default_vnodeop_p, ap->a_desc->vdesc_offset, ap));
131 }
132 
133 static int
134 vop_nostrategy (struct vop_strategy_args *ap)
135 {
136 	printf("No strategy for buffer at %p\n", ap->a_bp);
137 	vprint("", ap->a_vp);
138 	vprint("", ap->a_bp->b_vp);
139 	ap->a_bp->b_flags |= B_ERROR;
140 	ap->a_bp->b_error = EOPNOTSUPP;
141 	biodone(ap->a_bp);
142 	return (EOPNOTSUPP);
143 }
144 
145 int
146 vop_stdpathconf(ap)
147 	struct vop_pathconf_args /* {
148 	struct vnode *a_vp;
149 	int a_name;
150 	int *a_retval;
151 	} */ *ap;
152 {
153 
154 	switch (ap->a_name) {
155 		case _PC_LINK_MAX:
156 			*ap->a_retval = LINK_MAX;
157 			return (0);
158 		case _PC_MAX_CANON:
159 			*ap->a_retval = MAX_CANON;
160 			return (0);
161 		case _PC_MAX_INPUT:
162 			*ap->a_retval = MAX_INPUT;
163 			return (0);
164 		case _PC_PIPE_BUF:
165 			*ap->a_retval = PIPE_BUF;
166 			return (0);
167 		case _PC_CHOWN_RESTRICTED:
168 			*ap->a_retval = 1;
169 			return (0);
170 		case _PC_VDISABLE:
171 			*ap->a_retval = _POSIX_VDISABLE;
172 			return (0);
173 		default:
174 			return (EINVAL);
175 	}
176 	/* NOTREACHED */
177 }
178 
179 /*
180  * Standard lock, unlock and islocked functions.
181  *
182  * These depend on the lock structure being the first element in the
183  * inode, ie: vp->v_data points to the the lock!
184  */
185 int
186 vop_stdlock(ap)
187 	struct vop_lock_args /* {
188 		struct vnode *a_vp;
189 		int a_flags;
190 		struct proc *a_p;
191 	} */ *ap;
192 {
193 	struct lock *l;
194 
195 	if ((l = (struct lock *)ap->a_vp->v_data) == NULL) {
196 		if (ap->a_flags & LK_INTERLOCK)
197 			simple_unlock(&ap->a_vp->v_interlock);
198 		return 0;
199 	}
200 
201 	return (lockmgr(l, ap->a_flags, &ap->a_vp->v_interlock, ap->a_p));
202 }
203 
204 int
205 vop_stdunlock(ap)
206 	struct vop_unlock_args /* {
207 		struct vnode *a_vp;
208 		int a_flags;
209 		struct proc *a_p;
210 	} */ *ap;
211 {
212 	struct lock *l;
213 
214 	if ((l = (struct lock *)ap->a_vp->v_data) == NULL) {
215 		if (ap->a_flags & LK_INTERLOCK)
216 			simple_unlock(&ap->a_vp->v_interlock);
217 		return 0;
218 	}
219 
220 	return (lockmgr(l, ap->a_flags | LK_RELEASE, &ap->a_vp->v_interlock,
221 	    ap->a_p));
222 }
223 
224 int
225 vop_stdislocked(ap)
226 	struct vop_islocked_args /* {
227 		struct vnode *a_vp;
228 	} */ *ap;
229 {
230 	struct lock *l;
231 
232 	if ((l = (struct lock *)ap->a_vp->v_data) == NULL)
233 		return 0;
234 
235 	return (lockstatus(l));
236 }
237 
238 /*
239  * Return true for select/poll.
240  */
241 int
242 vop_nopoll(ap)
243 	struct vop_poll_args /* {
244 		struct vnode *a_vp;
245 		int  a_events;
246 		struct ucred *a_cred;
247 		struct proc *a_p;
248 	} */ *ap;
249 {
250 	/*
251 	 * Return true for read/write.  If the user asked for something
252 	 * special, return POLLNVAL, so that clients have a way of
253 	 * determining reliably whether or not the extended
254 	 * functionality is present without hard-coding knowledge
255 	 * of specific filesystem implementations.
256 	 */
257 	if (ap->a_events & ~POLLSTANDARD)
258 		return (POLLNVAL);
259 
260 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
261 }
262 
263 /*
264  * Implement poll for local filesystems that support it.
265  */
266 int
267 vop_stdpoll(ap)
268 	struct vop_poll_args /* {
269 		struct vnode *a_vp;
270 		int  a_events;
271 		struct ucred *a_cred;
272 		struct proc *a_p;
273 	} */ *ap;
274 {
275 	if ((ap->a_events & ~POLLSTANDARD) == 0)
276 		return (ap->a_events & (POLLRDNORM|POLLWRNORM));
277 	return (vn_pollrecord(ap->a_vp, ap->a_p, ap->a_events));
278 }
279 
280 int
281 vop_stdbwrite(ap)
282 	struct vop_bwrite_args *ap;
283 {
284 	return (bwrite(ap->a_bp));
285 }
286 
287 /*
288  * Stubs to use when there is no locking to be done on the underlying object.
289  * A minimal shared lock is necessary to ensure that the underlying object
290  * is not revoked while an operation is in progress. So, an active shared
291  * count is maintained in an auxillary vnode lock structure.
292  */
293 int
294 vop_sharedlock(ap)
295 	struct vop_lock_args /* {
296 		struct vnode *a_vp;
297 		int a_flags;
298 		struct proc *a_p;
299 	} */ *ap;
300 {
301 	/*
302 	 * This code cannot be used until all the non-locking filesystems
303 	 * (notably NFS) are converted to properly lock and release nodes.
304 	 * Also, certain vnode operations change the locking state within
305 	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
306 	 * and symlink). Ideally these operations should not change the
307 	 * lock state, but should be changed to let the caller of the
308 	 * function unlock them. Otherwise all intermediate vnode layers
309 	 * (such as union, umapfs, etc) must catch these functions to do
310 	 * the necessary locking at their layer. Note that the inactive
311 	 * and lookup operations also change their lock state, but this
312 	 * cannot be avoided, so these two operations will always need
313 	 * to be handled in intermediate layers.
314 	 */
315 	struct vnode *vp = ap->a_vp;
316 	int vnflags, flags = ap->a_flags;
317 
318 	if (vp->v_vnlock == NULL) {
319 		if ((flags & LK_TYPE_MASK) == LK_DRAIN)
320 			return (0);
321 		MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
322 		    M_VNODE, M_WAITOK);
323 		lockinit(vp->v_vnlock, PVFS, "vnlock", 0, LK_NOPAUSE);
324 	}
325 	switch (flags & LK_TYPE_MASK) {
326 	case LK_DRAIN:
327 		vnflags = LK_DRAIN;
328 		break;
329 	case LK_EXCLUSIVE:
330 #ifdef DEBUG_VFS_LOCKS
331 		/*
332 		 * Normally, we use shared locks here, but that confuses
333 		 * the locking assertions.
334 		 */
335 		vnflags = LK_EXCLUSIVE;
336 		break;
337 #endif
338 	case LK_SHARED:
339 		vnflags = LK_SHARED;
340 		break;
341 	case LK_UPGRADE:
342 	case LK_EXCLUPGRADE:
343 	case LK_DOWNGRADE:
344 		return (0);
345 	case LK_RELEASE:
346 	default:
347 		panic("vop_sharedlock: bad operation %d", flags & LK_TYPE_MASK);
348 	}
349 	if (flags & LK_INTERLOCK)
350 		vnflags |= LK_INTERLOCK;
351 	return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock, ap->a_p));
352 }
353 
354 /*
355  * Stubs to use when there is no locking to be done on the underlying object.
356  * A minimal shared lock is necessary to ensure that the underlying object
357  * is not revoked while an operation is in progress. So, an active shared
358  * count is maintained in an auxillary vnode lock structure.
359  */
360 int
361 vop_nolock(ap)
362 	struct vop_lock_args /* {
363 		struct vnode *a_vp;
364 		int a_flags;
365 		struct proc *a_p;
366 	} */ *ap;
367 {
368 #ifdef notyet
369 	/*
370 	 * This code cannot be used until all the non-locking filesystems
371 	 * (notably NFS) are converted to properly lock and release nodes.
372 	 * Also, certain vnode operations change the locking state within
373 	 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
374 	 * and symlink). Ideally these operations should not change the
375 	 * lock state, but should be changed to let the caller of the
376 	 * function unlock them. Otherwise all intermediate vnode layers
377 	 * (such as union, umapfs, etc) must catch these functions to do
378 	 * the necessary locking at their layer. Note that the inactive
379 	 * and lookup operations also change their lock state, but this
380 	 * cannot be avoided, so these two operations will always need
381 	 * to be handled in intermediate layers.
382 	 */
383 	struct vnode *vp = ap->a_vp;
384 	int vnflags, flags = ap->a_flags;
385 
386 	if (vp->v_vnlock == NULL) {
387 		if ((flags & LK_TYPE_MASK) == LK_DRAIN)
388 			return (0);
389 		MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
390 		    M_VNODE, M_WAITOK);
391 		lockinit(vp->v_vnlock, PVFS, "vnlock", 0, LK_NOPAUSE);
392 	}
393 	switch (flags & LK_TYPE_MASK) {
394 	case LK_DRAIN:
395 		vnflags = LK_DRAIN;
396 		break;
397 	case LK_EXCLUSIVE:
398 	case LK_SHARED:
399 		vnflags = LK_SHARED;
400 		break;
401 	case LK_UPGRADE:
402 	case LK_EXCLUPGRADE:
403 	case LK_DOWNGRADE:
404 		return (0);
405 	case LK_RELEASE:
406 	default:
407 		panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
408 	}
409 	if (flags & LK_INTERLOCK)
410 		vnflags |= LK_INTERLOCK;
411 	return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock, ap->a_p));
412 #else /* for now */
413 	/*
414 	 * Since we are not using the lock manager, we must clear
415 	 * the interlock here.
416 	 */
417 	if (ap->a_flags & LK_INTERLOCK)
418 		simple_unlock(&ap->a_vp->v_interlock);
419 	return (0);
420 #endif
421 }
422 
423 /*
424  * Do the inverse of vop_nolock, handling the interlock in a compatible way.
425  */
426 int
427 vop_nounlock(ap)
428 	struct vop_unlock_args /* {
429 		struct vnode *a_vp;
430 		int a_flags;
431 		struct proc *a_p;
432 	} */ *ap;
433 {
434 	struct vnode *vp = ap->a_vp;
435 
436 	if (vp->v_vnlock == NULL) {
437 		if (ap->a_flags & LK_INTERLOCK)
438 			simple_unlock(&ap->a_vp->v_interlock);
439 		return (0);
440 	}
441 	return (lockmgr(vp->v_vnlock, LK_RELEASE | ap->a_flags,
442 		&ap->a_vp->v_interlock, ap->a_p));
443 }
444 
445 /*
446  * Return whether or not the node is in use.
447  */
448 int
449 vop_noislocked(ap)
450 	struct vop_islocked_args /* {
451 		struct vnode *a_vp;
452 	} */ *ap;
453 {
454 	struct vnode *vp = ap->a_vp;
455 
456 	if (vp->v_vnlock == NULL)
457 		return (0);
458 	return (lockstatus(vp->v_vnlock));
459 }
460 
461