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/caps.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_fpp, vp); 884 vn_lock(vp, LK_SHARED | LK_RETRY); 885 if (error) 886 return error; 887 vop_stdopen(ap); 888 goto skip; 889 } 890 891 /* 892 * Slow code 893 */ 894 vn_lock(vp, LK_UPGRADE | LK_RETRY); 895 if (node && ap->a_fpp) { 896 int exists; 897 898 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n"); 899 lockmgr(&devfs_lock, LK_SHARED); 900 901 ndev = devfs_clone(dev, node->d_dir.d_name, 902 node->d_dir.d_namlen, 903 ap->a_mode, ap->a_cred); 904 if (ndev != NULL) { 905 lockmgr(&devfs_lock, LK_RELEASE); 906 lockmgr(&devfs_lock, LK_EXCLUSIVE); 907 newnode = devfs_create_device_node( 908 DEVFS_MNTDATA(vp->v_mount)->root_node, 909 ndev, &exists, NULL, NULL); 910 /* XXX: possibly destroy device if this happens */ 911 912 if (newnode != NULL) { 913 dev = ndev; 914 if (exists == 0) 915 devfs_link_dev(dev); 916 917 devfs_debug(DEVFS_DEBUG_DEBUG, 918 "parent here is: %s, node is: |%s|\n", 919 ((node->parent->node_type == Nroot) ? 920 "ROOT!" : node->parent->d_dir.d_name), 921 newnode->d_dir.d_name); 922 devfs_debug(DEVFS_DEBUG_DEBUG, 923 "test: %s\n", 924 ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name); 925 926 /* 927 * orig_vp is set to the original vp if we 928 * cloned. 929 */ 930 /* node->flags |= DEVFS_CLONED; */ 931 devfs_allocv(&vp, newnode); 932 orig_vp = ap->a_vp; 933 ap->a_vp = vp; 934 } 935 } 936 lockmgr(&devfs_lock, LK_RELEASE); 937 938 /* 939 * Synchronize devfs here to make sure that, if the cloned 940 * device creates other device nodes in addition to the 941 * cloned one, all of them are created by the time we return 942 * from opening the cloned one. 943 */ 944 if (ndev) 945 devfs_config(); 946 } 947 948 devfs_debug(DEVFS_DEBUG_DEBUG, 949 "devfs_spec_open() called on %s! \n", 950 dev->si_name); 951 952 /* 953 * Make this field valid before any I/O in ->d_open 954 * 955 * NOTE: Shared vnode lock probably held, but its ok as long 956 * as assignments are consistent. 957 */ 958 if (!dev->si_iosize_max) 959 /* XXX: old DFLTPHYS == 64KB dependency */ 960 dev->si_iosize_max = min(MAXPHYS,64*1024); 961 962 if (dev_dflags(dev) & D_TTY) 963 vsetflags(vp, VISTTY); 964 965 /* 966 * Open the underlying device. 967 * 968 * NOTE: If the dev open returns EALREADY it has completed the open 969 * operation and is returning a fully initialized *a->a_fpp 970 * (which it may also have replaced). This includes issuing 971 * any necessary VOP_OPEN(). 972 * 973 * Also, the returned ap->a_fpp might not be DTYPE_VNODE and 974 * if it is might not be using the vp we supplied to it. 975 */ 976 vn_unlock(vp); 977 error = dev_dopen(dev, ap->a_mode, S_IFCHR, 978 ap->a_cred, ap->a_fpp, vp); 979 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 980 981 if (__predict_false(error == EALREADY)) { 982 if (orig_vp) 983 vput(vp); 984 return 0; 985 } 986 987 /* 988 * Clean up any cloned vp if we error out. 989 */ 990 if (__predict_false(error != 0)) { 991 if (orig_vp) { 992 vput(vp); 993 ap->a_vp = orig_vp; 994 /* orig_vp = NULL; */ 995 } 996 return error; 997 } 998 999 /* 1000 * This checks if the disk device is going to be opened for writing. 1001 * It will be only allowed in the cases where securelevel permits it 1002 * and it's not mounted R/W. 1003 */ 1004 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) && 1005 (ap->a_cred != FSCRED)) { 1006 1007 /* Very secure mode. No open for writing allowed */ 1008 if (securelevel >= 2) 1009 return EPERM; 1010 1011 /* 1012 * If it is mounted R/W, do not allow to open for writing. 1013 * In the case it's mounted read-only but securelevel 1014 * is >= 1, then do not allow opening for writing either. 1015 */ 1016 if (vfs_mountedon(vp)) { 1017 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY)) 1018 return EBUSY; 1019 else if (securelevel >= 1) 1020 return EPERM; 1021 } 1022 } 1023 1024 /* 1025 * NOTE: vnode is still locked shared. t_stop assignment should 1026 * remain consistent so we should be ok. 1027 */ 1028 if (dev_dflags(dev) & D_TTY) { 1029 if (dev->si_tty) { 1030 struct tty *tp; 1031 tp = dev->si_tty; 1032 if (!tp->t_stop) { 1033 devfs_debug(DEVFS_DEBUG_DEBUG, 1034 "devfs: no t_stop\n"); 1035 tp->t_stop = nottystop; 1036 } 1037 } 1038 } 1039 1040 /* 1041 * NOTE: vnode is still locked shared. assignments should 1042 * remain consistent so we should be ok. However, 1043 * upgrade to exclusive if we need a VM object. 1044 */ 1045 if (vn_isdisk(vp, NULL)) { 1046 if (!dev->si_bsize_phys) 1047 dev->si_bsize_phys = DEV_BSIZE; 1048 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1); 1049 } 1050 1051 vop_stdopen(ap); 1052 #if 0 1053 if (node) 1054 vfs_timestamp(&node->atime); 1055 #endif 1056 /* 1057 * If we replaced the vp the vop_stdopen() call will have loaded 1058 * it into fp->f_data and vref()d the vp, giving us two refs. So 1059 * instead of just unlocking it here we have to vput() it. 1060 */ 1061 if (orig_vp) 1062 vput(vp); 1063 1064 /* Ugly pty magic, to make pty devices appear once they are opened */ 1065 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY) { 1066 if (node->flags & DEVFS_INVISIBLE) 1067 node->flags &= ~DEVFS_INVISIBLE; 1068 } 1069 1070 skip: 1071 if (ap->a_fpp) { 1072 struct file *fp = *ap->a_fpp; 1073 1074 KKASSERT(fp->f_type == DTYPE_VNODE); 1075 KKASSERT((fp->f_flag & FMASK) == (ap->a_mode & FMASK)); 1076 fp->f_ops = &devfs_dev_fileops; 1077 KKASSERT(fp->f_data == (void *)vp); 1078 } 1079 1080 return 0; 1081 } 1082 1083 static int 1084 devfs_spec_close(struct vop_close_args *ap) 1085 { 1086 struct devfs_node *node; 1087 struct proc *p = curproc; 1088 struct vnode *vp = ap->a_vp; 1089 cdev_t dev = vp->v_rdev; 1090 int error = 0; 1091 int needrelock; 1092 int opencount; 1093 1094 /* 1095 * Devices flagged D_QUICK require no special handling. 1096 */ 1097 if (dev && dev_dflags(dev) & D_QUICK) { 1098 opencount = vp->v_opencount; 1099 if (opencount <= 1) 1100 opencount = count_dev(dev); /* XXX NOT SMP SAFE */ 1101 if (((vp->v_flag & VRECLAIMED) || 1102 (dev_dflags(dev) & D_TRACKCLOSE) || 1103 (opencount == 1))) { 1104 vn_unlock(vp); 1105 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp); 1106 vn_lock(vp, LK_SHARED | LK_RETRY); 1107 } 1108 goto skip; 1109 } 1110 1111 /* 1112 * We do special tests on the opencount so unfortunately we need 1113 * an exclusive lock. 1114 */ 1115 vn_lock(vp, LK_UPGRADE | LK_RETRY); 1116 1117 if (dev) 1118 devfs_debug(DEVFS_DEBUG_DEBUG, 1119 "devfs_spec_close() called on %s! \n", 1120 dev->si_name); 1121 else 1122 devfs_debug(DEVFS_DEBUG_DEBUG, 1123 "devfs_spec_close() called, null vode!\n"); 1124 1125 /* 1126 * A couple of hacks for devices and tty devices. The 1127 * vnode ref count cannot be used to figure out the 1128 * last close, but we can use v_opencount now that 1129 * revoke works properly. 1130 * 1131 * Detect the last close on a controlling terminal and clear 1132 * the session (half-close). 1133 * 1134 * XXX opencount is not SMP safe. The vnode is locked but there 1135 * may be multiple vnodes referencing the same device. 1136 */ 1137 if (dev) { 1138 /* 1139 * NOTE: Try to avoid global tokens when testing opencount 1140 * XXX hack, fixme. needs a struct lock and opencount in 1141 * struct cdev itself. 1142 */ 1143 reference_dev(dev); 1144 opencount = vp->v_opencount; 1145 if (opencount <= 1) 1146 opencount = count_dev(dev); /* XXX NOT SMP SAFE */ 1147 } else { 1148 opencount = 0; 1149 } 1150 1151 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) { 1152 p->p_session->s_ttyvp = NULL; 1153 vrele(vp); 1154 } 1155 1156 /* 1157 * Vnodes can be opened and closed multiple times. Do not really 1158 * close the device unless (1) it is being closed forcibly, 1159 * (2) the device wants to track closes, or (3) this is the last 1160 * vnode doing its last close on the device. 1161 * 1162 * XXX the VXLOCK (force close) case can leave vnodes referencing 1163 * a closed device. This might not occur now that our revoke is 1164 * fixed. 1165 */ 1166 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n"); 1167 if (dev && ((vp->v_flag & VRECLAIMED) || 1168 (dev_dflags(dev) & D_TRACKCLOSE) || 1169 (opencount == 1))) { 1170 /* 1171 * Ugly pty magic, to make pty devices disappear again once 1172 * they are closed. 1173 */ 1174 node = DEVFS_NODE(ap->a_vp); 1175 if (node && (node->flags & DEVFS_PTY)) 1176 node->flags |= DEVFS_INVISIBLE; 1177 1178 /* 1179 * Unlock around dev_dclose(), unless the vnode is 1180 * undergoing a vgone/reclaim (during umount). 1181 */ 1182 needrelock = 0; 1183 if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) { 1184 needrelock = 1; 1185 vn_unlock(vp); 1186 } 1187 1188 /* 1189 * WARNING! If the device destroys itself the devfs node 1190 * can disappear here. 1191 * 1192 * WARNING! vn_lock() will fail if the vp is in a VRECLAIM, 1193 * which can occur during umount. 1194 */ 1195 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp); 1196 /* node is now stale */ 1197 1198 if (needrelock) { 1199 if (vn_lock(vp, LK_EXCLUSIVE | 1200 LK_RETRY | 1201 LK_FAILRECLAIM) != 0) { 1202 panic("devfs_spec_close: vnode %p " 1203 "unexpectedly could not be relocked", 1204 vp); 1205 } 1206 } 1207 } else { 1208 error = 0; 1209 } 1210 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n"); 1211 1212 /* 1213 * Track the actual opens and closes on the vnode. The last close 1214 * disassociates the rdev. If the rdev is already disassociated or 1215 * the opencount is already 0, the vnode might have been revoked 1216 * and no further opencount tracking occurs. 1217 */ 1218 if (dev) 1219 release_dev(dev); 1220 skip: 1221 if (vp->v_opencount > 0) 1222 vop_stdclose(ap); 1223 return(error); 1224 1225 } 1226 1227 1228 static int 1229 devfs_fo_close(struct file *fp) 1230 { 1231 struct vnode *vp = (struct vnode *)fp->f_data; 1232 int error; 1233 1234 fp->f_ops = &badfileops; 1235 error = vn_close(vp, fp->f_flag, fp); 1236 devfs_clear_cdevpriv(fp); 1237 1238 return (error); 1239 } 1240 1241 1242 /* 1243 * Device-optimized file table vnode read routine. 1244 * 1245 * This bypasses the VOP table and talks directly to the device. Most 1246 * filesystems just route to specfs and can make this optimization. 1247 */ 1248 static int 1249 devfs_fo_read(struct file *fp, struct uio *uio, 1250 struct ucred *cred, int flags) 1251 { 1252 struct devfs_node *node; 1253 struct vnode *vp; 1254 int ioflag; 1255 int error; 1256 cdev_t dev; 1257 1258 KASSERT(uio->uio_td == curthread, 1259 ("uio_td %p is not td %p", uio->uio_td, curthread)); 1260 1261 if (uio->uio_resid == 0) 1262 return 0; 1263 1264 vp = (struct vnode *)fp->f_data; 1265 if (vp == NULL || vp->v_type == VBAD) 1266 return EBADF; 1267 1268 node = DEVFS_NODE(vp); 1269 1270 if ((dev = vp->v_rdev) == NULL) 1271 return EBADF; 1272 1273 reference_dev(dev); 1274 1275 if ((flags & O_FOFFSET) == 0) 1276 uio->uio_offset = fp->f_offset; 1277 1278 ioflag = 0; 1279 if (flags & O_FBLOCKING) { 1280 /* ioflag &= ~IO_NDELAY; */ 1281 } else if (flags & O_FNONBLOCKING) { 1282 ioflag |= IO_NDELAY; 1283 } else if (fp->f_flag & FNONBLOCK) { 1284 ioflag |= IO_NDELAY; 1285 } 1286 if (fp->f_flag & O_DIRECT) { 1287 ioflag |= IO_DIRECT; 1288 } 1289 ioflag |= sequential_heuristic(uio, fp); 1290 1291 error = dev_dread(dev, uio, ioflag, fp); 1292 1293 release_dev(dev); 1294 if (node) 1295 vfs_timestamp(&node->atime); 1296 if ((flags & O_FOFFSET) == 0) 1297 fp->f_offset = uio->uio_offset; 1298 fp->f_nextoff = uio->uio_offset; 1299 1300 return (error); 1301 } 1302 1303 1304 static int 1305 devfs_fo_write(struct file *fp, struct uio *uio, 1306 struct ucred *cred, int flags) 1307 { 1308 struct devfs_node *node; 1309 struct vnode *vp; 1310 int ioflag; 1311 int error; 1312 cdev_t dev; 1313 1314 KASSERT(uio->uio_td == curthread, 1315 ("uio_td %p is not p %p", uio->uio_td, curthread)); 1316 1317 vp = (struct vnode *)fp->f_data; 1318 if (vp == NULL || vp->v_type == VBAD) 1319 return EBADF; 1320 1321 node = DEVFS_NODE(vp); 1322 1323 if (vp->v_type == VREG) 1324 bwillwrite(uio->uio_resid); 1325 1326 vp = (struct vnode *)fp->f_data; 1327 1328 if ((dev = vp->v_rdev) == NULL) 1329 return EBADF; 1330 1331 reference_dev(dev); 1332 1333 if ((flags & O_FOFFSET) == 0) 1334 uio->uio_offset = fp->f_offset; 1335 1336 ioflag = IO_UNIT; 1337 if (vp->v_type == VREG && 1338 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) { 1339 ioflag |= IO_APPEND; 1340 } 1341 1342 if (flags & O_FBLOCKING) { 1343 /* ioflag &= ~IO_NDELAY; */ 1344 } else if (flags & O_FNONBLOCKING) { 1345 ioflag |= IO_NDELAY; 1346 } else if (fp->f_flag & FNONBLOCK) { 1347 ioflag |= IO_NDELAY; 1348 } 1349 if (fp->f_flag & O_DIRECT) { 1350 ioflag |= IO_DIRECT; 1351 } 1352 if (flags & O_FASYNCWRITE) { 1353 /* ioflag &= ~IO_SYNC; */ 1354 } else if (flags & O_FSYNCWRITE) { 1355 ioflag |= IO_SYNC; 1356 } else if (fp->f_flag & O_FSYNC) { 1357 ioflag |= IO_SYNC; 1358 } 1359 1360 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)) 1361 ioflag |= IO_SYNC; 1362 ioflag |= sequential_heuristic(uio, fp); 1363 1364 error = dev_dwrite(dev, uio, ioflag, fp); 1365 1366 release_dev(dev); 1367 if (node) { 1368 vfs_timestamp(&node->atime); 1369 vfs_timestamp(&node->mtime); 1370 } 1371 1372 if ((flags & O_FOFFSET) == 0) 1373 fp->f_offset = uio->uio_offset; 1374 fp->f_nextoff = uio->uio_offset; 1375 1376 return (error); 1377 } 1378 1379 1380 static int 1381 devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred) 1382 { 1383 struct vnode *vp; 1384 struct vattr vattr; 1385 struct vattr *vap; 1386 u_short mode; 1387 cdev_t dev; 1388 int error; 1389 1390 vp = (struct vnode *)fp->f_data; 1391 if (vp == NULL || vp->v_type == VBAD) 1392 return EBADF; 1393 1394 error = vn_stat(vp, sb, cred); 1395 if (error) 1396 return (error); 1397 1398 vap = &vattr; 1399 error = VOP_GETATTR(vp, vap); 1400 if (error) 1401 return (error); 1402 1403 /* 1404 * Zero the spare stat fields 1405 */ 1406 sb->st_lspare = 0; 1407 sb->st_qspare2 = 0; 1408 1409 /* 1410 * Copy from vattr table ... or not in case it's a cloned device 1411 */ 1412 if (vap->va_fsid != VNOVAL) 1413 sb->st_dev = vap->va_fsid; 1414 else 1415 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 1416 1417 sb->st_ino = vap->va_fileid; 1418 1419 mode = vap->va_mode; 1420 mode |= S_IFCHR; 1421 sb->st_mode = mode; 1422 1423 if (vap->va_nlink > (nlink_t)-1) 1424 sb->st_nlink = (nlink_t)-1; 1425 else 1426 sb->st_nlink = vap->va_nlink; 1427 1428 sb->st_uid = vap->va_uid; 1429 sb->st_gid = vap->va_gid; 1430 sb->st_rdev = devid_from_dev(DEVFS_NODE(vp)->d_dev); 1431 sb->st_size = vap->va_bytes; 1432 sb->st_atimespec = vap->va_atime; 1433 sb->st_mtimespec = vap->va_mtime; 1434 sb->st_ctimespec = vap->va_ctime; 1435 1436 /* 1437 * A VCHR and VBLK device may track the last access and last modified 1438 * time independantly of the filesystem. This is particularly true 1439 * because device read and write calls may bypass the filesystem. 1440 */ 1441 if (vp->v_type == VCHR || vp->v_type == VBLK) { 1442 dev = vp->v_rdev; 1443 if (dev != NULL) { 1444 if (dev->si_lastread) { 1445 sb->st_atimespec.tv_sec = time_second + 1446 (dev->si_lastread - 1447 time_uptime); 1448 sb->st_atimespec.tv_nsec = 0; 1449 } 1450 if (dev->si_lastwrite) { 1451 sb->st_mtimespec.tv_sec = time_second + 1452 (dev->si_lastwrite - 1453 time_uptime); 1454 sb->st_mtimespec.tv_nsec = 0; 1455 } 1456 } 1457 } 1458 1459 /* 1460 * According to www.opengroup.org, the meaning of st_blksize is 1461 * "a filesystem-specific preferred I/O block size for this 1462 * object. In some filesystem types, this may vary from file 1463 * to file" 1464 * Default to PAGE_SIZE after much discussion. 1465 */ 1466 1467 sb->st_blksize = PAGE_SIZE; 1468 1469 sb->st_flags = vap->va_flags; 1470 1471 error = caps_priv_check(cred, SYSCAP_NOVFS_GENERATION); 1472 if (error) 1473 sb->st_gen = 0; 1474 else 1475 sb->st_gen = (u_int32_t)vap->va_gen; 1476 1477 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 1478 1479 /* 1480 * This is for ABI compatibility <= 5.7 (for ABI change made in 1481 * 5.7 master). 1482 */ 1483 sb->__old_st_blksize = sb->st_blksize; 1484 1485 return (0); 1486 } 1487 1488 1489 static int 1490 devfs_fo_kqfilter(struct file *fp, struct knote *kn) 1491 { 1492 struct vnode *vp; 1493 int error; 1494 cdev_t dev; 1495 1496 vp = (struct vnode *)fp->f_data; 1497 if (vp == NULL || vp->v_type == VBAD) { 1498 error = EBADF; 1499 goto done; 1500 } 1501 if ((dev = vp->v_rdev) == NULL) { 1502 error = EBADF; 1503 goto done; 1504 } 1505 reference_dev(dev); 1506 1507 error = dev_dkqfilter(dev, kn, fp); 1508 1509 release_dev(dev); 1510 1511 done: 1512 return (error); 1513 } 1514 1515 static int 1516 devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data, 1517 struct ucred *ucred, struct sysmsg *msg) 1518 { 1519 #if 0 1520 struct devfs_node *node; 1521 #endif 1522 struct vnode *vp; 1523 struct vnode *ovp; 1524 cdev_t dev; 1525 int error; 1526 struct fiodname_args *name_args; 1527 size_t namlen; 1528 const char *name; 1529 1530 vp = ((struct vnode *)fp->f_data); 1531 1532 if ((dev = vp->v_rdev) == NULL) 1533 return EBADF; /* device was revoked */ 1534 1535 reference_dev(dev); 1536 1537 #if 0 1538 node = DEVFS_NODE(vp); 1539 #endif 1540 1541 devfs_debug(DEVFS_DEBUG_DEBUG, 1542 "devfs_fo_ioctl() called! for dev %s\n", 1543 dev->si_name); 1544 1545 if (com == FIODTYPE) { 1546 *(int *)data = dev_dflags(dev) & D_TYPEMASK; 1547 error = 0; 1548 goto out; 1549 } else if (com == FIODNAME) { 1550 name_args = (struct fiodname_args *)data; 1551 name = dev->si_name; 1552 namlen = strlen(name) + 1; 1553 1554 devfs_debug(DEVFS_DEBUG_DEBUG, 1555 "ioctl, got: FIODNAME for %s\n", name); 1556 1557 if (namlen <= name_args->len) 1558 error = copyout(dev->si_name, name_args->name, namlen); 1559 else 1560 error = EINVAL; 1561 1562 devfs_debug(DEVFS_DEBUG_DEBUG, 1563 "ioctl stuff: error: %d\n", error); 1564 goto out; 1565 } 1566 1567 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp); 1568 1569 #if 0 1570 if (node) { 1571 vfs_timestamp(&node->atime); 1572 vfs_timestamp(&node->mtime); 1573 } 1574 #endif 1575 if (com == TIOCSCTTY) { 1576 devfs_debug(DEVFS_DEBUG_DEBUG, 1577 "devfs_fo_ioctl: got TIOCSCTTY on %s\n", 1578 dev->si_name); 1579 } 1580 if (error == 0 && com == TIOCSCTTY) { 1581 struct proc *p = curthread->td_proc; 1582 struct session *sess; 1583 1584 devfs_debug(DEVFS_DEBUG_DEBUG, 1585 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n", 1586 dev->si_name); 1587 if (p == NULL) { 1588 error = ENOTTY; 1589 goto out; 1590 } 1591 sess = p->p_session; 1592 1593 /* 1594 * Do nothing if reassigning same control tty 1595 */ 1596 if (sess->s_ttyvp == vp) { 1597 error = 0; 1598 goto out; 1599 } 1600 1601 /* 1602 * Get rid of reference to old control tty 1603 */ 1604 ovp = sess->s_ttyvp; 1605 vref(vp); 1606 sess->s_ttyvp = vp; 1607 if (ovp) 1608 vrele(ovp); 1609 } 1610 1611 out: 1612 release_dev(dev); 1613 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n"); 1614 return (error); 1615 } 1616 1617 1618 static int 1619 devfs_spec_fsync(struct vop_fsync_args *ap) 1620 { 1621 struct vnode *vp = ap->a_vp; 1622 int error; 1623 1624 if (!vn_isdisk(vp, NULL)) 1625 return (0); 1626 1627 /* 1628 * Flush all dirty buffers associated with a block device. 1629 */ 1630 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL); 1631 return (error); 1632 } 1633 1634 static int 1635 devfs_spec_read(struct vop_read_args *ap) 1636 { 1637 struct devfs_node *node; 1638 struct vnode *vp; 1639 struct uio *uio; 1640 cdev_t dev; 1641 int error; 1642 1643 vp = ap->a_vp; 1644 dev = vp->v_rdev; 1645 uio = ap->a_uio; 1646 node = DEVFS_NODE(vp); 1647 1648 if (dev == NULL) /* device was revoked */ 1649 return (EBADF); 1650 if (uio->uio_resid == 0) 1651 return (0); 1652 1653 vn_unlock(vp); 1654 error = dev_dread(dev, uio, ap->a_ioflag, NULL); 1655 vn_lock(vp, LK_SHARED | LK_RETRY); 1656 1657 if (node) 1658 vfs_timestamp(&node->atime); 1659 1660 return (error); 1661 } 1662 1663 /* 1664 * Vnode op for write 1665 * 1666 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 1667 * struct ucred *a_cred) 1668 */ 1669 static int 1670 devfs_spec_write(struct vop_write_args *ap) 1671 { 1672 struct devfs_node *node; 1673 struct vnode *vp; 1674 struct uio *uio; 1675 cdev_t dev; 1676 int error; 1677 1678 vp = ap->a_vp; 1679 dev = vp->v_rdev; 1680 uio = ap->a_uio; 1681 node = DEVFS_NODE(vp); 1682 1683 KKASSERT(uio->uio_segflg != UIO_NOCOPY); 1684 1685 if (dev == NULL) /* device was revoked */ 1686 return (EBADF); 1687 1688 vn_unlock(vp); 1689 error = dev_dwrite(dev, uio, ap->a_ioflag, NULL); 1690 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1691 1692 if (node) { 1693 vfs_timestamp(&node->atime); 1694 vfs_timestamp(&node->mtime); 1695 } 1696 1697 return (error); 1698 } 1699 1700 /* 1701 * Device ioctl operation. 1702 * 1703 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, 1704 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg) 1705 */ 1706 static int 1707 devfs_spec_ioctl(struct vop_ioctl_args *ap) 1708 { 1709 struct vnode *vp = ap->a_vp; 1710 #if 0 1711 struct devfs_node *node; 1712 #endif 1713 cdev_t dev; 1714 1715 if ((dev = vp->v_rdev) == NULL) 1716 return (EBADF); /* device was revoked */ 1717 #if 0 1718 node = DEVFS_NODE(vp); 1719 1720 if (node) { 1721 vfs_timestamp(&node->atime); 1722 vfs_timestamp(&node->mtime); 1723 } 1724 #endif 1725 1726 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag, 1727 ap->a_cred, ap->a_sysmsg, NULL)); 1728 } 1729 1730 /* 1731 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn) 1732 */ 1733 /* ARGSUSED */ 1734 static int 1735 devfs_spec_kqfilter(struct vop_kqfilter_args *ap) 1736 { 1737 struct vnode *vp = ap->a_vp; 1738 #if 0 1739 struct devfs_node *node; 1740 #endif 1741 cdev_t dev; 1742 1743 if ((dev = vp->v_rdev) == NULL) 1744 return (EBADF); /* device was revoked (EBADF) */ 1745 #if 0 1746 node = DEVFS_NODE(vp); 1747 1748 if (node) 1749 vfs_timestamp(&node->atime); 1750 #endif 1751 1752 return (dev_dkqfilter(dev, ap->a_kn, NULL)); 1753 } 1754 1755 /* 1756 * Convert a vnode strategy call into a device strategy call. Vnode strategy 1757 * calls are not limited to device DMA limits so we have to deal with the 1758 * case. 1759 * 1760 * spec_strategy(struct vnode *a_vp, struct bio *a_bio) 1761 */ 1762 static int 1763 devfs_spec_strategy(struct vop_strategy_args *ap) 1764 { 1765 struct bio *bio = ap->a_bio; 1766 struct buf *bp = bio->bio_buf; 1767 struct buf *nbp; 1768 struct vnode *vp; 1769 struct mount *mp; 1770 int chunksize; 1771 int maxiosize; 1772 1773 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL) 1774 buf_start(bp); 1775 1776 /* 1777 * Collect statistics on synchronous and asynchronous read 1778 * and write counts for disks that have associated filesystems. 1779 */ 1780 vp = ap->a_vp; 1781 KKASSERT(vp->v_rdev != NULL); /* XXX */ 1782 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) { 1783 if (bp->b_cmd == BUF_CMD_READ) { 1784 if (bp->b_flags & BIO_SYNC) 1785 mp->mnt_stat.f_syncreads++; 1786 else 1787 mp->mnt_stat.f_asyncreads++; 1788 } else { 1789 if (bp->b_flags & BIO_SYNC) 1790 mp->mnt_stat.f_syncwrites++; 1791 else 1792 mp->mnt_stat.f_asyncwrites++; 1793 } 1794 } 1795 1796 /* 1797 * Device iosize limitations only apply to read and write. Shortcut 1798 * the I/O if it fits. 1799 */ 1800 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) { 1801 devfs_debug(DEVFS_DEBUG_DEBUG, 1802 "%s: si_iosize_max not set!\n", 1803 dev_dname(vp->v_rdev)); 1804 maxiosize = MAXPHYS; 1805 } 1806 #if SPEC_CHAIN_DEBUG & 2 1807 maxiosize = 4096; 1808 #endif 1809 if (bp->b_bcount <= maxiosize || 1810 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) { 1811 dev_dstrategy_chain(vp->v_rdev, bio); 1812 return (0); 1813 } 1814 1815 /* 1816 * Clone the buffer and set up an I/O chain to chunk up the I/O. 1817 */ 1818 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO); 1819 initbufbio(nbp); 1820 buf_dep_init(nbp); 1821 BUF_LOCK(nbp, LK_EXCLUSIVE); 1822 BUF_KERNPROC(nbp); 1823 nbp->b_vp = vp; 1824 nbp->b_flags = B_PAGING | B_KVABIO | (bp->b_flags & B_BNOCLIP); 1825 nbp->b_cpumask = bp->b_cpumask; 1826 nbp->b_data = bp->b_data; 1827 nbp->b_bio1.bio_done = devfs_spec_strategy_done; 1828 nbp->b_bio1.bio_offset = bio->bio_offset; 1829 nbp->b_bio1.bio_caller_info1.ptr = bio; 1830 1831 /* 1832 * Start the first transfer 1833 */ 1834 if (vn_isdisk(vp, NULL)) 1835 chunksize = vp->v_rdev->si_bsize_phys; 1836 else 1837 chunksize = DEV_BSIZE; 1838 chunksize = rounddown(maxiosize, chunksize); 1839 #if SPEC_CHAIN_DEBUG & 1 1840 devfs_debug(DEVFS_DEBUG_DEBUG, 1841 "spec_strategy chained I/O chunksize=%d\n", 1842 chunksize); 1843 #endif 1844 nbp->b_cmd = bp->b_cmd; 1845 nbp->b_bcount = chunksize; 1846 nbp->b_bufsize = chunksize; /* used to detect a short I/O */ 1847 nbp->b_bio1.bio_caller_info2.index = chunksize; 1848 1849 #if SPEC_CHAIN_DEBUG & 1 1850 devfs_debug(DEVFS_DEBUG_DEBUG, 1851 "spec_strategy: chain %p offset %d/%d bcount %d\n", 1852 bp, 0, bp->b_bcount, nbp->b_bcount); 1853 #endif 1854 1855 dev_dstrategy(vp->v_rdev, &nbp->b_bio1); 1856 1857 if (DEVFS_NODE(vp)) { 1858 vfs_timestamp(&DEVFS_NODE(vp)->atime); 1859 vfs_timestamp(&DEVFS_NODE(vp)->mtime); 1860 } 1861 1862 return (0); 1863 } 1864 1865 /* 1866 * Chunked up transfer completion routine - chain transfers until done 1867 * 1868 * NOTE: MPSAFE callback. 1869 */ 1870 static 1871 void 1872 devfs_spec_strategy_done(struct bio *nbio) 1873 { 1874 struct buf *nbp = nbio->bio_buf; 1875 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */ 1876 struct buf *bp = bio->bio_buf; /* original bp */ 1877 int chunksize = nbio->bio_caller_info2.index; /* chunking */ 1878 int boffset = nbp->b_data - bp->b_data; 1879 1880 if (nbp->b_flags & B_ERROR) { 1881 /* 1882 * An error terminates the chain, propogate the error back 1883 * to the original bp 1884 */ 1885 bp->b_flags |= B_ERROR; 1886 bp->b_error = nbp->b_error; 1887 bp->b_resid = bp->b_bcount - boffset + 1888 (nbp->b_bcount - nbp->b_resid); 1889 #if SPEC_CHAIN_DEBUG & 1 1890 devfs_debug(DEVFS_DEBUG_DEBUG, 1891 "spec_strategy: chain %p error %d bcount %d/%d\n", 1892 bp, bp->b_error, bp->b_bcount, 1893 bp->b_bcount - bp->b_resid); 1894 #endif 1895 } else if (nbp->b_resid) { 1896 /* 1897 * A short read or write terminates the chain 1898 */ 1899 bp->b_error = nbp->b_error; 1900 bp->b_resid = bp->b_bcount - boffset + 1901 (nbp->b_bcount - nbp->b_resid); 1902 #if SPEC_CHAIN_DEBUG & 1 1903 devfs_debug(DEVFS_DEBUG_DEBUG, 1904 "spec_strategy: chain %p short read(1) " 1905 "bcount %d/%d\n", 1906 bp, bp->b_bcount - bp->b_resid, bp->b_bcount); 1907 #endif 1908 } else if (nbp->b_bcount != nbp->b_bufsize) { 1909 /* 1910 * A short read or write can also occur by truncating b_bcount 1911 */ 1912 #if SPEC_CHAIN_DEBUG & 1 1913 devfs_debug(DEVFS_DEBUG_DEBUG, 1914 "spec_strategy: chain %p short read(2) " 1915 "bcount %d/%d\n", 1916 bp, nbp->b_bcount + boffset, bp->b_bcount); 1917 #endif 1918 bp->b_error = 0; 1919 bp->b_bcount = nbp->b_bcount + boffset; 1920 bp->b_resid = nbp->b_resid; 1921 } else if (nbp->b_bcount + boffset == bp->b_bcount) { 1922 /* 1923 * No more data terminates the chain 1924 */ 1925 #if SPEC_CHAIN_DEBUG & 1 1926 devfs_debug(DEVFS_DEBUG_DEBUG, 1927 "spec_strategy: chain %p finished bcount %d\n", 1928 bp, bp->b_bcount); 1929 #endif 1930 bp->b_error = 0; 1931 bp->b_resid = 0; 1932 } else { 1933 /* 1934 * Continue the chain 1935 */ 1936 boffset += nbp->b_bcount; 1937 nbp->b_data = bp->b_data + boffset; 1938 nbp->b_bcount = bp->b_bcount - boffset; 1939 if (nbp->b_bcount > chunksize) 1940 nbp->b_bcount = chunksize; 1941 nbp->b_bio1.bio_done = devfs_spec_strategy_done; 1942 nbp->b_bio1.bio_offset = bio->bio_offset + boffset; 1943 1944 #if SPEC_CHAIN_DEBUG & 1 1945 devfs_debug(DEVFS_DEBUG_DEBUG, 1946 "spec_strategy: chain %p offset %d/%d bcount %d\n", 1947 bp, boffset, bp->b_bcount, nbp->b_bcount); 1948 #endif 1949 1950 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1); 1951 return; 1952 } 1953 1954 /* 1955 * Fall through to here on termination. biodone(bp) and 1956 * clean up and free nbp. 1957 */ 1958 biodone(bio); 1959 BUF_UNLOCK(nbp); 1960 uninitbufbio(nbp); 1961 kfree(nbp, M_DEVBUF); 1962 } 1963 1964 /* 1965 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length) 1966 */ 1967 static int 1968 devfs_spec_freeblks(struct vop_freeblks_args *ap) 1969 { 1970 struct buf *bp; 1971 1972 /* 1973 * Must be a synchronous operation 1974 */ 1975 KKASSERT(ap->a_vp->v_rdev != NULL); 1976 if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0) 1977 return (0); 1978 bp = getpbuf(NULL); 1979 bp->b_cmd = BUF_CMD_FREEBLKS; 1980 bp->b_bio1.bio_flags |= BIO_SYNC; 1981 bp->b_bio1.bio_offset = ap->a_offset; 1982 bp->b_bio1.bio_done = biodone_sync; 1983 bp->b_bcount = ap->a_length; 1984 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1); 1985 biowait(&bp->b_bio1, "TRIM"); 1986 relpbuf(bp, NULL); 1987 1988 return (0); 1989 } 1990 1991 /* 1992 * Implement degenerate case where the block requested is the block 1993 * returned, and assume that the entire device is contiguous in regards 1994 * to the contiguous block range (runp and runb). 1995 * 1996 * spec_bmap(struct vnode *a_vp, off_t a_loffset, 1997 * off_t *a_doffsetp, int *a_runp, int *a_runb) 1998 */ 1999 static int 2000 devfs_spec_bmap(struct vop_bmap_args *ap) 2001 { 2002 if (ap->a_doffsetp != NULL) 2003 *ap->a_doffsetp = ap->a_loffset; 2004 if (ap->a_runp != NULL) 2005 *ap->a_runp = MAXBSIZE; 2006 if (ap->a_runb != NULL) { 2007 if (ap->a_loffset < MAXBSIZE) 2008 *ap->a_runb = (int)ap->a_loffset; 2009 else 2010 *ap->a_runb = MAXBSIZE; 2011 } 2012 return (0); 2013 } 2014 2015 2016 /* 2017 * Special device advisory byte-level locks. 2018 * 2019 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, 2020 * struct flock *a_fl, int a_flags) 2021 */ 2022 /* ARGSUSED */ 2023 static int 2024 devfs_spec_advlock(struct vop_advlock_args *ap) 2025 { 2026 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP); 2027 } 2028 2029 /* 2030 * NOTE: MPSAFE callback. 2031 */ 2032 static void 2033 devfs_spec_getpages_iodone(struct bio *bio) 2034 { 2035 bio->bio_buf->b_cmd = BUF_CMD_DONE; 2036 wakeup(bio->bio_buf); 2037 } 2038 2039 /* 2040 * spec_getpages() - get pages associated with device vnode. 2041 * 2042 * Note that spec_read and spec_write do not use the buffer cache, so we 2043 * must fully implement getpages here. 2044 */ 2045 static int 2046 devfs_spec_getpages(struct vop_getpages_args *ap) 2047 { 2048 vm_offset_t kva; 2049 int error; 2050 int i, pcount, size; 2051 struct buf *bp; 2052 vm_page_t m; 2053 vm_ooffset_t offset; 2054 int toff, nextoff, nread; 2055 struct vnode *vp = ap->a_vp; 2056 int blksiz; 2057 int gotreqpage; 2058 2059 error = 0; 2060 pcount = round_page(ap->a_count) / PAGE_SIZE; 2061 2062 /* 2063 * Calculate the offset of the transfer and do sanity check. 2064 */ 2065 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset; 2066 2067 /* 2068 * Round up physical size for real devices. We cannot round using 2069 * v_mount's block size data because v_mount has nothing to do with 2070 * the device. i.e. it's usually '/dev'. We need the physical block 2071 * size for the device itself. 2072 * 2073 * We can't use v_rdev->si_mountpoint because it only exists when the 2074 * block device is mounted. However, we can use v_rdev. 2075 */ 2076 if (vn_isdisk(vp, NULL)) 2077 blksiz = vp->v_rdev->si_bsize_phys; 2078 else 2079 blksiz = DEV_BSIZE; 2080 2081 size = roundup2(ap->a_count, blksiz); 2082 2083 bp = getpbuf_kva(NULL); 2084 kva = (vm_offset_t)bp->b_data; 2085 2086 /* 2087 * Map the pages to be read into the kva. 2088 */ 2089 pmap_qenter_noinval(kva, ap->a_m, pcount); 2090 2091 /* Build a minimal buffer header. */ 2092 bp->b_cmd = BUF_CMD_READ; 2093 bp->b_flags |= B_KVABIO; 2094 bp->b_bcount = size; 2095 bp->b_resid = 0; 2096 bsetrunningbufspace(bp, size); 2097 2098 bp->b_bio1.bio_offset = offset; 2099 bp->b_bio1.bio_done = devfs_spec_getpages_iodone; 2100 2101 mycpu->gd_cnt.v_vnodein++; 2102 mycpu->gd_cnt.v_vnodepgsin += pcount; 2103 2104 /* Do the input. */ 2105 vn_strategy(ap->a_vp, &bp->b_bio1); 2106 2107 crit_enter(); 2108 2109 /* We definitely need to be at splbio here. */ 2110 while (bp->b_cmd != BUF_CMD_DONE) 2111 tsleep(bp, 0, "spread", 0); 2112 2113 crit_exit(); 2114 2115 if (bp->b_flags & B_ERROR) { 2116 if (bp->b_error) 2117 error = bp->b_error; 2118 else 2119 error = EIO; 2120 } 2121 2122 /* 2123 * If EOF is encountered we must zero-extend the result in order 2124 * to ensure that the page does not contain garabge. When no 2125 * error occurs, an early EOF is indicated if b_bcount got truncated. 2126 * b_resid is relative to b_bcount and should be 0, but some devices 2127 * might indicate an EOF with b_resid instead of truncating b_bcount. 2128 */ 2129 nread = bp->b_bcount - bp->b_resid; 2130 if (nread < ap->a_count) { 2131 bkvasync(bp); 2132 bzero((caddr_t)kva + nread, ap->a_count - nread); 2133 } 2134 pmap_qremove_noinval(kva, pcount); 2135 2136 gotreqpage = 0; 2137 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) { 2138 nextoff = toff + PAGE_SIZE; 2139 m = ap->a_m[i]; 2140 2141 /* 2142 * NOTE: vm_page_undirty/clear_dirty etc do not clear the 2143 * pmap modified bit. pmap modified bit should have 2144 * already been cleared. 2145 */ 2146 if (nextoff <= nread) { 2147 m->valid = VM_PAGE_BITS_ALL; 2148 vm_page_undirty(m); 2149 } else if (toff < nread) { 2150 /* 2151 * Since this is a VM request, we have to supply the 2152 * unaligned offset to allow vm_page_set_valid() 2153 * to zero sub-DEV_BSIZE'd portions of the page. 2154 */ 2155 vm_page_set_valid(m, 0, nread - toff); 2156 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff); 2157 } else { 2158 m->valid = 0; 2159 vm_page_undirty(m); 2160 } 2161 2162 if (i != ap->a_reqpage) { 2163 /* 2164 * Just in case someone was asking for this page we 2165 * now tell them that it is ok to use. 2166 */ 2167 if (!error || (m->valid == VM_PAGE_BITS_ALL)) { 2168 if (m->valid) { 2169 if (m->flags & PG_REFERENCED) { 2170 vm_page_activate(m); 2171 } else { 2172 vm_page_deactivate(m); 2173 } 2174 vm_page_wakeup(m); 2175 } else { 2176 vm_page_free(m); 2177 } 2178 } else { 2179 vm_page_free(m); 2180 } 2181 } else if (m->valid) { 2182 gotreqpage = 1; 2183 /* 2184 * Since this is a VM request, we need to make the 2185 * entire page presentable by zeroing invalid sections. 2186 */ 2187 if (m->valid != VM_PAGE_BITS_ALL) 2188 vm_page_zero_invalid(m, FALSE); 2189 } 2190 } 2191 if (!gotreqpage) { 2192 m = ap->a_m[ap->a_reqpage]; 2193 devfs_debug(DEVFS_DEBUG_WARNING, 2194 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n", 2195 devtoname(vp->v_rdev), error, bp, bp->b_vp); 2196 devfs_debug(DEVFS_DEBUG_WARNING, 2197 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n", 2198 size, bp->b_resid, ap->a_count, m->valid); 2199 devfs_debug(DEVFS_DEBUG_WARNING, 2200 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n", 2201 nread, ap->a_reqpage, (u_long)m->pindex, pcount); 2202 /* 2203 * Free the buffer header back to the swap buffer pool. 2204 */ 2205 relpbuf(bp, NULL); 2206 return VM_PAGER_ERROR; 2207 } 2208 /* 2209 * Free the buffer header back to the swap buffer pool. 2210 */ 2211 relpbuf(bp, NULL); 2212 if (DEVFS_NODE(ap->a_vp)) 2213 vfs_timestamp(&DEVFS_NODE(ap->a_vp)->mtime); 2214 return VM_PAGER_OK; 2215 } 2216 2217 static __inline 2218 int 2219 sequential_heuristic(struct uio *uio, struct file *fp) 2220 { 2221 /* 2222 * Sequential heuristic - detect sequential operation 2223 */ 2224 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || 2225 uio->uio_offset == fp->f_nextoff) { 2226 /* 2227 * XXX we assume that the filesystem block size is 2228 * the default. Not true, but still gives us a pretty 2229 * good indicator of how sequential the read operations 2230 * are. 2231 */ 2232 int tmpseq = fp->f_seqcount; 2233 2234 tmpseq += howmany(uio->uio_resid, MAXBSIZE); 2235 if (tmpseq > IO_SEQMAX) 2236 tmpseq = IO_SEQMAX; 2237 fp->f_seqcount = tmpseq; 2238 return(fp->f_seqcount << IO_SEQSHIFT); 2239 } 2240 2241 /* 2242 * Not sequential, quick draw-down of seqcount 2243 */ 2244 if (fp->f_seqcount > 1) 2245 fp->f_seqcount = 1; 2246 else 2247 fp->f_seqcount = 0; 2248 return(0); 2249 } 2250