mfs_vnops.c revision 1.8 1 /* $NetBSD: mfs_vnops.c,v 1.8 1996/03/17 02:16:32 christos 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.5 (Berkeley) 7/28/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/time.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/buf.h>
44 #include <sys/map.h>
45 #include <sys/vnode.h>
46 #include <sys/malloc.h>
47
48 #include <miscfs/specfs/specdev.h>
49
50 #include <machine/vmparam.h>
51
52 #include <ufs/mfs/mfsnode.h>
53 #include <ufs/mfs/mfsiom.h>
54 #include <ufs/mfs/mfs_extern.h>
55
56 /*
57 * mfs vnode operations.
58 */
59 int (**mfs_vnodeop_p) __P((void *));
60 struct vnodeopv_entry_desc mfs_vnodeop_entries[] = {
61 { &vop_default_desc, vn_default_error },
62 { &vop_lookup_desc, mfs_lookup }, /* lookup */
63 { &vop_create_desc, mfs_create }, /* create */
64 { &vop_mknod_desc, mfs_mknod }, /* mknod */
65 { &vop_open_desc, mfs_open }, /* open */
66 { &vop_close_desc, mfs_close }, /* close */
67 { &vop_access_desc, mfs_access }, /* access */
68 { &vop_getattr_desc, mfs_getattr }, /* getattr */
69 { &vop_setattr_desc, mfs_setattr }, /* setattr */
70 { &vop_read_desc, mfs_read }, /* read */
71 { &vop_write_desc, mfs_write }, /* write */
72 { &vop_ioctl_desc, mfs_ioctl }, /* ioctl */
73 { &vop_select_desc, mfs_select }, /* select */
74 { &vop_mmap_desc, mfs_mmap }, /* mmap */
75 { &vop_fsync_desc, spec_fsync }, /* fsync */
76 { &vop_seek_desc, mfs_seek }, /* seek */
77 { &vop_remove_desc, mfs_remove }, /* remove */
78 { &vop_link_desc, mfs_link }, /* link */
79 { &vop_rename_desc, mfs_rename }, /* rename */
80 { &vop_mkdir_desc, mfs_mkdir }, /* mkdir */
81 { &vop_rmdir_desc, mfs_rmdir }, /* rmdir */
82 { &vop_symlink_desc, mfs_symlink }, /* symlink */
83 { &vop_readdir_desc, mfs_readdir }, /* readdir */
84 { &vop_readlink_desc, mfs_readlink }, /* readlink */
85 { &vop_abortop_desc, mfs_abortop }, /* abortop */
86 { &vop_inactive_desc, mfs_inactive }, /* inactive */
87 { &vop_reclaim_desc, mfs_reclaim }, /* reclaim */
88 { &vop_lock_desc, mfs_lock }, /* lock */
89 { &vop_unlock_desc, mfs_unlock }, /* unlock */
90 { &vop_bmap_desc, mfs_bmap }, /* bmap */
91 { &vop_strategy_desc, mfs_strategy }, /* strategy */
92 { &vop_print_desc, mfs_print }, /* print */
93 { &vop_islocked_desc, mfs_islocked }, /* islocked */
94 { &vop_pathconf_desc, mfs_pathconf }, /* pathconf */
95 { &vop_advlock_desc, mfs_advlock }, /* advlock */
96 { &vop_blkatoff_desc, mfs_blkatoff }, /* blkatoff */
97 { &vop_valloc_desc, mfs_valloc }, /* valloc */
98 { &vop_vfree_desc, mfs_vfree }, /* vfree */
99 { &vop_truncate_desc, mfs_truncate }, /* truncate */
100 { &vop_update_desc, mfs_update }, /* update */
101 { &vop_bwrite_desc, mfs_bwrite }, /* bwrite */
102 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
103 };
104 struct vnodeopv_desc mfs_vnodeop_opv_desc =
105 { &mfs_vnodeop_p, mfs_vnodeop_entries };
106
107 /*
108 * Vnode Operations.
109 *
110 * Open called to allow memory filesystem to initialize and
111 * validate before actual IO. Record our process identifier
112 * so we can tell when we are doing I/O to ourself.
113 */
114 /* ARGSUSED */
115 int
116 mfs_open(v)
117 void *v;
118 {
119 struct vop_open_args /* {
120 struct vnode *a_vp;
121 int a_mode;
122 struct ucred *a_cred;
123 struct proc *a_p;
124 } */ *ap = v;
125
126 if (ap->a_vp->v_type != VBLK) {
127 panic("mfs_ioctl not VBLK");
128 /* NOTREACHED */
129 }
130 return (0);
131 }
132
133 /*
134 * Ioctl operation.
135 */
136 /* ARGSUSED */
137 int
138 mfs_ioctl(v)
139 void *v;
140 {
141 #if 0
142 struct vop_ioctl_args /* {
143 struct vnode *a_vp;
144 u_long a_command;
145 caddr_t a_data;
146 int a_fflag;
147 struct ucred *a_cred;
148 struct proc *a_p;
149 } */ *ap = v;
150 #endif
151
152 return (ENOTTY);
153 }
154
155 /*
156 * Pass I/O requests to the memory filesystem process.
157 */
158 int
159 mfs_strategy(v)
160 void *v;
161 {
162 struct vop_strategy_args /* {
163 struct buf *a_bp;
164 } */ *ap = v;
165 register struct buf *bp = ap->a_bp;
166 register struct mfsnode *mfsp;
167 struct vnode *vp;
168 struct proc *p = curproc; /* XXX */
169
170 if (!vfinddev(bp->b_dev, VBLK, &vp) || vp->v_usecount == 0)
171 panic("mfs_strategy: bad dev");
172 mfsp = VTOMFS(vp);
173 /* check for mini-root access */
174 if (mfsp->mfs_pid == 0) {
175 caddr_t base;
176
177 base = mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
178 if (bp->b_flags & B_READ)
179 bcopy(base, bp->b_data, bp->b_bcount);
180 else
181 bcopy(bp->b_data, base, bp->b_bcount);
182 biodone(bp);
183 } else if (mfsp->mfs_pid == p->p_pid) {
184 mfs_doio(bp, mfsp->mfs_baseoff);
185 } else {
186 bp->b_actf = mfsp->mfs_buflist;
187 mfsp->mfs_buflist = bp;
188 wakeup((caddr_t)vp);
189 }
190 return (0);
191 }
192
193 /*
194 * Memory file system I/O.
195 *
196 * Trivial on the HP since buffer has already been mapping into KVA space.
197 */
198 void
199 mfs_doio(bp, base)
200 register struct buf *bp;
201 caddr_t base;
202 {
203
204 base += (bp->b_blkno << DEV_BSHIFT);
205 if (bp->b_flags & B_READ)
206 bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
207 else
208 bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
209 if (bp->b_error)
210 bp->b_flags |= B_ERROR;
211 biodone(bp);
212 }
213
214 /*
215 * This is a noop, simply returning what one has been given.
216 */
217 int
218 mfs_bmap(v)
219 void *v;
220 {
221 struct vop_bmap_args /* {
222 struct vnode *a_vp;
223 daddr_t a_bn;
224 struct vnode **a_vpp;
225 daddr_t *a_bnp;
226 int *a_runp;
227 } */ *ap = v;
228
229 if (ap->a_vpp != NULL)
230 *ap->a_vpp = ap->a_vp;
231 if (ap->a_bnp != NULL)
232 *ap->a_bnp = ap->a_bn;
233 return (0);
234 }
235
236 /*
237 * Memory filesystem close routine
238 */
239 /* ARGSUSED */
240 int
241 mfs_close(v)
242 void *v;
243 {
244 struct vop_close_args /* {
245 struct vnode *a_vp;
246 int a_fflag;
247 struct ucred *a_cred;
248 struct proc *a_p;
249 } */ *ap = v;
250 register struct vnode *vp = ap->a_vp;
251 register struct mfsnode *mfsp = VTOMFS(vp);
252 register struct buf *bp;
253 int error;
254
255 /*
256 * Finish any pending I/O requests.
257 */
258 while ((bp = mfsp->mfs_buflist) != NULL) {
259 mfsp->mfs_buflist = bp->b_actf;
260 mfs_doio(bp, mfsp->mfs_baseoff);
261 wakeup((caddr_t)bp);
262 }
263 /*
264 * On last close of a memory filesystem
265 * we must invalidate any in core blocks, so that
266 * we can, free up its vnode.
267 */
268 if ((error = vinvalbuf(vp, 1, ap->a_cred, ap->a_p, 0, 0)) != 0)
269 return (error);
270 /*
271 * There should be no way to have any more uses of this
272 * vnode, so if we find any other uses, it is a panic.
273 */
274 if (vp->v_usecount > 1)
275 printf("mfs_close: ref count %d > 1\n", vp->v_usecount);
276 if (vp->v_usecount > 1 || mfsp->mfs_buflist)
277 panic("mfs_close");
278 /*
279 * Send a request to the filesystem server to exit.
280 */
281 mfsp->mfs_buflist = (struct buf *)(-1);
282 wakeup((caddr_t)vp);
283 return (0);
284 }
285
286 /*
287 * Memory filesystem inactive routine
288 */
289 /* ARGSUSED */
290 int
291 mfs_inactive(v)
292 void *v;
293 {
294 struct vop_inactive_args /* {
295 struct vnode *a_vp;
296 } */ *ap = v;
297 register struct mfsnode *mfsp = VTOMFS(ap->a_vp);
298
299 if (mfsp->mfs_buflist && mfsp->mfs_buflist != (struct buf *)(-1))
300 panic("mfs_inactive: not inactive (mfs_buflist %p)",
301 mfsp->mfs_buflist);
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 register 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 register struct mfsnode *mfsp = VTOMFS(ap->a_vp);
333
334 printf("tag VT_MFS, pid %d, base %p, size %ld\n", mfsp->mfs_pid,
335 mfsp->mfs_baseoff, mfsp->mfs_size);
336 return (0);
337 }
338
339 /*
340 * Block device bad operation
341 */
342 int
343 mfs_badop(v)
344 void *v;
345 {
346
347 panic("mfs_badop called\n");
348 /* NOTREACHED */
349 }
350
351 /*
352 * Memory based filesystem initialization.
353 */
354 void
355 mfs_init()
356 {
357
358 }
359