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