xref: /freebsd/sys/fs/deadfs/dead_vnops.c (revision 3e0f6b97b257a96f7275e4442204263e44b16686)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)dead_vnops.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/time.h>
41 #include <sys/kernel.h>
42 #include <sys/vnode.h>
43 #include <sys/errno.h>
44 #include <sys/namei.h>
45 #include <sys/buf.h>
46 
47 static int	chkvnlock __P((struct vnode *));
48 /*
49  * Prototypes for dead operations on vnodes.
50  */
51 static int	dead_badop __P((void));
52 static int	dead_ebadf __P((void));
53 static int	dead_lookup __P((struct vop_lookup_args *));
54 #define dead_create ((int (*) __P((struct  vop_create_args *)))dead_badop)
55 #define dead_mknod ((int (*) __P((struct  vop_mknod_args *)))dead_badop)
56 static int	dead_open __P((struct vop_open_args *));
57 #define dead_close ((int (*) __P((struct  vop_close_args *)))nullop)
58 #define dead_access ((int (*) __P((struct  vop_access_args *)))dead_ebadf)
59 #define dead_getattr ((int (*) __P((struct  vop_getattr_args *)))dead_ebadf)
60 #define dead_setattr ((int (*) __P((struct  vop_setattr_args *)))dead_ebadf)
61 static int	dead_read __P((struct vop_read_args *));
62 static int	dead_write __P((struct vop_write_args *));
63 static int	dead_ioctl __P((struct vop_ioctl_args *));
64 static int	dead_select __P((struct vop_select_args *));
65 #define dead_mmap ((int (*) __P((struct  vop_mmap_args *)))dead_badop)
66 #define dead_fsync ((int (*) __P((struct  vop_fsync_args *)))nullop)
67 #define dead_seek ((int (*) __P((struct  vop_seek_args *)))nullop)
68 #define dead_remove ((int (*) __P((struct  vop_remove_args *)))dead_badop)
69 #define dead_link ((int (*) __P((struct  vop_link_args *)))dead_badop)
70 #define dead_rename ((int (*) __P((struct  vop_rename_args *)))dead_badop)
71 #define dead_mkdir ((int (*) __P((struct  vop_mkdir_args *)))dead_badop)
72 #define dead_rmdir ((int (*) __P((struct  vop_rmdir_args *)))dead_badop)
73 #define dead_symlink ((int (*) __P((struct  vop_symlink_args *)))dead_badop)
74 #define dead_readdir ((int (*) __P((struct  vop_readdir_args *)))dead_ebadf)
75 #define dead_readlink ((int (*) __P((struct  vop_readlink_args *)))dead_ebadf)
76 #define dead_abortop ((int (*) __P((struct  vop_abortop_args *)))dead_badop)
77 #define dead_inactive ((int (*) __P((struct  vop_inactive_args *)))nullop)
78 #define dead_reclaim ((int (*) __P((struct  vop_reclaim_args *)))nullop)
79 static int	dead_lock __P((struct vop_lock_args *));
80 #define dead_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
81 static int	dead_bmap __P((struct vop_bmap_args *));
82 static int	dead_strategy __P((struct vop_strategy_args *));
83 static int	dead_print __P((struct vop_print_args *));
84 #define dead_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
85 #define dead_pathconf ((int (*) __P((struct  vop_pathconf_args *)))dead_ebadf)
86 #define dead_advlock ((int (*) __P((struct  vop_advlock_args *)))dead_ebadf)
87 #define dead_blkatoff ((int (*) __P((struct  vop_blkatoff_args *)))dead_badop)
88 #define dead_valloc ((int (*) __P((struct  vop_valloc_args *)))dead_badop)
89 #define dead_vfree ((int (*) __P((struct  vop_vfree_args *)))dead_badop)
90 #define dead_truncate ((int (*) __P((struct  vop_truncate_args *)))nullop)
91 #define dead_update ((int (*) __P((struct  vop_update_args *)))nullop)
92 #define dead_bwrite ((int (*) __P((struct  vop_bwrite_args *)))nullop)
93 
94 vop_t **dead_vnodeop_p;
95 static struct vnodeopv_entry_desc dead_vnodeop_entries[] = {
96 	{ &vop_default_desc, (vop_t *)vn_default_error },
97 	{ &vop_lookup_desc, (vop_t *)dead_lookup },	/* lookup */
98 	{ &vop_create_desc, (vop_t *)dead_create },	/* create */
99 	{ &vop_mknod_desc, (vop_t *)dead_mknod },	/* mknod */
100 	{ &vop_open_desc, (vop_t *)dead_open },		/* open */
101 	{ &vop_close_desc, (vop_t *)dead_close },	/* close */
102 	{ &vop_access_desc, (vop_t *)dead_access },	/* access */
103 	{ &vop_getattr_desc, (vop_t *)dead_getattr },	/* getattr */
104 	{ &vop_setattr_desc, (vop_t *)dead_setattr },	/* setattr */
105 	{ &vop_read_desc, (vop_t *)dead_read },		/* read */
106 	{ &vop_write_desc, (vop_t *)dead_write },	/* write */
107 	{ &vop_ioctl_desc, (vop_t *)dead_ioctl },	/* ioctl */
108 	{ &vop_select_desc, (vop_t *)dead_select },	/* select */
109 	{ &vop_mmap_desc, (vop_t *)dead_mmap },		/* mmap */
110 	{ &vop_fsync_desc, (vop_t *)dead_fsync },	/* fsync */
111 	{ &vop_seek_desc, (vop_t *)dead_seek },		/* seek */
112 	{ &vop_remove_desc, (vop_t *)dead_remove },	/* remove */
113 	{ &vop_link_desc, (vop_t *)dead_link },		/* link */
114 	{ &vop_rename_desc, (vop_t *)dead_rename },	/* rename */
115 	{ &vop_mkdir_desc, (vop_t *)dead_mkdir },	/* mkdir */
116 	{ &vop_rmdir_desc, (vop_t *)dead_rmdir },	/* rmdir */
117 	{ &vop_symlink_desc, (vop_t *)dead_symlink },	/* symlink */
118 	{ &vop_readdir_desc, (vop_t *)dead_readdir },	/* readdir */
119 	{ &vop_readlink_desc, (vop_t *)dead_readlink },	/* readlink */
120 	{ &vop_abortop_desc, (vop_t *)dead_abortop },	/* abortop */
121 	{ &vop_inactive_desc, (vop_t *)dead_inactive },	/* inactive */
122 	{ &vop_reclaim_desc, (vop_t *)dead_reclaim },	/* reclaim */
123 	{ &vop_lock_desc, (vop_t *)dead_lock },		/* lock */
124 	{ &vop_unlock_desc, (vop_t *)dead_unlock },	/* unlock */
125 	{ &vop_bmap_desc, (vop_t *)dead_bmap },		/* bmap */
126 	{ &vop_strategy_desc, (vop_t *)dead_strategy },	/* strategy */
127 	{ &vop_print_desc, (vop_t *)dead_print },	/* print */
128 	{ &vop_islocked_desc, (vop_t *)dead_islocked },	/* islocked */
129 	{ &vop_pathconf_desc, (vop_t *)dead_pathconf },	/* pathconf */
130 	{ &vop_advlock_desc, (vop_t *)dead_advlock },	/* advlock */
131 	{ &vop_blkatoff_desc, (vop_t *)dead_blkatoff },	/* blkatoff */
132 	{ &vop_valloc_desc, (vop_t *)dead_valloc },	/* valloc */
133 	{ &vop_vfree_desc, (vop_t *)dead_vfree },	/* vfree */
134 	{ &vop_truncate_desc, (vop_t *)dead_truncate },	/* truncate */
135 	{ &vop_update_desc, (vop_t *)dead_update },	/* update */
136 	{ &vop_bwrite_desc, (vop_t *)dead_bwrite },	/* bwrite */
137 	{ NULL, NULL }
138 };
139 static struct vnodeopv_desc dead_vnodeop_opv_desc =
140 	{ &dead_vnodeop_p, dead_vnodeop_entries };
141 
142 VNODEOP_SET(dead_vnodeop_opv_desc);
143 
144 /*
145  * Trivial lookup routine that always fails.
146  */
147 /* ARGSUSED */
148 static int
149 dead_lookup(ap)
150 	struct vop_lookup_args /* {
151 		struct vnode * a_dvp;
152 		struct vnode ** a_vpp;
153 		struct componentname * a_cnp;
154 	} */ *ap;
155 {
156 
157 	*ap->a_vpp = NULL;
158 	return (ENOTDIR);
159 }
160 
161 /*
162  * Open always fails as if device did not exist.
163  */
164 /* ARGSUSED */
165 static int
166 dead_open(ap)
167 	struct vop_open_args /* {
168 		struct vnode *a_vp;
169 		int  a_mode;
170 		struct ucred *a_cred;
171 		struct proc *a_p;
172 	} */ *ap;
173 {
174 
175 	return (ENXIO);
176 }
177 
178 /*
179  * Vnode op for read
180  */
181 /* ARGSUSED */
182 static int
183 dead_read(ap)
184 	struct vop_read_args /* {
185 		struct vnode *a_vp;
186 		struct uio *a_uio;
187 		int  a_ioflag;
188 		struct ucred *a_cred;
189 	} */ *ap;
190 {
191 
192 	if (chkvnlock(ap->a_vp))
193 		panic("dead_read: lock");
194 #if 0
195 	/* Lite2 behaviour */
196 	/*
197 	 * Return EOF for tty devices, EIO for others
198 	 */
199 	if ((ap->a_vp->v_flag & VISTTY) == 0)
200 		return (EIO);
201 #else
202 	/*
203 	 * Return EOF for character devices, EIO for others
204 	 */
205 	if (ap->a_vp->v_type != VCHR)
206 		return (EIO);
207 #endif
208 	return (0);
209 }
210 
211 /*
212  * Vnode op for write
213  */
214 /* ARGSUSED */
215 static int
216 dead_write(ap)
217 	struct vop_write_args /* {
218 		struct vnode *a_vp;
219 		struct uio *a_uio;
220 		int  a_ioflag;
221 		struct ucred *a_cred;
222 	} */ *ap;
223 {
224 
225 	if (chkvnlock(ap->a_vp))
226 		panic("dead_write: lock");
227 	return (EIO);
228 }
229 
230 /*
231  * Device ioctl operation.
232  */
233 /* ARGSUSED */
234 static int
235 dead_ioctl(ap)
236 	struct vop_ioctl_args /* {
237 		struct vnode *a_vp;
238 		int  a_command;
239 		caddr_t  a_data;
240 		int  a_fflag;
241 		struct ucred *a_cred;
242 		struct proc *a_p;
243 	} */ *ap;
244 {
245 
246 	if (!chkvnlock(ap->a_vp))
247 		return (EBADF);
248 	return (VCALL(ap->a_vp, VOFFSET(vop_ioctl), ap));
249 }
250 
251 /* ARGSUSED */
252 static int
253 dead_select(ap)
254 	struct vop_select_args /* {
255 		struct vnode *a_vp;
256 		int  a_which;
257 		int  a_fflags;
258 		struct ucred *a_cred;
259 		struct proc *a_p;
260 	} */ *ap;
261 {
262 
263 	/*
264 	 * Let the user find out that the descriptor is gone.
265 	 */
266 	return (1);
267 }
268 
269 /*
270  * Just call the device strategy routine
271  */
272 static int
273 dead_strategy(ap)
274 	struct vop_strategy_args /* {
275 		struct buf *a_bp;
276 	} */ *ap;
277 {
278 
279 	if (ap->a_bp->b_vp == NULL || !chkvnlock(ap->a_bp->b_vp)) {
280 		ap->a_bp->b_flags |= B_ERROR;
281 		biodone(ap->a_bp);
282 		return (EIO);
283 	}
284 	return (VOP_STRATEGY(ap->a_bp));
285 }
286 
287 /*
288  * Wait until the vnode has finished changing state.
289  */
290 static int
291 dead_lock(ap)
292 	struct vop_lock_args /* {
293 		struct vnode *a_vp;
294 		int a_flags;
295 		struct proc *a_p;
296 	} */ *ap;
297 {
298 	struct vnode *vp = ap->a_vp;
299 
300 	/*
301 	 * Since we are not using the lock manager, we must clear
302 	 * the interlock here.
303 	 */
304 	if (ap->a_flags & LK_INTERLOCK) {
305 		simple_unlock(&vp->v_interlock);
306 		ap->a_flags &= ~LK_INTERLOCK;
307 	}
308 	if (!chkvnlock(vp))
309 		return (0);
310 	return (VCALL(vp, VOFFSET(vop_lock), ap));
311 }
312 
313 /*
314  * Wait until the vnode has finished changing state.
315  */
316 static int
317 dead_bmap(ap)
318 	struct vop_bmap_args /* {
319 		struct vnode *a_vp;
320 		daddr_t  a_bn;
321 		struct vnode **a_vpp;
322 		daddr_t *a_bnp;
323 		int *a_runp;
324 		int *a_runb;
325 	} */ *ap;
326 {
327 
328 	if (!chkvnlock(ap->a_vp))
329 		return (EIO);
330 	return (VOP_BMAP(ap->a_vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp, ap->a_runb));
331 }
332 
333 /*
334  * Print out the contents of a dead vnode.
335  */
336 /* ARGSUSED */
337 static int
338 dead_print(ap)
339 	struct vop_print_args /* {
340 		struct vnode *a_vp;
341 	} */ *ap;
342 {
343 
344 	printf("tag VT_NON, dead vnode\n");
345 	return (0);
346 }
347 
348 /*
349  * Empty vnode failed operation
350  */
351 static int
352 dead_ebadf()
353 {
354 
355 	return (EBADF);
356 }
357 
358 /*
359  * Empty vnode bad operation
360  */
361 static int
362 dead_badop()
363 {
364 
365 	panic("dead_badop called");
366 	/* NOTREACHED */
367 }
368 
369 /*
370  * We have to wait during times when the vnode is
371  * in a state of change.
372  */
373 int
374 chkvnlock(vp)
375 	register struct vnode *vp;
376 {
377 	int locked = 0;
378 
379 	while (vp->v_flag & VXLOCK) {
380 		vp->v_flag |= VXWANT;
381 		(void) tsleep((caddr_t)vp, PINOD, "ckvnlk", 0);
382 		locked = 1;
383 	}
384 	return (locked);
385 }
386