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