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