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