1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 2009 The DragonFly Project. All rights reserved. 5 * 6 * This code is derived from software contributed to The DragonFly Project 7 * by Alex Hornung <ahornung@gmail.com> 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 3. Neither the name of The DragonFly Project nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific, prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/time.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/fcntl.h> 42 #include <sys/proc.h> 43 #include <sys/priv.h> 44 #include <sys/signalvar.h> 45 #include <sys/vnode.h> 46 #include <sys/uio.h> 47 #include <sys/mount.h> 48 #include <sys/file.h> 49 #include <sys/namei.h> 50 #include <sys/dirent.h> 51 #include <sys/malloc.h> 52 #include <sys/stat.h> 53 #include <sys/reg.h> 54 #include <vm/vm_pager.h> 55 #include <vm/vm_zone.h> 56 #include <vm/vm_object.h> 57 #include <sys/filio.h> 58 #include <sys/ttycom.h> 59 #include <sys/tty.h> 60 #include <sys/diskslice.h> 61 #include <sys/sysctl.h> 62 #include <sys/devfs.h> 63 #include <sys/pioctl.h> 64 #include <vfs/fifofs/fifo.h> 65 66 #include <machine/limits.h> 67 68 #include <sys/buf2.h> 69 #include <vm/vm_page2.h> 70 71 #ifndef SPEC_CHAIN_DEBUG 72 #define SPEC_CHAIN_DEBUG 0 73 #endif 74 75 MALLOC_DECLARE(M_DEVFS); 76 #define DEVFS_BADOP (void *)devfs_vop_badop 77 78 static int devfs_vop_badop(struct vop_generic_args *); 79 static int devfs_vop_access(struct vop_access_args *); 80 static int devfs_vop_inactive(struct vop_inactive_args *); 81 static int devfs_vop_reclaim(struct vop_reclaim_args *); 82 static int devfs_vop_readdir(struct vop_readdir_args *); 83 static int devfs_vop_getattr(struct vop_getattr_args *); 84 static int devfs_vop_setattr(struct vop_setattr_args *); 85 static int devfs_vop_readlink(struct vop_readlink_args *); 86 static int devfs_vop_print(struct vop_print_args *); 87 88 static int devfs_vop_nresolve(struct vop_nresolve_args *); 89 static int devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *); 90 static int devfs_vop_nmkdir(struct vop_nmkdir_args *); 91 static int devfs_vop_nsymlink(struct vop_nsymlink_args *); 92 static int devfs_vop_nrmdir(struct vop_nrmdir_args *); 93 static int devfs_vop_nremove(struct vop_nremove_args *); 94 95 static int devfs_spec_open(struct vop_open_args *); 96 static int devfs_spec_close(struct vop_close_args *); 97 static int devfs_spec_fsync(struct vop_fsync_args *); 98 99 static int devfs_spec_read(struct vop_read_args *); 100 static int devfs_spec_write(struct vop_write_args *); 101 static int devfs_spec_ioctl(struct vop_ioctl_args *); 102 static int devfs_spec_kqfilter(struct vop_kqfilter_args *); 103 static int devfs_spec_strategy(struct vop_strategy_args *); 104 static void devfs_spec_strategy_done(struct bio *); 105 static int devfs_spec_freeblks(struct vop_freeblks_args *); 106 static int devfs_spec_bmap(struct vop_bmap_args *); 107 static int devfs_spec_advlock(struct vop_advlock_args *); 108 static void devfs_spec_getpages_iodone(struct bio *); 109 static int devfs_spec_getpages(struct vop_getpages_args *); 110 111 static int devfs_fo_close(struct file *); 112 static int devfs_fo_read(struct file *, struct uio *, struct ucred *, int); 113 static int devfs_fo_write(struct file *, struct uio *, struct ucred *, int); 114 static int devfs_fo_stat(struct file *, struct stat *, struct ucred *); 115 static int devfs_fo_kqfilter(struct file *, struct knote *); 116 static int devfs_fo_ioctl(struct file *, u_long, caddr_t, 117 struct ucred *, struct sysmsg *); 118 static __inline int sequential_heuristic(struct uio *, struct file *); 119 120 extern struct lock devfs_lock; 121 122 /* 123 * devfs vnode operations for regular files. All vnode ops are MPSAFE. 124 */ 125 struct vop_ops devfs_vnode_norm_vops = { 126 .vop_default = vop_defaultop, 127 .vop_access = devfs_vop_access, 128 .vop_advlock = DEVFS_BADOP, 129 .vop_bmap = DEVFS_BADOP, 130 .vop_close = vop_stdclose, 131 .vop_getattr = devfs_vop_getattr, 132 .vop_inactive = devfs_vop_inactive, 133 .vop_ncreate = DEVFS_BADOP, 134 .vop_nresolve = devfs_vop_nresolve, 135 .vop_nlookupdotdot = devfs_vop_nlookupdotdot, 136 .vop_nlink = DEVFS_BADOP, 137 .vop_nmkdir = devfs_vop_nmkdir, 138 .vop_nmknod = DEVFS_BADOP, 139 .vop_nremove = devfs_vop_nremove, 140 .vop_nrename = DEVFS_BADOP, 141 .vop_nrmdir = devfs_vop_nrmdir, 142 .vop_nsymlink = devfs_vop_nsymlink, 143 .vop_open = vop_stdopen, 144 .vop_pathconf = vop_stdpathconf, 145 .vop_print = devfs_vop_print, 146 .vop_read = DEVFS_BADOP, 147 .vop_readdir = devfs_vop_readdir, 148 .vop_readlink = devfs_vop_readlink, 149 .vop_reallocblks = DEVFS_BADOP, 150 .vop_reclaim = devfs_vop_reclaim, 151 .vop_setattr = devfs_vop_setattr, 152 .vop_write = DEVFS_BADOP, 153 .vop_ioctl = DEVFS_BADOP 154 }; 155 156 /* 157 * devfs vnode operations for character devices. All vnode ops are MPSAFE. 158 */ 159 struct vop_ops devfs_vnode_dev_vops = { 160 .vop_default = vop_defaultop, 161 .vop_access = devfs_vop_access, 162 .vop_advlock = devfs_spec_advlock, 163 .vop_bmap = devfs_spec_bmap, 164 .vop_close = devfs_spec_close, 165 .vop_freeblks = devfs_spec_freeblks, 166 .vop_fsync = devfs_spec_fsync, 167 .vop_getattr = devfs_vop_getattr, 168 .vop_getpages = devfs_spec_getpages, 169 .vop_inactive = devfs_vop_inactive, 170 .vop_open = devfs_spec_open, 171 .vop_pathconf = vop_stdpathconf, 172 .vop_print = devfs_vop_print, 173 .vop_kqfilter = devfs_spec_kqfilter, 174 .vop_read = devfs_spec_read, 175 .vop_readdir = DEVFS_BADOP, 176 .vop_readlink = DEVFS_BADOP, 177 .vop_reallocblks = DEVFS_BADOP, 178 .vop_reclaim = devfs_vop_reclaim, 179 .vop_setattr = devfs_vop_setattr, 180 .vop_strategy = devfs_spec_strategy, 181 .vop_write = devfs_spec_write, 182 .vop_ioctl = devfs_spec_ioctl 183 }; 184 185 /* 186 * devfs file pointer operations. All fileops are MPSAFE. 187 */ 188 struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops; 189 190 struct fileops devfs_dev_fileops = { 191 .fo_read = devfs_fo_read, 192 .fo_write = devfs_fo_write, 193 .fo_ioctl = devfs_fo_ioctl, 194 .fo_kqfilter = devfs_fo_kqfilter, 195 .fo_stat = devfs_fo_stat, 196 .fo_close = devfs_fo_close, 197 .fo_shutdown = nofo_shutdown 198 }; 199 200 /* 201 * These two functions are possibly temporary hacks for devices (aka 202 * the pty code) which want to control the node attributes themselves. 203 * 204 * XXX we may ultimately desire to simply remove the uid/gid/mode 205 * from the node entirely. 206 * 207 * MPSAFE - sorta. Theoretically the overwrite can compete since they 208 * are loading from the same fields. 209 */ 210 static __inline void 211 node_sync_dev_get(struct devfs_node *node) 212 { 213 cdev_t dev; 214 215 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) { 216 node->uid = dev->si_uid; 217 node->gid = dev->si_gid; 218 node->mode = dev->si_perms; 219 } 220 } 221 222 static __inline void 223 node_sync_dev_set(struct devfs_node *node) 224 { 225 cdev_t dev; 226 227 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) { 228 dev->si_uid = node->uid; 229 dev->si_gid = node->gid; 230 dev->si_perms = node->mode; 231 } 232 } 233 234 /* 235 * generic entry point for unsupported operations 236 */ 237 static int 238 devfs_vop_badop(struct vop_generic_args *ap) 239 { 240 return (EIO); 241 } 242 243 244 static int 245 devfs_vop_access(struct vop_access_args *ap) 246 { 247 struct devfs_node *node = DEVFS_NODE(ap->a_vp); 248 int error; 249 250 if (!devfs_node_is_accessible(node)) 251 return ENOENT; 252 node_sync_dev_get(node); 253 error = vop_helper_access(ap, node->uid, node->gid, 254 node->mode, node->flags); 255 256 return error; 257 } 258 259 260 static int 261 devfs_vop_inactive(struct vop_inactive_args *ap) 262 { 263 struct devfs_node *node = DEVFS_NODE(ap->a_vp); 264 265 if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0) 266 vrecycle(ap->a_vp); 267 return 0; 268 } 269 270 271 static int 272 devfs_vop_reclaim(struct vop_reclaim_args *ap) 273 { 274 struct devfs_node *node; 275 struct vnode *vp; 276 int locked; 277 278 /* 279 * Check if it is locked already. if not, we acquire the devfs lock 280 */ 281 if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) { 282 lockmgr(&devfs_lock, LK_EXCLUSIVE); 283 locked = 1; 284 } else { 285 locked = 0; 286 } 287 288 /* 289 * Get rid of the devfs_node if it is no longer linked into the 290 * topology. 291 */ 292 vp = ap->a_vp; 293 if ((node = DEVFS_NODE(vp)) != NULL) { 294 node->v_node = NULL; 295 if ((node->flags & DEVFS_NODE_LINKED) == 0) 296 devfs_freep(node); 297 } 298 299 if (locked) 300 lockmgr(&devfs_lock, LK_RELEASE); 301 302 /* 303 * v_rdev needs to be properly released using v_release_rdev 304 * Make sure v_data is NULL as well. 305 */ 306 vp->v_data = NULL; 307 v_release_rdev(vp); 308 return 0; 309 } 310 311 312 static int 313 devfs_vop_readdir(struct vop_readdir_args *ap) 314 { 315 struct devfs_node *dnode = DEVFS_NODE(ap->a_vp); 316 struct devfs_node *node; 317 int cookie_index; 318 int ncookies; 319 int error2; 320 int error; 321 int r; 322 off_t *cookies; 323 off_t saveoff; 324 325 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n"); 326 327 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX) 328 return (EINVAL); 329 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM); 330 if (error) 331 return (error); 332 333 if (!devfs_node_is_accessible(dnode)) { 334 vn_unlock(ap->a_vp); 335 return ENOENT; 336 } 337 338 lockmgr(&devfs_lock, LK_EXCLUSIVE); 339 340 saveoff = ap->a_uio->uio_offset; 341 342 if (ap->a_ncookies) { 343 ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */ 344 if (ncookies > 256) 345 ncookies = 256; 346 cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK); 347 cookie_index = 0; 348 } else { 349 ncookies = -1; 350 cookies = NULL; 351 cookie_index = 0; 352 } 353 354 nanotime(&dnode->atime); 355 356 if (saveoff == 0) { 357 r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino, 358 DT_DIR, 1, "."); 359 if (r) 360 goto done; 361 if (cookies) 362 cookies[cookie_index] = saveoff; 363 saveoff++; 364 cookie_index++; 365 if (cookie_index == ncookies) 366 goto done; 367 } 368 369 if (saveoff == 1) { 370 if (dnode->parent) { 371 r = vop_write_dirent(&error, ap->a_uio, 372 dnode->parent->d_dir.d_ino, 373 DT_DIR, 2, ".."); 374 } else { 375 r = vop_write_dirent(&error, ap->a_uio, 376 dnode->d_dir.d_ino, 377 DT_DIR, 2, ".."); 378 } 379 if (r) 380 goto done; 381 if (cookies) 382 cookies[cookie_index] = saveoff; 383 saveoff++; 384 cookie_index++; 385 if (cookie_index == ncookies) 386 goto done; 387 } 388 389 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) { 390 if ((node->flags & DEVFS_HIDDEN) || 391 (node->flags & DEVFS_INVISIBLE)) { 392 continue; 393 } 394 395 /* 396 * If the node type is a valid devfs alias, then we make 397 * sure that the target isn't hidden. If it is, we don't 398 * show the link in the directory listing. 399 */ 400 if ((node->node_type == Nlink) && (node->link_target != NULL) && 401 (node->link_target->flags & DEVFS_HIDDEN)) 402 continue; 403 404 if (node->cookie < saveoff) 405 continue; 406 407 saveoff = node->cookie; 408 409 error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino, 410 node->d_dir.d_type, 411 node->d_dir.d_namlen, 412 node->d_dir.d_name); 413 414 if (error2) 415 break; 416 417 saveoff++; 418 419 if (cookies) 420 cookies[cookie_index] = node->cookie; 421 ++cookie_index; 422 if (cookie_index == ncookies) 423 break; 424 } 425 426 done: 427 lockmgr(&devfs_lock, LK_RELEASE); 428 vn_unlock(ap->a_vp); 429 430 ap->a_uio->uio_offset = saveoff; 431 if (error && cookie_index == 0) { 432 if (cookies) { 433 kfree(cookies, M_TEMP); 434 *ap->a_ncookies = 0; 435 *ap->a_cookies = NULL; 436 } 437 } else { 438 if (cookies) { 439 *ap->a_ncookies = cookie_index; 440 *ap->a_cookies = cookies; 441 } 442 } 443 return (error); 444 } 445 446 447 static int 448 devfs_vop_nresolve(struct vop_nresolve_args *ap) 449 { 450 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); 451 struct devfs_node *node, *found = NULL; 452 struct namecache *ncp; 453 struct vnode *vp = NULL; 454 int error = 0; 455 int len; 456 int depth; 457 458 ncp = ap->a_nch->ncp; 459 len = ncp->nc_nlen; 460 461 if (!devfs_node_is_accessible(dnode)) 462 return ENOENT; 463 464 lockmgr(&devfs_lock, LK_EXCLUSIVE); 465 466 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) { 467 error = ENOENT; 468 cache_setvp(ap->a_nch, NULL); 469 goto out; 470 } 471 472 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) { 473 if (len == node->d_dir.d_namlen) { 474 if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) { 475 found = node; 476 break; 477 } 478 } 479 } 480 481 if (found) { 482 depth = 0; 483 while ((found->node_type == Nlink) && (found->link_target)) { 484 if (depth >= 8) { 485 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8"); 486 break; 487 } 488 489 found = found->link_target; 490 ++depth; 491 } 492 493 if (!(found->flags & DEVFS_HIDDEN)) 494 devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found); 495 } 496 497 if (vp == NULL) { 498 error = ENOENT; 499 cache_setvp(ap->a_nch, NULL); 500 goto out; 501 502 } 503 KKASSERT(vp); 504 vn_unlock(vp); 505 cache_setvp(ap->a_nch, vp); 506 vrele(vp); 507 out: 508 lockmgr(&devfs_lock, LK_RELEASE); 509 510 return error; 511 } 512 513 514 static int 515 devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap) 516 { 517 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); 518 519 *ap->a_vpp = NULL; 520 if (!devfs_node_is_accessible(dnode)) 521 return ENOENT; 522 523 lockmgr(&devfs_lock, LK_EXCLUSIVE); 524 if (dnode->parent != NULL) { 525 devfs_allocv(ap->a_vpp, dnode->parent); 526 vn_unlock(*ap->a_vpp); 527 } 528 lockmgr(&devfs_lock, LK_RELEASE); 529 530 return ((*ap->a_vpp == NULL) ? ENOENT : 0); 531 } 532 533 534 /* 535 * getattr() - Does not need a lock since the vp is refd 536 */ 537 static int 538 devfs_vop_getattr(struct vop_getattr_args *ap) 539 { 540 struct devfs_node *node = DEVFS_NODE(ap->a_vp); 541 struct vattr *vap = ap->a_vap; 542 struct partinfo pinfo; 543 int error = 0; 544 545 #if 0 546 if (!devfs_node_is_accessible(node)) 547 return ENOENT; 548 #endif 549 node_sync_dev_get(node); 550 551 /* start by zeroing out the attributes */ 552 VATTR_NULL(vap); 553 554 /* next do all the common fields */ 555 vap->va_type = ap->a_vp->v_type; 556 vap->va_mode = node->mode; 557 vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ; 558 vap->va_flags = 0; 559 vap->va_blocksize = DEV_BSIZE; 560 vap->va_bytes = vap->va_size = 0; 561 562 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 563 564 vap->va_atime = node->atime; 565 vap->va_mtime = node->mtime; 566 vap->va_ctime = node->ctime; 567 568 vap->va_nlink = 1; /* number of references to file */ 569 570 vap->va_uid = node->uid; 571 vap->va_gid = node->gid; 572 573 vap->va_rmajor = 0; 574 vap->va_rminor = 0; 575 576 if ((node->node_type == Ndev) && node->d_dev) { 577 reference_dev(node->d_dev); 578 vap->va_rminor = node->d_dev->si_uminor; 579 release_dev(node->d_dev); 580 } 581 582 /* For a softlink the va_size is the length of the softlink */ 583 if (node->symlink_name != 0) { 584 vap->va_bytes = vap->va_size = node->symlink_namelen; 585 } 586 587 /* 588 * For a disk-type device, va_size is the size of the underlying 589 * device, so that lseek() works properly. 590 */ 591 if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) { 592 bzero(&pinfo, sizeof(pinfo)); 593 error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo, 594 0, proc0.p_ucred, NULL, NULL); 595 if ((error == 0) && (pinfo.media_blksize != 0)) { 596 vap->va_size = pinfo.media_size; 597 } else { 598 vap->va_size = 0; 599 error = 0; 600 } 601 } 602 603 return (error); 604 } 605 606 static int 607 devfs_vop_setattr(struct vop_setattr_args *ap) 608 { 609 struct devfs_node *node = DEVFS_NODE(ap->a_vp); 610 struct vattr *vap; 611 uid_t cur_uid; 612 gid_t cur_gid; 613 mode_t cur_mode; 614 int error = 0; 615 616 if (!devfs_node_is_accessible(node)) 617 return ENOENT; 618 node_sync_dev_get(node); 619 620 lockmgr(&devfs_lock, LK_EXCLUSIVE); 621 622 vap = ap->a_vap; 623 624 if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) { 625 cur_uid = node->uid; 626 cur_gid = node->gid; 627 cur_mode = node->mode; 628 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid, 629 ap->a_cred, &cur_uid, &cur_gid, &cur_mode); 630 if (error) 631 goto out; 632 633 if (node->uid != cur_uid || node->gid != cur_gid) { 634 node->uid = cur_uid; 635 node->gid = cur_gid; 636 node->mode = cur_mode; 637 } 638 } 639 640 if (vap->va_mode != (mode_t)VNOVAL) { 641 cur_mode = node->mode; 642 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred, 643 node->uid, node->gid, &cur_mode); 644 if (error == 0 && node->mode != cur_mode) { 645 node->mode = cur_mode; 646 } 647 } 648 649 out: 650 node_sync_dev_set(node); 651 nanotime(&node->ctime); 652 lockmgr(&devfs_lock, LK_RELEASE); 653 654 return error; 655 } 656 657 658 static int 659 devfs_vop_readlink(struct vop_readlink_args *ap) 660 { 661 struct devfs_node *node = DEVFS_NODE(ap->a_vp); 662 int ret; 663 664 if (!devfs_node_is_accessible(node)) 665 return ENOENT; 666 667 lockmgr(&devfs_lock, LK_SHARED); 668 ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio); 669 lockmgr(&devfs_lock, LK_RELEASE); 670 671 return ret; 672 } 673 674 675 static int 676 devfs_vop_print(struct vop_print_args *ap) 677 { 678 return (0); 679 } 680 681 static int 682 devfs_vop_nmkdir(struct vop_nmkdir_args *ap) 683 { 684 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); 685 struct devfs_node *node; 686 687 if (!devfs_node_is_accessible(dnode)) 688 return ENOENT; 689 690 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) 691 goto out; 692 693 lockmgr(&devfs_lock, LK_EXCLUSIVE); 694 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Ndir, 695 ap->a_nch->ncp->nc_name, dnode, NULL); 696 697 if (*ap->a_vpp) { 698 node = DEVFS_NODE(*ap->a_vpp); 699 node->flags |= DEVFS_USER_CREATED; 700 cache_setunresolved(ap->a_nch); 701 cache_setvp(ap->a_nch, *ap->a_vpp); 702 } 703 lockmgr(&devfs_lock, LK_RELEASE); 704 out: 705 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0); 706 } 707 708 static int 709 devfs_vop_nsymlink(struct vop_nsymlink_args *ap) 710 { 711 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); 712 struct devfs_node *node; 713 size_t targetlen; 714 715 if (!devfs_node_is_accessible(dnode)) 716 return ENOENT; 717 718 ap->a_vap->va_type = VLNK; 719 720 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) 721 goto out; 722 723 lockmgr(&devfs_lock, LK_EXCLUSIVE); 724 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Nlink, 725 ap->a_nch->ncp->nc_name, dnode, NULL); 726 727 targetlen = strlen(ap->a_target); 728 if (*ap->a_vpp) { 729 node = DEVFS_NODE(*ap->a_vpp); 730 node->flags |= DEVFS_USER_CREATED; 731 node->symlink_namelen = targetlen; 732 node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK); 733 memcpy(node->symlink_name, ap->a_target, targetlen); 734 node->symlink_name[targetlen] = '\0'; 735 cache_setunresolved(ap->a_nch); 736 cache_setvp(ap->a_nch, *ap->a_vpp); 737 } 738 lockmgr(&devfs_lock, LK_RELEASE); 739 out: 740 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0); 741 } 742 743 static int 744 devfs_vop_nrmdir(struct vop_nrmdir_args *ap) 745 { 746 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); 747 struct devfs_node *node; 748 struct namecache *ncp; 749 int error = ENOENT; 750 751 ncp = ap->a_nch->ncp; 752 753 if (!devfs_node_is_accessible(dnode)) 754 return ENOENT; 755 756 lockmgr(&devfs_lock, LK_EXCLUSIVE); 757 758 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) 759 goto out; 760 761 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) { 762 if (ncp->nc_nlen != node->d_dir.d_namlen) 763 continue; 764 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen)) 765 continue; 766 767 /* 768 * only allow removal of user created dirs 769 */ 770 if ((node->flags & DEVFS_USER_CREATED) == 0) { 771 error = EPERM; 772 goto out; 773 } else if (node->node_type != Ndir) { 774 error = ENOTDIR; 775 goto out; 776 } else if (node->nchildren > 2) { 777 error = ENOTEMPTY; 778 goto out; 779 } else { 780 if (node->v_node) 781 cache_inval_vp(node->v_node, CINV_DESTROY); 782 devfs_unlinkp(node); 783 error = 0; 784 break; 785 } 786 } 787 788 cache_unlink(ap->a_nch); 789 out: 790 lockmgr(&devfs_lock, LK_RELEASE); 791 return error; 792 } 793 794 static int 795 devfs_vop_nremove(struct vop_nremove_args *ap) 796 { 797 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); 798 struct devfs_node *node; 799 struct namecache *ncp; 800 int error = ENOENT; 801 802 ncp = ap->a_nch->ncp; 803 804 if (!devfs_node_is_accessible(dnode)) 805 return ENOENT; 806 807 lockmgr(&devfs_lock, LK_EXCLUSIVE); 808 809 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) 810 goto out; 811 812 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) { 813 if (ncp->nc_nlen != node->d_dir.d_namlen) 814 continue; 815 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen)) 816 continue; 817 818 /* 819 * only allow removal of user created stuff (e.g. symlinks) 820 */ 821 if ((node->flags & DEVFS_USER_CREATED) == 0) { 822 error = EPERM; 823 goto out; 824 } else if (node->node_type == Ndir) { 825 error = EISDIR; 826 goto out; 827 } else { 828 if (node->v_node) 829 cache_inval_vp(node->v_node, CINV_DESTROY); 830 devfs_unlinkp(node); 831 error = 0; 832 break; 833 } 834 } 835 836 cache_unlink(ap->a_nch); 837 out: 838 lockmgr(&devfs_lock, LK_RELEASE); 839 return error; 840 } 841 842 843 static int 844 devfs_spec_open(struct vop_open_args *ap) 845 { 846 struct vnode *vp = ap->a_vp; 847 struct vnode *orig_vp = NULL; 848 struct devfs_node *node = DEVFS_NODE(vp); 849 struct devfs_node *newnode; 850 cdev_t dev, ndev = NULL; 851 int error = 0; 852 853 if (node) { 854 if (node->d_dev == NULL) 855 return ENXIO; 856 if (!devfs_node_is_accessible(node)) 857 return ENOENT; 858 } 859 860 if ((dev = vp->v_rdev) == NULL) 861 return ENXIO; 862 863 /* 864 * Simple devices that don't care. Retain the shared lock. 865 */ 866 if (dev_dflags(dev) & D_QUICK) { 867 vn_unlock(vp); 868 error = dev_dopen(dev, ap->a_mode, S_IFCHR, 869 ap->a_cred, ap->a_fp); 870 vn_lock(vp, LK_SHARED | LK_RETRY); 871 vop_stdopen(ap); 872 goto skip; 873 } 874 875 /* 876 * Slow code 877 */ 878 vn_lock(vp, LK_UPGRADE | LK_RETRY); 879 if (node && ap->a_fp) { 880 int exists; 881 882 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n"); 883 lockmgr(&devfs_lock, LK_EXCLUSIVE); 884 885 ndev = devfs_clone(dev, node->d_dir.d_name, 886 node->d_dir.d_namlen, 887 ap->a_mode, ap->a_cred); 888 if (ndev != NULL) { 889 newnode = devfs_create_device_node( 890 DEVFS_MNTDATA(vp->v_mount)->root_node, 891 ndev, &exists, NULL, NULL); 892 /* XXX: possibly destroy device if this happens */ 893 894 if (newnode != NULL) { 895 dev = ndev; 896 if (exists == 0) 897 devfs_link_dev(dev); 898 899 devfs_debug(DEVFS_DEBUG_DEBUG, 900 "parent here is: %s, node is: |%s|\n", 901 ((node->parent->node_type == Nroot) ? 902 "ROOT!" : node->parent->d_dir.d_name), 903 newnode->d_dir.d_name); 904 devfs_debug(DEVFS_DEBUG_DEBUG, 905 "test: %s\n", 906 ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name); 907 908 /* 909 * orig_vp is set to the original vp if we 910 * cloned. 911 */ 912 /* node->flags |= DEVFS_CLONED; */ 913 devfs_allocv(&vp, newnode); 914 orig_vp = ap->a_vp; 915 ap->a_vp = vp; 916 } 917 } 918 lockmgr(&devfs_lock, LK_RELEASE); 919 920 /* 921 * Synchronize devfs here to make sure that, if the cloned 922 * device creates other device nodes in addition to the 923 * cloned one, all of them are created by the time we return 924 * from opening the cloned one. 925 */ 926 if (ndev) 927 devfs_config(); 928 } 929 930 devfs_debug(DEVFS_DEBUG_DEBUG, 931 "devfs_spec_open() called on %s! \n", 932 dev->si_name); 933 934 /* 935 * Make this field valid before any I/O in ->d_open 936 * 937 * NOTE: Shared vnode lock probably held, but its ok as long 938 * as assignments are consistent. 939 */ 940 if (!dev->si_iosize_max) 941 /* XXX: old DFLTPHYS == 64KB dependency */ 942 dev->si_iosize_max = min(MAXPHYS,64*1024); 943 944 if (dev_dflags(dev) & D_TTY) 945 vsetflags(vp, VISTTY); 946 947 /* 948 * Open the underlying device 949 */ 950 vn_unlock(vp); 951 error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred, ap->a_fp); 952 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 953 954 /* 955 * Clean up any cloned vp if we error out. 956 */ 957 if (error) { 958 if (orig_vp) { 959 vput(vp); 960 ap->a_vp = orig_vp; 961 /* orig_vp = NULL; */ 962 } 963 return error; 964 } 965 966 /* 967 * This checks if the disk device is going to be opened for writing. 968 * It will be only allowed in the cases where securelevel permits it 969 * and it's not mounted R/W. 970 */ 971 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) && 972 (ap->a_cred != FSCRED)) { 973 974 /* Very secure mode. No open for writing allowed */ 975 if (securelevel >= 2) 976 return EPERM; 977 978 /* 979 * If it is mounted R/W, do not allow to open for writing. 980 * In the case it's mounted read-only but securelevel 981 * is >= 1, then do not allow opening for writing either. 982 */ 983 if (vfs_mountedon(vp)) { 984 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY)) 985 return EBUSY; 986 else if (securelevel >= 1) 987 return EPERM; 988 } 989 } 990 991 /* 992 * NOTE: vnode is still locked shared. t_stop assignment should 993 * remain consistent so we should be ok. 994 */ 995 if (dev_dflags(dev) & D_TTY) { 996 if (dev->si_tty) { 997 struct tty *tp; 998 tp = dev->si_tty; 999 if (!tp->t_stop) { 1000 devfs_debug(DEVFS_DEBUG_DEBUG, 1001 "devfs: no t_stop\n"); 1002 tp->t_stop = nottystop; 1003 } 1004 } 1005 } 1006 1007 /* 1008 * NOTE: vnode is still locked shared. assignments should 1009 * remain consistent so we should be ok. However, 1010 * upgrade to exclusive if we need a VM object. 1011 */ 1012 if (vn_isdisk(vp, NULL)) { 1013 if (!dev->si_bsize_phys) 1014 dev->si_bsize_phys = DEV_BSIZE; 1015 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1); 1016 } 1017 1018 vop_stdopen(ap); 1019 #if 0 1020 if (node) 1021 nanotime(&node->atime); 1022 #endif 1023 /* 1024 * If we replaced the vp the vop_stdopen() call will have loaded 1025 * it into fp->f_data and vref()d the vp, giving us two refs. So 1026 * instead of just unlocking it here we have to vput() it. 1027 */ 1028 if (orig_vp) 1029 vput(vp); 1030 1031 /* Ugly pty magic, to make pty devices appear once they are opened */ 1032 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY) { 1033 if (node->flags & DEVFS_INVISIBLE) 1034 node->flags &= ~DEVFS_INVISIBLE; 1035 } 1036 1037 skip: 1038 if (ap->a_fp) { 1039 KKASSERT(ap->a_fp->f_type == DTYPE_VNODE); 1040 KKASSERT((ap->a_fp->f_flag & FMASK) == (ap->a_mode & FMASK)); 1041 ap->a_fp->f_ops = &devfs_dev_fileops; 1042 KKASSERT(ap->a_fp->f_data == (void *)vp); 1043 } 1044 1045 return 0; 1046 } 1047 1048 static int 1049 devfs_spec_close(struct vop_close_args *ap) 1050 { 1051 struct devfs_node *node; 1052 struct proc *p = curproc; 1053 struct vnode *vp = ap->a_vp; 1054 cdev_t dev = vp->v_rdev; 1055 int error = 0; 1056 int needrelock; 1057 int opencount; 1058 1059 /* 1060 * Devices flagged D_QUICK require no special handling. 1061 */ 1062 if (dev && dev_dflags(dev) & D_QUICK) { 1063 opencount = vp->v_opencount; 1064 if (opencount <= 1) 1065 opencount = count_dev(dev); /* XXX NOT SMP SAFE */ 1066 if (((vp->v_flag & VRECLAIMED) || 1067 (dev_dflags(dev) & D_TRACKCLOSE) || 1068 (opencount == 1))) { 1069 vn_unlock(vp); 1070 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp); 1071 vn_lock(vp, LK_SHARED | LK_RETRY); 1072 } 1073 goto skip; 1074 } 1075 1076 /* 1077 * We do special tests on the opencount so unfortunately we need 1078 * an exclusive lock. 1079 */ 1080 vn_lock(vp, LK_UPGRADE | LK_RETRY); 1081 1082 if (dev) 1083 devfs_debug(DEVFS_DEBUG_DEBUG, 1084 "devfs_spec_close() called on %s! \n", 1085 dev->si_name); 1086 else 1087 devfs_debug(DEVFS_DEBUG_DEBUG, 1088 "devfs_spec_close() called, null vode!\n"); 1089 1090 /* 1091 * A couple of hacks for devices and tty devices. The 1092 * vnode ref count cannot be used to figure out the 1093 * last close, but we can use v_opencount now that 1094 * revoke works properly. 1095 * 1096 * Detect the last close on a controlling terminal and clear 1097 * the session (half-close). 1098 * 1099 * XXX opencount is not SMP safe. The vnode is locked but there 1100 * may be multiple vnodes referencing the same device. 1101 */ 1102 if (dev) { 1103 /* 1104 * NOTE: Try to avoid global tokens when testing opencount 1105 * XXX hack, fixme. needs a struct lock and opencount in 1106 * struct cdev itself. 1107 */ 1108 reference_dev(dev); 1109 opencount = vp->v_opencount; 1110 if (opencount <= 1) 1111 opencount = count_dev(dev); /* XXX NOT SMP SAFE */ 1112 } else { 1113 opencount = 0; 1114 } 1115 1116 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) { 1117 p->p_session->s_ttyvp = NULL; 1118 vrele(vp); 1119 } 1120 1121 /* 1122 * Vnodes can be opened and closed multiple times. Do not really 1123 * close the device unless (1) it is being closed forcibly, 1124 * (2) the device wants to track closes, or (3) this is the last 1125 * vnode doing its last close on the device. 1126 * 1127 * XXX the VXLOCK (force close) case can leave vnodes referencing 1128 * a closed device. This might not occur now that our revoke is 1129 * fixed. 1130 */ 1131 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n"); 1132 if (dev && ((vp->v_flag & VRECLAIMED) || 1133 (dev_dflags(dev) & D_TRACKCLOSE) || 1134 (opencount == 1))) { 1135 /* 1136 * Ugly pty magic, to make pty devices disappear again once 1137 * they are closed. 1138 */ 1139 node = DEVFS_NODE(ap->a_vp); 1140 if (node && (node->flags & DEVFS_PTY)) 1141 node->flags |= DEVFS_INVISIBLE; 1142 1143 /* 1144 * Unlock around dev_dclose(), unless the vnode is 1145 * undergoing a vgone/reclaim (during umount). 1146 */ 1147 needrelock = 0; 1148 if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) { 1149 needrelock = 1; 1150 vn_unlock(vp); 1151 } 1152 1153 /* 1154 * WARNING! If the device destroys itself the devfs node 1155 * can disappear here. 1156 * 1157 * WARNING! vn_lock() will fail if the vp is in a VRECLAIM, 1158 * which can occur during umount. 1159 */ 1160 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp); 1161 /* node is now stale */ 1162 1163 if (needrelock) { 1164 if (vn_lock(vp, LK_EXCLUSIVE | 1165 LK_RETRY | 1166 LK_FAILRECLAIM) != 0) { 1167 panic("devfs_spec_close: vnode %p " 1168 "unexpectedly could not be relocked", 1169 vp); 1170 } 1171 } 1172 } else { 1173 error = 0; 1174 } 1175 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n"); 1176 1177 /* 1178 * Track the actual opens and closes on the vnode. The last close 1179 * disassociates the rdev. If the rdev is already disassociated or 1180 * the opencount is already 0, the vnode might have been revoked 1181 * and no further opencount tracking occurs. 1182 */ 1183 if (dev) 1184 release_dev(dev); 1185 skip: 1186 if (vp->v_opencount > 0) 1187 vop_stdclose(ap); 1188 return(error); 1189 1190 } 1191 1192 1193 static int 1194 devfs_fo_close(struct file *fp) 1195 { 1196 struct vnode *vp = (struct vnode *)fp->f_data; 1197 int error; 1198 1199 fp->f_ops = &badfileops; 1200 error = vn_close(vp, fp->f_flag, fp); 1201 devfs_clear_cdevpriv(fp); 1202 1203 return (error); 1204 } 1205 1206 1207 /* 1208 * Device-optimized file table vnode read routine. 1209 * 1210 * This bypasses the VOP table and talks directly to the device. Most 1211 * filesystems just route to specfs and can make this optimization. 1212 */ 1213 static int 1214 devfs_fo_read(struct file *fp, struct uio *uio, 1215 struct ucred *cred, int flags) 1216 { 1217 struct devfs_node *node; 1218 struct vnode *vp; 1219 int ioflag; 1220 int error; 1221 cdev_t dev; 1222 1223 KASSERT(uio->uio_td == curthread, 1224 ("uio_td %p is not td %p", uio->uio_td, curthread)); 1225 1226 if (uio->uio_resid == 0) 1227 return 0; 1228 1229 vp = (struct vnode *)fp->f_data; 1230 if (vp == NULL || vp->v_type == VBAD) 1231 return EBADF; 1232 1233 node = DEVFS_NODE(vp); 1234 1235 if ((dev = vp->v_rdev) == NULL) 1236 return EBADF; 1237 1238 reference_dev(dev); 1239 1240 if ((flags & O_FOFFSET) == 0) 1241 uio->uio_offset = fp->f_offset; 1242 1243 ioflag = 0; 1244 if (flags & O_FBLOCKING) { 1245 /* ioflag &= ~IO_NDELAY; */ 1246 } else if (flags & O_FNONBLOCKING) { 1247 ioflag |= IO_NDELAY; 1248 } else if (fp->f_flag & FNONBLOCK) { 1249 ioflag |= IO_NDELAY; 1250 } 1251 if (fp->f_flag & O_DIRECT) { 1252 ioflag |= IO_DIRECT; 1253 } 1254 ioflag |= sequential_heuristic(uio, fp); 1255 1256 error = dev_dread(dev, uio, ioflag, fp); 1257 1258 release_dev(dev); 1259 if (node) 1260 nanotime(&node->atime); 1261 if ((flags & O_FOFFSET) == 0) 1262 fp->f_offset = uio->uio_offset; 1263 fp->f_nextoff = uio->uio_offset; 1264 1265 return (error); 1266 } 1267 1268 1269 static int 1270 devfs_fo_write(struct file *fp, struct uio *uio, 1271 struct ucred *cred, int flags) 1272 { 1273 struct devfs_node *node; 1274 struct vnode *vp; 1275 int ioflag; 1276 int error; 1277 cdev_t dev; 1278 1279 KASSERT(uio->uio_td == curthread, 1280 ("uio_td %p is not p %p", uio->uio_td, curthread)); 1281 1282 vp = (struct vnode *)fp->f_data; 1283 if (vp == NULL || vp->v_type == VBAD) 1284 return EBADF; 1285 1286 node = DEVFS_NODE(vp); 1287 1288 if (vp->v_type == VREG) 1289 bwillwrite(uio->uio_resid); 1290 1291 vp = (struct vnode *)fp->f_data; 1292 1293 if ((dev = vp->v_rdev) == NULL) 1294 return EBADF; 1295 1296 reference_dev(dev); 1297 1298 if ((flags & O_FOFFSET) == 0) 1299 uio->uio_offset = fp->f_offset; 1300 1301 ioflag = IO_UNIT; 1302 if (vp->v_type == VREG && 1303 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) { 1304 ioflag |= IO_APPEND; 1305 } 1306 1307 if (flags & O_FBLOCKING) { 1308 /* ioflag &= ~IO_NDELAY; */ 1309 } else if (flags & O_FNONBLOCKING) { 1310 ioflag |= IO_NDELAY; 1311 } else if (fp->f_flag & FNONBLOCK) { 1312 ioflag |= IO_NDELAY; 1313 } 1314 if (fp->f_flag & O_DIRECT) { 1315 ioflag |= IO_DIRECT; 1316 } 1317 if (flags & O_FASYNCWRITE) { 1318 /* ioflag &= ~IO_SYNC; */ 1319 } else if (flags & O_FSYNCWRITE) { 1320 ioflag |= IO_SYNC; 1321 } else if (fp->f_flag & O_FSYNC) { 1322 ioflag |= IO_SYNC; 1323 } 1324 1325 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)) 1326 ioflag |= IO_SYNC; 1327 ioflag |= sequential_heuristic(uio, fp); 1328 1329 error = dev_dwrite(dev, uio, ioflag, fp); 1330 1331 release_dev(dev); 1332 if (node) { 1333 nanotime(&node->atime); 1334 nanotime(&node->mtime); 1335 } 1336 1337 if ((flags & O_FOFFSET) == 0) 1338 fp->f_offset = uio->uio_offset; 1339 fp->f_nextoff = uio->uio_offset; 1340 1341 return (error); 1342 } 1343 1344 1345 static int 1346 devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred) 1347 { 1348 struct vnode *vp; 1349 struct vattr vattr; 1350 struct vattr *vap; 1351 u_short mode; 1352 cdev_t dev; 1353 int error; 1354 1355 vp = (struct vnode *)fp->f_data; 1356 if (vp == NULL || vp->v_type == VBAD) 1357 return EBADF; 1358 1359 error = vn_stat(vp, sb, cred); 1360 if (error) 1361 return (error); 1362 1363 vap = &vattr; 1364 error = VOP_GETATTR(vp, vap); 1365 if (error) 1366 return (error); 1367 1368 /* 1369 * Zero the spare stat fields 1370 */ 1371 sb->st_lspare = 0; 1372 sb->st_qspare1 = 0; 1373 sb->st_qspare2 = 0; 1374 1375 /* 1376 * Copy from vattr table ... or not in case it's a cloned device 1377 */ 1378 if (vap->va_fsid != VNOVAL) 1379 sb->st_dev = vap->va_fsid; 1380 else 1381 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 1382 1383 sb->st_ino = vap->va_fileid; 1384 1385 mode = vap->va_mode; 1386 mode |= S_IFCHR; 1387 sb->st_mode = mode; 1388 1389 if (vap->va_nlink > (nlink_t)-1) 1390 sb->st_nlink = (nlink_t)-1; 1391 else 1392 sb->st_nlink = vap->va_nlink; 1393 1394 sb->st_uid = vap->va_uid; 1395 sb->st_gid = vap->va_gid; 1396 sb->st_rdev = dev2udev(DEVFS_NODE(vp)->d_dev); 1397 sb->st_size = vap->va_bytes; 1398 sb->st_atimespec = vap->va_atime; 1399 sb->st_mtimespec = vap->va_mtime; 1400 sb->st_ctimespec = vap->va_ctime; 1401 1402 /* 1403 * A VCHR and VBLK device may track the last access and last modified 1404 * time independantly of the filesystem. This is particularly true 1405 * because device read and write calls may bypass the filesystem. 1406 */ 1407 if (vp->v_type == VCHR || vp->v_type == VBLK) { 1408 dev = vp->v_rdev; 1409 if (dev != NULL) { 1410 if (dev->si_lastread) { 1411 sb->st_atimespec.tv_sec = time_second + 1412 (time_uptime - 1413 dev->si_lastread); 1414 sb->st_atimespec.tv_nsec = 0; 1415 } 1416 if (dev->si_lastwrite) { 1417 sb->st_atimespec.tv_sec = time_second + 1418 (time_uptime - 1419 dev->si_lastwrite); 1420 sb->st_atimespec.tv_nsec = 0; 1421 } 1422 } 1423 } 1424 1425 /* 1426 * According to www.opengroup.org, the meaning of st_blksize is 1427 * "a filesystem-specific preferred I/O block size for this 1428 * object. In some filesystem types, this may vary from file 1429 * to file" 1430 * Default to PAGE_SIZE after much discussion. 1431 */ 1432 1433 sb->st_blksize = PAGE_SIZE; 1434 1435 sb->st_flags = vap->va_flags; 1436 1437 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0); 1438 if (error) 1439 sb->st_gen = 0; 1440 else 1441 sb->st_gen = (u_int32_t)vap->va_gen; 1442 1443 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 1444 1445 return (0); 1446 } 1447 1448 1449 static int 1450 devfs_fo_kqfilter(struct file *fp, struct knote *kn) 1451 { 1452 struct vnode *vp; 1453 int error; 1454 cdev_t dev; 1455 1456 vp = (struct vnode *)fp->f_data; 1457 if (vp == NULL || vp->v_type == VBAD) { 1458 error = EBADF; 1459 goto done; 1460 } 1461 if ((dev = vp->v_rdev) == NULL) { 1462 error = EBADF; 1463 goto done; 1464 } 1465 reference_dev(dev); 1466 1467 error = dev_dkqfilter(dev, kn, fp); 1468 1469 release_dev(dev); 1470 1471 done: 1472 return (error); 1473 } 1474 1475 static int 1476 devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data, 1477 struct ucred *ucred, struct sysmsg *msg) 1478 { 1479 #if 0 1480 struct devfs_node *node; 1481 #endif 1482 struct vnode *vp; 1483 struct vnode *ovp; 1484 cdev_t dev; 1485 int error; 1486 struct fiodname_args *name_args; 1487 size_t namlen; 1488 const char *name; 1489 1490 vp = ((struct vnode *)fp->f_data); 1491 1492 if ((dev = vp->v_rdev) == NULL) 1493 return EBADF; /* device was revoked */ 1494 1495 reference_dev(dev); 1496 1497 #if 0 1498 node = DEVFS_NODE(vp); 1499 #endif 1500 1501 devfs_debug(DEVFS_DEBUG_DEBUG, 1502 "devfs_fo_ioctl() called! for dev %s\n", 1503 dev->si_name); 1504 1505 if (com == FIODTYPE) { 1506 *(int *)data = dev_dflags(dev) & D_TYPEMASK; 1507 error = 0; 1508 goto out; 1509 } else if (com == FIODNAME) { 1510 name_args = (struct fiodname_args *)data; 1511 name = dev->si_name; 1512 namlen = strlen(name) + 1; 1513 1514 devfs_debug(DEVFS_DEBUG_DEBUG, 1515 "ioctl, got: FIODNAME for %s\n", name); 1516 1517 if (namlen <= name_args->len) 1518 error = copyout(dev->si_name, name_args->name, namlen); 1519 else 1520 error = EINVAL; 1521 1522 devfs_debug(DEVFS_DEBUG_DEBUG, 1523 "ioctl stuff: error: %d\n", error); 1524 goto out; 1525 } 1526 1527 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp); 1528 1529 #if 0 1530 if (node) { 1531 nanotime(&node->atime); 1532 nanotime(&node->mtime); 1533 } 1534 #endif 1535 if (com == TIOCSCTTY) { 1536 devfs_debug(DEVFS_DEBUG_DEBUG, 1537 "devfs_fo_ioctl: got TIOCSCTTY on %s\n", 1538 dev->si_name); 1539 } 1540 if (error == 0 && com == TIOCSCTTY) { 1541 struct proc *p = curthread->td_proc; 1542 struct session *sess; 1543 1544 devfs_debug(DEVFS_DEBUG_DEBUG, 1545 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n", 1546 dev->si_name); 1547 if (p == NULL) { 1548 error = ENOTTY; 1549 goto out; 1550 } 1551 sess = p->p_session; 1552 1553 /* 1554 * Do nothing if reassigning same control tty 1555 */ 1556 if (sess->s_ttyvp == vp) { 1557 error = 0; 1558 goto out; 1559 } 1560 1561 /* 1562 * Get rid of reference to old control tty 1563 */ 1564 ovp = sess->s_ttyvp; 1565 vref(vp); 1566 sess->s_ttyvp = vp; 1567 if (ovp) 1568 vrele(ovp); 1569 } 1570 1571 out: 1572 release_dev(dev); 1573 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n"); 1574 return (error); 1575 } 1576 1577 1578 static int 1579 devfs_spec_fsync(struct vop_fsync_args *ap) 1580 { 1581 struct vnode *vp = ap->a_vp; 1582 int error; 1583 1584 if (!vn_isdisk(vp, NULL)) 1585 return (0); 1586 1587 /* 1588 * Flush all dirty buffers associated with a block device. 1589 */ 1590 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL); 1591 return (error); 1592 } 1593 1594 static int 1595 devfs_spec_read(struct vop_read_args *ap) 1596 { 1597 struct devfs_node *node; 1598 struct vnode *vp; 1599 struct uio *uio; 1600 cdev_t dev; 1601 int error; 1602 1603 vp = ap->a_vp; 1604 dev = vp->v_rdev; 1605 uio = ap->a_uio; 1606 node = DEVFS_NODE(vp); 1607 1608 if (dev == NULL) /* device was revoked */ 1609 return (EBADF); 1610 if (uio->uio_resid == 0) 1611 return (0); 1612 1613 vn_unlock(vp); 1614 error = dev_dread(dev, uio, ap->a_ioflag, NULL); 1615 vn_lock(vp, LK_SHARED | LK_RETRY); 1616 1617 if (node) 1618 nanotime(&node->atime); 1619 1620 return (error); 1621 } 1622 1623 /* 1624 * Vnode op for write 1625 * 1626 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 1627 * struct ucred *a_cred) 1628 */ 1629 static int 1630 devfs_spec_write(struct vop_write_args *ap) 1631 { 1632 struct devfs_node *node; 1633 struct vnode *vp; 1634 struct uio *uio; 1635 cdev_t dev; 1636 int error; 1637 1638 vp = ap->a_vp; 1639 dev = vp->v_rdev; 1640 uio = ap->a_uio; 1641 node = DEVFS_NODE(vp); 1642 1643 KKASSERT(uio->uio_segflg != UIO_NOCOPY); 1644 1645 if (dev == NULL) /* device was revoked */ 1646 return (EBADF); 1647 1648 vn_unlock(vp); 1649 error = dev_dwrite(dev, uio, ap->a_ioflag, NULL); 1650 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1651 1652 if (node) { 1653 nanotime(&node->atime); 1654 nanotime(&node->mtime); 1655 } 1656 1657 return (error); 1658 } 1659 1660 /* 1661 * Device ioctl operation. 1662 * 1663 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, 1664 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg) 1665 */ 1666 static int 1667 devfs_spec_ioctl(struct vop_ioctl_args *ap) 1668 { 1669 struct vnode *vp = ap->a_vp; 1670 #if 0 1671 struct devfs_node *node; 1672 #endif 1673 cdev_t dev; 1674 1675 if ((dev = vp->v_rdev) == NULL) 1676 return (EBADF); /* device was revoked */ 1677 #if 0 1678 node = DEVFS_NODE(vp); 1679 1680 if (node) { 1681 nanotime(&node->atime); 1682 nanotime(&node->mtime); 1683 } 1684 #endif 1685 1686 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag, 1687 ap->a_cred, ap->a_sysmsg, NULL)); 1688 } 1689 1690 /* 1691 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn) 1692 */ 1693 /* ARGSUSED */ 1694 static int 1695 devfs_spec_kqfilter(struct vop_kqfilter_args *ap) 1696 { 1697 struct vnode *vp = ap->a_vp; 1698 #if 0 1699 struct devfs_node *node; 1700 #endif 1701 cdev_t dev; 1702 1703 if ((dev = vp->v_rdev) == NULL) 1704 return (EBADF); /* device was revoked (EBADF) */ 1705 #if 0 1706 node = DEVFS_NODE(vp); 1707 1708 if (node) 1709 nanotime(&node->atime); 1710 #endif 1711 1712 return (dev_dkqfilter(dev, ap->a_kn, NULL)); 1713 } 1714 1715 /* 1716 * Convert a vnode strategy call into a device strategy call. Vnode strategy 1717 * calls are not limited to device DMA limits so we have to deal with the 1718 * case. 1719 * 1720 * spec_strategy(struct vnode *a_vp, struct bio *a_bio) 1721 */ 1722 static int 1723 devfs_spec_strategy(struct vop_strategy_args *ap) 1724 { 1725 struct bio *bio = ap->a_bio; 1726 struct buf *bp = bio->bio_buf; 1727 struct buf *nbp; 1728 struct vnode *vp; 1729 struct mount *mp; 1730 int chunksize; 1731 int maxiosize; 1732 1733 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL) 1734 buf_start(bp); 1735 1736 /* 1737 * Collect statistics on synchronous and asynchronous read 1738 * and write counts for disks that have associated filesystems. 1739 */ 1740 vp = ap->a_vp; 1741 KKASSERT(vp->v_rdev != NULL); /* XXX */ 1742 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) { 1743 if (bp->b_cmd == BUF_CMD_READ) { 1744 if (bp->b_flags & BIO_SYNC) 1745 mp->mnt_stat.f_syncreads++; 1746 else 1747 mp->mnt_stat.f_asyncreads++; 1748 } else { 1749 if (bp->b_flags & BIO_SYNC) 1750 mp->mnt_stat.f_syncwrites++; 1751 else 1752 mp->mnt_stat.f_asyncwrites++; 1753 } 1754 } 1755 1756 /* 1757 * Device iosize limitations only apply to read and write. Shortcut 1758 * the I/O if it fits. 1759 */ 1760 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) { 1761 devfs_debug(DEVFS_DEBUG_DEBUG, 1762 "%s: si_iosize_max not set!\n", 1763 dev_dname(vp->v_rdev)); 1764 maxiosize = MAXPHYS; 1765 } 1766 #if SPEC_CHAIN_DEBUG & 2 1767 maxiosize = 4096; 1768 #endif 1769 if (bp->b_bcount <= maxiosize || 1770 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) { 1771 dev_dstrategy_chain(vp->v_rdev, bio); 1772 return (0); 1773 } 1774 1775 /* 1776 * Clone the buffer and set up an I/O chain to chunk up the I/O. 1777 */ 1778 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO); 1779 initbufbio(nbp); 1780 buf_dep_init(nbp); 1781 BUF_LOCK(nbp, LK_EXCLUSIVE); 1782 BUF_KERNPROC(nbp); 1783 nbp->b_vp = vp; 1784 nbp->b_flags = B_PAGING | B_KVABIO | (bp->b_flags & B_BNOCLIP); 1785 nbp->b_cpumask = bp->b_cpumask; 1786 nbp->b_data = bp->b_data; 1787 nbp->b_bio1.bio_done = devfs_spec_strategy_done; 1788 nbp->b_bio1.bio_offset = bio->bio_offset; 1789 nbp->b_bio1.bio_caller_info1.ptr = bio; 1790 1791 /* 1792 * Start the first transfer 1793 */ 1794 if (vn_isdisk(vp, NULL)) 1795 chunksize = vp->v_rdev->si_bsize_phys; 1796 else 1797 chunksize = DEV_BSIZE; 1798 chunksize = maxiosize / chunksize * chunksize; 1799 #if SPEC_CHAIN_DEBUG & 1 1800 devfs_debug(DEVFS_DEBUG_DEBUG, 1801 "spec_strategy chained I/O chunksize=%d\n", 1802 chunksize); 1803 #endif 1804 nbp->b_cmd = bp->b_cmd; 1805 nbp->b_bcount = chunksize; 1806 nbp->b_bufsize = chunksize; /* used to detect a short I/O */ 1807 nbp->b_bio1.bio_caller_info2.index = chunksize; 1808 1809 #if SPEC_CHAIN_DEBUG & 1 1810 devfs_debug(DEVFS_DEBUG_DEBUG, 1811 "spec_strategy: chain %p offset %d/%d bcount %d\n", 1812 bp, 0, bp->b_bcount, nbp->b_bcount); 1813 #endif 1814 1815 dev_dstrategy(vp->v_rdev, &nbp->b_bio1); 1816 1817 if (DEVFS_NODE(vp)) { 1818 nanotime(&DEVFS_NODE(vp)->atime); 1819 nanotime(&DEVFS_NODE(vp)->mtime); 1820 } 1821 1822 return (0); 1823 } 1824 1825 /* 1826 * Chunked up transfer completion routine - chain transfers until done 1827 * 1828 * NOTE: MPSAFE callback. 1829 */ 1830 static 1831 void 1832 devfs_spec_strategy_done(struct bio *nbio) 1833 { 1834 struct buf *nbp = nbio->bio_buf; 1835 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */ 1836 struct buf *bp = bio->bio_buf; /* original bp */ 1837 int chunksize = nbio->bio_caller_info2.index; /* chunking */ 1838 int boffset = nbp->b_data - bp->b_data; 1839 1840 if (nbp->b_flags & B_ERROR) { 1841 /* 1842 * An error terminates the chain, propogate the error back 1843 * to the original bp 1844 */ 1845 bp->b_flags |= B_ERROR; 1846 bp->b_error = nbp->b_error; 1847 bp->b_resid = bp->b_bcount - boffset + 1848 (nbp->b_bcount - nbp->b_resid); 1849 #if SPEC_CHAIN_DEBUG & 1 1850 devfs_debug(DEVFS_DEBUG_DEBUG, 1851 "spec_strategy: chain %p error %d bcount %d/%d\n", 1852 bp, bp->b_error, bp->b_bcount, 1853 bp->b_bcount - bp->b_resid); 1854 #endif 1855 } else if (nbp->b_resid) { 1856 /* 1857 * A short read or write terminates the chain 1858 */ 1859 bp->b_error = nbp->b_error; 1860 bp->b_resid = bp->b_bcount - boffset + 1861 (nbp->b_bcount - nbp->b_resid); 1862 #if SPEC_CHAIN_DEBUG & 1 1863 devfs_debug(DEVFS_DEBUG_DEBUG, 1864 "spec_strategy: chain %p short read(1) " 1865 "bcount %d/%d\n", 1866 bp, bp->b_bcount - bp->b_resid, bp->b_bcount); 1867 #endif 1868 } else if (nbp->b_bcount != nbp->b_bufsize) { 1869 /* 1870 * A short read or write can also occur by truncating b_bcount 1871 */ 1872 #if SPEC_CHAIN_DEBUG & 1 1873 devfs_debug(DEVFS_DEBUG_DEBUG, 1874 "spec_strategy: chain %p short read(2) " 1875 "bcount %d/%d\n", 1876 bp, nbp->b_bcount + boffset, bp->b_bcount); 1877 #endif 1878 bp->b_error = 0; 1879 bp->b_bcount = nbp->b_bcount + boffset; 1880 bp->b_resid = nbp->b_resid; 1881 } else if (nbp->b_bcount + boffset == bp->b_bcount) { 1882 /* 1883 * No more data terminates the chain 1884 */ 1885 #if SPEC_CHAIN_DEBUG & 1 1886 devfs_debug(DEVFS_DEBUG_DEBUG, 1887 "spec_strategy: chain %p finished bcount %d\n", 1888 bp, bp->b_bcount); 1889 #endif 1890 bp->b_error = 0; 1891 bp->b_resid = 0; 1892 } else { 1893 /* 1894 * Continue the chain 1895 */ 1896 boffset += nbp->b_bcount; 1897 nbp->b_data = bp->b_data + boffset; 1898 nbp->b_bcount = bp->b_bcount - boffset; 1899 if (nbp->b_bcount > chunksize) 1900 nbp->b_bcount = chunksize; 1901 nbp->b_bio1.bio_done = devfs_spec_strategy_done; 1902 nbp->b_bio1.bio_offset = bio->bio_offset + boffset; 1903 1904 #if SPEC_CHAIN_DEBUG & 1 1905 devfs_debug(DEVFS_DEBUG_DEBUG, 1906 "spec_strategy: chain %p offset %d/%d bcount %d\n", 1907 bp, boffset, bp->b_bcount, nbp->b_bcount); 1908 #endif 1909 1910 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1); 1911 return; 1912 } 1913 1914 /* 1915 * Fall through to here on termination. biodone(bp) and 1916 * clean up and free nbp. 1917 */ 1918 biodone(bio); 1919 BUF_UNLOCK(nbp); 1920 uninitbufbio(nbp); 1921 kfree(nbp, M_DEVBUF); 1922 } 1923 1924 /* 1925 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length) 1926 */ 1927 static int 1928 devfs_spec_freeblks(struct vop_freeblks_args *ap) 1929 { 1930 struct buf *bp; 1931 1932 /* 1933 * Must be a synchronous operation 1934 */ 1935 KKASSERT(ap->a_vp->v_rdev != NULL); 1936 if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0) 1937 return (0); 1938 bp = getpbuf(NULL); 1939 bp->b_cmd = BUF_CMD_FREEBLKS; 1940 bp->b_bio1.bio_flags |= BIO_SYNC; 1941 bp->b_bio1.bio_offset = ap->a_offset; 1942 bp->b_bio1.bio_done = biodone_sync; 1943 bp->b_bcount = ap->a_length; 1944 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1); 1945 biowait(&bp->b_bio1, "TRIM"); 1946 relpbuf(bp, NULL); 1947 1948 return (0); 1949 } 1950 1951 /* 1952 * Implement degenerate case where the block requested is the block 1953 * returned, and assume that the entire device is contiguous in regards 1954 * to the contiguous block range (runp and runb). 1955 * 1956 * spec_bmap(struct vnode *a_vp, off_t a_loffset, 1957 * off_t *a_doffsetp, int *a_runp, int *a_runb) 1958 */ 1959 static int 1960 devfs_spec_bmap(struct vop_bmap_args *ap) 1961 { 1962 if (ap->a_doffsetp != NULL) 1963 *ap->a_doffsetp = ap->a_loffset; 1964 if (ap->a_runp != NULL) 1965 *ap->a_runp = MAXBSIZE; 1966 if (ap->a_runb != NULL) { 1967 if (ap->a_loffset < MAXBSIZE) 1968 *ap->a_runb = (int)ap->a_loffset; 1969 else 1970 *ap->a_runb = MAXBSIZE; 1971 } 1972 return (0); 1973 } 1974 1975 1976 /* 1977 * Special device advisory byte-level locks. 1978 * 1979 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, 1980 * struct flock *a_fl, int a_flags) 1981 */ 1982 /* ARGSUSED */ 1983 static int 1984 devfs_spec_advlock(struct vop_advlock_args *ap) 1985 { 1986 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP); 1987 } 1988 1989 /* 1990 * NOTE: MPSAFE callback. 1991 */ 1992 static void 1993 devfs_spec_getpages_iodone(struct bio *bio) 1994 { 1995 bio->bio_buf->b_cmd = BUF_CMD_DONE; 1996 wakeup(bio->bio_buf); 1997 } 1998 1999 /* 2000 * spec_getpages() - get pages associated with device vnode. 2001 * 2002 * Note that spec_read and spec_write do not use the buffer cache, so we 2003 * must fully implement getpages here. 2004 */ 2005 static int 2006 devfs_spec_getpages(struct vop_getpages_args *ap) 2007 { 2008 vm_offset_t kva; 2009 int error; 2010 int i, pcount, size; 2011 struct buf *bp; 2012 vm_page_t m; 2013 vm_ooffset_t offset; 2014 int toff, nextoff, nread; 2015 struct vnode *vp = ap->a_vp; 2016 int blksiz; 2017 int gotreqpage; 2018 2019 error = 0; 2020 pcount = round_page(ap->a_count) / PAGE_SIZE; 2021 2022 /* 2023 * Calculate the offset of the transfer and do sanity check. 2024 */ 2025 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset; 2026 2027 /* 2028 * Round up physical size for real devices. We cannot round using 2029 * v_mount's block size data because v_mount has nothing to do with 2030 * the device. i.e. it's usually '/dev'. We need the physical block 2031 * size for the device itself. 2032 * 2033 * We can't use v_rdev->si_mountpoint because it only exists when the 2034 * block device is mounted. However, we can use v_rdev. 2035 */ 2036 if (vn_isdisk(vp, NULL)) 2037 blksiz = vp->v_rdev->si_bsize_phys; 2038 else 2039 blksiz = DEV_BSIZE; 2040 2041 size = roundup2(ap->a_count, blksiz); 2042 2043 bp = getpbuf_kva(NULL); 2044 kva = (vm_offset_t)bp->b_data; 2045 2046 /* 2047 * Map the pages to be read into the kva. 2048 */ 2049 pmap_qenter_noinval(kva, ap->a_m, pcount); 2050 2051 /* Build a minimal buffer header. */ 2052 bp->b_cmd = BUF_CMD_READ; 2053 bp->b_flags |= B_KVABIO; 2054 bp->b_bcount = size; 2055 bp->b_resid = 0; 2056 bsetrunningbufspace(bp, size); 2057 2058 bp->b_bio1.bio_offset = offset; 2059 bp->b_bio1.bio_done = devfs_spec_getpages_iodone; 2060 2061 mycpu->gd_cnt.v_vnodein++; 2062 mycpu->gd_cnt.v_vnodepgsin += pcount; 2063 2064 /* Do the input. */ 2065 vn_strategy(ap->a_vp, &bp->b_bio1); 2066 2067 crit_enter(); 2068 2069 /* We definitely need to be at splbio here. */ 2070 while (bp->b_cmd != BUF_CMD_DONE) 2071 tsleep(bp, 0, "spread", 0); 2072 2073 crit_exit(); 2074 2075 if (bp->b_flags & B_ERROR) { 2076 if (bp->b_error) 2077 error = bp->b_error; 2078 else 2079 error = EIO; 2080 } 2081 2082 /* 2083 * If EOF is encountered we must zero-extend the result in order 2084 * to ensure that the page does not contain garabge. When no 2085 * error occurs, an early EOF is indicated if b_bcount got truncated. 2086 * b_resid is relative to b_bcount and should be 0, but some devices 2087 * might indicate an EOF with b_resid instead of truncating b_bcount. 2088 */ 2089 nread = bp->b_bcount - bp->b_resid; 2090 if (nread < ap->a_count) { 2091 bkvasync(bp); 2092 bzero((caddr_t)kva + nread, ap->a_count - nread); 2093 } 2094 pmap_qremove_noinval(kva, pcount); 2095 2096 gotreqpage = 0; 2097 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) { 2098 nextoff = toff + PAGE_SIZE; 2099 m = ap->a_m[i]; 2100 2101 /* 2102 * NOTE: vm_page_undirty/clear_dirty etc do not clear the 2103 * pmap modified bit. pmap modified bit should have 2104 * already been cleared. 2105 */ 2106 if (nextoff <= nread) { 2107 m->valid = VM_PAGE_BITS_ALL; 2108 vm_page_undirty(m); 2109 } else if (toff < nread) { 2110 /* 2111 * Since this is a VM request, we have to supply the 2112 * unaligned offset to allow vm_page_set_valid() 2113 * to zero sub-DEV_BSIZE'd portions of the page. 2114 */ 2115 vm_page_set_valid(m, 0, nread - toff); 2116 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff); 2117 } else { 2118 m->valid = 0; 2119 vm_page_undirty(m); 2120 } 2121 2122 if (i != ap->a_reqpage) { 2123 /* 2124 * Just in case someone was asking for this page we 2125 * now tell them that it is ok to use. 2126 */ 2127 if (!error || (m->valid == VM_PAGE_BITS_ALL)) { 2128 if (m->valid) { 2129 if (m->flags & PG_REFERENCED) { 2130 vm_page_activate(m); 2131 } else { 2132 vm_page_deactivate(m); 2133 } 2134 vm_page_wakeup(m); 2135 } else { 2136 vm_page_free(m); 2137 } 2138 } else { 2139 vm_page_free(m); 2140 } 2141 } else if (m->valid) { 2142 gotreqpage = 1; 2143 /* 2144 * Since this is a VM request, we need to make the 2145 * entire page presentable by zeroing invalid sections. 2146 */ 2147 if (m->valid != VM_PAGE_BITS_ALL) 2148 vm_page_zero_invalid(m, FALSE); 2149 } 2150 } 2151 if (!gotreqpage) { 2152 m = ap->a_m[ap->a_reqpage]; 2153 devfs_debug(DEVFS_DEBUG_WARNING, 2154 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n", 2155 devtoname(vp->v_rdev), error, bp, bp->b_vp); 2156 devfs_debug(DEVFS_DEBUG_WARNING, 2157 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n", 2158 size, bp->b_resid, ap->a_count, m->valid); 2159 devfs_debug(DEVFS_DEBUG_WARNING, 2160 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n", 2161 nread, ap->a_reqpage, (u_long)m->pindex, pcount); 2162 /* 2163 * Free the buffer header back to the swap buffer pool. 2164 */ 2165 relpbuf(bp, NULL); 2166 return VM_PAGER_ERROR; 2167 } 2168 /* 2169 * Free the buffer header back to the swap buffer pool. 2170 */ 2171 relpbuf(bp, NULL); 2172 if (DEVFS_NODE(ap->a_vp)) 2173 nanotime(&DEVFS_NODE(ap->a_vp)->mtime); 2174 return VM_PAGER_OK; 2175 } 2176 2177 static __inline 2178 int 2179 sequential_heuristic(struct uio *uio, struct file *fp) 2180 { 2181 /* 2182 * Sequential heuristic - detect sequential operation 2183 */ 2184 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || 2185 uio->uio_offset == fp->f_nextoff) { 2186 /* 2187 * XXX we assume that the filesystem block size is 2188 * the default. Not true, but still gives us a pretty 2189 * good indicator of how sequential the read operations 2190 * are. 2191 */ 2192 int tmpseq = fp->f_seqcount; 2193 2194 tmpseq += (uio->uio_resid + MAXBSIZE - 1) / MAXBSIZE; 2195 if (tmpseq > IO_SEQMAX) 2196 tmpseq = IO_SEQMAX; 2197 fp->f_seqcount = tmpseq; 2198 return(fp->f_seqcount << IO_SEQSHIFT); 2199 } 2200 2201 /* 2202 * Not sequential, quick draw-down of seqcount 2203 */ 2204 if (fp->f_seqcount > 1) 2205 fp->f_seqcount = 1; 2206 else 2207 fp->f_seqcount = 0; 2208 return(0); 2209 } 2210