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 (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 *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 GIANT_REQUIRED; 445 446 vp = object->handle; 447 if (vp->v_mount == NULL) 448 return VM_PAGER_BAD; 449 450 bsize = vp->v_mount->mnt_stat.f_iosize; 451 452 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 453 454 sf = sf_buf_alloc(m, 0); 455 456 for (i = 0; i < PAGE_SIZE / bsize; i++) { 457 vm_ooffset_t address; 458 459 if (vm_page_bits(i * bsize, bsize) & m->valid) 460 continue; 461 462 address = IDX_TO_OFF(m->pindex) + i * bsize; 463 if (address >= object->un_pager.vnp.vnp_size) { 464 fileaddr = -1; 465 } else { 466 fileaddr = vnode_pager_addr(vp, address, NULL); 467 } 468 if (fileaddr != -1) { 469 bp = getpbuf(&vnode_pbuf_freecnt); 470 471 /* build a minimal buffer header */ 472 bp->b_iocmd = BIO_READ; 473 bp->b_iodone = bdone; 474 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 475 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 476 bp->b_rcred = crhold(curthread->td_ucred); 477 bp->b_wcred = crhold(curthread->td_ucred); 478 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 479 bp->b_blkno = fileaddr; 480 pbgetbo(bo, bp); 481 bp->b_bcount = bsize; 482 bp->b_bufsize = bsize; 483 bp->b_runningbufspace = bp->b_bufsize; 484 runningbufspace += bp->b_runningbufspace; 485 486 /* do the input */ 487 bp->b_iooffset = dbtob(bp->b_blkno); 488 bstrategy(bp); 489 490 /* we definitely need to be at splvm here */ 491 492 bwait(bp, PVM, "vnsrd"); 493 494 if ((bp->b_ioflags & BIO_ERROR) != 0) 495 error = EIO; 496 497 /* 498 * free the buffer header back to the swap buffer pool 499 */ 500 pbrelbo(bp); 501 relpbuf(bp, &vnode_pbuf_freecnt); 502 if (error) 503 break; 504 505 VM_OBJECT_LOCK(object); 506 vm_page_lock_queues(); 507 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 508 vm_page_unlock_queues(); 509 VM_OBJECT_UNLOCK(object); 510 } else { 511 VM_OBJECT_LOCK(object); 512 vm_page_lock_queues(); 513 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 514 vm_page_unlock_queues(); 515 VM_OBJECT_UNLOCK(object); 516 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 517 } 518 } 519 sf_buf_free(sf); 520 vm_page_lock_queues(); 521 pmap_clear_modify(m); 522 vm_page_unlock_queues(); 523 if (error) { 524 return VM_PAGER_ERROR; 525 } 526 return VM_PAGER_OK; 527 528 } 529 530 531 /* 532 * old style vnode pager input routine 533 */ 534 static int 535 vnode_pager_input_old(object, m) 536 vm_object_t object; 537 vm_page_t m; 538 { 539 struct uio auio; 540 struct iovec aiov; 541 int error; 542 int size; 543 struct sf_buf *sf; 544 struct vnode *vp; 545 546 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 547 error = 0; 548 549 /* 550 * Return failure if beyond current EOF 551 */ 552 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 553 return VM_PAGER_BAD; 554 } else { 555 size = PAGE_SIZE; 556 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 557 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 558 vp = object->handle; 559 VM_OBJECT_UNLOCK(object); 560 561 /* 562 * Allocate a kernel virtual address and initialize so that 563 * we can use VOP_READ/WRITE routines. 564 */ 565 sf = sf_buf_alloc(m, 0); 566 567 aiov.iov_base = (caddr_t)sf_buf_kva(sf); 568 aiov.iov_len = size; 569 auio.uio_iov = &aiov; 570 auio.uio_iovcnt = 1; 571 auio.uio_offset = IDX_TO_OFF(m->pindex); 572 auio.uio_segflg = UIO_SYSSPACE; 573 auio.uio_rw = UIO_READ; 574 auio.uio_resid = size; 575 auio.uio_td = curthread; 576 577 error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 578 if (!error) { 579 int count = size - auio.uio_resid; 580 581 if (count == 0) 582 error = EINVAL; 583 else if (count != PAGE_SIZE) 584 bzero((caddr_t)sf_buf_kva(sf) + count, 585 PAGE_SIZE - count); 586 } 587 sf_buf_free(sf); 588 589 VM_OBJECT_LOCK(object); 590 } 591 vm_page_lock_queues(); 592 pmap_clear_modify(m); 593 vm_page_undirty(m); 594 vm_page_unlock_queues(); 595 if (!error) 596 m->valid = VM_PAGE_BITS_ALL; 597 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 598 } 599 600 /* 601 * generic vnode pager input routine 602 */ 603 604 /* 605 * Local media VFS's that do not implement their own VOP_GETPAGES 606 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 607 * to implement the previous behaviour. 608 * 609 * All other FS's should use the bypass to get to the local media 610 * backing vp's VOP_GETPAGES. 611 */ 612 static int 613 vnode_pager_getpages(object, m, count, reqpage) 614 vm_object_t object; 615 vm_page_t *m; 616 int count; 617 int reqpage; 618 { 619 int rtval; 620 struct vnode *vp; 621 int bytes = count * PAGE_SIZE; 622 623 vp = object->handle; 624 VM_OBJECT_UNLOCK(object); 625 mtx_lock(&Giant); 626 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 627 KASSERT(rtval != EOPNOTSUPP, 628 ("vnode_pager: FS getpages not implemented\n")); 629 mtx_unlock(&Giant); 630 VM_OBJECT_LOCK(object); 631 return rtval; 632 } 633 634 /* 635 * This is now called from local media FS's to operate against their 636 * own vnodes if they fail to implement VOP_GETPAGES. 637 */ 638 int 639 vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 640 struct vnode *vp; 641 vm_page_t *m; 642 int bytecount; 643 int reqpage; 644 { 645 vm_object_t object; 646 vm_offset_t kva; 647 off_t foff, tfoff, nextoff; 648 int i, j, size, bsize, first, firstaddr; 649 struct bufobj *bo; 650 int runpg; 651 int runend; 652 struct buf *bp; 653 int count; 654 int error = 0; 655 656 GIANT_REQUIRED; 657 object = vp->v_object; 658 count = bytecount / PAGE_SIZE; 659 660 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 661 ("vnode_pager_generic_getpages does not support devices")); 662 if (vp->v_mount == NULL) 663 return VM_PAGER_BAD; 664 665 bsize = vp->v_mount->mnt_stat.f_iosize; 666 667 /* get the UNDERLYING device for the file with VOP_BMAP() */ 668 669 /* 670 * originally, we did not check for an error return value -- assuming 671 * an fs always has a bmap entry point -- that assumption is wrong!!! 672 */ 673 foff = IDX_TO_OFF(m[reqpage]->pindex); 674 675 /* 676 * if we can't bmap, use old VOP code 677 */ 678 if (VOP_BMAP(vp, 0, &bo, 0, NULL, NULL)) { 679 VM_OBJECT_LOCK(object); 680 vm_page_lock_queues(); 681 for (i = 0; i < count; i++) 682 if (i != reqpage) 683 vm_page_free(m[i]); 684 vm_page_unlock_queues(); 685 cnt.v_vnodein++; 686 cnt.v_vnodepgsin++; 687 error = vnode_pager_input_old(object, m[reqpage]); 688 VM_OBJECT_UNLOCK(object); 689 return (error); 690 691 /* 692 * if the blocksize is smaller than a page size, then use 693 * special small filesystem code. NFS sometimes has a small 694 * blocksize, but it can handle large reads itself. 695 */ 696 } else if ((PAGE_SIZE / bsize) > 1 && 697 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 698 VM_OBJECT_LOCK(object); 699 vm_page_lock_queues(); 700 for (i = 0; i < count; i++) 701 if (i != reqpage) 702 vm_page_free(m[i]); 703 vm_page_unlock_queues(); 704 VM_OBJECT_UNLOCK(object); 705 cnt.v_vnodein++; 706 cnt.v_vnodepgsin++; 707 return vnode_pager_input_smlfs(object, m[reqpage]); 708 } 709 710 /* 711 * If we have a completely valid page available to us, we can 712 * clean up and return. Otherwise we have to re-read the 713 * media. 714 */ 715 VM_OBJECT_LOCK(object); 716 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 717 vm_page_lock_queues(); 718 for (i = 0; i < count; i++) 719 if (i != reqpage) 720 vm_page_free(m[i]); 721 vm_page_unlock_queues(); 722 VM_OBJECT_UNLOCK(object); 723 return VM_PAGER_OK; 724 } 725 m[reqpage]->valid = 0; 726 VM_OBJECT_UNLOCK(object); 727 728 /* 729 * here on direct device I/O 730 */ 731 firstaddr = -1; 732 733 /* 734 * calculate the run that includes the required page 735 */ 736 for (first = 0, i = 0; i < count; i = runend) { 737 firstaddr = vnode_pager_addr(vp, 738 IDX_TO_OFF(m[i]->pindex), &runpg); 739 if (firstaddr == -1) { 740 VM_OBJECT_LOCK(object); 741 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 742 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx", 743 firstaddr, (uintmax_t)(foff >> 32), 744 (uintmax_t)foff, 745 (uintmax_t) 746 (object->un_pager.vnp.vnp_size >> 32), 747 (uintmax_t)object->un_pager.vnp.vnp_size); 748 } 749 vm_page_lock_queues(); 750 vm_page_free(m[i]); 751 vm_page_unlock_queues(); 752 VM_OBJECT_UNLOCK(object); 753 runend = i + 1; 754 first = runend; 755 continue; 756 } 757 runend = i + runpg; 758 if (runend <= reqpage) { 759 VM_OBJECT_LOCK(object); 760 vm_page_lock_queues(); 761 for (j = i; j < runend; j++) 762 vm_page_free(m[j]); 763 vm_page_unlock_queues(); 764 VM_OBJECT_UNLOCK(object); 765 } else { 766 if (runpg < (count - first)) { 767 VM_OBJECT_LOCK(object); 768 vm_page_lock_queues(); 769 for (i = first + runpg; i < count; i++) 770 vm_page_free(m[i]); 771 vm_page_unlock_queues(); 772 VM_OBJECT_UNLOCK(object); 773 count = first + runpg; 774 } 775 break; 776 } 777 first = runend; 778 } 779 780 /* 781 * the first and last page have been calculated now, move input pages 782 * to be zero based... 783 */ 784 if (first != 0) { 785 for (i = first; i < count; i++) { 786 m[i - first] = m[i]; 787 } 788 count -= first; 789 reqpage -= first; 790 } 791 792 /* 793 * calculate the file virtual address for the transfer 794 */ 795 foff = IDX_TO_OFF(m[0]->pindex); 796 797 /* 798 * calculate the size of the transfer 799 */ 800 size = count * PAGE_SIZE; 801 KASSERT(count > 0, ("zero count")); 802 if ((foff + size) > object->un_pager.vnp.vnp_size) 803 size = object->un_pager.vnp.vnp_size - foff; 804 KASSERT(size > 0, ("zero size")); 805 806 /* 807 * round up physical size for real devices. 808 */ 809 if (1) { 810 int secmask = bo->bo_bsize - 1; 811 KASSERT(secmask < PAGE_SIZE && secmask > 0, 812 ("vnode_pager_generic_getpages: sector size %d too large", 813 secmask + 1)); 814 size = (size + secmask) & ~secmask; 815 } 816 817 bp = getpbuf(&vnode_pbuf_freecnt); 818 kva = (vm_offset_t) bp->b_data; 819 820 /* 821 * and map the pages to be read into the kva 822 */ 823 pmap_qenter(kva, m, count); 824 825 /* build a minimal buffer header */ 826 bp->b_iocmd = BIO_READ; 827 bp->b_iodone = bdone; 828 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 829 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 830 bp->b_rcred = crhold(curthread->td_ucred); 831 bp->b_wcred = crhold(curthread->td_ucred); 832 bp->b_blkno = firstaddr; 833 pbgetbo(bo, bp); 834 bp->b_bcount = size; 835 bp->b_bufsize = size; 836 bp->b_runningbufspace = bp->b_bufsize; 837 runningbufspace += bp->b_runningbufspace; 838 839 cnt.v_vnodein++; 840 cnt.v_vnodepgsin += count; 841 842 /* do the input */ 843 bp->b_iooffset = dbtob(bp->b_blkno); 844 bstrategy(bp); 845 846 bwait(bp, PVM, "vnread"); 847 848 if ((bp->b_ioflags & BIO_ERROR) != 0) 849 error = EIO; 850 851 if (!error) { 852 if (size != count * PAGE_SIZE) 853 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 854 } 855 pmap_qremove(kva, count); 856 857 /* 858 * free the buffer header back to the swap buffer pool 859 */ 860 pbrelbo(bp); 861 relpbuf(bp, &vnode_pbuf_freecnt); 862 863 VM_OBJECT_LOCK(object); 864 vm_page_lock_queues(); 865 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 866 vm_page_t mt; 867 868 nextoff = tfoff + PAGE_SIZE; 869 mt = m[i]; 870 871 if (nextoff <= object->un_pager.vnp.vnp_size) { 872 /* 873 * Read filled up entire page. 874 */ 875 mt->valid = VM_PAGE_BITS_ALL; 876 vm_page_undirty(mt); /* should be an assert? XXX */ 877 pmap_clear_modify(mt); 878 } else { 879 /* 880 * Read did not fill up entire page. Since this 881 * is getpages, the page may be mapped, so we have 882 * to zero the invalid portions of the page even 883 * though we aren't setting them valid. 884 * 885 * Currently we do not set the entire page valid, 886 * we just try to clear the piece that we couldn't 887 * read. 888 */ 889 vm_page_set_validclean(mt, 0, 890 object->un_pager.vnp.vnp_size - tfoff); 891 /* handled by vm_fault now */ 892 /* vm_page_zero_invalid(mt, FALSE); */ 893 } 894 895 if (i != reqpage) { 896 897 /* 898 * whether or not to leave the page activated is up in 899 * the air, but we should put the page on a page queue 900 * somewhere. (it already is in the object). Result: 901 * It appears that empirical results show that 902 * deactivating pages is best. 903 */ 904 905 /* 906 * just in case someone was asking for this page we 907 * now tell them that it is ok to use 908 */ 909 if (!error) { 910 if (mt->flags & PG_WANTED) 911 vm_page_activate(mt); 912 else 913 vm_page_deactivate(mt); 914 vm_page_wakeup(mt); 915 } else { 916 vm_page_free(mt); 917 } 918 } 919 } 920 vm_page_unlock_queues(); 921 VM_OBJECT_UNLOCK(object); 922 if (error) { 923 printf("vnode_pager_getpages: I/O read error\n"); 924 } 925 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 926 } 927 928 /* 929 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 930 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 931 * vnode_pager_generic_putpages() to implement the previous behaviour. 932 * 933 * All other FS's should use the bypass to get to the local media 934 * backing vp's VOP_PUTPAGES. 935 */ 936 static void 937 vnode_pager_putpages(object, m, count, sync, rtvals) 938 vm_object_t object; 939 vm_page_t *m; 940 int count; 941 boolean_t sync; 942 int *rtvals; 943 { 944 int rtval; 945 struct vnode *vp; 946 struct mount *mp; 947 int bytes = count * PAGE_SIZE; 948 949 GIANT_REQUIRED; 950 /* 951 * Force synchronous operation if we are extremely low on memory 952 * to prevent a low-memory deadlock. VOP operations often need to 953 * allocate more memory to initiate the I/O ( i.e. do a BMAP 954 * operation ). The swapper handles the case by limiting the amount 955 * of asynchronous I/O, but that sort of solution doesn't scale well 956 * for the vnode pager without a lot of work. 957 * 958 * Also, the backing vnode's iodone routine may not wake the pageout 959 * daemon up. This should be probably be addressed XXX. 960 */ 961 962 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 963 sync |= OBJPC_SYNC; 964 965 /* 966 * Call device-specific putpages function 967 */ 968 vp = object->handle; 969 VM_OBJECT_UNLOCK(object); 970 if (vp->v_type != VREG) 971 mp = NULL; 972 (void)vn_start_write(vp, &mp, V_WAIT); 973 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 974 KASSERT(rtval != EOPNOTSUPP, 975 ("vnode_pager: stale FS putpages\n")); 976 vn_finished_write(mp); 977 VM_OBJECT_LOCK(object); 978 } 979 980 981 /* 982 * This is now called from local media FS's to operate against their 983 * own vnodes if they fail to implement VOP_PUTPAGES. 984 * 985 * This is typically called indirectly via the pageout daemon and 986 * clustering has already typically occured, so in general we ask the 987 * underlying filesystem to write the data out asynchronously rather 988 * then delayed. 989 */ 990 int 991 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 992 struct vnode *vp; 993 vm_page_t *m; 994 int bytecount; 995 int flags; 996 int *rtvals; 997 { 998 int i; 999 vm_object_t object; 1000 int count; 1001 1002 int maxsize, ncount; 1003 vm_ooffset_t poffset; 1004 struct uio auio; 1005 struct iovec aiov; 1006 int error; 1007 int ioflags; 1008 1009 GIANT_REQUIRED; 1010 object = vp->v_object; 1011 count = bytecount / PAGE_SIZE; 1012 1013 for (i = 0; i < count; i++) 1014 rtvals[i] = VM_PAGER_AGAIN; 1015 1016 if ((int64_t)m[0]->pindex < 0) { 1017 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n", 1018 (long)m[0]->pindex, (u_long)m[0]->dirty); 1019 rtvals[0] = VM_PAGER_BAD; 1020 return VM_PAGER_BAD; 1021 } 1022 1023 maxsize = count * PAGE_SIZE; 1024 ncount = count; 1025 1026 poffset = IDX_TO_OFF(m[0]->pindex); 1027 1028 /* 1029 * If the page-aligned write is larger then the actual file we 1030 * have to invalidate pages occuring beyond the file EOF. However, 1031 * there is an edge case where a file may not be page-aligned where 1032 * the last page is partially invalid. In this case the filesystem 1033 * may not properly clear the dirty bits for the entire page (which 1034 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 1035 * With the page locked we are free to fix-up the dirty bits here. 1036 * 1037 * We do not under any circumstances truncate the valid bits, as 1038 * this will screw up bogus page replacement. 1039 */ 1040 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 1041 if (object->un_pager.vnp.vnp_size > poffset) { 1042 int pgoff; 1043 1044 maxsize = object->un_pager.vnp.vnp_size - poffset; 1045 ncount = btoc(maxsize); 1046 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1047 vm_page_lock_queues(); 1048 vm_page_clear_dirty(m[ncount - 1], pgoff, 1049 PAGE_SIZE - pgoff); 1050 vm_page_unlock_queues(); 1051 } 1052 } else { 1053 maxsize = 0; 1054 ncount = 0; 1055 } 1056 if (ncount < count) { 1057 for (i = ncount; i < count; i++) { 1058 rtvals[i] = VM_PAGER_BAD; 1059 } 1060 } 1061 } 1062 1063 /* 1064 * pageouts are already clustered, use IO_ASYNC t o force a bawrite() 1065 * rather then a bdwrite() to prevent paging I/O from saturating 1066 * the buffer cache. Dummy-up the sequential heuristic to cause 1067 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 1068 * the system decides how to cluster. 1069 */ 1070 ioflags = IO_VMIO; 1071 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 1072 ioflags |= IO_SYNC; 1073 else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 1074 ioflags |= IO_ASYNC; 1075 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 1076 ioflags |= IO_SEQMAX << IO_SEQSHIFT; 1077 1078 aiov.iov_base = (caddr_t) 0; 1079 aiov.iov_len = maxsize; 1080 auio.uio_iov = &aiov; 1081 auio.uio_iovcnt = 1; 1082 auio.uio_offset = poffset; 1083 auio.uio_segflg = UIO_NOCOPY; 1084 auio.uio_rw = UIO_WRITE; 1085 auio.uio_resid = maxsize; 1086 auio.uio_td = (struct thread *) 0; 1087 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1088 cnt.v_vnodeout++; 1089 cnt.v_vnodepgsout += ncount; 1090 1091 if (error) { 1092 printf("vnode_pager_putpages: I/O error %d\n", error); 1093 } 1094 if (auio.uio_resid) { 1095 printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1096 auio.uio_resid, (u_long)m[0]->pindex); 1097 } 1098 for (i = 0; i < ncount; i++) { 1099 rtvals[i] = VM_PAGER_OK; 1100 } 1101 return rtvals[0]; 1102 } 1103 1104 struct vnode * 1105 vnode_pager_lock(vm_object_t first_object) 1106 { 1107 struct vnode *vp; 1108 vm_object_t backing_object, object; 1109 1110 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 1111 for (object = first_object; object != NULL; object = backing_object) { 1112 if (object->type != OBJT_VNODE) { 1113 if ((backing_object = object->backing_object) != NULL) 1114 VM_OBJECT_LOCK(backing_object); 1115 if (object != first_object) 1116 VM_OBJECT_UNLOCK(object); 1117 continue; 1118 } 1119 retry: 1120 if (object->flags & OBJ_DEAD) { 1121 if (object != first_object) 1122 VM_OBJECT_UNLOCK(object); 1123 return NULL; 1124 } 1125 vp = object->handle; 1126 VI_LOCK(vp); 1127 VM_OBJECT_UNLOCK(object); 1128 if (first_object != object) 1129 VM_OBJECT_UNLOCK(first_object); 1130 if (vget(vp, LK_CANRECURSE | LK_INTERLOCK | LK_NOPAUSE | 1131 LK_RETRY | LK_SHARED, curthread)) { 1132 VM_OBJECT_LOCK(first_object); 1133 if (object != first_object) 1134 VM_OBJECT_LOCK(object); 1135 if (object->type != OBJT_VNODE) { 1136 if (object != first_object) 1137 VM_OBJECT_UNLOCK(object); 1138 return NULL; 1139 } 1140 printf("vnode_pager_lock: retrying\n"); 1141 goto retry; 1142 } 1143 VM_OBJECT_LOCK(first_object); 1144 return (vp); 1145 } 1146 return NULL; 1147 } 1148