vnode_pager.c revision 15583
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.59 1996/03/19 05:13:22 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 74static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address, 75 int *run)); 76static void vnode_pager_iodone __P((struct buf *bp)); 77static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m)); 78static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m)); 79static void vnode_pager_dealloc __P((vm_object_t)); 80static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 81static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *)); 82static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 83 84struct 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 94static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m, 95 int count, int reqpage)); 96static 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 */ 104vm_object_t 105vnode_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 VREF(vp); 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 178static void 179vnode_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 204static boolean_t 205vnode_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 */ 277void 278vnode_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 326void 327vnode_pager_umount(mp) 328 register struct mount *mp; 329{ 330 struct vnode *vp, *nvp; 331 332loop: 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 */ 364void 365vnode_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 VOP_UNLOCK(vp); 379 pager_cache(object, FALSE); 380 VOP_LOCK(vp); 381 return; 382} 383 384 385void 386vnode_pager_freepage(m) 387 vm_page_t m; 388{ 389 PAGE_WAKEUP(m); 390 vm_page_free(m); 391} 392 393/* 394 * calculate the linear (byte) disk address of specified virtual 395 * file address 396 */ 397static vm_offset_t 398vnode_pager_addr(vp, address, run) 399 struct vnode *vp; 400 vm_ooffset_t address; 401 int *run; 402{ 403 int rtaddress; 404 int bsize; 405 daddr_t block; 406 struct vnode *rtvp; 407 int err; 408 daddr_t vblock; 409 int voffset; 410 411 if ((int) address < 0) 412 return -1; 413 414 if (vp->v_mount == NULL) 415 return -1; 416 417 bsize = vp->v_mount->mnt_stat.f_iosize; 418 vblock = address / bsize; 419 voffset = address % bsize; 420 421 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 422 423 if (err || (block == -1)) 424 rtaddress = -1; 425 else { 426 rtaddress = block + voffset / DEV_BSIZE; 427 if( run) { 428 *run += 1; 429 *run *= bsize/PAGE_SIZE; 430 *run -= voffset/PAGE_SIZE; 431 } 432 } 433 434 return rtaddress; 435} 436 437/* 438 * interrupt routine for I/O completion 439 */ 440static void 441vnode_pager_iodone(bp) 442 struct buf *bp; 443{ 444 bp->b_flags |= B_DONE; 445 wakeup(bp); 446} 447 448/* 449 * small block file system vnode pager input 450 */ 451static int 452vnode_pager_input_smlfs(object, m) 453 vm_object_t object; 454 vm_page_t m; 455{ 456 int i; 457 int s; 458 struct vnode *dp, *vp; 459 struct buf *bp; 460 vm_offset_t kva; 461 int fileaddr; 462 vm_offset_t bsize; 463 int error = 0; 464 465 vp = object->handle; 466 if (vp->v_mount == NULL) 467 return VM_PAGER_BAD; 468 469 bsize = vp->v_mount->mnt_stat.f_iosize; 470 471 472 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 473 474 kva = vm_pager_map_page(m); 475 476 for (i = 0; i < PAGE_SIZE / bsize; i++) { 477 478 if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid)) 479 continue; 480 481 fileaddr = vnode_pager_addr(vp, 482 IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 483 if (fileaddr != -1) { 484 bp = getpbuf(); 485 486 /* build a minimal buffer header */ 487 bp->b_flags = B_BUSY | B_READ | B_CALL; 488 bp->b_iodone = vnode_pager_iodone; 489 bp->b_proc = curproc; 490 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 491 if (bp->b_rcred != NOCRED) 492 crhold(bp->b_rcred); 493 if (bp->b_wcred != NOCRED) 494 crhold(bp->b_wcred); 495 bp->b_un.b_addr = (caddr_t) kva + i * bsize; 496 bp->b_blkno = fileaddr; 497 pbgetvp(dp, bp); 498 bp->b_bcount = bsize; 499 bp->b_bufsize = bsize; 500 501 /* do the input */ 502 VOP_STRATEGY(bp); 503 504 /* we definitely need to be at splbio here */ 505 506 s = splbio(); 507 while ((bp->b_flags & B_DONE) == 0) { 508 tsleep(bp, PVM, "vnsrd", 0); 509 } 510 splx(s); 511 if ((bp->b_flags & B_ERROR) != 0) 512 error = EIO; 513 514 /* 515 * free the buffer header back to the swap buffer pool 516 */ 517 relpbuf(bp); 518 if (error) 519 break; 520 521 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 522 } else { 523 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 524 bzero((caddr_t) kva + i * bsize, bsize); 525 } 526 } 527 vm_pager_unmap_page(kva); 528 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 529 m->flags &= ~PG_ZERO; 530 if (error) { 531 return VM_PAGER_ERROR; 532 } 533 return VM_PAGER_OK; 534 535} 536 537 538/* 539 * old style vnode pager output routine 540 */ 541static int 542vnode_pager_input_old(object, m) 543 vm_object_t object; 544 vm_page_t m; 545{ 546 struct uio auio; 547 struct iovec aiov; 548 int error; 549 int size; 550 vm_offset_t kva; 551 552 error = 0; 553 554 /* 555 * Return failure if beyond current EOF 556 */ 557 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 558 return VM_PAGER_BAD; 559 } else { 560 size = PAGE_SIZE; 561 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 562 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 563 564 /* 565 * Allocate a kernel virtual address and initialize so that 566 * we can use VOP_READ/WRITE routines. 567 */ 568 kva = vm_pager_map_page(m); 569 570 aiov.iov_base = (caddr_t) kva; 571 aiov.iov_len = size; 572 auio.uio_iov = &aiov; 573 auio.uio_iovcnt = 1; 574 auio.uio_offset = IDX_TO_OFF(m->pindex); 575 auio.uio_segflg = UIO_SYSSPACE; 576 auio.uio_rw = UIO_READ; 577 auio.uio_resid = size; 578 auio.uio_procp = (struct proc *) 0; 579 580 error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 581 if (!error) { 582 register int count = size - auio.uio_resid; 583 584 if (count == 0) 585 error = EINVAL; 586 else if (count != PAGE_SIZE) 587 bzero((caddr_t) kva + count, PAGE_SIZE - count); 588 } 589 vm_pager_unmap_page(kva); 590 } 591 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 592 m->dirty = 0; 593 m->flags &= ~PG_ZERO; 594 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 595} 596 597/* 598 * generic vnode pager input routine 599 */ 600 601static int 602vnode_pager_getpages(object, m, count, reqpage) 603 vm_object_t object; 604 vm_page_t *m; 605 int count; 606 int reqpage; 607{ 608 int rtval; 609 struct vnode *vp; 610 vp = object->handle; 611 rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0); 612 if (rtval == EOPNOTSUPP) 613 return vnode_pager_leaf_getpages(object, m, count, reqpage); 614 else 615 return rtval; 616} 617 618static int 619vnode_pager_leaf_getpages(object, m, count, reqpage) 620 vm_object_t object; 621 vm_page_t *m; 622 int count; 623 int reqpage; 624{ 625 vm_offset_t kva; 626 off_t foff; 627 int i, size, bsize, first, firstaddr; 628 struct vnode *dp, *vp; 629 int runpg; 630 int runend; 631 struct buf *bp; 632 int s; 633 int error = 0; 634 635 vp = object->handle; 636 if (vp->v_mount == NULL) 637 return VM_PAGER_BAD; 638 639 bsize = vp->v_mount->mnt_stat.f_iosize; 640 641 /* get the UNDERLYING device for the file with VOP_BMAP() */ 642 643 /* 644 * originally, we did not check for an error return value -- assuming 645 * an fs always has a bmap entry point -- that assumption is wrong!!! 646 */ 647 foff = IDX_TO_OFF(m[reqpage]->pindex); 648 649 /* 650 * if we can't bmap, use old VOP code 651 */ 652 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 653 for (i = 0; i < count; i++) { 654 if (i != reqpage) { 655 vnode_pager_freepage(m[i]); 656 } 657 } 658 cnt.v_vnodein++; 659 cnt.v_vnodepgsin++; 660 return vnode_pager_input_old(object, m[reqpage]); 661 662 /* 663 * if the blocksize is smaller than a page size, then use 664 * special small filesystem code. NFS sometimes has a small 665 * blocksize, but it can handle large reads itself. 666 */ 667 } else if ((PAGE_SIZE / bsize) > 1 && 668 (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) { 669 670 for (i = 0; i < count; i++) { 671 if (i != reqpage) { 672 vnode_pager_freepage(m[i]); 673 } 674 } 675 cnt.v_vnodein++; 676 cnt.v_vnodepgsin++; 677 return vnode_pager_input_smlfs(object, m[reqpage]); 678 } 679 /* 680 * if ANY DEV_BSIZE blocks are valid on a large filesystem block 681 * then, the entire page is valid -- 682 */ 683 if (m[reqpage]->valid) { 684 m[reqpage]->valid = VM_PAGE_BITS_ALL; 685 for (i = 0; i < count; i++) { 686 if (i != reqpage) 687 vnode_pager_freepage(m[i]); 688 } 689 return VM_PAGER_OK; 690 } 691 692 /* 693 * here on direct device I/O 694 */ 695 696 firstaddr = -1; 697 /* 698 * calculate the run that includes the required page 699 */ 700 for(first = 0, i = 0; i < count; i = runend) { 701 firstaddr = vnode_pager_addr(vp, 702 IDX_TO_OFF(m[i]->pindex), &runpg); 703 if (firstaddr == -1) { 704 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 705 panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d", 706 firstaddr, foff, object->un_pager.vnp.vnp_size); 707 } 708 vnode_pager_freepage(m[i]); 709 runend = i + 1; 710 first = runend; 711 continue; 712 } 713 runend = i + runpg; 714 if (runend <= reqpage) { 715 int j; 716 for (j = i; j < runend; j++) { 717 vnode_pager_freepage(m[j]); 718 } 719 } else { 720 if (runpg < (count - first)) { 721 for (i = first + runpg; i < count; i++) 722 vnode_pager_freepage(m[i]); 723 count = first + runpg; 724 } 725 break; 726 } 727 first = runend; 728 } 729 730 /* 731 * the first and last page have been calculated now, move input pages 732 * to be zero based... 733 */ 734 if (first != 0) { 735 for (i = first; i < count; i++) { 736 m[i - first] = m[i]; 737 } 738 count -= first; 739 reqpage -= first; 740 } 741 742 /* 743 * calculate the file virtual address for the transfer 744 */ 745 foff = IDX_TO_OFF(m[0]->pindex); 746 747 /* 748 * calculate the size of the transfer 749 */ 750 size = count * PAGE_SIZE; 751 if ((foff + size) > object->un_pager.vnp.vnp_size) 752 size = object->un_pager.vnp.vnp_size - foff; 753 754 /* 755 * round up physical size for real devices 756 */ 757 if (dp->v_type == VBLK || dp->v_type == VCHR) 758 size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 759 760 bp = getpbuf(); 761 kva = (vm_offset_t) bp->b_data; 762 763 /* 764 * and map the pages to be read into the kva 765 */ 766 pmap_qenter(kva, m, count); 767 768 /* build a minimal buffer header */ 769 bp->b_flags = B_BUSY | B_READ | B_CALL; 770 bp->b_iodone = vnode_pager_iodone; 771 /* B_PHYS is not set, but it is nice to fill this in */ 772 bp->b_proc = curproc; 773 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 774 if (bp->b_rcred != NOCRED) 775 crhold(bp->b_rcred); 776 if (bp->b_wcred != NOCRED) 777 crhold(bp->b_wcred); 778 bp->b_blkno = firstaddr; 779 pbgetvp(dp, bp); 780 bp->b_bcount = size; 781 bp->b_bufsize = size; 782 783 cnt.v_vnodein++; 784 cnt.v_vnodepgsin += count; 785 786 /* do the input */ 787 VOP_STRATEGY(bp); 788 789 s = splbio(); 790 /* we definitely need to be at splbio here */ 791 792 while ((bp->b_flags & B_DONE) == 0) { 793 tsleep(bp, PVM, "vnread", 0); 794 } 795 splx(s); 796 if ((bp->b_flags & B_ERROR) != 0) 797 error = EIO; 798 799 if (!error) { 800 if (size != count * PAGE_SIZE) 801 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 802 } 803 pmap_qremove(kva, count); 804 805 /* 806 * free the buffer header back to the swap buffer pool 807 */ 808 relpbuf(bp); 809 810 for (i = 0; i < count; i++) { 811 pmap_clear_modify(VM_PAGE_TO_PHYS(m[i])); 812 m[i]->dirty = 0; 813 m[i]->valid = VM_PAGE_BITS_ALL; 814 m[i]->flags &= ~PG_ZERO; 815 if (i != reqpage) { 816 817 /* 818 * whether or not to leave the page activated is up in 819 * the air, but we should put the page on a page queue 820 * somewhere. (it already is in the object). Result: 821 * It appears that emperical results show that 822 * deactivating pages is best. 823 */ 824 825 /* 826 * just in case someone was asking for this page we 827 * now tell them that it is ok to use 828 */ 829 if (!error) { 830 vm_page_deactivate(m[i]); 831 PAGE_WAKEUP(m[i]); 832 } else { 833 vnode_pager_freepage(m[i]); 834 } 835 } 836 } 837 if (error) { 838 printf("vnode_pager_getpages: I/O read error\n"); 839 } 840 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 841} 842 843static int 844vnode_pager_putpages(object, m, count, sync, rtvals) 845 vm_object_t object; 846 vm_page_t *m; 847 int count; 848 boolean_t sync; 849 int *rtvals; 850{ 851 int rtval; 852 struct vnode *vp; 853 vp = object->handle; 854 rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0); 855 if (rtval == EOPNOTSUPP) 856 return vnode_pager_leaf_putpages(object, m, count, sync, rtvals); 857 else 858 return rtval; 859} 860 861/* 862 * generic vnode pager output routine 863 */ 864static int 865vnode_pager_leaf_putpages(object, m, count, sync, rtvals) 866 vm_object_t object; 867 vm_page_t *m; 868 int count; 869 boolean_t sync; 870 int *rtvals; 871{ 872 int i; 873 874 struct vnode *vp; 875 int maxsize, ncount; 876 vm_ooffset_t poffset; 877 struct uio auio; 878 struct iovec aiov; 879 int error; 880 881 vp = object->handle;; 882 for (i = 0; i < count; i++) 883 rtvals[i] = VM_PAGER_AGAIN; 884 885 if ((int) m[0]->pindex < 0) { 886 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty); 887 rtvals[0] = VM_PAGER_BAD; 888 return VM_PAGER_BAD; 889 } 890 891 maxsize = count * PAGE_SIZE; 892 ncount = count; 893 894 poffset = IDX_TO_OFF(m[0]->pindex); 895 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 896 if (object->un_pager.vnp.vnp_size > poffset) 897 maxsize = object->un_pager.vnp.vnp_size - poffset; 898 else 899 maxsize = 0; 900 ncount = btoc(maxsize); 901 if (ncount < count) { 902 for (i = ncount; i < count; i++) { 903 rtvals[i] = VM_PAGER_BAD; 904 } 905#ifdef BOGUS 906 if (ncount == 0) { 907 printf("vnode_pager_putpages: write past end of file: %d, %lu\n", 908 poffset, 909 (unsigned long) object->un_pager.vnp.vnp_size); 910 return rtvals[0]; 911 } 912#endif 913 } 914 } 915 916 for (i = 0; i < count; i++) { 917 m[i]->busy++; 918 m[i]->flags &= ~PG_BUSY; 919 } 920 921 aiov.iov_base = (caddr_t) 0; 922 aiov.iov_len = maxsize; 923 auio.uio_iov = &aiov; 924 auio.uio_iovcnt = 1; 925 auio.uio_offset = poffset; 926 auio.uio_segflg = UIO_NOCOPY; 927 auio.uio_rw = UIO_WRITE; 928 auio.uio_resid = maxsize; 929 auio.uio_procp = (struct proc *) 0; 930 error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred); 931 cnt.v_vnodeout++; 932 cnt.v_vnodepgsout += ncount; 933 934 if (error) { 935 printf("vnode_pager_putpages: I/O error %d\n", error); 936 } 937 if (auio.uio_resid) { 938 printf("vnode_pager_putpages: residual I/O %d at %ld\n", 939 auio.uio_resid, m[0]->pindex); 940 } 941 for (i = 0; i < count; i++) { 942 m[i]->busy--; 943 if (i < ncount) { 944 rtvals[i] = VM_PAGER_OK; 945 } 946 if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED)) 947 wakeup(m[i]); 948 } 949 return rtvals[0]; 950} 951 952struct vnode * 953vnode_pager_lock(object) 954 vm_object_t object; 955{ 956 for (; object != NULL; object = object->backing_object) { 957 if (object->type != OBJT_VNODE) 958 continue; 959 960 VOP_LOCK(object->handle); 961 return object->handle; 962 } 963 return NULL; 964} 965