dead_vnops.c revision 140165
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: head/sys/fs/deadfs/dead_vnops.c 140165 2005-01-13 07:53:01Z phk $
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
41static int	chkvnlock(struct vnode *);
42/*
43 * Prototypes for dead operations on vnodes.
44 */
45static vop_bmap_t	dead_bmap;
46static vop_ioctl_t	dead_ioctl;
47static vop_lock_t	dead_lock;
48static vop_lookup_t	dead_lookup;
49static vop_open_t	dead_open;
50static vop_poll_t	dead_poll;
51static vop_read_t	dead_read;
52static vop_write_t	dead_write;
53
54struct vop_vector dead_vnodeops = {
55	.vop_default =		&default_vnodeops,
56	.vop_access =		VOP_EBADF,
57	.vop_advlock =		VOP_EBADF,
58	.vop_bmap =		dead_bmap,
59	.vop_create =		VOP_PANIC,
60	.vop_getattr =		VOP_EBADF,
61	.vop_inactive =		VOP_NULL,
62	.vop_ioctl =		dead_ioctl,
63	.vop_link =		VOP_PANIC,
64	.vop_lock =		dead_lock,
65	.vop_lookup =		dead_lookup,
66	.vop_mkdir =		VOP_PANIC,
67	.vop_mknod =		VOP_PANIC,
68	.vop_open =		dead_open,
69	.vop_pathconf =		VOP_EBADF,	/* per pathconf(2) */
70	.vop_poll =		dead_poll,
71	.vop_read =		dead_read,
72	.vop_readdir =		VOP_EBADF,
73	.vop_readlink =		VOP_EBADF,
74	.vop_reclaim =		VOP_NULL,
75	.vop_remove =		VOP_PANIC,
76	.vop_rename =		VOP_PANIC,
77	.vop_rmdir =		VOP_PANIC,
78	.vop_setattr =		VOP_EBADF,
79	.vop_symlink =		VOP_PANIC,
80	.vop_write =		dead_write,
81};
82
83/*
84 * Trivial lookup routine that always fails.
85 */
86/* ARGSUSED */
87static int
88dead_lookup(ap)
89	struct vop_lookup_args /* {
90		struct vnode * a_dvp;
91		struct vnode ** a_vpp;
92		struct componentname * a_cnp;
93	} */ *ap;
94{
95
96	*ap->a_vpp = NULL;
97	return (ENOTDIR);
98}
99
100/*
101 * Open always fails as if device did not exist.
102 */
103/* ARGSUSED */
104static int
105dead_open(ap)
106	struct vop_open_args /* {
107		struct vnode *a_vp;
108		int  a_mode;
109		struct ucred *a_cred;
110		struct proc *a_p;
111	} */ *ap;
112{
113
114	return (ENXIO);
115}
116
117/*
118 * Vnode op for read
119 */
120/* ARGSUSED */
121static int
122dead_read(ap)
123	struct vop_read_args /* {
124		struct vnode *a_vp;
125		struct uio *a_uio;
126		int  a_ioflag;
127		struct ucred *a_cred;
128	} */ *ap;
129{
130
131	if (chkvnlock(ap->a_vp))
132		panic("dead_read: lock");
133	/*
134	 * Return EOF for tty devices, EIO for others
135	 */
136	if ((ap->a_vp->v_vflag & VV_ISTTY) == 0)
137		return (EIO);
138	return (0);
139}
140
141/*
142 * Vnode op for write
143 */
144/* ARGSUSED */
145static int
146dead_write(ap)
147	struct vop_write_args /* {
148		struct vnode *a_vp;
149		struct uio *a_uio;
150		int  a_ioflag;
151		struct ucred *a_cred;
152	} */ *ap;
153{
154
155	if (chkvnlock(ap->a_vp))
156		panic("dead_write: lock");
157	return (EIO);
158}
159
160/*
161 * Device ioctl operation.
162 */
163/* ARGSUSED */
164static int
165dead_ioctl(ap)
166	struct vop_ioctl_args /* {
167		struct vnode *a_vp;
168		u_long  a_command;
169		caddr_t  a_data;
170		int  a_fflag;
171		struct ucred *a_cred;
172		struct proc *a_p;
173	} */ *ap;
174{
175
176	if (!chkvnlock(ap->a_vp))
177		return (ENOTTY);
178	/* XXX: Doesn't this just recurse back here ? */
179	return (VOP_IOCTL_AP(ap));
180}
181
182
183/*
184 * Wait until the vnode has finished changing state.
185 */
186static int
187dead_lock(ap)
188	struct vop_lock_args /* {
189		struct vnode *a_vp;
190		int a_flags;
191		struct proc *a_p;
192	} */ *ap;
193{
194	struct vnode *vp = ap->a_vp;
195
196	/*
197	 * Since we are not using the lock manager, we must clear
198	 * the interlock here.
199	 */
200	if (ap->a_flags & LK_INTERLOCK) {
201		mtx_unlock(&vp->v_interlock);
202		ap->a_flags &= ~LK_INTERLOCK;
203	}
204	if (!chkvnlock(vp))
205		return (0);
206	return (VOP_LOCK_AP(ap));
207}
208
209/*
210 * Wait until the vnode has finished changing state.
211 */
212static int
213dead_bmap(ap)
214	struct vop_bmap_args /* {
215		struct vnode *a_vp;
216		daddr_t  a_bn;
217		struct bufobj **a_bop;
218		daddr_t *a_bnp;
219		int *a_runp;
220		int *a_runb;
221	} */ *ap;
222{
223
224	if (!chkvnlock(ap->a_vp))
225		return (EIO);
226	return (VOP_BMAP(ap->a_vp, ap->a_bn, ap->a_bop, ap->a_bnp, ap->a_runp, ap->a_runb));
227}
228
229/*
230 * We have to wait during times when the vnode is
231 * in a state of change.
232 */
233static int
234chkvnlock(vp)
235	register struct vnode *vp;
236{
237	int locked = 0;
238
239	VI_LOCK(vp);
240	while (vp->v_iflag & VI_XLOCK) {
241		vp->v_iflag |= VI_XWANT;
242		(void) msleep((caddr_t)vp, VI_MTX(vp), PINOD, "ckvnlk", 0);
243		locked = 1;
244	}
245	VI_UNLOCK(vp);
246	return (locked);
247}
248
249/*
250 * Trivial poll routine that always returns POLLHUP.
251 * This is necessary so that a process which is polling a file
252 * gets notified when that file is revoke()d.
253 */
254static int
255dead_poll(ap)
256	struct vop_poll_args *ap;
257{
258	return (POLLHUP);
259}
260