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