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