xref: /freebsd/sys/fs/deadfs/dead_vnops.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
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/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/poll.h>
43 #include <sys/vnode.h>
44 
45 static int	chkvnlock(struct vnode *);
46 /*
47  * Prototypes for dead operations on vnodes.
48  */
49 static int	dead_bmap(struct vop_bmap_args *);
50 static int	dead_ioctl(struct vop_ioctl_args *);
51 static int	dead_lock(struct vop_lock_args *);
52 static int	dead_lookup(struct vop_lookup_args *);
53 static int	dead_open(struct vop_open_args *);
54 static int	dead_poll(struct vop_poll_args *);
55 static int	dead_print(struct vop_print_args *);
56 static int	dead_read(struct vop_read_args *);
57 static int	dead_write(struct vop_write_args *);
58 
59 vop_t **dead_vnodeop_p;
60 static struct vnodeopv_entry_desc dead_vnodeop_entries[] = {
61 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
62 	{ &vop_access_desc,		(vop_t *) vop_ebadf },
63 	{ &vop_advlock_desc,		(vop_t *) vop_ebadf },
64 	{ &vop_bmap_desc,		(vop_t *) dead_bmap },
65 	{ &vop_create_desc,		(vop_t *) vop_panic },
66 	{ &vop_getattr_desc,		(vop_t *) vop_ebadf },
67 	{ &vop_inactive_desc,		(vop_t *) vop_null },
68 	{ &vop_ioctl_desc,		(vop_t *) dead_ioctl },
69 	{ &vop_link_desc,		(vop_t *) vop_panic },
70 	{ &vop_lock_desc,		(vop_t *) dead_lock },
71 	{ &vop_lookup_desc,		(vop_t *) dead_lookup },
72 	{ &vop_mkdir_desc,		(vop_t *) vop_panic },
73 	{ &vop_mknod_desc,		(vop_t *) vop_panic },
74 	{ &vop_open_desc,		(vop_t *) dead_open },
75 	{ &vop_pathconf_desc,		(vop_t *) vop_ebadf },	/* per pathconf(2) */
76 	{ &vop_poll_desc,		(vop_t *) dead_poll },
77 	{ &vop_print_desc,		(vop_t *) dead_print },
78 	{ &vop_read_desc,		(vop_t *) dead_read },
79 	{ &vop_readdir_desc,		(vop_t *) vop_ebadf },
80 	{ &vop_readlink_desc,		(vop_t *) vop_ebadf },
81 	{ &vop_reclaim_desc,		(vop_t *) vop_null },
82 	{ &vop_remove_desc,		(vop_t *) vop_panic },
83 	{ &vop_rename_desc,		(vop_t *) vop_panic },
84 	{ &vop_rmdir_desc,		(vop_t *) vop_panic },
85 	{ &vop_setattr_desc,		(vop_t *) vop_ebadf },
86 	{ &vop_symlink_desc,		(vop_t *) vop_panic },
87 	{ &vop_write_desc,		(vop_t *) dead_write },
88 	{ NULL, NULL }
89 };
90 static struct vnodeopv_desc dead_vnodeop_opv_desc =
91 	{ &dead_vnodeop_p, dead_vnodeop_entries };
92 
93 VNODEOP_SET(dead_vnodeop_opv_desc);
94 
95 /*
96  * Trivial lookup routine that always fails.
97  */
98 /* ARGSUSED */
99 static int
100 dead_lookup(ap)
101 	struct vop_lookup_args /* {
102 		struct vnode * a_dvp;
103 		struct vnode ** a_vpp;
104 		struct componentname * a_cnp;
105 	} */ *ap;
106 {
107 
108 	*ap->a_vpp = NULL;
109 	return (ENOTDIR);
110 }
111 
112 /*
113  * Open always fails as if device did not exist.
114  */
115 /* ARGSUSED */
116 static int
117 dead_open(ap)
118 	struct vop_open_args /* {
119 		struct vnode *a_vp;
120 		int  a_mode;
121 		struct ucred *a_cred;
122 		struct proc *a_p;
123 	} */ *ap;
124 {
125 
126 	return (ENXIO);
127 }
128 
129 /*
130  * Vnode op for read
131  */
132 /* ARGSUSED */
133 static int
134 dead_read(ap)
135 	struct vop_read_args /* {
136 		struct vnode *a_vp;
137 		struct uio *a_uio;
138 		int  a_ioflag;
139 		struct ucred *a_cred;
140 	} */ *ap;
141 {
142 
143 	if (chkvnlock(ap->a_vp))
144 		panic("dead_read: lock");
145 	/*
146 	 * Return EOF for tty devices, EIO for others
147 	 */
148 	if ((ap->a_vp->v_vflag & VV_ISTTY) == 0)
149 		return (EIO);
150 	return (0);
151 }
152 
153 /*
154  * Vnode op for write
155  */
156 /* ARGSUSED */
157 static int
158 dead_write(ap)
159 	struct vop_write_args /* {
160 		struct vnode *a_vp;
161 		struct uio *a_uio;
162 		int  a_ioflag;
163 		struct ucred *a_cred;
164 	} */ *ap;
165 {
166 
167 	if (chkvnlock(ap->a_vp))
168 		panic("dead_write: lock");
169 	return (EIO);
170 }
171 
172 /*
173  * Device ioctl operation.
174  */
175 /* ARGSUSED */
176 static int
177 dead_ioctl(ap)
178 	struct vop_ioctl_args /* {
179 		struct vnode *a_vp;
180 		int  a_command;
181 		caddr_t  a_data;
182 		int  a_fflag;
183 		struct ucred *a_cred;
184 		struct proc *a_p;
185 	} */ *ap;
186 {
187 
188 	if (!chkvnlock(ap->a_vp))
189 		return (ENOTTY);
190 	return (VCALL(ap->a_vp, VOFFSET(vop_ioctl), ap));
191 }
192 
193 
194 /*
195  * Wait until the vnode has finished changing state.
196  */
197 static int
198 dead_lock(ap)
199 	struct vop_lock_args /* {
200 		struct vnode *a_vp;
201 		int a_flags;
202 		struct proc *a_p;
203 	} */ *ap;
204 {
205 	struct vnode *vp = ap->a_vp;
206 
207 	/*
208 	 * Since we are not using the lock manager, we must clear
209 	 * the interlock here.
210 	 */
211 	if (ap->a_flags & LK_INTERLOCK) {
212 		mtx_unlock(&vp->v_interlock);
213 		ap->a_flags &= ~LK_INTERLOCK;
214 	}
215 	if (!chkvnlock(vp))
216 		return (0);
217 	return (VCALL(vp, VOFFSET(vop_lock), ap));
218 }
219 
220 /*
221  * Wait until the vnode has finished changing state.
222  */
223 static int
224 dead_bmap(ap)
225 	struct vop_bmap_args /* {
226 		struct vnode *a_vp;
227 		daddr_t  a_bn;
228 		struct vnode **a_vpp;
229 		daddr_t *a_bnp;
230 		int *a_runp;
231 		int *a_runb;
232 	} */ *ap;
233 {
234 
235 	if (!chkvnlock(ap->a_vp))
236 		return (EIO);
237 	return (VOP_BMAP(ap->a_vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp, ap->a_runb));
238 }
239 
240 /*
241  * Print out the contents of a dead vnode.
242  */
243 /* ARGSUSED */
244 static int
245 dead_print(ap)
246 	struct vop_print_args /* {
247 		struct vnode *a_vp;
248 	} */ *ap;
249 {
250 
251 	printf("tag VT_NON, dead vnode\n");
252 	return (0);
253 }
254 
255 /*
256  * We have to wait during times when the vnode is
257  * in a state of change.
258  */
259 int
260 chkvnlock(vp)
261 	register struct vnode *vp;
262 {
263 	int locked = 0;
264 
265 	VI_LOCK(vp);
266 	while (vp->v_iflag & VI_XLOCK) {
267 		vp->v_iflag |= VI_XWANT;
268 		(void) msleep((caddr_t)vp, VI_MTX(vp), PINOD, "ckvnlk", 0);
269 		locked = 1;
270 	}
271 	VI_UNLOCK(vp);
272 	return (locked);
273 }
274 
275 /*
276  * Trivial poll routine that always returns POLLHUP.
277  * This is necessary so that a process which is polling a file
278  * gets notified when that file is revoke()d.
279  */
280 static int
281 dead_poll(ap)
282 	struct vop_poll_args *ap;
283 {
284 	return (POLLHUP);
285 }
286