vnode_pager.c revision 140723
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: head/sys/vm/vnode_pager.c 140723 2005-01-24 10:48:29Z jeff $"); 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 75static void vnode_pager_init(void); 76static vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 77 int *run); 78static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 79static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 80static void vnode_pager_dealloc(vm_object_t); 81static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int); 82static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 83static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 84 85struct 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 94int vnode_pbuf_freecnt; 95 96static void 97vnode_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 */ 109vm_object_t 110vnode_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 */ 181static void 182vnode_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 204static boolean_t 205vnode_pager_haspage(object, pindex, before, after) 206 vm_object_t object; 207 vm_pindex_t pindex; 208 int *before; 209 int *after; 210{ 211 struct vnode *vp = object->handle; 212 daddr_t bn; 213 int err; 214 daddr_t reqblock; 215 int poff; 216 int bsize; 217 int pagesperblock, blocksperpage; 218 int vfslocked; 219 220 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 221 /* 222 * If no vp or vp is doomed or marked transparent to VM, we do not 223 * have the page. 224 */ 225 if (vp == NULL) 226 return FALSE; 227 228 VI_LOCK(vp); 229 if (vp->v_iflag & VI_DOOMED) { 230 VI_UNLOCK(vp); 231 return FALSE; 232 } 233 VI_UNLOCK(vp); 234 /* 235 * If filesystem no longer mounted or offset beyond end of file we do 236 * not have the page. 237 */ 238 if ((vp->v_mount == NULL) || 239 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 240 return FALSE; 241 242 bsize = vp->v_mount->mnt_stat.f_iosize; 243 pagesperblock = bsize / PAGE_SIZE; 244 blocksperpage = 0; 245 if (pagesperblock > 0) { 246 reqblock = pindex / pagesperblock; 247 } else { 248 blocksperpage = (PAGE_SIZE / bsize); 249 reqblock = pindex * blocksperpage; 250 } 251 VM_OBJECT_UNLOCK(object); 252 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 253 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 254 VFS_UNLOCK_GIANT(vfslocked); 255 VM_OBJECT_LOCK(object); 256 if (err) 257 return TRUE; 258 if (bn == -1) 259 return FALSE; 260 if (pagesperblock > 0) { 261 poff = pindex - (reqblock * pagesperblock); 262 if (before) { 263 *before *= pagesperblock; 264 *before += poff; 265 } 266 if (after) { 267 int numafter; 268 *after *= pagesperblock; 269 numafter = pagesperblock - (poff + 1); 270 if (IDX_TO_OFF(pindex + numafter) > 271 object->un_pager.vnp.vnp_size) { 272 numafter = 273 OFF_TO_IDX(object->un_pager.vnp.vnp_size) - 274 pindex; 275 } 276 *after += numafter; 277 } 278 } else { 279 if (before) { 280 *before /= blocksperpage; 281 } 282 283 if (after) { 284 *after /= blocksperpage; 285 } 286 } 287 return TRUE; 288} 289 290/* 291 * Lets the VM system know about a change in size for a file. 292 * We adjust our own internal size and flush any cached pages in 293 * the associated object that are affected by the size change. 294 * 295 * Note: this routine may be invoked as a result of a pager put 296 * operation (possibly at object termination time), so we must be careful. 297 */ 298void 299vnode_pager_setsize(vp, nsize) 300 struct vnode *vp; 301 vm_ooffset_t nsize; 302{ 303 vm_object_t object; 304 vm_page_t m; 305 vm_pindex_t nobjsize; 306 307 if ((object = vp->v_object) == NULL) 308 return; 309 VM_OBJECT_LOCK(object); 310 if (nsize == object->un_pager.vnp.vnp_size) { 311 /* 312 * Hasn't changed size 313 */ 314 VM_OBJECT_UNLOCK(object); 315 return; 316 } 317 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 318 if (nsize < object->un_pager.vnp.vnp_size) { 319 /* 320 * File has shrunk. Toss any cached pages beyond the new EOF. 321 */ 322 if (nobjsize < object->size) 323 vm_object_page_remove(object, nobjsize, object->size, 324 FALSE); 325 /* 326 * this gets rid of garbage at the end of a page that is now 327 * only partially backed by the vnode. 328 * 329 * XXX for some reason (I don't know yet), if we take a 330 * completely invalid page and mark it partially valid 331 * it can screw up NFS reads, so we don't allow the case. 332 */ 333 if ((nsize & PAGE_MASK) && 334 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 335 m->valid != 0) { 336 int base = (int)nsize & PAGE_MASK; 337 int size = PAGE_SIZE - base; 338 339 /* 340 * Clear out partial-page garbage in case 341 * the page has been mapped. 342 */ 343 pmap_zero_page_area(m, base, size); 344 345 /* 346 * XXX work around SMP data integrity race 347 * by unmapping the page from user processes. 348 * The garbage we just cleared may be mapped 349 * to a user process running on another cpu 350 * and this code is not running through normal 351 * I/O channels which handle SMP issues for 352 * us, so unmap page to synchronize all cpus. 353 * 354 * XXX should vm_pager_unmap_page() have 355 * dealt with this? 356 */ 357 vm_page_lock_queues(); 358 pmap_remove_all(m); 359 360 /* 361 * Clear out partial-page dirty bits. This 362 * has the side effect of setting the valid 363 * bits, but that is ok. There are a bunch 364 * of places in the VM system where we expected 365 * m->dirty == VM_PAGE_BITS_ALL. The file EOF 366 * case is one of them. If the page is still 367 * partially dirty, make it fully dirty. 368 * 369 * note that we do not clear out the valid 370 * bits. This would prevent bogus_page 371 * replacement from working properly. 372 */ 373 vm_page_set_validclean(m, base, size); 374 if (m->dirty != 0) 375 m->dirty = VM_PAGE_BITS_ALL; 376 vm_page_unlock_queues(); 377 } 378 } 379 object->un_pager.vnp.vnp_size = nsize; 380 object->size = nobjsize; 381 VM_OBJECT_UNLOCK(object); 382} 383 384/* 385 * calculate the linear (byte) disk address of specified virtual 386 * file address 387 */ 388static vm_offset_t 389vnode_pager_addr(vp, address, run) 390 struct vnode *vp; 391 vm_ooffset_t address; 392 int *run; 393{ 394 int rtaddress; 395 int bsize; 396 daddr_t block; 397 int err; 398 daddr_t vblock; 399 int voffset; 400 401 if (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 */ 430static int 431vnode_pager_input_smlfs(object, m) 432 vm_object_t object; 433 vm_page_t m; 434{ 435 int i; 436 struct vnode *vp; 437 struct bufobj *bo; 438 struct buf *bp; 439 struct sf_buf *sf; 440 int fileaddr; 441 vm_offset_t bsize; 442 int error = 0; 443 444 vp = object->handle; 445 if (vp->v_mount == NULL) 446 return VM_PAGER_BAD; 447 448 bsize = vp->v_mount->mnt_stat.f_iosize; 449 450 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 451 452 sf = sf_buf_alloc(m, 0); 453 454 for (i = 0; i < PAGE_SIZE / bsize; i++) { 455 vm_ooffset_t address; 456 457 if (vm_page_bits(i * bsize, bsize) & m->valid) 458 continue; 459 460 address = IDX_TO_OFF(m->pindex) + i * bsize; 461 if (address >= object->un_pager.vnp.vnp_size) { 462 fileaddr = -1; 463 } else { 464 fileaddr = vnode_pager_addr(vp, address, NULL); 465 } 466 if (fileaddr != -1) { 467 bp = getpbuf(&vnode_pbuf_freecnt); 468 469 /* build a minimal buffer header */ 470 bp->b_iocmd = BIO_READ; 471 bp->b_iodone = bdone; 472 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 473 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 474 bp->b_rcred = crhold(curthread->td_ucred); 475 bp->b_wcred = crhold(curthread->td_ucred); 476 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 477 bp->b_blkno = fileaddr; 478 pbgetbo(bo, bp); 479 bp->b_bcount = bsize; 480 bp->b_bufsize = bsize; 481 bp->b_runningbufspace = bp->b_bufsize; 482 runningbufspace += bp->b_runningbufspace; 483 484 /* do the input */ 485 bp->b_iooffset = dbtob(bp->b_blkno); 486 bstrategy(bp); 487 488 /* we definitely need to be at splvm here */ 489 490 bwait(bp, PVM, "vnsrd"); 491 492 if ((bp->b_ioflags & BIO_ERROR) != 0) 493 error = EIO; 494 495 /* 496 * free the buffer header back to the swap buffer pool 497 */ 498 pbrelbo(bp); 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 input routine 531 */ 532static int 533vnode_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 */ 610static int 611vnode_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 int vfslocked; 621 622 vp = object->handle; 623 VM_OBJECT_UNLOCK(object); 624 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 625 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 626 KASSERT(rtval != EOPNOTSUPP, 627 ("vnode_pager: FS getpages not implemented\n")); 628 VFS_UNLOCK_GIANT(vfslocked); 629 VM_OBJECT_LOCK(object); 630 return rtval; 631} 632 633/* 634 * This is now called from local media FS's to operate against their 635 * own vnodes if they fail to implement VOP_GETPAGES. 636 */ 637int 638vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 639 struct vnode *vp; 640 vm_page_t *m; 641 int bytecount; 642 int reqpage; 643{ 644 vm_object_t object; 645 vm_offset_t kva; 646 off_t foff, tfoff, nextoff; 647 int i, j, size, bsize, first, firstaddr; 648 struct bufobj *bo; 649 int runpg; 650 int runend; 651 struct buf *bp; 652 int count; 653 int error = 0; 654 655 object = vp->v_object; 656 count = bytecount / PAGE_SIZE; 657 658 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 659 ("vnode_pager_generic_getpages does not support devices")); 660 if (vp->v_mount == NULL) 661 return VM_PAGER_BAD; 662 663 bsize = vp->v_mount->mnt_stat.f_iosize; 664 665 /* get the UNDERLYING device for the file with VOP_BMAP() */ 666 667 /* 668 * originally, we did not check for an error return value -- assuming 669 * an fs always has a bmap entry point -- that assumption is wrong!!! 670 */ 671 foff = IDX_TO_OFF(m[reqpage]->pindex); 672 673 /* 674 * if we can't bmap, use old VOP code 675 */ 676 if (VOP_BMAP(vp, 0, &bo, 0, NULL, NULL)) { 677 VM_OBJECT_LOCK(object); 678 vm_page_lock_queues(); 679 for (i = 0; i < count; i++) 680 if (i != reqpage) 681 vm_page_free(m[i]); 682 vm_page_unlock_queues(); 683 cnt.v_vnodein++; 684 cnt.v_vnodepgsin++; 685 error = vnode_pager_input_old(object, m[reqpage]); 686 VM_OBJECT_UNLOCK(object); 687 return (error); 688 689 /* 690 * if the blocksize is smaller than a page size, then use 691 * special small filesystem code. NFS sometimes has a small 692 * blocksize, but it can handle large reads itself. 693 */ 694 } else if ((PAGE_SIZE / bsize) > 1 && 695 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 696 VM_OBJECT_LOCK(object); 697 vm_page_lock_queues(); 698 for (i = 0; i < count; i++) 699 if (i != reqpage) 700 vm_page_free(m[i]); 701 vm_page_unlock_queues(); 702 VM_OBJECT_UNLOCK(object); 703 cnt.v_vnodein++; 704 cnt.v_vnodepgsin++; 705 return vnode_pager_input_smlfs(object, m[reqpage]); 706 } 707 708 /* 709 * If we have a completely valid page available to us, we can 710 * clean up and return. Otherwise we have to re-read the 711 * media. 712 */ 713 VM_OBJECT_LOCK(object); 714 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 715 vm_page_lock_queues(); 716 for (i = 0; i < count; i++) 717 if (i != reqpage) 718 vm_page_free(m[i]); 719 vm_page_unlock_queues(); 720 VM_OBJECT_UNLOCK(object); 721 return VM_PAGER_OK; 722 } 723 m[reqpage]->valid = 0; 724 VM_OBJECT_UNLOCK(object); 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 KASSERT(count > 0, ("zero count")); 800 if ((foff + size) > object->un_pager.vnp.vnp_size) 801 size = object->un_pager.vnp.vnp_size - foff; 802 KASSERT(size > 0, ("zero size")); 803 804 /* 805 * round up physical size for real devices. 806 */ 807 if (1) { 808 int secmask = bo->bo_bsize - 1; 809 KASSERT(secmask < PAGE_SIZE && secmask > 0, 810 ("vnode_pager_generic_getpages: sector size %d too large", 811 secmask + 1)); 812 size = (size + secmask) & ~secmask; 813 } 814 815 bp = getpbuf(&vnode_pbuf_freecnt); 816 kva = (vm_offset_t) bp->b_data; 817 818 /* 819 * and map the pages to be read into the kva 820 */ 821 pmap_qenter(kva, m, count); 822 823 /* build a minimal buffer header */ 824 bp->b_iocmd = BIO_READ; 825 bp->b_iodone = bdone; 826 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 827 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 828 bp->b_rcred = crhold(curthread->td_ucred); 829 bp->b_wcred = crhold(curthread->td_ucred); 830 bp->b_blkno = firstaddr; 831 pbgetbo(bo, bp); 832 bp->b_bcount = size; 833 bp->b_bufsize = size; 834 bp->b_runningbufspace = bp->b_bufsize; 835 runningbufspace += bp->b_runningbufspace; 836 837 cnt.v_vnodein++; 838 cnt.v_vnodepgsin += count; 839 840 /* do the input */ 841 bp->b_iooffset = dbtob(bp->b_blkno); 842 bstrategy(bp); 843 844 bwait(bp, PVM, "vnread"); 845 846 if ((bp->b_ioflags & BIO_ERROR) != 0) 847 error = EIO; 848 849 if (!error) { 850 if (size != count * PAGE_SIZE) 851 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 852 } 853 pmap_qremove(kva, count); 854 855 /* 856 * free the buffer header back to the swap buffer pool 857 */ 858 pbrelbo(bp); 859 relpbuf(bp, &vnode_pbuf_freecnt); 860 861 VM_OBJECT_LOCK(object); 862 vm_page_lock_queues(); 863 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 864 vm_page_t mt; 865 866 nextoff = tfoff + PAGE_SIZE; 867 mt = m[i]; 868 869 if (nextoff <= object->un_pager.vnp.vnp_size) { 870 /* 871 * Read filled up entire page. 872 */ 873 mt->valid = VM_PAGE_BITS_ALL; 874 vm_page_undirty(mt); /* should be an assert? XXX */ 875 pmap_clear_modify(mt); 876 } else { 877 /* 878 * Read did not fill up entire page. Since this 879 * is getpages, the page may be mapped, so we have 880 * to zero the invalid portions of the page even 881 * though we aren't setting them valid. 882 * 883 * Currently we do not set the entire page valid, 884 * we just try to clear the piece that we couldn't 885 * read. 886 */ 887 vm_page_set_validclean(mt, 0, 888 object->un_pager.vnp.vnp_size - tfoff); 889 /* handled by vm_fault now */ 890 /* vm_page_zero_invalid(mt, FALSE); */ 891 } 892 893 if (i != reqpage) { 894 895 /* 896 * whether or not to leave the page activated is up in 897 * the air, but we should put the page on a page queue 898 * somewhere. (it already is in the object). Result: 899 * It appears that empirical results show that 900 * deactivating pages is best. 901 */ 902 903 /* 904 * just in case someone was asking for this page we 905 * now tell them that it is ok to use 906 */ 907 if (!error) { 908 if (mt->flags & PG_WANTED) 909 vm_page_activate(mt); 910 else 911 vm_page_deactivate(mt); 912 vm_page_wakeup(mt); 913 } else { 914 vm_page_free(mt); 915 } 916 } 917 } 918 vm_page_unlock_queues(); 919 VM_OBJECT_UNLOCK(object); 920 if (error) { 921 printf("vnode_pager_getpages: I/O read error\n"); 922 } 923 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 924} 925 926/* 927 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 928 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 929 * vnode_pager_generic_putpages() to implement the previous behaviour. 930 * 931 * All other FS's should use the bypass to get to the local media 932 * backing vp's VOP_PUTPAGES. 933 */ 934static void 935vnode_pager_putpages(object, m, count, sync, rtvals) 936 vm_object_t object; 937 vm_page_t *m; 938 int count; 939 boolean_t sync; 940 int *rtvals; 941{ 942 int rtval; 943 struct vnode *vp; 944 struct mount *mp; 945 int bytes = count * PAGE_SIZE; 946 947 /* 948 * Force synchronous operation if we are extremely low on memory 949 * to prevent a low-memory deadlock. VOP operations often need to 950 * allocate more memory to initiate the I/O ( i.e. do a BMAP 951 * operation ). The swapper handles the case by limiting the amount 952 * of asynchronous I/O, but that sort of solution doesn't scale well 953 * for the vnode pager without a lot of work. 954 * 955 * Also, the backing vnode's iodone routine may not wake the pageout 956 * daemon up. This should be probably be addressed XXX. 957 */ 958 959 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 960 sync |= OBJPC_SYNC; 961 962 /* 963 * Call device-specific putpages function 964 */ 965 vp = object->handle; 966 VM_OBJECT_UNLOCK(object); 967 if (vp->v_type != VREG) 968 mp = NULL; 969 (void)vn_start_write(vp, &mp, V_WAIT); 970 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 971 KASSERT(rtval != EOPNOTSUPP, 972 ("vnode_pager: stale FS putpages\n")); 973 vn_finished_write(mp); 974 VM_OBJECT_LOCK(object); 975} 976 977 978/* 979 * This is now called from local media FS's to operate against their 980 * own vnodes if they fail to implement VOP_PUTPAGES. 981 * 982 * This is typically called indirectly via the pageout daemon and 983 * clustering has already typically occured, so in general we ask the 984 * underlying filesystem to write the data out asynchronously rather 985 * then delayed. 986 */ 987int 988vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 989 struct vnode *vp; 990 vm_page_t *m; 991 int bytecount; 992 int flags; 993 int *rtvals; 994{ 995 int i; 996 vm_object_t object; 997 int count; 998 999 int maxsize, ncount; 1000 vm_ooffset_t poffset; 1001 struct uio auio; 1002 struct iovec aiov; 1003 int error; 1004 int ioflags; 1005 1006 object = vp->v_object; 1007 count = bytecount / PAGE_SIZE; 1008 1009 for (i = 0; i < count; i++) 1010 rtvals[i] = VM_PAGER_AGAIN; 1011 1012 if ((int64_t)m[0]->pindex < 0) { 1013 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n", 1014 (long)m[0]->pindex, (u_long)m[0]->dirty); 1015 rtvals[0] = VM_PAGER_BAD; 1016 return VM_PAGER_BAD; 1017 } 1018 1019 maxsize = count * PAGE_SIZE; 1020 ncount = count; 1021 1022 poffset = IDX_TO_OFF(m[0]->pindex); 1023 1024 /* 1025 * If the page-aligned write is larger then the actual file we 1026 * have to invalidate pages occuring beyond the file EOF. However, 1027 * there is an edge case where a file may not be page-aligned where 1028 * the last page is partially invalid. In this case the filesystem 1029 * may not properly clear the dirty bits for the entire page (which 1030 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 1031 * With the page locked we are free to fix-up the dirty bits here. 1032 * 1033 * We do not under any circumstances truncate the valid bits, as 1034 * this will screw up bogus page replacement. 1035 */ 1036 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 1037 if (object->un_pager.vnp.vnp_size > poffset) { 1038 int pgoff; 1039 1040 maxsize = object->un_pager.vnp.vnp_size - poffset; 1041 ncount = btoc(maxsize); 1042 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1043 vm_page_lock_queues(); 1044 vm_page_clear_dirty(m[ncount - 1], pgoff, 1045 PAGE_SIZE - pgoff); 1046 vm_page_unlock_queues(); 1047 } 1048 } else { 1049 maxsize = 0; 1050 ncount = 0; 1051 } 1052 if (ncount < count) { 1053 for (i = ncount; i < count; i++) { 1054 rtvals[i] = VM_PAGER_BAD; 1055 } 1056 } 1057 } 1058 1059 /* 1060 * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 1061 * rather then a bdwrite() to prevent paging I/O from saturating 1062 * the buffer cache. Dummy-up the sequential heuristic to cause 1063 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 1064 * the system decides how to cluster. 1065 */ 1066 ioflags = IO_VMIO; 1067 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 1068 ioflags |= IO_SYNC; 1069 else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 1070 ioflags |= IO_ASYNC; 1071 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 1072 ioflags |= IO_SEQMAX << IO_SEQSHIFT; 1073 1074 aiov.iov_base = (caddr_t) 0; 1075 aiov.iov_len = maxsize; 1076 auio.uio_iov = &aiov; 1077 auio.uio_iovcnt = 1; 1078 auio.uio_offset = poffset; 1079 auio.uio_segflg = UIO_NOCOPY; 1080 auio.uio_rw = UIO_WRITE; 1081 auio.uio_resid = maxsize; 1082 auio.uio_td = (struct thread *) 0; 1083 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1084 cnt.v_vnodeout++; 1085 cnt.v_vnodepgsout += ncount; 1086 1087 if (error) { 1088 printf("vnode_pager_putpages: I/O error %d\n", error); 1089 } 1090 if (auio.uio_resid) { 1091 printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1092 auio.uio_resid, (u_long)m[0]->pindex); 1093 } 1094 for (i = 0; i < ncount; i++) { 1095 rtvals[i] = VM_PAGER_OK; 1096 } 1097 return rtvals[0]; 1098} 1099 1100struct vnode * 1101vnode_pager_lock(vm_object_t first_object) 1102{ 1103 struct vnode *vp; 1104 vm_object_t backing_object, object; 1105 1106 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 1107 for (object = first_object; object != NULL; object = backing_object) { 1108 if (object->type != OBJT_VNODE) { 1109 if ((backing_object = object->backing_object) != NULL) 1110 VM_OBJECT_LOCK(backing_object); 1111 if (object != first_object) 1112 VM_OBJECT_UNLOCK(object); 1113 continue; 1114 } 1115 retry: 1116 if (object->flags & OBJ_DEAD) { 1117 if (object != first_object) 1118 VM_OBJECT_UNLOCK(object); 1119 return NULL; 1120 } 1121 vp = object->handle; 1122 VI_LOCK(vp); 1123 VM_OBJECT_UNLOCK(object); 1124 if (first_object != object) 1125 VM_OBJECT_UNLOCK(first_object); 1126 if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | LK_NOPAUSE | 1127 LK_RETRY | LK_SHARED, curthread)) { 1128 VM_OBJECT_LOCK(first_object); 1129 if (object != first_object) 1130 VM_OBJECT_LOCK(object); 1131 if (object->type != OBJT_VNODE) { 1132 if (object != first_object) 1133 VM_OBJECT_UNLOCK(object); 1134 return NULL; 1135 } 1136 printf("vnode_pager_lock: retrying\n"); 1137 goto retry; 1138 } 1139 VM_OBJECT_LOCK(first_object); 1140 return (vp); 1141 } 1142 return NULL; 1143} 1144