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