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