1 /* $NetBSD: mfs_vnops.c,v 1.56 2015/01/14 11:21:31 hannken 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.56 2015/01/14 11:21:31 hannken 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_fallocate_desc
, genfs_eopnotsupp
}, /* fallocate */
72 { &vop_fdiscard_desc
, genfs_eopnotsupp
}, /* fdiscard */
73 { &vop_ioctl_desc
, mfs_ioctl
}, /* ioctl */
74 { &vop_poll_desc
, mfs_poll
}, /* poll */
75 { &vop_revoke_desc
, mfs_revoke
}, /* revoke */
76 { &vop_mmap_desc
, mfs_mmap
}, /* mmap */
77 { &vop_fsync_desc
, spec_fsync
}, /* fsync */
78 { &vop_seek_desc
, mfs_seek
}, /* seek */
79 { &vop_remove_desc
, mfs_remove
}, /* remove */
80 { &vop_link_desc
, mfs_link
}, /* link */
81 { &vop_rename_desc
, mfs_rename
}, /* rename */
82 { &vop_mkdir_desc
, mfs_mkdir
}, /* mkdir */
83 { &vop_rmdir_desc
, mfs_rmdir
}, /* rmdir */
84 { &vop_symlink_desc
, mfs_symlink
}, /* symlink */
85 { &vop_readdir_desc
, mfs_readdir
}, /* readdir */
86 { &vop_readlink_desc
, mfs_readlink
}, /* readlink */
87 { &vop_abortop_desc
, mfs_abortop
}, /* abortop */
88 { &vop_inactive_desc
, mfs_inactive
}, /* inactive */
89 { &vop_reclaim_desc
, mfs_reclaim
}, /* reclaim */
90 { &vop_lock_desc
, genfs_nolock
}, /* lock */
91 { &vop_unlock_desc
, genfs_nounlock
}, /* unlock */
92 { &vop_bmap_desc
, mfs_bmap
}, /* bmap */
93 { &vop_strategy_desc
, mfs_strategy
}, /* strategy */
94 { &vop_print_desc
, mfs_print
}, /* print */
95 { &vop_islocked_desc
, mfs_islocked
}, /* islocked */
96 { &vop_pathconf_desc
, mfs_pathconf
}, /* pathconf */
97 { &vop_advlock_desc
, mfs_advlock
}, /* advlock */
98 { &vop_bwrite_desc
, mfs_bwrite
}, /* bwrite */
99 { &vop_putpages_desc
, mfs_putpages
}, /* putpages */
102 const struct vnodeopv_desc mfs_vnodeop_opv_desc
=
103 { &mfs_vnodeop_p
, mfs_vnodeop_entries
};
108 * Open called to allow memory filesystem to initialize and
109 * validate before actual IO. Record our process identifier
110 * so we can tell when we are doing I/O to ourself.
116 struct vop_open_args
/* {
122 if (ap
->a_vp
->v_type
!= VBLK
) {
123 panic("mfs_ioctl not VBLK");
130 * Pass I/O requests to the memory filesystem process.
133 mfs_strategy(void *v
)
135 struct vop_strategy_args
/* {
139 struct vnode
*vp
= ap
->a_vp
;
140 struct buf
*bp
= ap
->a_bp
;
141 struct mfsnode
*mfsp
;
143 if (vp
->v_type
!= VBLK
|| vp
->v_usecount
== 0)
144 panic("mfs_strategy: bad dev");
146 /* check for mini-root access */
147 if (mfsp
->mfs_proc
== NULL
) {
150 base
= (char *)mfsp
->mfs_baseoff
+ (bp
->b_blkno
<< DEV_BSHIFT
);
151 if (bp
->b_flags
& B_READ
)
152 memcpy(bp
->b_data
, base
, bp
->b_bcount
);
154 memcpy(base
, bp
->b_data
, bp
->b_bcount
);
157 } else if (mfsp
->mfs_proc
== curproc
) {
158 mfs_doio(bp
, mfsp
->mfs_baseoff
);
159 } else if (doing_shutdown
) {
161 * bitbucket I/O during shutdown.
162 * Note that reads should *not* happen here, but..
164 if (bp
->b_flags
& B_READ
)
165 printf("warning: mfs read during shutdown\n");
169 mutex_enter(&mfs_lock
);
170 bufq_put(mfsp
->mfs_buflist
, bp
);
171 cv_broadcast(&mfsp
->mfs_cv
);
172 mutex_exit(&mfs_lock
);
178 * Memory file system I/O.
181 mfs_doio(struct buf
*bp
, void *base
)
184 base
= (char *)base
+ (bp
->b_blkno
<< DEV_BSHIFT
);
185 if (bp
->b_flags
& B_READ
)
186 bp
->b_error
= copyin(base
, bp
->b_data
, bp
->b_bcount
);
188 bp
->b_error
= copyout(bp
->b_data
, base
, bp
->b_bcount
);
189 if (bp
->b_error
== 0)
195 * This is a noop, simply returning what one has been given.
200 struct vop_bmap_args
/* {
203 struct vnode **a_vpp;
208 if (ap
->a_vpp
!= NULL
)
209 *ap
->a_vpp
= ap
->a_vp
;
210 if (ap
->a_bnp
!= NULL
)
211 *ap
->a_bnp
= ap
->a_bn
;
212 if (ap
->a_runp
!= NULL
)
218 * Memory filesystem close routine
224 struct vop_close_args
/* {
229 struct vnode
*vp
= ap
->a_vp
;
230 struct mfsnode
*mfsp
= VTOMFS(vp
);
235 * Finish any pending I/O requests.
237 mutex_enter(&mfs_lock
);
238 while ((bp
= bufq_get(mfsp
->mfs_buflist
)) != NULL
) {
239 mutex_exit(&mfs_lock
);
240 mfs_doio(bp
, mfsp
->mfs_baseoff
);
241 mutex_enter(&mfs_lock
);
243 mutex_exit(&mfs_lock
);
245 * On last close of a memory filesystem
246 * we must invalidate any in core blocks, so that
247 * we can, free up its vnode.
249 if ((error
= vinvalbuf(vp
, V_SAVE
, ap
->a_cred
, curlwp
, 0, 0)) != 0)
252 * There should be no way to have any more uses of this
253 * vnode, so if we find any other uses, it is a panic.
255 if (bufq_peek(mfsp
->mfs_buflist
) != NULL
)
258 * Send a request to the filesystem server to exit.
260 mutex_enter(&mfs_lock
);
261 mfsp
->mfs_shutdown
= 1;
262 cv_broadcast(&mfsp
->mfs_cv
);
263 mutex_exit(&mfs_lock
);
268 * Memory filesystem inactive routine
272 mfs_inactive(void *v
)
274 struct vop_inactive_args
/* {
277 struct vnode
*vp
= ap
->a_vp
;
278 struct mfsnode
*mfsp
= VTOMFS(vp
);
280 if (bufq_peek(mfsp
->mfs_buflist
) != NULL
)
281 panic("mfs_inactive: not inactive (mfs_buflist %p)",
282 bufq_peek(mfsp
->mfs_buflist
));
284 return VOCALL(spec_vnodeop_p
, VOFFSET(vop_inactive
), ap
);
288 * Reclaim a memory filesystem devvp so that it can be reused.
293 struct vop_reclaim_args
/* {
296 struct vnode
*vp
= ap
->a_vp
;
297 struct mfsnode
*mfsp
= VTOMFS(vp
);
300 mutex_enter(&mfs_lock
);
302 refcnt
= --mfsp
->mfs_refcnt
;
303 mutex_exit(&mfs_lock
);
306 bufq_free(mfsp
->mfs_buflist
);
307 cv_destroy(&mfsp
->mfs_cv
);
308 kmem_free(mfsp
, sizeof(*mfsp
));
311 return VOCALL(spec_vnodeop_p
, VOFFSET(vop_reclaim
), ap
);
315 * Print out the contents of an mfsnode.
320 struct vop_print_args
/* {
323 struct mfsnode
*mfsp
= VTOMFS(ap
->a_vp
);
325 printf("tag VT_MFS, pid %d, base %p, size %ld\n",
326 (mfsp
->mfs_proc
!= NULL
) ? mfsp
->mfs_proc
->p_pid
: 0,
327 mfsp
->mfs_baseoff
, mfsp
->mfs_size
);