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.55 1995/12/11 04:58:32 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) && (object->flags & OBJ_DEAD)) { 137 tsleep(object, PVM, "vadead", 0); 138 } 139 140 if (object == NULL) { 141 /* 142 * And an object of the appropriate size 143 */ 144 object = vm_object_allocate(OBJT_VNODE, size); 145 object->flags = OBJ_CANPERSIST; 146 147 /* 148 * Hold a reference to the vnode and initialize object data. 149 */ 150 VREF(vp); 151 object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE; 152 153 object->handle = handle; 154 vp->v_object = object; 155 } else { 156 /* 157 * vm_object_reference() will remove the object from the cache if 158 * found and gain a reference to the object. 159 */ 160 vm_object_reference(object); 161 } 162 163 if (vp->v_type == VREG) 164 vp->v_flag |= VVMIO; 165 166 vp->v_flag &= ~VOLOCK; 167 if (vp->v_flag & VOWANT) { 168 vp->v_flag &= ~VOWANT; 169 wakeup(vp); 170 } 171 return (object); 172 } 173 174 static void 175 vnode_pager_dealloc(object) 176 vm_object_t object; 177 { 178 register struct vnode *vp = object->handle; 179 180 if (vp == NULL) 181 panic("vnode_pager_dealloc: pager already dealloced"); 182 183 if (object->paging_in_progress) { 184 int s = splbio(); 185 while (object->paging_in_progress) { 186 object->flags |= OBJ_PIPWNT; 187 tsleep(object, PVM, "vnpdea", 0); 188 } 189 splx(s); 190 } 191 192 object->handle = NULL; 193 194 vp->v_object = NULL; 195 vp->v_flag &= ~(VTEXT | VVMIO); 196 vp->v_flag |= VAGE; 197 vrele(vp); 198 } 199 200 static boolean_t 201 vnode_pager_haspage(object, pindex, before, after) 202 vm_object_t object; 203 vm_pindex_t pindex; 204 int *before; 205 int *after; 206 { 207 struct vnode *vp = object->handle; 208 daddr_t bn; 209 int err; 210 daddr_t reqblock; 211 int poff; 212 int bsize; 213 int pagesperblock; 214 215 /* 216 * If filesystem no longer mounted or offset beyond end of file we do 217 * not have the page. 218 */ 219 if ((vp->v_mount == NULL) || 220 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 221 return FALSE; 222 223 bsize = vp->v_mount->mnt_stat.f_iosize; 224 pagesperblock = bsize / PAGE_SIZE; 225 reqblock = pindex / pagesperblock; 226 err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 227 after, before); 228 if (err) 229 return TRUE; 230 if ( bn == -1) 231 return FALSE; 232 poff = pindex - (reqblock * pagesperblock); 233 if (before) { 234 *before *= pagesperblock; 235 *before += poff; 236 } 237 if (after) { 238 int numafter; 239 *after *= pagesperblock; 240 numafter = pagesperblock - (poff + 1); 241 if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 242 numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 243 } 244 *after += numafter; 245 } 246 return TRUE; 247 } 248 249 /* 250 * Lets the VM system know about a change in size for a file. 251 * We adjust our own internal size and flush any cached pages in 252 * the associated object that are affected by the size change. 253 * 254 * Note: this routine may be invoked as a result of a pager put 255 * operation (possibly at object termination time), so we must be careful. 256 */ 257 void 258 vnode_pager_setsize(vp, nsize) 259 struct vnode *vp; 260 vm_ooffset_t nsize; 261 { 262 vm_object_t object = vp->v_object; 263 264 if (object == NULL) 265 return; 266 267 /* 268 * Hasn't changed size 269 */ 270 if (nsize == object->un_pager.vnp.vnp_size) 271 return; 272 273 /* 274 * File has shrunk. Toss any cached pages beyond the new EOF. 275 */ 276 if (nsize < object->un_pager.vnp.vnp_size) { 277 vm_ooffset_t nsizerounded; 278 nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_SIZE - 1)); 279 if (nsizerounded < object->un_pager.vnp.vnp_size) { 280 vm_object_page_remove(object, 281 OFF_TO_IDX(nsize + PAGE_SIZE - 1), 282 OFF_TO_IDX(object->un_pager.vnp.vnp_size), 283 FALSE); 284 } 285 /* 286 * this gets rid of garbage at the end of a page that is now 287 * only partially backed by the vnode... 288 */ 289 if (nsize & PAGE_MASK) { 290 vm_offset_t kva; 291 vm_page_t m; 292 293 m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 294 if (m) { 295 kva = vm_pager_map_page(m); 296 bzero((caddr_t) kva + (nsize & PAGE_MASK), 297 (int) (round_page(nsize) - nsize)); 298 vm_pager_unmap_page(kva); 299 } 300 } 301 } 302 object->un_pager.vnp.vnp_size = nsize; 303 object->size = OFF_TO_IDX(nsize + PAGE_SIZE - 1); 304 } 305 306 void 307 vnode_pager_umount(mp) 308 register struct mount *mp; 309 { 310 struct vnode *vp, *nvp; 311 312 loop: 313 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 314 /* 315 * Vnode can be reclaimed by getnewvnode() while we 316 * traverse the list. 317 */ 318 if (vp->v_mount != mp) 319 goto loop; 320 321 /* 322 * Save the next pointer now since uncaching may terminate the 323 * object and render vnode invalid 324 */ 325 nvp = vp->v_mntvnodes.le_next; 326 327 if (vp->v_object != NULL) { 328 VOP_LOCK(vp); 329 vnode_pager_uncache(vp); 330 VOP_UNLOCK(vp); 331 } 332 } 333 } 334 335 /* 336 * Remove vnode associated object from the object cache. 337 * This routine must be called with the vnode locked. 338 * 339 * XXX unlock the vnode. 340 * We must do this since uncaching the object may result in its 341 * destruction which may initiate paging activity which may necessitate 342 * re-locking the vnode. 343 */ 344 void 345 vnode_pager_uncache(vp) 346 struct vnode *vp; 347 { 348 vm_object_t object; 349 350 /* 351 * Not a mapped vnode 352 */ 353 object = vp->v_object; 354 if (object == NULL) 355 return; 356 357 vm_object_reference(object); 358 VOP_UNLOCK(vp); 359 pager_cache(object, FALSE); 360 VOP_LOCK(vp); 361 return; 362 } 363 364 365 void 366 vnode_pager_freepage(m) 367 vm_page_t m; 368 { 369 PAGE_WAKEUP(m); 370 vm_page_free(m); 371 } 372 373 /* 374 * calculate the linear (byte) disk address of specified virtual 375 * file address 376 */ 377 static vm_offset_t 378 vnode_pager_addr(vp, address, run) 379 struct vnode *vp; 380 vm_ooffset_t address; 381 int *run; 382 { 383 int rtaddress; 384 int bsize; 385 daddr_t block; 386 struct vnode *rtvp; 387 int err; 388 daddr_t vblock; 389 int voffset; 390 391 if ((int) address < 0) 392 return -1; 393 394 if (vp->v_mount == NULL) 395 return -1; 396 397 bsize = vp->v_mount->mnt_stat.f_iosize; 398 vblock = address / bsize; 399 voffset = address % bsize; 400 401 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 402 403 if (err || (block == -1)) 404 rtaddress = -1; 405 else { 406 rtaddress = block + voffset / DEV_BSIZE; 407 if( run) { 408 *run += 1; 409 *run *= bsize/PAGE_SIZE; 410 *run -= voffset/PAGE_SIZE; 411 } 412 } 413 414 return rtaddress; 415 } 416 417 /* 418 * interrupt routine for I/O completion 419 */ 420 static void 421 vnode_pager_iodone(bp) 422 struct buf *bp; 423 { 424 bp->b_flags |= B_DONE; 425 wakeup(bp); 426 } 427 428 /* 429 * small block file system vnode pager input 430 */ 431 static int 432 vnode_pager_input_smlfs(object, m) 433 vm_object_t object; 434 vm_page_t m; 435 { 436 int i; 437 int s; 438 struct vnode *dp, *vp; 439 struct buf *bp; 440 vm_offset_t kva; 441 int fileaddr; 442 vm_offset_t bsize; 443 int error = 0; 444 445 vp = object->handle; 446 if (vp->v_mount == NULL) 447 return VM_PAGER_BAD; 448 449 bsize = vp->v_mount->mnt_stat.f_iosize; 450 451 452 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 453 454 kva = vm_pager_map_page(m); 455 456 for (i = 0; i < PAGE_SIZE / bsize; i++) { 457 458 if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid)) 459 continue; 460 461 fileaddr = vnode_pager_addr(vp, 462 IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 463 if (fileaddr != -1) { 464 bp = getpbuf(); 465 466 /* build a minimal buffer header */ 467 bp->b_flags = B_BUSY | B_READ | B_CALL; 468 bp->b_iodone = vnode_pager_iodone; 469 bp->b_proc = curproc; 470 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 471 if (bp->b_rcred != NOCRED) 472 crhold(bp->b_rcred); 473 if (bp->b_wcred != NOCRED) 474 crhold(bp->b_wcred); 475 bp->b_un.b_addr = (caddr_t) kva + i * bsize; 476 bp->b_blkno = fileaddr; 477 pbgetvp(dp, bp); 478 bp->b_bcount = bsize; 479 bp->b_bufsize = bsize; 480 481 /* do the input */ 482 VOP_STRATEGY(bp); 483 484 /* we definitely need to be at splbio here */ 485 486 s = splbio(); 487 while ((bp->b_flags & B_DONE) == 0) { 488 tsleep(bp, PVM, "vnsrd", 0); 489 } 490 splx(s); 491 if ((bp->b_flags & B_ERROR) != 0) 492 error = EIO; 493 494 /* 495 * free the buffer header back to the swap buffer pool 496 */ 497 relpbuf(bp); 498 if (error) 499 break; 500 501 vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize); 502 } else { 503 vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize); 504 bzero((caddr_t) kva + i * bsize, bsize); 505 } 506 } 507 vm_pager_unmap_page(kva); 508 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 509 m->flags &= ~PG_ZERO; 510 if (error) { 511 return VM_PAGER_ERROR; 512 } 513 return VM_PAGER_OK; 514 515 } 516 517 518 /* 519 * old style vnode pager output routine 520 */ 521 static int 522 vnode_pager_input_old(object, m) 523 vm_object_t object; 524 vm_page_t m; 525 { 526 struct uio auio; 527 struct iovec aiov; 528 int error; 529 int size; 530 vm_offset_t kva; 531 532 error = 0; 533 534 /* 535 * Return failure if beyond current EOF 536 */ 537 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 538 return VM_PAGER_BAD; 539 } else { 540 size = PAGE_SIZE; 541 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 542 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 543 544 /* 545 * Allocate a kernel virtual address and initialize so that 546 * we can use VOP_READ/WRITE routines. 547 */ 548 kva = vm_pager_map_page(m); 549 550 aiov.iov_base = (caddr_t) kva; 551 aiov.iov_len = size; 552 auio.uio_iov = &aiov; 553 auio.uio_iovcnt = 1; 554 auio.uio_offset = IDX_TO_OFF(m->pindex); 555 auio.uio_segflg = UIO_SYSSPACE; 556 auio.uio_rw = UIO_READ; 557 auio.uio_resid = size; 558 auio.uio_procp = (struct proc *) 0; 559 560 error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 561 if (!error) { 562 register int count = size - auio.uio_resid; 563 564 if (count == 0) 565 error = EINVAL; 566 else if (count != PAGE_SIZE) 567 bzero((caddr_t) kva + count, PAGE_SIZE - count); 568 } 569 vm_pager_unmap_page(kva); 570 } 571 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 572 m->dirty = 0; 573 m->flags &= ~PG_ZERO; 574 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 575 } 576 577 /* 578 * generic vnode pager input routine 579 */ 580 581 static int 582 vnode_pager_getpages(object, m, count, reqpage) 583 vm_object_t object; 584 vm_page_t *m; 585 int count; 586 int reqpage; 587 { 588 int rtval; 589 struct vnode *vp; 590 vp = object->handle; 591 rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0); 592 if (rtval == EOPNOTSUPP) 593 return vnode_pager_leaf_getpages(object, m, count, reqpage); 594 else 595 return rtval; 596 } 597 598 static int 599 vnode_pager_leaf_getpages(object, m, count, reqpage) 600 vm_object_t object; 601 vm_page_t *m; 602 int count; 603 int reqpage; 604 { 605 vm_offset_t kva; 606 off_t foff; 607 int i, size, bsize, first, firstaddr; 608 struct vnode *dp, *vp; 609 int runpg; 610 int runend; 611 struct buf *bp; 612 int s; 613 int error = 0; 614 615 vp = object->handle; 616 if (vp->v_mount == NULL) 617 return VM_PAGER_BAD; 618 619 bsize = vp->v_mount->mnt_stat.f_iosize; 620 621 /* get the UNDERLYING device for the file with VOP_BMAP() */ 622 623 /* 624 * originally, we did not check for an error return value -- assuming 625 * an fs always has a bmap entry point -- that assumption is wrong!!! 626 */ 627 foff = IDX_TO_OFF(m[reqpage]->pindex); 628 629 /* 630 * if we can't bmap, use old VOP code 631 */ 632 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 633 for (i = 0; i < count; i++) { 634 if (i != reqpage) { 635 vnode_pager_freepage(m[i]); 636 } 637 } 638 cnt.v_vnodein++; 639 cnt.v_vnodepgsin++; 640 return vnode_pager_input_old(object, m[reqpage]); 641 642 /* 643 * if the blocksize is smaller than a page size, then use 644 * special small filesystem code. NFS sometimes has a small 645 * blocksize, but it can handle large reads itself. 646 */ 647 } else if ((PAGE_SIZE / bsize) > 1 && 648 (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) { 649 650 for (i = 0; i < count; i++) { 651 if (i != reqpage) { 652 vnode_pager_freepage(m[i]); 653 } 654 } 655 cnt.v_vnodein++; 656 cnt.v_vnodepgsin++; 657 return vnode_pager_input_smlfs(object, m[reqpage]); 658 } 659 /* 660 * if ANY DEV_BSIZE blocks are valid on a large filesystem block 661 * then, the entire page is valid -- 662 */ 663 if (m[reqpage]->valid) { 664 m[reqpage]->valid = VM_PAGE_BITS_ALL; 665 for (i = 0; i < count; i++) { 666 if (i != reqpage) 667 vnode_pager_freepage(m[i]); 668 } 669 return VM_PAGER_OK; 670 } 671 672 /* 673 * here on direct device I/O 674 */ 675 676 firstaddr = -1; 677 /* 678 * calculate the run that includes the required page 679 */ 680 for(first = 0, i = 0; i < count; i = runend) { 681 firstaddr = vnode_pager_addr(vp, 682 IDX_TO_OFF(m[i]->pindex), &runpg); 683 if (firstaddr == -1) { 684 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 685 panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d", 686 firstaddr, foff, object->un_pager.vnp.vnp_size); 687 } 688 vnode_pager_freepage(m[i]); 689 runend = i + 1; 690 first = runend; 691 continue; 692 } 693 runend = i + runpg; 694 if (runend <= reqpage) { 695 int j; 696 for (j = i; j < runend; j++) { 697 vnode_pager_freepage(m[j]); 698 } 699 } else { 700 if (runpg < (count - first)) { 701 for (i = first + runpg; i < count; i++) 702 vnode_pager_freepage(m[i]); 703 count = first + runpg; 704 } 705 break; 706 } 707 first = runend; 708 } 709 710 /* 711 * the first and last page have been calculated now, move input pages 712 * to be zero based... 713 */ 714 if (first != 0) { 715 for (i = first; i < count; i++) { 716 m[i - first] = m[i]; 717 } 718 count -= first; 719 reqpage -= first; 720 } 721 722 /* 723 * calculate the file virtual address for the transfer 724 */ 725 foff = IDX_TO_OFF(m[0]->pindex); 726 727 /* 728 * calculate the size of the transfer 729 */ 730 size = count * PAGE_SIZE; 731 if ((foff + size) > object->un_pager.vnp.vnp_size) 732 size = object->un_pager.vnp.vnp_size - foff; 733 734 /* 735 * round up physical size for real devices 736 */ 737 if (dp->v_type == VBLK || dp->v_type == VCHR) 738 size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 739 740 bp = getpbuf(); 741 kva = (vm_offset_t) bp->b_data; 742 743 /* 744 * and map the pages to be read into the kva 745 */ 746 pmap_qenter(kva, m, count); 747 748 /* build a minimal buffer header */ 749 bp->b_flags = B_BUSY | B_READ | B_CALL; 750 bp->b_iodone = vnode_pager_iodone; 751 /* B_PHYS is not set, but it is nice to fill this in */ 752 bp->b_proc = curproc; 753 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 754 if (bp->b_rcred != NOCRED) 755 crhold(bp->b_rcred); 756 if (bp->b_wcred != NOCRED) 757 crhold(bp->b_wcred); 758 bp->b_blkno = firstaddr; 759 pbgetvp(dp, bp); 760 bp->b_bcount = size; 761 bp->b_bufsize = size; 762 763 cnt.v_vnodein++; 764 cnt.v_vnodepgsin += count; 765 766 /* do the input */ 767 VOP_STRATEGY(bp); 768 769 s = splbio(); 770 /* we definitely need to be at splbio here */ 771 772 while ((bp->b_flags & B_DONE) == 0) { 773 tsleep(bp, PVM, "vnread", 0); 774 } 775 splx(s); 776 if ((bp->b_flags & B_ERROR) != 0) 777 error = EIO; 778 779 if (!error) { 780 if (size != count * PAGE_SIZE) 781 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 782 } 783 pmap_qremove(kva, count); 784 785 /* 786 * free the buffer header back to the swap buffer pool 787 */ 788 relpbuf(bp); 789 790 for (i = 0; i < count; i++) { 791 pmap_clear_modify(VM_PAGE_TO_PHYS(m[i])); 792 m[i]->dirty = 0; 793 m[i]->valid = VM_PAGE_BITS_ALL; 794 m[i]->flags &= ~PG_ZERO; 795 if (i != reqpage) { 796 797 /* 798 * whether or not to leave the page activated is up in 799 * the air, but we should put the page on a page queue 800 * somewhere. (it already is in the object). Result: 801 * It appears that emperical results show that 802 * deactivating pages is best. 803 */ 804 805 /* 806 * just in case someone was asking for this page we 807 * now tell them that it is ok to use 808 */ 809 if (!error) { 810 vm_page_deactivate(m[i]); 811 PAGE_WAKEUP(m[i]); 812 } else { 813 vnode_pager_freepage(m[i]); 814 } 815 } 816 } 817 if (error) { 818 printf("vnode_pager_getpages: I/O read error\n"); 819 } 820 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 821 } 822 823 static int 824 vnode_pager_putpages(object, m, count, sync, rtvals) 825 vm_object_t object; 826 vm_page_t *m; 827 int count; 828 boolean_t sync; 829 int *rtvals; 830 { 831 int rtval; 832 struct vnode *vp; 833 vp = object->handle; 834 rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0); 835 if (rtval == EOPNOTSUPP) 836 return vnode_pager_leaf_putpages(object, m, count, sync, rtvals); 837 else 838 return rtval; 839 } 840 841 /* 842 * generic vnode pager output routine 843 */ 844 static int 845 vnode_pager_leaf_putpages(object, m, count, sync, rtvals) 846 vm_object_t object; 847 vm_page_t *m; 848 int count; 849 boolean_t sync; 850 int *rtvals; 851 { 852 int i; 853 854 struct vnode *vp; 855 int maxsize, ncount; 856 vm_ooffset_t poffset; 857 struct uio auio; 858 struct iovec aiov; 859 int error; 860 861 vp = object->handle;; 862 for (i = 0; i < count; i++) 863 rtvals[i] = VM_PAGER_AGAIN; 864 865 if ((int) m[0]->pindex < 0) { 866 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty); 867 rtvals[0] = VM_PAGER_BAD; 868 return VM_PAGER_BAD; 869 } 870 871 maxsize = count * PAGE_SIZE; 872 ncount = count; 873 874 poffset = IDX_TO_OFF(m[0]->pindex); 875 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 876 if (object->un_pager.vnp.vnp_size > poffset) 877 maxsize = object->un_pager.vnp.vnp_size - poffset; 878 else 879 maxsize = 0; 880 ncount = (maxsize + PAGE_SIZE - 1) / PAGE_SIZE; 881 if (ncount < count) { 882 for (i = ncount; i < count; i++) { 883 rtvals[i] = VM_PAGER_BAD; 884 } 885 #ifdef BOGUS 886 if (ncount == 0) { 887 printf("vnode_pager_putpages: write past end of file: %d, %lu\n", 888 poffset, 889 (unsigned long) object->un_pager.vnp.vnp_size); 890 return rtvals[0]; 891 } 892 #endif 893 } 894 } 895 896 for (i = 0; i < count; i++) { 897 m[i]->busy++; 898 m[i]->flags &= ~PG_BUSY; 899 } 900 901 aiov.iov_base = (caddr_t) 0; 902 aiov.iov_len = maxsize; 903 auio.uio_iov = &aiov; 904 auio.uio_iovcnt = 1; 905 auio.uio_offset = poffset; 906 auio.uio_segflg = UIO_NOCOPY; 907 auio.uio_rw = UIO_WRITE; 908 auio.uio_resid = maxsize; 909 auio.uio_procp = (struct proc *) 0; 910 error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred); 911 cnt.v_vnodeout++; 912 cnt.v_vnodepgsout += ncount; 913 914 if (error) { 915 printf("vnode_pager_putpages: I/O error %d\n", error); 916 } 917 if (auio.uio_resid) { 918 printf("vnode_pager_putpages: residual I/O %d at %ld\n", 919 auio.uio_resid, m[0]->pindex); 920 } 921 for (i = 0; i < count; i++) { 922 m[i]->busy--; 923 if (i < ncount) { 924 rtvals[i] = VM_PAGER_OK; 925 } 926 if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED)) 927 wakeup(m[i]); 928 } 929 return rtvals[0]; 930 } 931 932 struct vnode * 933 vnode_pager_lock(object) 934 vm_object_t object; 935 { 936 for (; object != NULL; object = object->backing_object) { 937 if (object->type != OBJT_VNODE) 938 continue; 939 940 VOP_LOCK(object->handle); 941 return object->handle; 942 } 943 return NULL; 944 } 945