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 */ 42 43 /* 44 * Page to/from files (vnodes). 45 */ 46 47 /* 48 * TODO: 49 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 50 * greatly re-simplify the vnode_pager. 51 */ 52 53 #include <sys/cdefs.h> 54 __FBSDID("$FreeBSD$"); 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/proc.h> 59 #include <sys/vnode.h> 60 #include <sys/mount.h> 61 #include <sys/bio.h> 62 #include <sys/buf.h> 63 #include <sys/vmmeter.h> 64 #include <sys/conf.h> 65 66 #include <vm/vm.h> 67 #include <vm/vm_object.h> 68 #include <vm/vm_page.h> 69 #include <vm/vm_pager.h> 70 #include <vm/vm_map.h> 71 #include <vm/vnode_pager.h> 72 #include <vm/vm_extern.h> 73 74 static void vnode_pager_init(void); 75 static vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 76 int *run); 77 static void vnode_pager_iodone(struct buf *bp); 78 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 79 static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 80 static void vnode_pager_dealloc(vm_object_t); 81 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int); 82 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 83 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 84 85 struct pagerops vnodepagerops = { 86 vnode_pager_init, 87 vnode_pager_alloc, 88 vnode_pager_dealloc, 89 vnode_pager_getpages, 90 vnode_pager_putpages, 91 vnode_pager_haspage, 92 NULL 93 }; 94 95 int vnode_pbuf_freecnt; 96 97 static void 98 vnode_pager_init(void) 99 { 100 101 vnode_pbuf_freecnt = nswbuf / 2 + 1; 102 } 103 104 /* 105 * Allocate (or lookup) pager for a vnode. 106 * Handle is a vnode pointer. 107 * 108 * MPSAFE 109 */ 110 vm_object_t 111 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 112 vm_ooffset_t offset) 113 { 114 vm_object_t object; 115 struct vnode *vp; 116 117 /* 118 * Pageout to vnode, no can do yet. 119 */ 120 if (handle == NULL) 121 return (NULL); 122 123 vp = (struct vnode *) handle; 124 125 ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc"); 126 127 mtx_lock(&Giant); 128 /* 129 * Prevent race condition when allocating the object. This 130 * can happen with NFS vnodes since the nfsnode isn't locked. 131 */ 132 VI_LOCK(vp); 133 while (vp->v_iflag & VI_OLOCK) { 134 vp->v_iflag |= VI_OWANT; 135 msleep(vp, VI_MTX(vp), PVM, "vnpobj", 0); 136 } 137 vp->v_iflag |= VI_OLOCK; 138 VI_UNLOCK(vp); 139 140 /* 141 * If the object is being terminated, wait for it to 142 * go away. 143 */ 144 while ((object = vp->v_object) != NULL) { 145 VM_OBJECT_LOCK(object); 146 if ((object->flags & OBJ_DEAD) == 0) 147 break; 148 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0); 149 } 150 151 if (vp->v_usecount == 0) 152 panic("vnode_pager_alloc: no vnode reference"); 153 154 if (object == NULL) { 155 /* 156 * And an object of the appropriate size 157 */ 158 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 159 160 object->un_pager.vnp.vnp_size = size; 161 162 object->handle = handle; 163 vp->v_object = object; 164 } else { 165 object->ref_count++; 166 VM_OBJECT_UNLOCK(object); 167 } 168 VI_LOCK(vp); 169 vp->v_usecount++; 170 vp->v_iflag &= ~VI_OLOCK; 171 if (vp->v_iflag & VI_OWANT) { 172 vp->v_iflag &= ~VI_OWANT; 173 wakeup(vp); 174 } 175 VI_UNLOCK(vp); 176 mtx_unlock(&Giant); 177 return (object); 178 } 179 180 /* 181 * The object must be locked. 182 */ 183 static void 184 vnode_pager_dealloc(object) 185 vm_object_t object; 186 { 187 struct vnode *vp = object->handle; 188 189 if (vp == NULL) 190 panic("vnode_pager_dealloc: pager already dealloced"); 191 192 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 193 vm_object_pip_wait(object, "vnpdea"); 194 195 object->handle = NULL; 196 object->type = OBJT_DEAD; 197 ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc"); 198 vp->v_object = NULL; 199 vp->v_vflag &= ~(VV_TEXT | VV_OBJBUF); 200 } 201 202 static boolean_t 203 vnode_pager_haspage(object, pindex, before, after) 204 vm_object_t object; 205 vm_pindex_t pindex; 206 int *before; 207 int *after; 208 { 209 struct vnode *vp = object->handle; 210 daddr_t bn; 211 int err; 212 daddr_t reqblock; 213 int poff; 214 int bsize; 215 int pagesperblock, blocksperpage; 216 217 GIANT_REQUIRED; 218 /* 219 * If no vp or vp is doomed or marked transparent to VM, we do not 220 * have the page. 221 */ 222 if (vp == NULL) 223 return FALSE; 224 225 VI_LOCK(vp); 226 if (vp->v_iflag & VI_DOOMED) { 227 VI_UNLOCK(vp); 228 return FALSE; 229 } 230 VI_UNLOCK(vp); 231 /* 232 * If filesystem no longer mounted or offset beyond end of file we do 233 * not have the page. 234 */ 235 if ((vp->v_mount == NULL) || 236 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 237 return FALSE; 238 239 bsize = vp->v_mount->mnt_stat.f_iosize; 240 pagesperblock = bsize / PAGE_SIZE; 241 blocksperpage = 0; 242 if (pagesperblock > 0) { 243 reqblock = pindex / pagesperblock; 244 } else { 245 blocksperpage = (PAGE_SIZE / bsize); 246 reqblock = pindex * blocksperpage; 247 } 248 err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 249 after, before); 250 if (err) 251 return TRUE; 252 if (bn == -1) 253 return FALSE; 254 if (pagesperblock > 0) { 255 poff = pindex - (reqblock * pagesperblock); 256 if (before) { 257 *before *= pagesperblock; 258 *before += poff; 259 } 260 if (after) { 261 int numafter; 262 *after *= pagesperblock; 263 numafter = pagesperblock - (poff + 1); 264 if (IDX_TO_OFF(pindex + numafter) > 265 object->un_pager.vnp.vnp_size) { 266 numafter = 267 OFF_TO_IDX(object->un_pager.vnp.vnp_size) - 268 pindex; 269 } 270 *after += numafter; 271 } 272 } else { 273 if (before) { 274 *before /= blocksperpage; 275 } 276 277 if (after) { 278 *after /= blocksperpage; 279 } 280 } 281 return TRUE; 282 } 283 284 /* 285 * Lets the VM system know about a change in size for a file. 286 * We adjust our own internal size and flush any cached pages in 287 * the associated object that are affected by the size change. 288 * 289 * Note: this routine may be invoked as a result of a pager put 290 * operation (possibly at object termination time), so we must be careful. 291 */ 292 void 293 vnode_pager_setsize(vp, nsize) 294 struct vnode *vp; 295 vm_ooffset_t nsize; 296 { 297 vm_object_t object; 298 vm_page_t m; 299 vm_pindex_t nobjsize; 300 301 if ((object = vp->v_object) == NULL) 302 return; 303 VM_OBJECT_LOCK(object); 304 if (nsize == object->un_pager.vnp.vnp_size) { 305 /* 306 * Hasn't changed size 307 */ 308 VM_OBJECT_UNLOCK(object); 309 return; 310 } 311 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 312 if (nsize < object->un_pager.vnp.vnp_size) { 313 /* 314 * File has shrunk. Toss any cached pages beyond the new EOF. 315 */ 316 if (nobjsize < object->size) 317 vm_object_page_remove(object, nobjsize, object->size, 318 FALSE); 319 /* 320 * this gets rid of garbage at the end of a page that is now 321 * only partially backed by the vnode. 322 * 323 * XXX for some reason (I don't know yet), if we take a 324 * completely invalid page and mark it partially valid 325 * it can screw up NFS reads, so we don't allow the case. 326 */ 327 if ((nsize & PAGE_MASK) && 328 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL) { 329 vm_page_lock_queues(); 330 if (m->valid) { 331 int base = (int)nsize & PAGE_MASK; 332 int size = PAGE_SIZE - base; 333 334 /* 335 * Clear out partial-page garbage in case 336 * the page has been mapped. 337 */ 338 pmap_zero_page_area(m, base, size); 339 340 /* 341 * XXX work around SMP data integrity race 342 * by unmapping the page from user processes. 343 * The garbage we just cleared may be mapped 344 * to a user process running on another cpu 345 * and this code is not running through normal 346 * I/O channels which handle SMP issues for 347 * us, so unmap page to synchronize all cpus. 348 * 349 * XXX should vm_pager_unmap_page() have 350 * dealt with this? 351 */ 352 pmap_remove_all(m); 353 354 /* 355 * Clear out partial-page dirty bits. This 356 * has the side effect of setting the valid 357 * bits, but that is ok. There are a bunch 358 * of places in the VM system where we expected 359 * m->dirty == VM_PAGE_BITS_ALL. The file EOF 360 * case is one of them. If the page is still 361 * partially dirty, make it fully dirty. 362 * 363 * note that we do not clear out the valid 364 * bits. This would prevent bogus_page 365 * replacement from working properly. 366 */ 367 vm_page_set_validclean(m, base, size); 368 if (m->dirty != 0) 369 m->dirty = VM_PAGE_BITS_ALL; 370 } 371 vm_page_unlock_queues(); 372 } 373 } 374 object->un_pager.vnp.vnp_size = nsize; 375 object->size = nobjsize; 376 VM_OBJECT_UNLOCK(object); 377 } 378 379 /* 380 * calculate the linear (byte) disk address of specified virtual 381 * file address 382 */ 383 static vm_offset_t 384 vnode_pager_addr(vp, address, run) 385 struct vnode *vp; 386 vm_ooffset_t address; 387 int *run; 388 { 389 int rtaddress; 390 int bsize; 391 daddr_t block; 392 struct vnode *rtvp; 393 int err; 394 daddr_t vblock; 395 int voffset; 396 397 GIANT_REQUIRED; 398 if ((int) address < 0) 399 return -1; 400 401 if (vp->v_mount == NULL) 402 return -1; 403 404 bsize = vp->v_mount->mnt_stat.f_iosize; 405 vblock = address / bsize; 406 voffset = address % bsize; 407 408 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 409 410 if (err || (block == -1)) 411 rtaddress = -1; 412 else { 413 rtaddress = block + voffset / DEV_BSIZE; 414 if (run) { 415 *run += 1; 416 *run *= bsize/PAGE_SIZE; 417 *run -= voffset/PAGE_SIZE; 418 } 419 } 420 421 return rtaddress; 422 } 423 424 /* 425 * interrupt routine for I/O completion 426 */ 427 static void 428 vnode_pager_iodone(bp) 429 struct buf *bp; 430 { 431 bp->b_flags |= B_DONE; 432 wakeup(bp); 433 } 434 435 /* 436 * small block filesystem vnode pager input 437 */ 438 static int 439 vnode_pager_input_smlfs(object, m) 440 vm_object_t object; 441 vm_page_t m; 442 { 443 int i; 444 int s; 445 struct vnode *dp, *vp; 446 struct buf *bp; 447 vm_offset_t kva; 448 int fileaddr; 449 vm_offset_t bsize; 450 int error = 0; 451 452 GIANT_REQUIRED; 453 454 vp = object->handle; 455 if (vp->v_mount == NULL) 456 return VM_PAGER_BAD; 457 458 bsize = vp->v_mount->mnt_stat.f_iosize; 459 460 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 461 462 kva = vm_pager_map_page(m); 463 464 for (i = 0; i < PAGE_SIZE / bsize; i++) { 465 vm_ooffset_t address; 466 467 if (vm_page_bits(i * bsize, bsize) & m->valid) 468 continue; 469 470 address = IDX_TO_OFF(m->pindex) + i * bsize; 471 if (address >= object->un_pager.vnp.vnp_size) { 472 fileaddr = -1; 473 } else { 474 fileaddr = vnode_pager_addr(vp, address, NULL); 475 } 476 if (fileaddr != -1) { 477 bp = getpbuf(&vnode_pbuf_freecnt); 478 479 /* build a minimal buffer header */ 480 bp->b_iocmd = BIO_READ; 481 bp->b_iodone = vnode_pager_iodone; 482 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 483 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 484 bp->b_rcred = crhold(curthread->td_ucred); 485 bp->b_wcred = crhold(curthread->td_ucred); 486 bp->b_data = (caddr_t) kva + i * bsize; 487 bp->b_blkno = fileaddr; 488 pbgetvp(dp, bp); 489 bp->b_bcount = bsize; 490 bp->b_bufsize = bsize; 491 bp->b_runningbufspace = bp->b_bufsize; 492 runningbufspace += bp->b_runningbufspace; 493 494 /* do the input */ 495 VOP_SPECSTRATEGY(bp->b_vp, bp); 496 497 /* we definitely need to be at splvm here */ 498 499 s = splvm(); 500 while ((bp->b_flags & B_DONE) == 0) { 501 tsleep(bp, PVM, "vnsrd", 0); 502 } 503 splx(s); 504 if ((bp->b_ioflags & BIO_ERROR) != 0) 505 error = EIO; 506 507 /* 508 * free the buffer header back to the swap buffer pool 509 */ 510 relpbuf(bp, &vnode_pbuf_freecnt); 511 if (error) 512 break; 513 514 vm_page_lock_queues(); 515 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 516 vm_page_unlock_queues(); 517 } else { 518 vm_page_lock_queues(); 519 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 520 vm_page_unlock_queues(); 521 bzero((caddr_t) kva + i * bsize, bsize); 522 } 523 } 524 vm_pager_unmap_page(kva); 525 vm_page_lock_queues(); 526 pmap_clear_modify(m); 527 vm_page_flag_clear(m, PG_ZERO); 528 vm_page_unlock_queues(); 529 if (error) { 530 return VM_PAGER_ERROR; 531 } 532 return VM_PAGER_OK; 533 534 } 535 536 537 /* 538 * old style vnode pager output routine 539 */ 540 static int 541 vnode_pager_input_old(object, m) 542 vm_object_t object; 543 vm_page_t m; 544 { 545 struct uio auio; 546 struct iovec aiov; 547 int error; 548 int size; 549 vm_offset_t kva; 550 struct vnode *vp; 551 552 GIANT_REQUIRED; 553 error = 0; 554 555 /* 556 * Return failure if beyond current EOF 557 */ 558 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 559 return VM_PAGER_BAD; 560 } else { 561 size = PAGE_SIZE; 562 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 563 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 564 565 /* 566 * Allocate a kernel virtual address and initialize so that 567 * we can use VOP_READ/WRITE routines. 568 */ 569 kva = vm_pager_map_page(m); 570 571 vp = object->handle; 572 aiov.iov_base = (caddr_t) kva; 573 aiov.iov_len = size; 574 auio.uio_iov = &aiov; 575 auio.uio_iovcnt = 1; 576 auio.uio_offset = IDX_TO_OFF(m->pindex); 577 auio.uio_segflg = UIO_SYSSPACE; 578 auio.uio_rw = UIO_READ; 579 auio.uio_resid = size; 580 auio.uio_td = curthread; 581 582 error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 583 if (!error) { 584 int count = size - auio.uio_resid; 585 586 if (count == 0) 587 error = EINVAL; 588 else if (count != PAGE_SIZE) 589 bzero((caddr_t) kva + count, PAGE_SIZE - count); 590 } 591 vm_pager_unmap_page(kva); 592 } 593 vm_page_lock_queues(); 594 pmap_clear_modify(m); 595 vm_page_undirty(m); 596 vm_page_flag_clear(m, PG_ZERO); 597 if (!error) 598 m->valid = VM_PAGE_BITS_ALL; 599 vm_page_unlock_queues(); 600 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 601 } 602 603 /* 604 * generic vnode pager input routine 605 */ 606 607 /* 608 * Local media VFS's that do not implement their own VOP_GETPAGES 609 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 610 * to implement the previous behaviour. 611 * 612 * All other FS's should use the bypass to get to the local media 613 * backing vp's VOP_GETPAGES. 614 */ 615 static int 616 vnode_pager_getpages(object, m, count, reqpage) 617 vm_object_t object; 618 vm_page_t *m; 619 int count; 620 int reqpage; 621 { 622 int rtval; 623 struct vnode *vp; 624 int bytes = count * PAGE_SIZE; 625 626 vp = object->handle; 627 VM_OBJECT_UNLOCK(object); 628 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 629 KASSERT(rtval != EOPNOTSUPP, 630 ("vnode_pager: FS getpages not implemented\n")); 631 VM_OBJECT_LOCK(object); 632 return rtval; 633 } 634 635 /* 636 * This is now called from local media FS's to operate against their 637 * own vnodes if they fail to implement VOP_GETPAGES. 638 */ 639 int 640 vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 641 struct vnode *vp; 642 vm_page_t *m; 643 int bytecount; 644 int reqpage; 645 { 646 vm_object_t object; 647 vm_offset_t kva; 648 off_t foff, tfoff, nextoff; 649 int i, j, size, bsize, first, firstaddr; 650 struct vnode *dp; 651 int runpg; 652 int runend; 653 struct buf *bp; 654 int s; 655 int count; 656 int error = 0; 657 658 GIANT_REQUIRED; 659 object = vp->v_object; 660 count = bytecount / PAGE_SIZE; 661 662 if (vp->v_mount == NULL) 663 return VM_PAGER_BAD; 664 665 bsize = vp->v_mount->mnt_stat.f_iosize; 666 667 /* get the UNDERLYING device for the file with VOP_BMAP() */ 668 669 /* 670 * originally, we did not check for an error return value -- assuming 671 * an fs always has a bmap entry point -- that assumption is wrong!!! 672 */ 673 foff = IDX_TO_OFF(m[reqpage]->pindex); 674 675 /* 676 * if we can't bmap, use old VOP code 677 */ 678 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 679 VM_OBJECT_LOCK(object); 680 vm_page_lock_queues(); 681 for (i = 0; i < count; i++) 682 if (i != reqpage) 683 vm_page_free(m[i]); 684 vm_page_unlock_queues(); 685 VM_OBJECT_UNLOCK(object); 686 cnt.v_vnodein++; 687 cnt.v_vnodepgsin++; 688 return vnode_pager_input_old(object, m[reqpage]); 689 690 /* 691 * if the blocksize is smaller than a page size, then use 692 * special small filesystem code. NFS sometimes has a small 693 * blocksize, but it can handle large reads itself. 694 */ 695 } else if ((PAGE_SIZE / bsize) > 1 && 696 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 697 VM_OBJECT_LOCK(object); 698 vm_page_lock_queues(); 699 for (i = 0; i < count; i++) 700 if (i != reqpage) 701 vm_page_free(m[i]); 702 vm_page_unlock_queues(); 703 VM_OBJECT_UNLOCK(object); 704 cnt.v_vnodein++; 705 cnt.v_vnodepgsin++; 706 return vnode_pager_input_smlfs(object, m[reqpage]); 707 } 708 709 /* 710 * If we have a completely valid page available to us, we can 711 * clean up and return. Otherwise we have to re-read the 712 * media. 713 */ 714 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 715 VM_OBJECT_LOCK(object); 716 vm_page_lock_queues(); 717 for (i = 0; i < count; i++) 718 if (i != reqpage) 719 vm_page_free(m[i]); 720 vm_page_unlock_queues(); 721 VM_OBJECT_UNLOCK(object); 722 return VM_PAGER_OK; 723 } 724 m[reqpage]->valid = 0; 725 726 /* 727 * here on direct device I/O 728 */ 729 firstaddr = -1; 730 731 /* 732 * calculate the run that includes the required page 733 */ 734 for (first = 0, i = 0; i < count; i = runend) { 735 firstaddr = vnode_pager_addr(vp, 736 IDX_TO_OFF(m[i]->pindex), &runpg); 737 if (firstaddr == -1) { 738 VM_OBJECT_LOCK(object); 739 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 740 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx", 741 firstaddr, (uintmax_t)(foff >> 32), 742 (uintmax_t)foff, 743 (uintmax_t) 744 (object->un_pager.vnp.vnp_size >> 32), 745 (uintmax_t)object->un_pager.vnp.vnp_size); 746 } 747 vm_page_lock_queues(); 748 vm_page_free(m[i]); 749 vm_page_unlock_queues(); 750 VM_OBJECT_UNLOCK(object); 751 runend = i + 1; 752 first = runend; 753 continue; 754 } 755 runend = i + runpg; 756 if (runend <= reqpage) { 757 VM_OBJECT_LOCK(object); 758 vm_page_lock_queues(); 759 for (j = i; j < runend; j++) 760 vm_page_free(m[j]); 761 vm_page_unlock_queues(); 762 VM_OBJECT_UNLOCK(object); 763 } else { 764 if (runpg < (count - first)) { 765 VM_OBJECT_LOCK(object); 766 vm_page_lock_queues(); 767 for (i = first + runpg; i < count; i++) 768 vm_page_free(m[i]); 769 vm_page_unlock_queues(); 770 VM_OBJECT_UNLOCK(object); 771 count = first + runpg; 772 } 773 break; 774 } 775 first = runend; 776 } 777 778 /* 779 * the first and last page have been calculated now, move input pages 780 * to be zero based... 781 */ 782 if (first != 0) { 783 for (i = first; i < count; i++) { 784 m[i - first] = m[i]; 785 } 786 count -= first; 787 reqpage -= first; 788 } 789 790 /* 791 * calculate the file virtual address for the transfer 792 */ 793 foff = IDX_TO_OFF(m[0]->pindex); 794 795 /* 796 * calculate the size of the transfer 797 */ 798 size = count * PAGE_SIZE; 799 if ((foff + size) > object->un_pager.vnp.vnp_size) 800 size = object->un_pager.vnp.vnp_size - foff; 801 802 /* 803 * round up physical size for real devices. 804 */ 805 if (dp->v_type == VBLK || dp->v_type == VCHR) { 806 int secmask = dp->v_rdev->si_bsize_phys - 1; 807 KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1)); 808 size = (size + secmask) & ~secmask; 809 } 810 811 bp = getpbuf(&vnode_pbuf_freecnt); 812 kva = (vm_offset_t) bp->b_data; 813 814 /* 815 * and map the pages to be read into the kva 816 */ 817 pmap_qenter(kva, m, count); 818 819 /* build a minimal buffer header */ 820 bp->b_iocmd = BIO_READ; 821 bp->b_iodone = vnode_pager_iodone; 822 /* B_PHYS is not set, but it is nice to fill this in */ 823 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 824 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 825 bp->b_rcred = crhold(curthread->td_ucred); 826 bp->b_wcred = crhold(curthread->td_ucred); 827 bp->b_blkno = firstaddr; 828 pbgetvp(dp, bp); 829 bp->b_bcount = size; 830 bp->b_bufsize = size; 831 bp->b_runningbufspace = bp->b_bufsize; 832 runningbufspace += bp->b_runningbufspace; 833 834 cnt.v_vnodein++; 835 cnt.v_vnodepgsin += count; 836 837 /* do the input */ 838 if (dp->v_type == VCHR) 839 VOP_SPECSTRATEGY(bp->b_vp, bp); 840 else 841 VOP_STRATEGY(bp->b_vp, bp); 842 843 s = splvm(); 844 /* we definitely need to be at splvm here */ 845 846 while ((bp->b_flags & B_DONE) == 0) { 847 tsleep(bp, PVM, "vnread", 0); 848 } 849 splx(s); 850 if ((bp->b_ioflags & BIO_ERROR) != 0) 851 error = EIO; 852 853 if (!error) { 854 if (size != count * PAGE_SIZE) 855 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 856 } 857 pmap_qremove(kva, count); 858 859 /* 860 * free the buffer header back to the swap buffer pool 861 */ 862 relpbuf(bp, &vnode_pbuf_freecnt); 863 864 VM_OBJECT_LOCK(object); 865 vm_page_lock_queues(); 866 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 867 vm_page_t mt; 868 869 nextoff = tfoff + PAGE_SIZE; 870 mt = m[i]; 871 872 if (nextoff <= object->un_pager.vnp.vnp_size) { 873 /* 874 * Read filled up entire page. 875 */ 876 mt->valid = VM_PAGE_BITS_ALL; 877 vm_page_undirty(mt); /* should be an assert? XXX */ 878 pmap_clear_modify(mt); 879 } else { 880 /* 881 * Read did not fill up entire page. Since this 882 * is getpages, the page may be mapped, so we have 883 * to zero the invalid portions of the page even 884 * though we aren't setting them valid. 885 * 886 * Currently we do not set the entire page valid, 887 * we just try to clear the piece that we couldn't 888 * read. 889 */ 890 vm_page_set_validclean(mt, 0, 891 object->un_pager.vnp.vnp_size - tfoff); 892 /* handled by vm_fault now */ 893 /* vm_page_zero_invalid(mt, FALSE); */ 894 } 895 896 vm_page_flag_clear(mt, PG_ZERO); 897 if (i != reqpage) { 898 899 /* 900 * whether or not to leave the page activated is up in 901 * the air, but we should put the page on a page queue 902 * somewhere. (it already is in the object). Result: 903 * It appears that empirical results show that 904 * deactivating pages is best. 905 */ 906 907 /* 908 * just in case someone was asking for this page we 909 * now tell them that it is ok to use 910 */ 911 if (!error) { 912 if (mt->flags & PG_WANTED) 913 vm_page_activate(mt); 914 else 915 vm_page_deactivate(mt); 916 vm_page_wakeup(mt); 917 } else { 918 vm_page_free(mt); 919 } 920 } 921 } 922 vm_page_unlock_queues(); 923 VM_OBJECT_UNLOCK(object); 924 if (error) { 925 printf("vnode_pager_getpages: I/O read error\n"); 926 } 927 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 928 } 929 930 /* 931 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 932 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 933 * vnode_pager_generic_putpages() to implement the previous behaviour. 934 * 935 * All other FS's should use the bypass to get to the local media 936 * backing vp's VOP_PUTPAGES. 937 */ 938 static void 939 vnode_pager_putpages(object, m, count, sync, rtvals) 940 vm_object_t object; 941 vm_page_t *m; 942 int count; 943 boolean_t sync; 944 int *rtvals; 945 { 946 int rtval; 947 struct vnode *vp; 948 struct mount *mp; 949 int bytes = count * PAGE_SIZE; 950 951 GIANT_REQUIRED; 952 /* 953 * Force synchronous operation if we are extremely low on memory 954 * to prevent a low-memory deadlock. VOP operations often need to 955 * allocate more memory to initiate the I/O ( i.e. do a BMAP 956 * operation ). The swapper handles the case by limiting the amount 957 * of asynchronous I/O, but that sort of solution doesn't scale well 958 * for the vnode pager without a lot of work. 959 * 960 * Also, the backing vnode's iodone routine may not wake the pageout 961 * daemon up. This should be probably be addressed XXX. 962 */ 963 964 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 965 sync |= OBJPC_SYNC; 966 967 /* 968 * Call device-specific putpages function 969 */ 970 vp = object->handle; 971 if (vp->v_type != VREG) 972 mp = NULL; 973 (void)vn_start_write(vp, &mp, V_WAIT); 974 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 975 KASSERT(rtval != EOPNOTSUPP, 976 ("vnode_pager: stale FS putpages\n")); 977 vn_finished_write(mp); 978 } 979 980 981 /* 982 * This is now called from local media FS's to operate against their 983 * own vnodes if they fail to implement VOP_PUTPAGES. 984 * 985 * This is typically called indirectly via the pageout daemon and 986 * clustering has already typically occured, so in general we ask the 987 * underlying filesystem to write the data out asynchronously rather 988 * then delayed. 989 */ 990 int 991 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 992 struct vnode *vp; 993 vm_page_t *m; 994 int bytecount; 995 int flags; 996 int *rtvals; 997 { 998 int i; 999 vm_object_t object; 1000 int count; 1001 1002 int maxsize, ncount; 1003 vm_ooffset_t poffset; 1004 struct uio auio; 1005 struct iovec aiov; 1006 int error; 1007 int ioflags; 1008 1009 GIANT_REQUIRED; 1010 object = vp->v_object; 1011 count = bytecount / PAGE_SIZE; 1012 1013 for (i = 0; i < count; i++) 1014 rtvals[i] = VM_PAGER_AGAIN; 1015 1016 if ((int) m[0]->pindex < 0) { 1017 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n", 1018 (long)m[0]->pindex, m[0]->dirty); 1019 rtvals[0] = VM_PAGER_BAD; 1020 return VM_PAGER_BAD; 1021 } 1022 1023 maxsize = count * PAGE_SIZE; 1024 ncount = count; 1025 1026 poffset = IDX_TO_OFF(m[0]->pindex); 1027 1028 /* 1029 * If the page-aligned write is larger then the actual file we 1030 * have to invalidate pages occuring beyond the file EOF. However, 1031 * there is an edge case where a file may not be page-aligned where 1032 * the last page is partially invalid. In this case the filesystem 1033 * may not properly clear the dirty bits for the entire page (which 1034 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 1035 * With the page locked we are free to fix-up the dirty bits here. 1036 * 1037 * We do not under any circumstances truncate the valid bits, as 1038 * this will screw up bogus page replacement. 1039 */ 1040 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 1041 if (object->un_pager.vnp.vnp_size > poffset) { 1042 int pgoff; 1043 1044 maxsize = object->un_pager.vnp.vnp_size - poffset; 1045 ncount = btoc(maxsize); 1046 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1047 vm_page_clear_dirty(m[ncount - 1], pgoff, 1048 PAGE_SIZE - pgoff); 1049 } 1050 } else { 1051 maxsize = 0; 1052 ncount = 0; 1053 } 1054 if (ncount < count) { 1055 for (i = ncount; i < count; i++) { 1056 rtvals[i] = VM_PAGER_BAD; 1057 } 1058 } 1059 } 1060 1061 /* 1062 * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 1063 * rather then a bdwrite() to prevent paging I/O from saturating 1064 * the buffer cache. Dummy-up the sequential heuristic to cause 1065 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 1066 * the system decides how to cluster. 1067 */ 1068 ioflags = IO_VMIO; 1069 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 1070 ioflags |= IO_SYNC; 1071 else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 1072 ioflags |= IO_ASYNC; 1073 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 1074 ioflags |= IO_SEQMAX << IO_SEQSHIFT; 1075 1076 aiov.iov_base = (caddr_t) 0; 1077 aiov.iov_len = maxsize; 1078 auio.uio_iov = &aiov; 1079 auio.uio_iovcnt = 1; 1080 auio.uio_offset = poffset; 1081 auio.uio_segflg = UIO_NOCOPY; 1082 auio.uio_rw = UIO_WRITE; 1083 auio.uio_resid = maxsize; 1084 auio.uio_td = (struct thread *) 0; 1085 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1086 cnt.v_vnodeout++; 1087 cnt.v_vnodepgsout += ncount; 1088 1089 if (error) { 1090 printf("vnode_pager_putpages: I/O error %d\n", error); 1091 } 1092 if (auio.uio_resid) { 1093 printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1094 auio.uio_resid, (u_long)m[0]->pindex); 1095 } 1096 for (i = 0; i < ncount; i++) { 1097 rtvals[i] = VM_PAGER_OK; 1098 } 1099 return rtvals[0]; 1100 } 1101 1102 struct vnode * 1103 vnode_pager_lock(object) 1104 vm_object_t object; 1105 { 1106 struct thread *td = curthread; /* XXX */ 1107 1108 GIANT_REQUIRED; 1109 1110 for (; object != NULL; object = object->backing_object) { 1111 if (object->type != OBJT_VNODE) 1112 continue; 1113 if (object->flags & OBJ_DEAD) { 1114 return NULL; 1115 } 1116 1117 /* XXX; If object->handle can change, we need to cache it. */ 1118 while (vget(object->handle, 1119 LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, td)){ 1120 if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE)) 1121 return NULL; 1122 printf("vnode_pager_lock: retrying\n"); 1123 } 1124 return object->handle; 1125 } 1126 return NULL; 1127 } 1128