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