xref: /freebsd/sys/fs/deadfs/dead_vnops.c (revision 83c64397)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)dead_vnops.c	8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/poll.h>
39 #include <sys/vnode.h>
40 
41 static int	chkvnlock(struct vnode *);
42 /*
43  * Prototypes for dead operations on vnodes.
44  */
45 static vop_bmap_t	dead_bmap;
46 static vop_ioctl_t	dead_ioctl;
47 static vop_lock_t	dead_lock;
48 static vop_lookup_t	dead_lookup;
49 static vop_open_t	dead_open;
50 static vop_poll_t	dead_poll;
51 static vop_read_t	dead_read;
52 static vop_write_t	dead_write;
53 
54 struct vop_vector dead_vnodeops = {
55 	.vop_default =		&default_vnodeops,
56 
57 	.vop_access =		VOP_EBADF,
58 	.vop_advlock =		VOP_EBADF,
59 	.vop_bmap =		dead_bmap,
60 	.vop_create =		VOP_PANIC,
61 	.vop_getattr =		VOP_EBADF,
62 	.vop_inactive =		VOP_NULL,
63 	.vop_ioctl =		dead_ioctl,
64 	.vop_link =		VOP_PANIC,
65 	.vop_lock =		dead_lock,
66 	.vop_lookup =		dead_lookup,
67 	.vop_mkdir =		VOP_PANIC,
68 	.vop_mknod =		VOP_PANIC,
69 	.vop_open =		dead_open,
70 	.vop_pathconf =		VOP_EBADF,	/* per pathconf(2) */
71 	.vop_poll =		dead_poll,
72 	.vop_read =		dead_read,
73 	.vop_readdir =		VOP_EBADF,
74 	.vop_readlink =		VOP_EBADF,
75 	.vop_reclaim =		VOP_NULL,
76 	.vop_remove =		VOP_PANIC,
77 	.vop_rename =		VOP_PANIC,
78 	.vop_rmdir =		VOP_PANIC,
79 	.vop_setattr =		VOP_EBADF,
80 	.vop_symlink =		VOP_PANIC,
81 	.vop_write =		dead_write,
82 };
83 
84 /*
85  * Trivial lookup routine that always fails.
86  */
87 /* ARGSUSED */
88 static int
89 dead_lookup(ap)
90 	struct vop_lookup_args /* {
91 		struct vnode * a_dvp;
92 		struct vnode ** a_vpp;
93 		struct componentname * a_cnp;
94 	} */ *ap;
95 {
96 
97 	*ap->a_vpp = NULL;
98 	return (ENOTDIR);
99 }
100 
101 /*
102  * Open always fails as if device did not exist.
103  */
104 /* ARGSUSED */
105 static int
106 dead_open(ap)
107 	struct vop_open_args /* {
108 		struct vnode *a_vp;
109 		int  a_mode;
110 		struct ucred *a_cred;
111 		struct proc *a_p;
112 	} */ *ap;
113 {
114 
115 	return (ENXIO);
116 }
117 
118 /*
119  * Vnode op for read
120  */
121 /* ARGSUSED */
122 static int
123 dead_read(ap)
124 	struct vop_read_args /* {
125 		struct vnode *a_vp;
126 		struct uio *a_uio;
127 		int  a_ioflag;
128 		struct ucred *a_cred;
129 	} */ *ap;
130 {
131 
132 	if (chkvnlock(ap->a_vp))
133 		panic("dead_read: lock");
134 	/*
135 	 * Return EOF for tty devices, EIO for others
136 	 */
137 	if ((ap->a_vp->v_vflag & VV_ISTTY) == 0)
138 		return (EIO);
139 	return (0);
140 }
141 
142 /*
143  * Vnode op for write
144  */
145 /* ARGSUSED */
146 static int
147 dead_write(ap)
148 	struct vop_write_args /* {
149 		struct vnode *a_vp;
150 		struct uio *a_uio;
151 		int  a_ioflag;
152 		struct ucred *a_cred;
153 	} */ *ap;
154 {
155 
156 	if (chkvnlock(ap->a_vp))
157 		panic("dead_write: lock");
158 	return (EIO);
159 }
160 
161 /*
162  * Device ioctl operation.
163  */
164 /* ARGSUSED */
165 static int
166 dead_ioctl(ap)
167 	struct vop_ioctl_args /* {
168 		struct vnode *a_vp;
169 		u_long  a_command;
170 		caddr_t  a_data;
171 		int  a_fflag;
172 		struct ucred *a_cred;
173 		struct proc *a_p;
174 	} */ *ap;
175 {
176 
177 	if (!chkvnlock(ap->a_vp))
178 		return (ENOTTY);
179 	/* XXX: Doesn't this just recurse back here ? */
180 	return (VOP_IOCTL_AP(ap));
181 }
182 
183 
184 /*
185  * Wait until the vnode has finished changing state.
186  */
187 static int
188 dead_lock(ap)
189 	struct vop_lock_args /* {
190 		struct vnode *a_vp;
191 		int a_flags;
192 		struct proc *a_p;
193 	} */ *ap;
194 {
195 	struct vnode *vp = ap->a_vp;
196 
197 	/*
198 	 * Since we are not using the lock manager, we must clear
199 	 * the interlock here.
200 	 */
201 	if (ap->a_flags & LK_INTERLOCK) {
202 		mtx_unlock(&vp->v_interlock);
203 		ap->a_flags &= ~LK_INTERLOCK;
204 	}
205 	if (!chkvnlock(vp))
206 		return (0);
207 	return (VOP_LOCK_AP(ap));
208 }
209 
210 /*
211  * Wait until the vnode has finished changing state.
212  */
213 static int
214 dead_bmap(ap)
215 	struct vop_bmap_args /* {
216 		struct vnode *a_vp;
217 		daddr_t  a_bn;
218 		struct bufobj **a_bop;
219 		daddr_t *a_bnp;
220 		int *a_runp;
221 		int *a_runb;
222 	} */ *ap;
223 {
224 
225 	if (!chkvnlock(ap->a_vp))
226 		return (EIO);
227 	return (VOP_BMAP(ap->a_vp, ap->a_bn, ap->a_bop, ap->a_bnp, ap->a_runp, ap->a_runb));
228 }
229 
230 /*
231  * We have to wait during times when the vnode is
232  * in a state of change.
233  */
234 static int
235 chkvnlock(vp)
236 	register struct vnode *vp;
237 {
238 	int locked = 0;
239 
240 	VI_LOCK(vp);
241 	while (vp->v_iflag & VI_XLOCK) {
242 		vp->v_iflag |= VI_XWANT;
243 		(void) msleep((caddr_t)vp, VI_MTX(vp), PINOD, "ckvnlk", 0);
244 		locked = 1;
245 	}
246 	VI_UNLOCK(vp);
247 	return (locked);
248 }
249 
250 /*
251  * Trivial poll routine that always returns POLLHUP.
252  * This is necessary so that a process which is polling a file
253  * gets notified when that file is revoke()d.
254  */
255 static int
256 dead_poll(ap)
257 	struct vop_poll_args *ap;
258 {
259 	return (POLLHUP);
260 }
261