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