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