1 /* $NetBSD: mfs_vnops.c,v 1.52 2008/06/02 00:24:28 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)mfs_vnops.c 8.11 (Berkeley) 5/22/95
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: mfs_vnops.c,v 1.52 2008/06/02 00:24:28 christos Exp $");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
44 #include <sys/vnode.h>
47 #include <miscfs/genfs/genfs.h>
48 #include <miscfs/specfs/specdev.h>
50 #include <machine/vmparam.h>
52 #include <ufs/mfs/mfsnode.h>
53 #include <ufs/mfs/mfs_extern.h>
56 * mfs vnode operations.
58 int (**mfs_vnodeop_p
)(void *);
59 const struct vnodeopv_entry_desc mfs_vnodeop_entries
[] = {
60 { &vop_default_desc
, vn_default_error
},
61 { &vop_lookup_desc
, mfs_lookup
}, /* lookup */
62 { &vop_create_desc
, mfs_create
}, /* create */
63 { &vop_mknod_desc
, mfs_mknod
}, /* mknod */
64 { &vop_open_desc
, mfs_open
}, /* open */
65 { &vop_close_desc
, mfs_close
}, /* close */
66 { &vop_access_desc
, mfs_access
}, /* access */
67 { &vop_getattr_desc
, mfs_getattr
}, /* getattr */
68 { &vop_setattr_desc
, mfs_setattr
}, /* setattr */
69 { &vop_read_desc
, mfs_read
}, /* read */
70 { &vop_write_desc
, mfs_write
}, /* write */
71 { &vop_ioctl_desc
, mfs_ioctl
}, /* ioctl */
72 { &vop_poll_desc
, mfs_poll
}, /* poll */
73 { &vop_revoke_desc
, mfs_revoke
}, /* revoke */
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
, genfs_nolock
}, /* lock */
89 { &vop_unlock_desc
, genfs_nounlock
}, /* 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_bwrite_desc
, mfs_bwrite
}, /* bwrite */
97 { &vop_putpages_desc
, mfs_putpages
}, /* putpages */
100 const struct vnodeopv_desc mfs_vnodeop_opv_desc
=
101 { &mfs_vnodeop_p
, mfs_vnodeop_entries
};
106 * Open called to allow memory filesystem to initialize and
107 * validate before actual IO. Record our process identifier
108 * so we can tell when we are doing I/O to ourself.
114 struct vop_open_args
/* {
120 if (ap
->a_vp
->v_type
!= VBLK
) {
121 panic("mfs_ioctl not VBLK");
128 * Pass I/O requests to the memory filesystem process.
131 mfs_strategy(void *v
)
133 struct vop_strategy_args
/* {
137 struct vnode
*vp
= ap
->a_vp
;
138 struct buf
*bp
= ap
->a_bp
;
139 struct mfsnode
*mfsp
;
141 if (vp
->v_type
!= VBLK
|| vp
->v_usecount
== 0)
142 panic("mfs_strategy: bad dev");
144 /* check for mini-root access */
145 if (mfsp
->mfs_proc
== NULL
) {
148 base
= (char *)mfsp
->mfs_baseoff
+ (bp
->b_blkno
<< DEV_BSHIFT
);
149 if (bp
->b_flags
& B_READ
)
150 memcpy(bp
->b_data
, base
, bp
->b_bcount
);
152 memcpy(base
, bp
->b_data
, bp
->b_bcount
);
155 } else if (mfsp
->mfs_proc
== curproc
) {
156 mfs_doio(bp
, mfsp
->mfs_baseoff
);
157 } else if (doing_shutdown
) {
159 * bitbucket I/O during shutdown.
160 * Note that reads should *not* happen here, but..
162 if (bp
->b_flags
& B_READ
)
163 printf("warning: mfs read during shutdown\n");
167 mutex_enter(&mfs_lock
);
168 bufq_put(mfsp
->mfs_buflist
, bp
);
169 cv_broadcast(&mfsp
->mfs_cv
);
170 mutex_exit(&mfs_lock
);
176 * Memory file system I/O.
179 mfs_doio(struct buf
*bp
, void *base
)
182 base
= (char *)base
+ (bp
->b_blkno
<< DEV_BSHIFT
);
183 if (bp
->b_flags
& B_READ
)
184 bp
->b_error
= copyin(base
, bp
->b_data
, bp
->b_bcount
);
186 bp
->b_error
= copyout(bp
->b_data
, base
, bp
->b_bcount
);
187 if (bp
->b_error
== 0)
193 * This is a noop, simply returning what one has been given.
198 struct vop_bmap_args
/* {
201 struct vnode **a_vpp;
206 if (ap
->a_vpp
!= NULL
)
207 *ap
->a_vpp
= ap
->a_vp
;
208 if (ap
->a_bnp
!= NULL
)
209 *ap
->a_bnp
= ap
->a_bn
;
210 if (ap
->a_runp
!= NULL
)
216 * Memory filesystem close routine
222 struct vop_close_args
/* {
227 struct vnode
*vp
= ap
->a_vp
;
228 struct mfsnode
*mfsp
= VTOMFS(vp
);
233 * Finish any pending I/O requests.
235 mutex_enter(&mfs_lock
);
236 while ((bp
= bufq_get(mfsp
->mfs_buflist
)) != NULL
) {
237 mutex_exit(&mfs_lock
);
238 mfs_doio(bp
, mfsp
->mfs_baseoff
);
239 mutex_enter(&mfs_lock
);
241 mutex_exit(&mfs_lock
);
243 * On last close of a memory filesystem
244 * we must invalidate any in core blocks, so that
245 * we can, free up its vnode.
247 if ((error
= vinvalbuf(vp
, V_SAVE
, ap
->a_cred
, curlwp
, 0, 0)) != 0)
250 * There should be no way to have any more uses of this
251 * vnode, so if we find any other uses, it is a panic.
253 if (bufq_peek(mfsp
->mfs_buflist
) != NULL
)
256 * Send a request to the filesystem server to exit.
258 mutex_enter(&mfs_lock
);
259 mfsp
->mfs_shutdown
= 1;
260 cv_broadcast(&mfsp
->mfs_cv
);
261 mutex_exit(&mfs_lock
);
266 * Memory filesystem inactive routine
270 mfs_inactive(void *v
)
272 struct vop_inactive_args
/* {
275 struct vnode
*vp
= ap
->a_vp
;
276 struct mfsnode
*mfsp
= VTOMFS(vp
);
278 if (bufq_peek(mfsp
->mfs_buflist
) != NULL
)
279 panic("mfs_inactive: not inactive (mfs_buflist %p)",
280 bufq_peek(mfsp
->mfs_buflist
));
286 * Reclaim a memory filesystem devvp so that it can be reused.
291 struct vop_reclaim_args
/* {
294 struct vnode
*vp
= ap
->a_vp
;
295 struct mfsnode
*mfsp
= VTOMFS(vp
);
298 mutex_enter(&mfs_lock
);
300 refcnt
= --mfsp
->mfs_refcnt
;
301 mutex_exit(&mfs_lock
);
304 bufq_free(mfsp
->mfs_buflist
);
305 cv_destroy(&mfsp
->mfs_cv
);
306 kmem_free(mfsp
, sizeof(*mfsp
));
313 * Print out the contents of an mfsnode.
318 struct vop_print_args
/* {
321 struct mfsnode
*mfsp
= VTOMFS(ap
->a_vp
);
323 printf("tag VT_MFS, pid %d, base %p, size %ld\n",
324 (mfsp
->mfs_proc
!= NULL
) ? mfsp
->mfs_proc
->p_pid
: 0,
325 mfsp
->mfs_baseoff
, mfsp
->mfs_size
);