xref: /openbsd/sys/kern/vfs_default.c (revision a6445c1d)
1 /*	$OpenBSD: vfs_default.c,v 1.40 2014/09/14 14:17:26 jsg Exp $  */
2 
3 /*
4  * Portions of this code are:
5  *
6  * Copyright (c) 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mount.h>
42 #include <sys/vnode.h>
43 #include <sys/namei.h>
44 #include <sys/malloc.h>
45 #include <sys/pool.h>
46 #include <sys/event.h>
47 #include <sys/specdev.h>
48 
49 int filt_generic_readwrite(struct knote *, long);
50 void filt_generic_detach(struct knote *);
51 
52 /*
53  * Eliminate all activity associated with the requested vnode
54  * and with all vnodes aliased to the requested vnode.
55  */
56 int
57 vop_generic_revoke(void *v)
58 {
59 	struct vop_revoke_args *ap = v;
60 	struct vnode *vp, *vq;
61 	struct proc *p = curproc;
62 
63 #ifdef DIAGNOSTIC
64 	if ((ap->a_flags & REVOKEALL) == 0)
65 		panic("vop_generic_revoke");
66 #endif
67 
68 	vp = ap->a_vp;
69 
70 	if (vp->v_type == VBLK && vp->v_specinfo != 0) {
71 		struct mount *mp = vp->v_specmountpoint;
72 
73 		/*
74 		 * If we have a mount point associated with the vnode, we must
75 		 * flush it out now, as to not leave a dangling zombie mount
76 		 * point laying around in VFS.
77 		 */
78 		if (mp != NULL && !vfs_busy(mp, VB_WRITE|VB_WAIT))
79 			dounmount(mp, MNT_FORCE | MNT_DOOMED, p, NULL);
80 	}
81 
82 	if (vp->v_flag & VALIASED) {
83 		/*
84 		 * If a vgone (or vclean) is already in progress,
85 		 * wait until it is done and return.
86 		 */
87 		if (vp->v_flag & VXLOCK) {
88 			vp->v_flag |= VXWANT;
89 			tsleep(vp, PINOD, "vop_generic_revokeall", 0);
90 
91 			return(0);
92 		}
93 
94 		/*
95 		 * Ensure that vp will not be vgone'd while we
96 		 * are eliminating its aliases.
97 		 */
98 		vp->v_flag |= VXLOCK;
99 		while (vp->v_flag & VALIASED) {
100 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
101 				if (vq->v_rdev != vp->v_rdev ||
102 				    vq->v_type != vp->v_type || vp == vq)
103 					continue;
104 				vgone(vq);
105 				break;
106 			}
107 		}
108 
109 		/*
110 		 * Remove the lock so that vgone below will
111 		 * really eliminate the vnode after which time
112 		 * vgone will awaken any sleepers.
113 		 */
114 		vp->v_flag &= ~VXLOCK;
115 	}
116 
117 	vgonel(vp, p);
118 
119 	return (0);
120 }
121 
122 int
123 vop_generic_bmap(void *v)
124 {
125 	struct vop_bmap_args *ap = v;
126 
127 	if (ap->a_vpp)
128 		*ap->a_vpp = ap->a_vp;
129 	if (ap->a_bnp)
130 		*ap->a_bnp = ap->a_bn;
131 	if (ap->a_runp)
132 		*ap->a_runp = 0;
133 
134 	return (0);
135 }
136 
137 int
138 vop_generic_bwrite(void *v)
139 {
140 	struct vop_bwrite_args *ap = v;
141 
142 	return (bwrite(ap->a_bp));
143 }
144 
145 int
146 vop_generic_abortop(void *v)
147 {
148 	struct vop_abortop_args *ap = v;
149 
150 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
151 		pool_put(&namei_pool, ap->a_cnp->cn_pnbuf);
152 
153 	return (0);
154 }
155 
156 /*
157  * Stubs to use when there is no locking to be done on the underlying object.
158  * A minimal shared lock is necessary to ensure that the underlying object
159  * is not revoked while an operation is in progress. So, an active shared
160  * count should be maintained in an auxiliary vnode lock structure. However,
161  * that's not done now.
162  */
163 int
164 vop_generic_lock(void *v)
165 {
166 	return (0);
167 }
168 
169 /*
170  * Decrement the active use count. (Not done currently)
171  */
172 int
173 vop_generic_unlock(void *v)
174 {
175 	return (0);
176 }
177 
178 /*
179  * Return whether or not the node is in use. (Not done currently)
180  */
181 int
182 vop_generic_islocked(void *v)
183 {
184 	return (0);
185 }
186 
187 struct filterops generic_filtops =
188 	{ 1, NULL, filt_generic_detach, filt_generic_readwrite };
189 
190 int
191 vop_generic_kqfilter(void *v)
192 {
193 	struct vop_kqfilter_args *ap = v;
194 	struct knote *kn = ap->a_kn;
195 
196 	switch (kn->kn_filter) {
197 	case EVFILT_READ:
198 	case EVFILT_WRITE:
199 		kn->kn_fop = &generic_filtops;
200 		break;
201 	default:
202 		return (EINVAL);
203 	}
204 
205 	return (0);
206 }
207 
208 /* Trivial lookup routine that always fails. */
209 int
210 vop_generic_lookup(void *v)
211 {
212 	struct vop_lookup_args	*ap = v;
213 
214 	*ap->a_vpp = NULL;
215 	return (ENOTDIR);
216 }
217 
218 void
219 filt_generic_detach(struct knote *kn)
220 {
221 }
222 
223 int
224 filt_generic_readwrite(struct knote *kn, long hint)
225 {
226 	/*
227 	 * filesystem is gone, so set the EOF flag and schedule
228 	 * the knote for deletion.
229 	 */
230 	if (hint == NOTE_REVOKE) {
231 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
232 		return (1);
233 	}
234 
235         kn->kn_data = 0;
236 
237         return (1);
238 }
239