mfs_vnops.c revision 1.27.2.4 1 /* $NetBSD: mfs_vnops.c,v 1.27.2.4 2002/06/24 22:12:42 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)mfs_vnops.c 8.11 (Berkeley) 5/22/95
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: mfs_vnops.c,v 1.27.2.4 2002/06/24 22:12:42 nathanw Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/lwp.h>
46 #include <sys/proc.h>
47 #include <sys/buf.h>
48 #include <sys/map.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51
52 #include <miscfs/genfs/genfs.h>
53 #include <miscfs/specfs/specdev.h>
54
55 #include <machine/vmparam.h>
56
57 #include <ufs/mfs/mfsnode.h>
58 #include <ufs/mfs/mfs_extern.h>
59
60 /*
61 * mfs vnode operations.
62 */
63 int (**mfs_vnodeop_p) __P((void *));
64 const struct vnodeopv_entry_desc mfs_vnodeop_entries[] = {
65 { &vop_default_desc, vn_default_error },
66 { &vop_lookup_desc, mfs_lookup }, /* lookup */
67 { &vop_create_desc, mfs_create }, /* create */
68 { &vop_mknod_desc, mfs_mknod }, /* mknod */
69 { &vop_open_desc, mfs_open }, /* open */
70 { &vop_close_desc, mfs_close }, /* close */
71 { &vop_access_desc, mfs_access }, /* access */
72 { &vop_getattr_desc, mfs_getattr }, /* getattr */
73 { &vop_setattr_desc, mfs_setattr }, /* setattr */
74 { &vop_read_desc, mfs_read }, /* read */
75 { &vop_write_desc, mfs_write }, /* write */
76 { &vop_ioctl_desc, mfs_ioctl }, /* ioctl */
77 { &vop_poll_desc, mfs_poll }, /* poll */
78 { &vop_revoke_desc, mfs_revoke }, /* revoke */
79 { &vop_mmap_desc, mfs_mmap }, /* mmap */
80 { &vop_fsync_desc, spec_fsync }, /* fsync */
81 { &vop_seek_desc, mfs_seek }, /* seek */
82 { &vop_remove_desc, mfs_remove }, /* remove */
83 { &vop_link_desc, mfs_link }, /* link */
84 { &vop_rename_desc, mfs_rename }, /* rename */
85 { &vop_mkdir_desc, mfs_mkdir }, /* mkdir */
86 { &vop_rmdir_desc, mfs_rmdir }, /* rmdir */
87 { &vop_symlink_desc, mfs_symlink }, /* symlink */
88 { &vop_readdir_desc, mfs_readdir }, /* readdir */
89 { &vop_readlink_desc, mfs_readlink }, /* readlink */
90 { &vop_abortop_desc, mfs_abortop }, /* abortop */
91 { &vop_inactive_desc, mfs_inactive }, /* inactive */
92 { &vop_reclaim_desc, mfs_reclaim }, /* reclaim */
93 { &vop_lock_desc, mfs_lock }, /* lock */
94 { &vop_unlock_desc, mfs_unlock }, /* unlock */
95 { &vop_bmap_desc, mfs_bmap }, /* bmap */
96 { &vop_strategy_desc, mfs_strategy }, /* strategy */
97 { &vop_print_desc, mfs_print }, /* print */
98 { &vop_islocked_desc, mfs_islocked }, /* islocked */
99 { &vop_pathconf_desc, mfs_pathconf }, /* pathconf */
100 { &vop_advlock_desc, mfs_advlock }, /* advlock */
101 { &vop_blkatoff_desc, mfs_blkatoff }, /* blkatoff */
102 { &vop_valloc_desc, mfs_valloc }, /* valloc */
103 { &vop_vfree_desc, mfs_vfree }, /* vfree */
104 { &vop_truncate_desc, mfs_truncate }, /* truncate */
105 { &vop_update_desc, mfs_update }, /* update */
106 { &vop_bwrite_desc, mfs_bwrite }, /* bwrite */
107 { &vop_putpages_desc, mfs_putpages }, /* putpages */
108 { NULL, NULL }
109 };
110 const struct vnodeopv_desc mfs_vnodeop_opv_desc =
111 { &mfs_vnodeop_p, mfs_vnodeop_entries };
112
113 /*
114 * Vnode Operations.
115 *
116 * Open called to allow memory filesystem to initialize and
117 * validate before actual IO. Record our process identifier
118 * so we can tell when we are doing I/O to ourself.
119 */
120 /* ARGSUSED */
121 int
122 mfs_open(v)
123 void *v;
124 {
125 struct vop_open_args /* {
126 struct vnode *a_vp;
127 int a_mode;
128 struct ucred *a_cred;
129 struct proc *a_p;
130 } */ *ap = v;
131
132 if (ap->a_vp->v_type != VBLK) {
133 panic("mfs_ioctl not VBLK");
134 /* NOTREACHED */
135 }
136 return (0);
137 }
138
139 /*
140 * Pass I/O requests to the memory filesystem process.
141 */
142 int
143 mfs_strategy(v)
144 void *v;
145 {
146 struct vop_strategy_args /* {
147 struct buf *a_bp;
148 } */ *ap = v;
149 struct buf *bp = ap->a_bp;
150 struct mfsnode *mfsp;
151 struct vnode *vp;
152 struct proc *p = curproc; /* XXX */
153
154 if (!vfinddev(bp->b_dev, VBLK, &vp) || vp->v_usecount == 0)
155 panic("mfs_strategy: bad dev");
156 mfsp = VTOMFS(vp);
157 /* check for mini-root access */
158 if (mfsp->mfs_proc == NULL) {
159 caddr_t base;
160
161 base = mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
162 if (bp->b_flags & B_READ)
163 memcpy(bp->b_data, base, bp->b_bcount);
164 else
165 memcpy(base, bp->b_data, bp->b_bcount);
166 bp->b_resid = 0;
167 biodone(bp);
168 } else if (mfsp->mfs_proc == p) {
169 mfs_doio(bp, mfsp->mfs_baseoff);
170 } else if (doing_shutdown) {
171 /*
172 * bitbucket I/O during shutdown.
173 * Note that reads should *not* happen here, but..
174 */
175 if (bp->b_flags & B_READ)
176 printf("warning: mfs read during shutdown\n");
177 bp->b_resid = 0;
178 biodone(bp);
179 } else {
180 BUFQ_INSERT_TAIL(&mfsp->mfs_buflist, bp);
181 wakeup((caddr_t)vp);
182 }
183 return (0);
184 }
185
186 /*
187 * Memory file system I/O.
188 *
189 * Trivial on the HP since buffer has already been mapping into KVA space.
190 */
191 void
192 mfs_doio(bp, base)
193 struct buf *bp;
194 caddr_t base;
195 {
196 base += (bp->b_blkno << DEV_BSHIFT);
197 if (bp->b_flags & B_READ)
198 bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
199 else
200 bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
201 if (bp->b_error)
202 bp->b_flags |= B_ERROR;
203 else
204 bp->b_resid = 0;
205 biodone(bp);
206 }
207
208 /*
209 * This is a noop, simply returning what one has been given.
210 */
211 int
212 mfs_bmap(v)
213 void *v;
214 {
215 struct vop_bmap_args /* {
216 struct vnode *a_vp;
217 daddr_t a_bn;
218 struct vnode **a_vpp;
219 daddr_t *a_bnp;
220 int *a_runp;
221 } */ *ap = v;
222
223 if (ap->a_vpp != NULL)
224 *ap->a_vpp = ap->a_vp;
225 if (ap->a_bnp != NULL)
226 *ap->a_bnp = ap->a_bn;
227 if (ap->a_runp != NULL)
228 *ap->a_runp = 0;
229 return (0);
230 }
231
232 /*
233 * Memory filesystem close routine
234 */
235 /* ARGSUSED */
236 int
237 mfs_close(v)
238 void *v;
239 {
240 struct vop_close_args /* {
241 struct vnode *a_vp;
242 int a_fflag;
243 struct ucred *a_cred;
244 struct proc *a_p;
245 } */ *ap = v;
246 struct vnode *vp = ap->a_vp;
247 struct mfsnode *mfsp = VTOMFS(vp);
248 struct buf *bp;
249 int error;
250
251 /*
252 * Finish any pending I/O requests.
253 */
254 while ((bp = BUFQ_FIRST(&mfsp->mfs_buflist)) != NULL) {
255 BUFQ_REMOVE(&mfsp->mfs_buflist, bp);
256 mfs_doio(bp, mfsp->mfs_baseoff);
257 wakeup((caddr_t)bp);
258 }
259 /*
260 * On last close of a memory filesystem
261 * we must invalidate any in core blocks, so that
262 * we can, free up its vnode.
263 */
264 if ((error = vinvalbuf(vp, 1, ap->a_cred, ap->a_p, 0, 0)) != 0)
265 return (error);
266 /*
267 * There should be no way to have any more uses of this
268 * vnode, so if we find any other uses, it is a panic.
269 */
270 if (vp->v_usecount > 1)
271 printf("mfs_close: ref count %d > 1\n", vp->v_usecount);
272 if (vp->v_usecount > 1 || BUFQ_FIRST(&mfsp->mfs_buflist) != NULL)
273 panic("mfs_close");
274 /*
275 * Send a request to the filesystem server to exit.
276 */
277 BUFQ_FIRST(&mfsp->mfs_buflist) = (struct buf *) -1;
278 wakeup((caddr_t)vp);
279 return (0);
280 }
281
282 /*
283 * Memory filesystem inactive routine
284 */
285 /* ARGSUSED */
286 int
287 mfs_inactive(v)
288 void *v;
289 {
290 struct vop_inactive_args /* {
291 struct vnode *a_vp;
292 struct proc *a_p;
293 } */ *ap = v;
294 struct vnode *vp = ap->a_vp;
295 struct mfsnode *mfsp = VTOMFS(vp);
296
297 if (BUFQ_FIRST(&mfsp->mfs_buflist) != NULL &&
298 BUFQ_FIRST(&mfsp->mfs_buflist) != (struct buf *) -1)
299 panic("mfs_inactive: not inactive (mfs_buflist %p)",
300 BUFQ_FIRST(&mfsp->mfs_buflist));
301 VOP_UNLOCK(vp, 0);
302 return (0);
303 }
304
305 /*
306 * Reclaim a memory filesystem devvp so that it can be reused.
307 */
308 int
309 mfs_reclaim(v)
310 void *v;
311 {
312 struct vop_reclaim_args /* {
313 struct vnode *a_vp;
314 } */ *ap = v;
315 struct vnode *vp = ap->a_vp;
316
317 FREE(vp->v_data, M_MFSNODE);
318 vp->v_data = NULL;
319 return (0);
320 }
321
322 /*
323 * Print out the contents of an mfsnode.
324 */
325 int
326 mfs_print(v)
327 void *v;
328 {
329 struct vop_print_args /* {
330 struct vnode *a_vp;
331 } */ *ap = v;
332 struct mfsnode *mfsp = VTOMFS(ap->a_vp);
333
334 printf("tag VT_MFS, pid %d, base %p, size %ld\n",
335 (mfsp->mfs_proc != NULL) ? mfsp->mfs_proc->p_pid : 0,
336 mfsp->mfs_baseoff, mfsp->mfs_size);
337 return (0);
338 }
339