1 /* 2 * Copyright (c) 1990 University of Utah. 3 * Copyright (c) 1991 The Regents of the University of California. 4 * All rights reserved. 5 * Copyright (c) 1993, 1994 John S. Dyson 6 * Copyright (c) 1995, David Greenman 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91 41 * $Id: vnode_pager.c,v 1.63 1996/08/21 21:56:23 dyson Exp $ 42 */ 43 44 /* 45 * Page to/from files (vnodes). 46 */ 47 48 /* 49 * TODO: 50 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 51 * greatly re-simplify the vnode_pager. 52 */ 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/kernel.h> 57 #include <sys/proc.h> 58 #include <sys/malloc.h> 59 #include <sys/vnode.h> 60 #include <sys/uio.h> 61 #include <sys/mount.h> 62 #include <sys/buf.h> 63 #include <sys/vmmeter.h> 64 65 #include <vm/vm.h> 66 #include <vm/vm_param.h> 67 #include <vm/vm_prot.h> 68 #include <vm/vm_object.h> 69 #include <vm/vm_page.h> 70 #include <vm/vm_pager.h> 71 #include <vm/vnode_pager.h> 72 #include <vm/vm_extern.h> 73 74 static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address, 75 int *run)); 76 static void vnode_pager_iodone __P((struct buf *bp)); 77 static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m)); 78 static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m)); 79 static void vnode_pager_dealloc __P((vm_object_t)); 80 static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 81 static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *)); 82 static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 83 84 struct pagerops vnodepagerops = { 85 NULL, 86 vnode_pager_alloc, 87 vnode_pager_dealloc, 88 vnode_pager_getpages, 89 vnode_pager_putpages, 90 vnode_pager_haspage, 91 NULL 92 }; 93 94 static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m, 95 int count, int reqpage)); 96 static int vnode_pager_leaf_putpages __P((vm_object_t object, vm_page_t *m, 97 int count, boolean_t sync, 98 int *rtvals)); 99 100 /* 101 * Allocate (or lookup) pager for a vnode. 102 * Handle is a vnode pointer. 103 */ 104 vm_object_t 105 vnode_pager_alloc(handle, size, prot, offset) 106 void *handle; 107 vm_size_t size; 108 vm_prot_t prot; 109 vm_ooffset_t offset; 110 { 111 vm_object_t object; 112 struct vnode *vp; 113 114 /* 115 * Pageout to vnode, no can do yet. 116 */ 117 if (handle == NULL) 118 return (NULL); 119 120 vp = (struct vnode *) handle; 121 122 /* 123 * Prevent race condition when allocating the object. This 124 * can happen with NFS vnodes since the nfsnode isn't locked. 125 */ 126 while (vp->v_flag & VOLOCK) { 127 vp->v_flag |= VOWANT; 128 tsleep(vp, PVM, "vnpobj", 0); 129 } 130 vp->v_flag |= VOLOCK; 131 132 /* 133 * If the object is being terminated, wait for it to 134 * go away. 135 */ 136 while (((object = vp->v_object) != NULL) && 137 (object->flags & OBJ_DEAD)) { 138 tsleep(object, PVM, "vadead", 0); 139 } 140 141 if (object == NULL) { 142 /* 143 * And an object of the appropriate size 144 */ 145 object = vm_object_allocate(OBJT_VNODE, size); 146 if (vp->v_type == VREG) 147 object->flags = OBJ_CANPERSIST; 148 else 149 object->flags = 0; 150 151 /* 152 * Hold a reference to the vnode and initialize object data. 153 */ 154 vp->v_usecount++; 155 object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE; 156 157 object->handle = handle; 158 vp->v_object = object; 159 } else { 160 /* 161 * vm_object_reference() will remove the object from the cache if 162 * found and gain a reference to the object. 163 */ 164 vm_object_reference(object); 165 } 166 167 if (vp->v_type == VREG) 168 vp->v_flag |= VVMIO; 169 170 vp->v_flag &= ~VOLOCK; 171 if (vp->v_flag & VOWANT) { 172 vp->v_flag &= ~VOWANT; 173 wakeup(vp); 174 } 175 return (object); 176 } 177 178 static void 179 vnode_pager_dealloc(object) 180 vm_object_t object; 181 { 182 register struct vnode *vp = object->handle; 183 184 if (vp == NULL) 185 panic("vnode_pager_dealloc: pager already dealloced"); 186 187 if (object->paging_in_progress) { 188 int s = splbio(); 189 while (object->paging_in_progress) { 190 object->flags |= OBJ_PIPWNT; 191 tsleep(object, PVM, "vnpdea", 0); 192 } 193 splx(s); 194 } 195 196 object->handle = NULL; 197 198 vp->v_object = NULL; 199 vp->v_flag &= ~(VTEXT | VVMIO); 200 vp->v_flag |= VAGE; 201 vrele(vp); 202 } 203 204 static boolean_t 205 vnode_pager_haspage(object, pindex, before, after) 206 vm_object_t object; 207 vm_pindex_t pindex; 208 int *before; 209 int *after; 210 { 211 struct vnode *vp = object->handle; 212 daddr_t bn; 213 int err; 214 daddr_t reqblock; 215 int poff; 216 int bsize; 217 int pagesperblock, blocksperpage; 218 219 /* 220 * If filesystem no longer mounted or offset beyond end of file we do 221 * not have the page. 222 */ 223 if ((vp->v_mount == NULL) || 224 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 225 return FALSE; 226 227 bsize = vp->v_mount->mnt_stat.f_iosize; 228 pagesperblock = bsize / PAGE_SIZE; 229 blocksperpage = 0; 230 if (pagesperblock > 0) { 231 reqblock = pindex / pagesperblock; 232 } else { 233 blocksperpage = (PAGE_SIZE / bsize); 234 reqblock = pindex * blocksperpage; 235 } 236 err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 237 after, before); 238 if (err) 239 return TRUE; 240 if ( bn == -1) 241 return FALSE; 242 if (pagesperblock > 0) { 243 poff = pindex - (reqblock * pagesperblock); 244 if (before) { 245 *before *= pagesperblock; 246 *before += poff; 247 } 248 if (after) { 249 int numafter; 250 *after *= pagesperblock; 251 numafter = pagesperblock - (poff + 1); 252 if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 253 numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 254 } 255 *after += numafter; 256 } 257 } else { 258 if (before) { 259 *before /= blocksperpage; 260 } 261 262 if (after) { 263 *after /= blocksperpage; 264 } 265 } 266 return TRUE; 267 } 268 269 /* 270 * Lets the VM system know about a change in size for a file. 271 * We adjust our own internal size and flush any cached pages in 272 * the associated object that are affected by the size change. 273 * 274 * Note: this routine may be invoked as a result of a pager put 275 * operation (possibly at object termination time), so we must be careful. 276 */ 277 void 278 vnode_pager_setsize(vp, nsize) 279 struct vnode *vp; 280 vm_ooffset_t nsize; 281 { 282 vm_object_t object = vp->v_object; 283 284 if (object == NULL) 285 return; 286 287 /* 288 * Hasn't changed size 289 */ 290 if (nsize == object->un_pager.vnp.vnp_size) 291 return; 292 293 /* 294 * File has shrunk. Toss any cached pages beyond the new EOF. 295 */ 296 if (nsize < object->un_pager.vnp.vnp_size) { 297 vm_ooffset_t nsizerounded; 298 nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_MASK)); 299 if (nsizerounded < object->un_pager.vnp.vnp_size) { 300 vm_object_page_remove(object, 301 OFF_TO_IDX(nsize + PAGE_MASK), 302 OFF_TO_IDX(object->un_pager.vnp.vnp_size), 303 FALSE); 304 } 305 /* 306 * this gets rid of garbage at the end of a page that is now 307 * only partially backed by the vnode... 308 */ 309 if (nsize & PAGE_MASK) { 310 vm_offset_t kva; 311 vm_page_t m; 312 313 m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 314 if (m) { 315 kva = vm_pager_map_page(m); 316 bzero((caddr_t) kva + (nsize & PAGE_MASK), 317 (int) (round_page(nsize) - nsize)); 318 vm_pager_unmap_page(kva); 319 } 320 } 321 } 322 object->un_pager.vnp.vnp_size = nsize; 323 object->size = OFF_TO_IDX(nsize + PAGE_MASK); 324 } 325 326 void 327 vnode_pager_umount(mp) 328 register struct mount *mp; 329 { 330 struct vnode *vp, *nvp; 331 332 loop: 333 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 334 /* 335 * Vnode can be reclaimed by getnewvnode() while we 336 * traverse the list. 337 */ 338 if (vp->v_mount != mp) 339 goto loop; 340 341 /* 342 * Save the next pointer now since uncaching may terminate the 343 * object and render vnode invalid 344 */ 345 nvp = vp->v_mntvnodes.le_next; 346 347 if (vp->v_object != NULL) { 348 VOP_LOCK(vp); 349 vnode_pager_uncache(vp); 350 VOP_UNLOCK(vp); 351 } 352 } 353 } 354 355 /* 356 * Remove vnode associated object from the object cache. 357 * This routine must be called with the vnode locked. 358 * 359 * XXX unlock the vnode. 360 * We must do this since uncaching the object may result in its 361 * destruction which may initiate paging activity which may necessitate 362 * re-locking the vnode. 363 */ 364 void 365 vnode_pager_uncache(vp) 366 struct vnode *vp; 367 { 368 vm_object_t object; 369 370 /* 371 * Not a mapped vnode 372 */ 373 object = vp->v_object; 374 if (object == NULL) 375 return; 376 377 vm_object_reference(object); 378 379 /* 380 * XXX We really should handle locking on 381 * VBLK devices... 382 */ 383 if (vp->v_type != VBLK) 384 VOP_UNLOCK(vp); 385 pager_cache(object, FALSE); 386 if (vp->v_type != VBLK) 387 VOP_LOCK(vp); 388 return; 389 } 390 391 392 void 393 vnode_pager_freepage(m) 394 vm_page_t m; 395 { 396 PAGE_WAKEUP(m); 397 vm_page_free(m); 398 } 399 400 /* 401 * calculate the linear (byte) disk address of specified virtual 402 * file address 403 */ 404 static vm_offset_t 405 vnode_pager_addr(vp, address, run) 406 struct vnode *vp; 407 vm_ooffset_t address; 408 int *run; 409 { 410 int rtaddress; 411 int bsize; 412 daddr_t block; 413 struct vnode *rtvp; 414 int err; 415 daddr_t vblock; 416 int voffset; 417 418 if ((int) address < 0) 419 return -1; 420 421 if (vp->v_mount == NULL) 422 return -1; 423 424 bsize = vp->v_mount->mnt_stat.f_iosize; 425 vblock = address / bsize; 426 voffset = address % bsize; 427 428 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 429 430 if (err || (block == -1)) 431 rtaddress = -1; 432 else { 433 rtaddress = block + voffset / DEV_BSIZE; 434 if( run) { 435 *run += 1; 436 *run *= bsize/PAGE_SIZE; 437 *run -= voffset/PAGE_SIZE; 438 } 439 } 440 441 return rtaddress; 442 } 443 444 /* 445 * interrupt routine for I/O completion 446 */ 447 static void 448 vnode_pager_iodone(bp) 449 struct buf *bp; 450 { 451 bp->b_flags |= B_DONE; 452 wakeup(bp); 453 } 454 455 /* 456 * small block file system vnode pager input 457 */ 458 static int 459 vnode_pager_input_smlfs(object, m) 460 vm_object_t object; 461 vm_page_t m; 462 { 463 int i; 464 int s; 465 struct vnode *dp, *vp; 466 struct buf *bp; 467 vm_offset_t kva; 468 int fileaddr; 469 vm_offset_t bsize; 470 int error = 0; 471 472 vp = object->handle; 473 if (vp->v_mount == NULL) 474 return VM_PAGER_BAD; 475 476 bsize = vp->v_mount->mnt_stat.f_iosize; 477 478 479 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 480 481 kva = vm_pager_map_page(m); 482 483 for (i = 0; i < PAGE_SIZE / bsize; i++) { 484 485 if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid)) 486 continue; 487 488 fileaddr = vnode_pager_addr(vp, 489 IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 490 if (fileaddr != -1) { 491 bp = getpbuf(); 492 493 /* build a minimal buffer header */ 494 bp->b_flags = B_BUSY | B_READ | B_CALL; 495 bp->b_iodone = vnode_pager_iodone; 496 bp->b_proc = curproc; 497 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 498 if (bp->b_rcred != NOCRED) 499 crhold(bp->b_rcred); 500 if (bp->b_wcred != NOCRED) 501 crhold(bp->b_wcred); 502 bp->b_un.b_addr = (caddr_t) kva + i * bsize; 503 bp->b_blkno = fileaddr; 504 pbgetvp(dp, bp); 505 bp->b_bcount = bsize; 506 bp->b_bufsize = bsize; 507 508 /* do the input */ 509 VOP_STRATEGY(bp); 510 511 /* we definitely need to be at splbio here */ 512 513 s = splbio(); 514 while ((bp->b_flags & B_DONE) == 0) { 515 tsleep(bp, PVM, "vnsrd", 0); 516 } 517 splx(s); 518 if ((bp->b_flags & B_ERROR) != 0) 519 error = EIO; 520 521 /* 522 * free the buffer header back to the swap buffer pool 523 */ 524 relpbuf(bp); 525 if (error) 526 break; 527 528 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 529 } else { 530 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 531 bzero((caddr_t) kva + i * bsize, bsize); 532 } 533 } 534 vm_pager_unmap_page(kva); 535 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 536 m->flags &= ~PG_ZERO; 537 if (error) { 538 return VM_PAGER_ERROR; 539 } 540 return VM_PAGER_OK; 541 542 } 543 544 545 /* 546 * old style vnode pager output routine 547 */ 548 static int 549 vnode_pager_input_old(object, m) 550 vm_object_t object; 551 vm_page_t m; 552 { 553 struct uio auio; 554 struct iovec aiov; 555 int error; 556 int size; 557 vm_offset_t kva; 558 559 error = 0; 560 561 /* 562 * Return failure if beyond current EOF 563 */ 564 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 565 return VM_PAGER_BAD; 566 } else { 567 size = PAGE_SIZE; 568 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 569 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 570 571 /* 572 * Allocate a kernel virtual address and initialize so that 573 * we can use VOP_READ/WRITE routines. 574 */ 575 kva = vm_pager_map_page(m); 576 577 aiov.iov_base = (caddr_t) kva; 578 aiov.iov_len = size; 579 auio.uio_iov = &aiov; 580 auio.uio_iovcnt = 1; 581 auio.uio_offset = IDX_TO_OFF(m->pindex); 582 auio.uio_segflg = UIO_SYSSPACE; 583 auio.uio_rw = UIO_READ; 584 auio.uio_resid = size; 585 auio.uio_procp = (struct proc *) 0; 586 587 error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 588 if (!error) { 589 register int count = size - auio.uio_resid; 590 591 if (count == 0) 592 error = EINVAL; 593 else if (count != PAGE_SIZE) 594 bzero((caddr_t) kva + count, PAGE_SIZE - count); 595 } 596 vm_pager_unmap_page(kva); 597 } 598 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 599 m->dirty = 0; 600 m->flags &= ~PG_ZERO; 601 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 602 } 603 604 /* 605 * generic vnode pager input routine 606 */ 607 608 static int 609 vnode_pager_getpages(object, m, count, reqpage) 610 vm_object_t object; 611 vm_page_t *m; 612 int count; 613 int reqpage; 614 { 615 int rtval; 616 struct vnode *vp; 617 vp = object->handle; 618 rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0); 619 if (rtval == EOPNOTSUPP) 620 return vnode_pager_leaf_getpages(object, m, count, reqpage); 621 else 622 return rtval; 623 } 624 625 static int 626 vnode_pager_leaf_getpages(object, m, count, reqpage) 627 vm_object_t object; 628 vm_page_t *m; 629 int count; 630 int reqpage; 631 { 632 vm_offset_t kva; 633 off_t foff; 634 int i, size, bsize, first, firstaddr; 635 struct vnode *dp, *vp; 636 int runpg; 637 int runend; 638 struct buf *bp; 639 int s; 640 int error = 0; 641 642 vp = object->handle; 643 if (vp->v_mount == NULL) 644 return VM_PAGER_BAD; 645 646 bsize = vp->v_mount->mnt_stat.f_iosize; 647 648 /* get the UNDERLYING device for the file with VOP_BMAP() */ 649 650 /* 651 * originally, we did not check for an error return value -- assuming 652 * an fs always has a bmap entry point -- that assumption is wrong!!! 653 */ 654 foff = IDX_TO_OFF(m[reqpage]->pindex); 655 656 /* 657 * if we can't bmap, use old VOP code 658 */ 659 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 660 for (i = 0; i < count; i++) { 661 if (i != reqpage) { 662 vnode_pager_freepage(m[i]); 663 } 664 } 665 cnt.v_vnodein++; 666 cnt.v_vnodepgsin++; 667 return vnode_pager_input_old(object, m[reqpage]); 668 669 /* 670 * if the blocksize is smaller than a page size, then use 671 * special small filesystem code. NFS sometimes has a small 672 * blocksize, but it can handle large reads itself. 673 */ 674 } else if ((PAGE_SIZE / bsize) > 1 && 675 (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) { 676 677 for (i = 0; i < count; i++) { 678 if (i != reqpage) { 679 vnode_pager_freepage(m[i]); 680 } 681 } 682 cnt.v_vnodein++; 683 cnt.v_vnodepgsin++; 684 return vnode_pager_input_smlfs(object, m[reqpage]); 685 } 686 /* 687 * if ANY DEV_BSIZE blocks are valid on a large filesystem block 688 * then, the entire page is valid -- 689 */ 690 if (m[reqpage]->valid) { 691 m[reqpage]->valid = VM_PAGE_BITS_ALL; 692 for (i = 0; i < count; i++) { 693 if (i != reqpage) 694 vnode_pager_freepage(m[i]); 695 } 696 return VM_PAGER_OK; 697 } 698 699 /* 700 * here on direct device I/O 701 */ 702 703 firstaddr = -1; 704 /* 705 * calculate the run that includes the required page 706 */ 707 for(first = 0, i = 0; i < count; i = runend) { 708 firstaddr = vnode_pager_addr(vp, 709 IDX_TO_OFF(m[i]->pindex), &runpg); 710 if (firstaddr == -1) { 711 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 712 panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d", 713 firstaddr, foff, object->un_pager.vnp.vnp_size); 714 } 715 vnode_pager_freepage(m[i]); 716 runend = i + 1; 717 first = runend; 718 continue; 719 } 720 runend = i + runpg; 721 if (runend <= reqpage) { 722 int j; 723 for (j = i; j < runend; j++) { 724 vnode_pager_freepage(m[j]); 725 } 726 } else { 727 if (runpg < (count - first)) { 728 for (i = first + runpg; i < count; i++) 729 vnode_pager_freepage(m[i]); 730 count = first + runpg; 731 } 732 break; 733 } 734 first = runend; 735 } 736 737 /* 738 * the first and last page have been calculated now, move input pages 739 * to be zero based... 740 */ 741 if (first != 0) { 742 for (i = first; i < count; i++) { 743 m[i - first] = m[i]; 744 } 745 count -= first; 746 reqpage -= first; 747 } 748 749 /* 750 * calculate the file virtual address for the transfer 751 */ 752 foff = IDX_TO_OFF(m[0]->pindex); 753 754 /* 755 * calculate the size of the transfer 756 */ 757 size = count * PAGE_SIZE; 758 if ((foff + size) > object->un_pager.vnp.vnp_size) 759 size = object->un_pager.vnp.vnp_size - foff; 760 761 /* 762 * round up physical size for real devices 763 */ 764 if (dp->v_type == VBLK || dp->v_type == VCHR) 765 size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 766 767 bp = getpbuf(); 768 kva = (vm_offset_t) bp->b_data; 769 770 /* 771 * and map the pages to be read into the kva 772 */ 773 pmap_qenter(kva, m, count); 774 775 /* build a minimal buffer header */ 776 bp->b_flags = B_BUSY | B_READ | B_CALL; 777 bp->b_iodone = vnode_pager_iodone; 778 /* B_PHYS is not set, but it is nice to fill this in */ 779 bp->b_proc = curproc; 780 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 781 if (bp->b_rcred != NOCRED) 782 crhold(bp->b_rcred); 783 if (bp->b_wcred != NOCRED) 784 crhold(bp->b_wcred); 785 bp->b_blkno = firstaddr; 786 pbgetvp(dp, bp); 787 bp->b_bcount = size; 788 bp->b_bufsize = size; 789 790 cnt.v_vnodein++; 791 cnt.v_vnodepgsin += count; 792 793 /* do the input */ 794 VOP_STRATEGY(bp); 795 796 s = splbio(); 797 /* we definitely need to be at splbio here */ 798 799 while ((bp->b_flags & B_DONE) == 0) { 800 tsleep(bp, PVM, "vnread", 0); 801 } 802 splx(s); 803 if ((bp->b_flags & B_ERROR) != 0) 804 error = EIO; 805 806 if (!error) { 807 if (size != count * PAGE_SIZE) 808 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 809 } 810 pmap_qremove(kva, count); 811 812 /* 813 * free the buffer header back to the swap buffer pool 814 */ 815 relpbuf(bp); 816 817 for (i = 0; i < count; i++) { 818 pmap_clear_modify(VM_PAGE_TO_PHYS(m[i])); 819 m[i]->dirty = 0; 820 m[i]->valid = VM_PAGE_BITS_ALL; 821 m[i]->flags &= ~PG_ZERO; 822 if (i != reqpage) { 823 824 /* 825 * whether or not to leave the page activated is up in 826 * the air, but we should put the page on a page queue 827 * somewhere. (it already is in the object). Result: 828 * It appears that emperical results show that 829 * deactivating pages is best. 830 */ 831 832 /* 833 * just in case someone was asking for this page we 834 * now tell them that it is ok to use 835 */ 836 if (!error) { 837 vm_page_deactivate(m[i]); 838 PAGE_WAKEUP(m[i]); 839 } else { 840 vnode_pager_freepage(m[i]); 841 } 842 } 843 } 844 if (error) { 845 printf("vnode_pager_getpages: I/O read error\n"); 846 } 847 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 848 } 849 850 static int 851 vnode_pager_putpages(object, m, count, sync, rtvals) 852 vm_object_t object; 853 vm_page_t *m; 854 int count; 855 boolean_t sync; 856 int *rtvals; 857 { 858 int rtval; 859 struct vnode *vp; 860 vp = object->handle; 861 rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0); 862 if (rtval == EOPNOTSUPP) 863 return vnode_pager_leaf_putpages(object, m, count, sync, rtvals); 864 else 865 return rtval; 866 } 867 868 /* 869 * generic vnode pager output routine 870 */ 871 static int 872 vnode_pager_leaf_putpages(object, m, count, sync, rtvals) 873 vm_object_t object; 874 vm_page_t *m; 875 int count; 876 boolean_t sync; 877 int *rtvals; 878 { 879 int i; 880 881 struct vnode *vp; 882 int maxsize, ncount; 883 vm_ooffset_t poffset; 884 struct uio auio; 885 struct iovec aiov; 886 int error; 887 888 vp = object->handle;; 889 for (i = 0; i < count; i++) 890 rtvals[i] = VM_PAGER_AGAIN; 891 892 if ((int) m[0]->pindex < 0) { 893 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty); 894 rtvals[0] = VM_PAGER_BAD; 895 return VM_PAGER_BAD; 896 } 897 898 maxsize = count * PAGE_SIZE; 899 ncount = count; 900 901 poffset = IDX_TO_OFF(m[0]->pindex); 902 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 903 if (object->un_pager.vnp.vnp_size > poffset) 904 maxsize = object->un_pager.vnp.vnp_size - poffset; 905 else 906 maxsize = 0; 907 ncount = btoc(maxsize); 908 if (ncount < count) { 909 for (i = ncount; i < count; i++) { 910 rtvals[i] = VM_PAGER_BAD; 911 } 912 #ifdef BOGUS 913 if (ncount == 0) { 914 printf("vnode_pager_putpages: write past end of file: %d, %lu\n", 915 poffset, 916 (unsigned long) object->un_pager.vnp.vnp_size); 917 return rtvals[0]; 918 } 919 #endif 920 } 921 } 922 923 for (i = 0; i < count; i++) { 924 m[i]->busy++; 925 m[i]->flags &= ~PG_BUSY; 926 } 927 928 aiov.iov_base = (caddr_t) 0; 929 aiov.iov_len = maxsize; 930 auio.uio_iov = &aiov; 931 auio.uio_iovcnt = 1; 932 auio.uio_offset = poffset; 933 auio.uio_segflg = UIO_NOCOPY; 934 auio.uio_rw = UIO_WRITE; 935 auio.uio_resid = maxsize; 936 auio.uio_procp = (struct proc *) 0; 937 error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred); 938 cnt.v_vnodeout++; 939 cnt.v_vnodepgsout += ncount; 940 941 if (error) { 942 printf("vnode_pager_putpages: I/O error %d\n", error); 943 } 944 if (auio.uio_resid) { 945 printf("vnode_pager_putpages: residual I/O %d at %ld\n", 946 auio.uio_resid, m[0]->pindex); 947 } 948 for (i = 0; i < count; i++) { 949 m[i]->busy--; 950 if (i < ncount) { 951 rtvals[i] = VM_PAGER_OK; 952 } 953 if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED)) 954 wakeup(m[i]); 955 } 956 return rtvals[0]; 957 } 958 959 struct vnode * 960 vnode_pager_lock(object) 961 vm_object_t object; 962 { 963 for (; object != NULL; object = object->backing_object) { 964 if (object->type != OBJT_VNODE) 965 continue; 966 967 VOP_LOCK(object->handle); 968 return object->handle; 969 } 970 return NULL; 971 } 972