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