1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)spec_vnops.c 7.24 (Berkeley) 03/27/90 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "user.h" 23 #include "kernel.h" 24 #include "conf.h" 25 #include "buf.h" 26 #include "mount.h" 27 #include "vnode.h" 28 #include "specdev.h" 29 #include "stat.h" 30 #include "errno.h" 31 #include "ioctl.h" 32 #include "file.h" 33 #include "disklabel.h" 34 35 int spec_lookup(), 36 spec_open(), 37 spec_read(), 38 spec_write(), 39 spec_strategy(), 40 spec_bmap(), 41 spec_ioctl(), 42 spec_select(), 43 spec_lock(), 44 spec_unlock(), 45 spec_close(), 46 spec_print(), 47 spec_ebadf(), 48 spec_badop(), 49 spec_nullop(); 50 51 struct vnodeops spec_vnodeops = { 52 spec_lookup, /* lookup */ 53 spec_badop, /* create */ 54 spec_badop, /* mknod */ 55 spec_open, /* open */ 56 spec_close, /* close */ 57 spec_ebadf, /* access */ 58 spec_ebadf, /* getattr */ 59 spec_ebadf, /* setattr */ 60 spec_read, /* read */ 61 spec_write, /* write */ 62 spec_ioctl, /* ioctl */ 63 spec_select, /* select */ 64 spec_badop, /* mmap */ 65 spec_nullop, /* fsync */ 66 spec_badop, /* seek */ 67 spec_badop, /* remove */ 68 spec_badop, /* link */ 69 spec_badop, /* rename */ 70 spec_badop, /* mkdir */ 71 spec_badop, /* rmdir */ 72 spec_badop, /* symlink */ 73 spec_badop, /* readdir */ 74 spec_badop, /* readlink */ 75 spec_badop, /* abortop */ 76 spec_nullop, /* inactive */ 77 spec_nullop, /* reclaim */ 78 spec_lock, /* lock */ 79 spec_unlock, /* unlock */ 80 spec_bmap, /* bmap */ 81 spec_strategy, /* strategy */ 82 spec_print, /* print */ 83 spec_nullop, /* islocked */ 84 }; 85 86 /* 87 * Trivial lookup routine that always fails. 88 */ 89 spec_lookup(vp, ndp) 90 struct vnode *vp; 91 struct nameidata *ndp; 92 { 93 94 ndp->ni_dvp = vp; 95 ndp->ni_vp = NULL; 96 return (ENOTDIR); 97 } 98 99 /* 100 * Open called to allow handler 101 * of special files to initialize and 102 * validate before actual IO. 103 */ 104 /* ARGSUSED */ 105 spec_open(vp, mode, cred) 106 register struct vnode *vp; 107 int mode; 108 struct ucred *cred; 109 { 110 dev_t dev = (dev_t)vp->v_rdev; 111 register int maj = major(dev); 112 int error; 113 114 if (vp->v_mount && (vp->v_mount->m_flag & M_NODEV)) 115 return (ENXIO); 116 117 switch (vp->v_type) { 118 119 case VCHR: 120 if ((u_int)maj >= nchrdev) 121 return (ENXIO); 122 return ((*cdevsw[maj].d_open)(dev, mode, S_IFCHR)); 123 124 case VBLK: 125 if ((u_int)maj >= nblkdev) 126 return (ENXIO); 127 if (error = mountedon(vp)) 128 return (error); 129 return ((*bdevsw[maj].d_open)(dev, mode, S_IFBLK)); 130 } 131 return (0); 132 } 133 134 /* 135 * Vnode op for read 136 */ 137 spec_read(vp, uio, ioflag, cred) 138 register struct vnode *vp; 139 register struct uio *uio; 140 int ioflag; 141 struct ucred *cred; 142 { 143 struct buf *bp; 144 daddr_t bn; 145 long bsize, bscale; 146 struct partinfo dpart; 147 register int n, on; 148 int error = 0; 149 extern int mem_no; 150 151 if (uio->uio_rw != UIO_READ) 152 panic("spec_read mode"); 153 if (uio->uio_resid == 0) 154 return (0); 155 156 switch (vp->v_type) { 157 158 case VCHR: 159 /* 160 * Negative offsets allowed only for /dev/kmem 161 */ 162 if (uio->uio_offset < 0 && major(vp->v_rdev) != mem_no) 163 return (EINVAL); 164 VOP_UNLOCK(vp); 165 error = (*cdevsw[major(vp->v_rdev)].d_read) 166 (vp->v_rdev, uio, ioflag); 167 VOP_LOCK(vp); 168 return (error); 169 170 case VBLK: 171 if (uio->uio_offset < 0) 172 return (EINVAL); 173 bsize = BLKDEV_IOSIZE; 174 if ((*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, DIOCGPART, 175 (caddr_t)&dpart, FREAD) == 0) { 176 if (dpart.part->p_fstype == FS_BSDFFS && 177 dpart.part->p_frag != 0 && dpart.part->p_fsize != 0) 178 bsize = dpart.part->p_frag * 179 dpart.part->p_fsize; 180 } 181 bscale = bsize / DEV_BSIZE; 182 do { 183 bn = (uio->uio_offset / DEV_BSIZE) &~ (bscale - 1); 184 on = uio->uio_offset % bsize; 185 n = MIN((unsigned)(bsize - on), uio->uio_resid); 186 if (vp->v_lastr + bscale == bn) 187 error = breada(vp, bn, (int)bsize, bn + bscale, 188 (int)bsize, NOCRED, &bp); 189 else 190 error = bread(vp, bn, (int)bsize, NOCRED, &bp); 191 vp->v_lastr = bn; 192 n = MIN(n, bsize - bp->b_resid); 193 if (error) { 194 brelse(bp); 195 return (error); 196 } 197 error = uiomove(bp->b_un.b_addr + on, n, uio); 198 if (n + on == bsize) 199 bp->b_flags |= B_AGE; 200 brelse(bp); 201 } while (error == 0 && uio->uio_resid > 0 && n != 0); 202 return (error); 203 204 default: 205 panic("spec_read type"); 206 } 207 /* NOTREACHED */ 208 } 209 210 /* 211 * Vnode op for write 212 */ 213 spec_write(vp, uio, ioflag, cred) 214 register struct vnode *vp; 215 register struct uio *uio; 216 int ioflag; 217 struct ucred *cred; 218 { 219 struct buf *bp; 220 daddr_t bn; 221 int bsize, blkmask; 222 struct partinfo dpart; 223 register int n, on, i; 224 int count, error = 0; 225 extern int mem_no; 226 227 if (uio->uio_rw != UIO_WRITE) 228 panic("spec_write mode"); 229 230 switch (vp->v_type) { 231 232 case VCHR: 233 /* 234 * Negative offsets allowed only for /dev/kmem 235 */ 236 if (uio->uio_offset < 0 && major(vp->v_rdev) != mem_no) 237 return (EINVAL); 238 VOP_UNLOCK(vp); 239 error = (*cdevsw[major(vp->v_rdev)].d_write) 240 (vp->v_rdev, uio, ioflag); 241 VOP_LOCK(vp); 242 return (error); 243 244 case VBLK: 245 if (uio->uio_resid == 0) 246 return (0); 247 if (uio->uio_offset < 0) 248 return (EINVAL); 249 bsize = BLKDEV_IOSIZE; 250 if ((*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, DIOCGPART, 251 (caddr_t)&dpart, FREAD) == 0) { 252 if (dpart.part->p_fstype == FS_BSDFFS && 253 dpart.part->p_frag != 0 && dpart.part->p_fsize != 0) 254 bsize = dpart.part->p_frag * 255 dpart.part->p_fsize; 256 } 257 blkmask = (bsize / DEV_BSIZE) - 1; 258 do { 259 bn = (uio->uio_offset / DEV_BSIZE) &~ blkmask; 260 on = uio->uio_offset % bsize; 261 n = MIN((unsigned)(bsize - on), uio->uio_resid); 262 count = howmany(bsize, CLBYTES); 263 for (i = 0; i < count; i++) 264 munhash(vp, bn + i * (CLBYTES / DEV_BSIZE)); 265 if (n == bsize) 266 bp = getblk(vp, bn, bsize); 267 else 268 error = bread(vp, bn, bsize, NOCRED, &bp); 269 n = MIN(n, bsize - bp->b_resid); 270 if (error) { 271 brelse(bp); 272 return (error); 273 } 274 error = uiomove(bp->b_un.b_addr + on, n, uio); 275 if (n + on == bsize) { 276 bp->b_flags |= B_AGE; 277 bawrite(bp); 278 } else 279 bdwrite(bp); 280 } while (error == 0 && uio->uio_resid > 0 && n != 0); 281 return (error); 282 283 default: 284 panic("spec_write type"); 285 } 286 /* NOTREACHED */ 287 } 288 289 /* 290 * Device ioctl operation. 291 */ 292 /* ARGSUSED */ 293 spec_ioctl(vp, com, data, fflag, cred) 294 struct vnode *vp; 295 int com; 296 caddr_t data; 297 int fflag; 298 struct ucred *cred; 299 { 300 dev_t dev = vp->v_rdev; 301 302 switch (vp->v_type) { 303 304 case VCHR: 305 return ((*cdevsw[major(dev)].d_ioctl)(dev, com, data, fflag)); 306 307 case VBLK: 308 if (com == 0 && (int)data == B_TAPE) 309 if (bdevsw[major(dev)].d_flags & B_TAPE) 310 return (0); 311 else 312 return (1); 313 return ((*bdevsw[major(dev)].d_ioctl)(dev, com, data, fflag)); 314 315 default: 316 panic("spec_ioctl"); 317 /* NOTREACHED */ 318 } 319 } 320 321 /* ARGSUSED */ 322 spec_select(vp, which, fflags, cred) 323 struct vnode *vp; 324 int which, fflags; 325 struct ucred *cred; 326 { 327 register dev_t dev; 328 329 switch (vp->v_type) { 330 331 default: 332 return (1); /* XXX */ 333 334 case VCHR: 335 dev = vp->v_rdev; 336 return (*cdevsw[major(dev)].d_select)(dev, which); 337 } 338 } 339 340 /* 341 * Just call the device strategy routine 342 */ 343 spec_strategy(bp) 344 register struct buf *bp; 345 { 346 347 (*bdevsw[major(bp->b_dev)].d_strategy)(bp); 348 return (0); 349 } 350 351 /* 352 * This is a noop, simply returning what one has been given. 353 */ 354 spec_bmap(vp, bn, vpp, bnp) 355 struct vnode *vp; 356 daddr_t bn; 357 struct vnode **vpp; 358 daddr_t *bnp; 359 { 360 361 if (vpp != NULL) 362 *vpp = vp; 363 if (bnp != NULL) 364 *bnp = bn; 365 return (0); 366 } 367 368 /* 369 * At the moment we do not do any locking. 370 */ 371 /* ARGSUSED */ 372 spec_lock(vp) 373 struct vnode *vp; 374 { 375 376 return (0); 377 } 378 379 /* ARGSUSED */ 380 spec_unlock(vp) 381 struct vnode *vp; 382 { 383 384 return (0); 385 } 386 387 /* 388 * Device close routine 389 */ 390 /* ARGSUSED */ 391 spec_close(vp, flag, cred) 392 register struct vnode *vp; 393 int flag; 394 struct ucred *cred; 395 { 396 dev_t dev = vp->v_rdev; 397 int (*cfunc)(); 398 int error, mode; 399 400 switch (vp->v_type) { 401 402 case VCHR: 403 /* 404 * If the vnode is locked, then we are in the midst 405 * of forcably closing the device, otherwise we only 406 * close on last reference. 407 */ 408 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0) 409 return (0); 410 cfunc = cdevsw[major(dev)].d_close; 411 mode = S_IFCHR; 412 break; 413 414 case VBLK: 415 /* 416 * On last close of a block device (that isn't mounted) 417 * we must invalidate any in core blocks, so that 418 * we can, for instance, change floppy disks. 419 */ 420 vflushbuf(vp, 0); 421 if (vinvalbuf(vp, 1)) 422 return (0); 423 /* 424 * We do not want to really close the device if it 425 * is still in use unless we are trying to close it 426 * forcibly. Since every use (buffer, vnode, swap, cmap) 427 * holds a reference to the vnode, and because we mark 428 * any other vnodes that alias this device, when the 429 * sum of the reference counts on all the aliased 430 * vnodes descends to one, we are on last close. 431 */ 432 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0) 433 return (0); 434 cfunc = bdevsw[major(dev)].d_close; 435 mode = S_IFBLK; 436 break; 437 438 default: 439 panic("spec_close: not special"); 440 } 441 442 if (setjmp(&u.u_qsave)) { 443 /* 444 * If device close routine is interrupted, 445 * must return so closef can clean up. 446 */ 447 error = EINTR; 448 } else 449 error = (*cfunc)(dev, flag, mode); 450 return (error); 451 } 452 453 /* 454 * Print out the contents of a special device vnode. 455 */ 456 spec_print(vp) 457 struct vnode *vp; 458 { 459 460 printf("tag VT_NON, dev %d, %d\n", major(vp->v_rdev), 461 minor(vp->v_rdev)); 462 } 463 464 /* 465 * Special device failed operation 466 */ 467 spec_ebadf() 468 { 469 470 return (EBADF); 471 } 472 473 /* 474 * Special device bad operation 475 */ 476 spec_badop() 477 { 478 479 panic("spec_badop called"); 480 /* NOTREACHED */ 481 } 482 483 /* 484 * Special device null operation 485 */ 486 spec_nullop() 487 { 488 489 return (0); 490 } 491