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/limits.h> 65 #include <sys/conf.h> 66 #include <sys/rwlock.h> 67 #include <sys/sf_buf.h> 68 69 #include <machine/atomic.h> 70 71 #include <vm/vm.h> 72 #include <vm/vm_param.h> 73 #include <vm/vm_object.h> 74 #include <vm/vm_page.h> 75 #include <vm/vm_pager.h> 76 #include <vm/vm_map.h> 77 #include <vm/vnode_pager.h> 78 #include <vm/vm_extern.h> 79 80 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 81 daddr_t *rtaddress, int *run); 82 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 83 static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 84 static void vnode_pager_dealloc(vm_object_t); 85 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int); 86 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 87 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 88 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 89 vm_ooffset_t, struct ucred *cred); 90 91 struct pagerops vnodepagerops = { 92 .pgo_alloc = vnode_pager_alloc, 93 .pgo_dealloc = vnode_pager_dealloc, 94 .pgo_getpages = vnode_pager_getpages, 95 .pgo_putpages = vnode_pager_putpages, 96 .pgo_haspage = vnode_pager_haspage, 97 }; 98 99 int vnode_pbuf_freecnt; 100 101 /* Create the VM system backing object for this vnode */ 102 int 103 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td) 104 { 105 vm_object_t object; 106 vm_ooffset_t size = isize; 107 struct vattr va; 108 109 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE) 110 return (0); 111 112 while ((object = vp->v_object) != NULL) { 113 VM_OBJECT_WLOCK(object); 114 if (!(object->flags & OBJ_DEAD)) { 115 VM_OBJECT_WUNLOCK(object); 116 return (0); 117 } 118 VOP_UNLOCK(vp, 0); 119 vm_object_set_flag(object, OBJ_DISCONNECTWNT); 120 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0); 121 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 122 } 123 124 if (size == 0) { 125 if (vn_isdisk(vp, NULL)) { 126 size = IDX_TO_OFF(INT_MAX); 127 } else { 128 if (VOP_GETATTR(vp, &va, td->td_ucred)) 129 return (0); 130 size = va.va_size; 131 } 132 } 133 134 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred); 135 /* 136 * Dereference the reference we just created. This assumes 137 * that the object is associated with the vp. 138 */ 139 VM_OBJECT_WLOCK(object); 140 object->ref_count--; 141 VM_OBJECT_WUNLOCK(object); 142 vrele(vp); 143 144 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object")); 145 146 return (0); 147 } 148 149 void 150 vnode_destroy_vobject(struct vnode *vp) 151 { 152 struct vm_object *obj; 153 154 obj = vp->v_object; 155 if (obj == NULL) 156 return; 157 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject"); 158 VM_OBJECT_WLOCK(obj); 159 if (obj->ref_count == 0) { 160 /* 161 * don't double-terminate the object 162 */ 163 if ((obj->flags & OBJ_DEAD) == 0) 164 vm_object_terminate(obj); 165 else 166 VM_OBJECT_WUNLOCK(obj); 167 } else { 168 /* 169 * Woe to the process that tries to page now :-). 170 */ 171 vm_pager_deallocate(obj); 172 VM_OBJECT_WUNLOCK(obj); 173 } 174 vp->v_object = NULL; 175 } 176 177 178 /* 179 * Allocate (or lookup) pager for a vnode. 180 * Handle is a vnode pointer. 181 * 182 * MPSAFE 183 */ 184 vm_object_t 185 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 186 vm_ooffset_t offset, struct ucred *cred) 187 { 188 vm_object_t object; 189 struct vnode *vp; 190 191 /* 192 * Pageout to vnode, no can do yet. 193 */ 194 if (handle == NULL) 195 return (NULL); 196 197 vp = (struct vnode *) handle; 198 199 /* 200 * If the object is being terminated, wait for it to 201 * go away. 202 */ 203 retry: 204 while ((object = vp->v_object) != NULL) { 205 VM_OBJECT_WLOCK(object); 206 if ((object->flags & OBJ_DEAD) == 0) 207 break; 208 vm_object_set_flag(object, OBJ_DISCONNECTWNT); 209 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0); 210 } 211 212 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference")); 213 214 if (object == NULL) { 215 /* 216 * Add an object of the appropriate size 217 */ 218 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 219 220 object->un_pager.vnp.vnp_size = size; 221 object->un_pager.vnp.writemappings = 0; 222 223 object->handle = handle; 224 VI_LOCK(vp); 225 if (vp->v_object != NULL) { 226 /* 227 * Object has been created while we were sleeping 228 */ 229 VI_UNLOCK(vp); 230 vm_object_destroy(object); 231 goto retry; 232 } 233 vp->v_object = object; 234 VI_UNLOCK(vp); 235 } else { 236 object->ref_count++; 237 VM_OBJECT_WUNLOCK(object); 238 } 239 vref(vp); 240 return (object); 241 } 242 243 /* 244 * The object must be locked. 245 */ 246 static void 247 vnode_pager_dealloc(vm_object_t object) 248 { 249 struct vnode *vp; 250 int refs; 251 252 vp = object->handle; 253 if (vp == NULL) 254 panic("vnode_pager_dealloc: pager already dealloced"); 255 256 VM_OBJECT_ASSERT_WLOCKED(object); 257 vm_object_pip_wait(object, "vnpdea"); 258 refs = object->ref_count; 259 260 object->handle = NULL; 261 object->type = OBJT_DEAD; 262 if (object->flags & OBJ_DISCONNECTWNT) { 263 vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 264 wakeup(object); 265 } 266 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc"); 267 if (object->un_pager.vnp.writemappings > 0) { 268 object->un_pager.vnp.writemappings = 0; 269 VOP_ADD_WRITECOUNT(vp, -1); 270 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 271 __func__, vp, vp->v_writecount); 272 } 273 vp->v_object = NULL; 274 VOP_UNSET_TEXT(vp); 275 VM_OBJECT_WUNLOCK(object); 276 while (refs-- > 0) 277 vunref(vp); 278 VM_OBJECT_WLOCK(object); 279 } 280 281 static boolean_t 282 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 283 int *after) 284 { 285 struct vnode *vp = object->handle; 286 daddr_t bn; 287 int err; 288 daddr_t reqblock; 289 int poff; 290 int bsize; 291 int pagesperblock, blocksperpage; 292 293 VM_OBJECT_ASSERT_WLOCKED(object); 294 /* 295 * If no vp or vp is doomed or marked transparent to VM, we do not 296 * have the page. 297 */ 298 if (vp == NULL || vp->v_iflag & VI_DOOMED) 299 return FALSE; 300 /* 301 * If the offset is beyond end of file we do 302 * not have the page. 303 */ 304 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 305 return FALSE; 306 307 bsize = vp->v_mount->mnt_stat.f_iosize; 308 pagesperblock = bsize / PAGE_SIZE; 309 blocksperpage = 0; 310 if (pagesperblock > 0) { 311 reqblock = pindex / pagesperblock; 312 } else { 313 blocksperpage = (PAGE_SIZE / bsize); 314 reqblock = pindex * blocksperpage; 315 } 316 VM_OBJECT_WUNLOCK(object); 317 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 318 VM_OBJECT_WLOCK(object); 319 if (err) 320 return TRUE; 321 if (bn == -1) 322 return FALSE; 323 if (pagesperblock > 0) { 324 poff = pindex - (reqblock * pagesperblock); 325 if (before) { 326 *before *= pagesperblock; 327 *before += poff; 328 } 329 if (after) { 330 int numafter; 331 *after *= pagesperblock; 332 numafter = pagesperblock - (poff + 1); 333 if (IDX_TO_OFF(pindex + numafter) > 334 object->un_pager.vnp.vnp_size) { 335 numafter = 336 OFF_TO_IDX(object->un_pager.vnp.vnp_size) - 337 pindex; 338 } 339 *after += numafter; 340 } 341 } else { 342 if (before) { 343 *before /= blocksperpage; 344 } 345 346 if (after) { 347 *after /= blocksperpage; 348 } 349 } 350 return TRUE; 351 } 352 353 /* 354 * Lets the VM system know about a change in size for a file. 355 * We adjust our own internal size and flush any cached pages in 356 * the associated object that are affected by the size change. 357 * 358 * Note: this routine may be invoked as a result of a pager put 359 * operation (possibly at object termination time), so we must be careful. 360 */ 361 void 362 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize) 363 { 364 vm_object_t object; 365 vm_page_t m; 366 vm_pindex_t nobjsize; 367 368 if ((object = vp->v_object) == NULL) 369 return; 370 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */ 371 VM_OBJECT_WLOCK(object); 372 if (object->type == OBJT_DEAD) { 373 VM_OBJECT_WUNLOCK(object); 374 return; 375 } 376 KASSERT(object->type == OBJT_VNODE, 377 ("not vnode-backed object %p", object)); 378 if (nsize == object->un_pager.vnp.vnp_size) { 379 /* 380 * Hasn't changed size 381 */ 382 VM_OBJECT_WUNLOCK(object); 383 return; 384 } 385 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 386 if (nsize < object->un_pager.vnp.vnp_size) { 387 /* 388 * File has shrunk. Toss any cached pages beyond the new EOF. 389 */ 390 if (nobjsize < object->size) 391 vm_object_page_remove(object, nobjsize, object->size, 392 0); 393 /* 394 * this gets rid of garbage at the end of a page that is now 395 * only partially backed by the vnode. 396 * 397 * XXX for some reason (I don't know yet), if we take a 398 * completely invalid page and mark it partially valid 399 * it can screw up NFS reads, so we don't allow the case. 400 */ 401 if ((nsize & PAGE_MASK) && 402 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 403 m->valid != 0) { 404 int base = (int)nsize & PAGE_MASK; 405 int size = PAGE_SIZE - base; 406 407 /* 408 * Clear out partial-page garbage in case 409 * the page has been mapped. 410 */ 411 pmap_zero_page_area(m, base, size); 412 413 /* 414 * Update the valid bits to reflect the blocks that 415 * have been zeroed. Some of these valid bits may 416 * have already been set. 417 */ 418 vm_page_set_valid_range(m, base, size); 419 420 /* 421 * Round "base" to the next block boundary so that the 422 * dirty bit for a partially zeroed block is not 423 * cleared. 424 */ 425 base = roundup2(base, DEV_BSIZE); 426 427 /* 428 * Clear out partial-page dirty bits. 429 * 430 * note that we do not clear out the valid 431 * bits. This would prevent bogus_page 432 * replacement from working properly. 433 */ 434 vm_page_clear_dirty(m, base, PAGE_SIZE - base); 435 } else if ((nsize & PAGE_MASK) && 436 vm_page_is_cached(object, OFF_TO_IDX(nsize))) { 437 vm_page_cache_free(object, OFF_TO_IDX(nsize), 438 nobjsize); 439 } 440 } 441 object->un_pager.vnp.vnp_size = nsize; 442 object->size = nobjsize; 443 VM_OBJECT_WUNLOCK(object); 444 } 445 446 /* 447 * calculate the linear (byte) disk address of specified virtual 448 * file address 449 */ 450 static int 451 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 452 int *run) 453 { 454 int bsize; 455 int err; 456 daddr_t vblock; 457 daddr_t voffset; 458 459 if (address < 0) 460 return -1; 461 462 if (vp->v_iflag & VI_DOOMED) 463 return -1; 464 465 bsize = vp->v_mount->mnt_stat.f_iosize; 466 vblock = address / bsize; 467 voffset = address % bsize; 468 469 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 470 if (err == 0) { 471 if (*rtaddress != -1) 472 *rtaddress += voffset / DEV_BSIZE; 473 if (run) { 474 *run += 1; 475 *run *= bsize/PAGE_SIZE; 476 *run -= voffset/PAGE_SIZE; 477 } 478 } 479 480 return (err); 481 } 482 483 /* 484 * small block filesystem vnode pager input 485 */ 486 static int 487 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m) 488 { 489 struct vnode *vp; 490 struct bufobj *bo; 491 struct buf *bp; 492 struct sf_buf *sf; 493 daddr_t fileaddr; 494 vm_offset_t bsize; 495 vm_page_bits_t bits; 496 int error, i; 497 498 error = 0; 499 vp = object->handle; 500 if (vp->v_iflag & VI_DOOMED) 501 return VM_PAGER_BAD; 502 503 bsize = vp->v_mount->mnt_stat.f_iosize; 504 505 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 506 507 sf = sf_buf_alloc(m, 0); 508 509 for (i = 0; i < PAGE_SIZE / bsize; i++) { 510 vm_ooffset_t address; 511 512 bits = vm_page_bits(i * bsize, bsize); 513 if (m->valid & bits) 514 continue; 515 516 address = IDX_TO_OFF(m->pindex) + i * bsize; 517 if (address >= object->un_pager.vnp.vnp_size) { 518 fileaddr = -1; 519 } else { 520 error = vnode_pager_addr(vp, address, &fileaddr, NULL); 521 if (error) 522 break; 523 } 524 if (fileaddr != -1) { 525 bp = getpbuf(&vnode_pbuf_freecnt); 526 527 /* build a minimal buffer header */ 528 bp->b_iocmd = BIO_READ; 529 bp->b_iodone = bdone; 530 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 531 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 532 bp->b_rcred = crhold(curthread->td_ucred); 533 bp->b_wcred = crhold(curthread->td_ucred); 534 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 535 bp->b_blkno = fileaddr; 536 pbgetbo(bo, bp); 537 bp->b_vp = vp; 538 bp->b_bcount = bsize; 539 bp->b_bufsize = bsize; 540 bp->b_runningbufspace = bp->b_bufsize; 541 atomic_add_long(&runningbufspace, bp->b_runningbufspace); 542 543 /* do the input */ 544 bp->b_iooffset = dbtob(bp->b_blkno); 545 bstrategy(bp); 546 547 bwait(bp, PVM, "vnsrd"); 548 549 if ((bp->b_ioflags & BIO_ERROR) != 0) 550 error = EIO; 551 552 /* 553 * free the buffer header back to the swap buffer pool 554 */ 555 bp->b_vp = NULL; 556 pbrelbo(bp); 557 relpbuf(bp, &vnode_pbuf_freecnt); 558 if (error) 559 break; 560 } else 561 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 562 KASSERT((m->dirty & bits) == 0, 563 ("vnode_pager_input_smlfs: page %p is dirty", m)); 564 VM_OBJECT_WLOCK(object); 565 m->valid |= bits; 566 VM_OBJECT_WUNLOCK(object); 567 } 568 sf_buf_free(sf); 569 if (error) { 570 return VM_PAGER_ERROR; 571 } 572 return VM_PAGER_OK; 573 } 574 575 /* 576 * old style vnode pager input routine 577 */ 578 static int 579 vnode_pager_input_old(vm_object_t object, vm_page_t m) 580 { 581 struct uio auio; 582 struct iovec aiov; 583 int error; 584 int size; 585 struct sf_buf *sf; 586 struct vnode *vp; 587 588 VM_OBJECT_ASSERT_WLOCKED(object); 589 error = 0; 590 591 /* 592 * Return failure if beyond current EOF 593 */ 594 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 595 return VM_PAGER_BAD; 596 } else { 597 size = PAGE_SIZE; 598 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 599 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 600 vp = object->handle; 601 VM_OBJECT_WUNLOCK(object); 602 603 /* 604 * Allocate a kernel virtual address and initialize so that 605 * we can use VOP_READ/WRITE routines. 606 */ 607 sf = sf_buf_alloc(m, 0); 608 609 aiov.iov_base = (caddr_t)sf_buf_kva(sf); 610 aiov.iov_len = size; 611 auio.uio_iov = &aiov; 612 auio.uio_iovcnt = 1; 613 auio.uio_offset = IDX_TO_OFF(m->pindex); 614 auio.uio_segflg = UIO_SYSSPACE; 615 auio.uio_rw = UIO_READ; 616 auio.uio_resid = size; 617 auio.uio_td = curthread; 618 619 error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 620 if (!error) { 621 int count = size - auio.uio_resid; 622 623 if (count == 0) 624 error = EINVAL; 625 else if (count != PAGE_SIZE) 626 bzero((caddr_t)sf_buf_kva(sf) + count, 627 PAGE_SIZE - count); 628 } 629 sf_buf_free(sf); 630 631 VM_OBJECT_WLOCK(object); 632 } 633 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m)); 634 if (!error) 635 m->valid = VM_PAGE_BITS_ALL; 636 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 637 } 638 639 /* 640 * generic vnode pager input routine 641 */ 642 643 /* 644 * Local media VFS's that do not implement their own VOP_GETPAGES 645 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 646 * to implement the previous behaviour. 647 * 648 * All other FS's should use the bypass to get to the local media 649 * backing vp's VOP_GETPAGES. 650 */ 651 static int 652 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage) 653 { 654 int rtval; 655 struct vnode *vp; 656 int bytes = count * PAGE_SIZE; 657 658 vp = object->handle; 659 VM_OBJECT_WUNLOCK(object); 660 rtval = VOP_GETPAGES(vp, m, bytes, reqpage); 661 KASSERT(rtval != EOPNOTSUPP, 662 ("vnode_pager: FS getpages not implemented\n")); 663 VM_OBJECT_WLOCK(object); 664 return rtval; 665 } 666 667 /* 668 * The implementation of VOP_GETPAGES() for local filesystems, where 669 * partially valid pages can only occur at the end of file. 670 */ 671 int 672 vnode_pager_local_getpages(struct vop_getpages_args *ap) 673 { 674 vm_page_t mreq; 675 676 mreq = ap->a_m[ap->a_reqpage]; 677 678 /* 679 * Since the caller has busied the requested page, that page's valid 680 * field will not be changed by other threads. 681 */ 682 vm_page_assert_xbusied(mreq); 683 684 /* 685 * The requested page has valid blocks. Invalid part can only 686 * exist at the end of file, and the page is made fully valid 687 * by zeroing in vm_pager_getpages(). Free non-requested 688 * pages, since no i/o is done to read its content. 689 */ 690 if (mreq->valid != 0) { 691 vm_pager_free_nonreq(mreq->object, ap->a_m, ap->a_reqpage, 692 round_page(ap->a_count) / PAGE_SIZE); 693 return (VM_PAGER_OK); 694 } 695 696 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, 697 ap->a_count, ap->a_reqpage)); 698 } 699 700 /* 701 * This is now called from local media FS's to operate against their 702 * own vnodes if they fail to implement VOP_GETPAGES. 703 */ 704 int 705 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount, 706 int reqpage) 707 { 708 vm_object_t object; 709 vm_offset_t kva; 710 off_t foff, tfoff, nextoff; 711 int i, j, size, bsize, first; 712 daddr_t firstaddr, reqblock; 713 struct bufobj *bo; 714 int runpg; 715 int runend; 716 struct buf *bp; 717 int count; 718 int error; 719 720 object = vp->v_object; 721 count = bytecount / PAGE_SIZE; 722 723 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 724 ("vnode_pager_generic_getpages does not support devices")); 725 if (vp->v_iflag & VI_DOOMED) 726 return VM_PAGER_BAD; 727 728 bsize = vp->v_mount->mnt_stat.f_iosize; 729 foff = IDX_TO_OFF(m[reqpage]->pindex); 730 731 /* 732 * Get the underlying device blocks for the file with VOP_BMAP(). 733 * If the file system doesn't support VOP_BMAP, use old way of 734 * getting pages via VOP_READ. 735 */ 736 error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL); 737 if (error == EOPNOTSUPP) { 738 VM_OBJECT_WLOCK(object); 739 740 for (i = 0; i < count; i++) 741 if (i != reqpage) { 742 vm_page_lock(m[i]); 743 vm_page_free(m[i]); 744 vm_page_unlock(m[i]); 745 } 746 PCPU_INC(cnt.v_vnodein); 747 PCPU_INC(cnt.v_vnodepgsin); 748 error = vnode_pager_input_old(object, m[reqpage]); 749 VM_OBJECT_WUNLOCK(object); 750 return (error); 751 } else if (error != 0) { 752 vm_pager_free_nonreq(object, m, reqpage, count); 753 return (VM_PAGER_ERROR); 754 755 /* 756 * if the blocksize is smaller than a page size, then use 757 * special small filesystem code. NFS sometimes has a small 758 * blocksize, but it can handle large reads itself. 759 */ 760 } else if ((PAGE_SIZE / bsize) > 1 && 761 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 762 vm_pager_free_nonreq(object, m, reqpage, count); 763 PCPU_INC(cnt.v_vnodein); 764 PCPU_INC(cnt.v_vnodepgsin); 765 return vnode_pager_input_smlfs(object, m[reqpage]); 766 } 767 768 /* 769 * Since the caller has busied the requested page, that page's valid 770 * field will not be changed by other threads. 771 */ 772 vm_page_assert_xbusied(m[reqpage]); 773 774 /* 775 * If we have a completely valid page available to us, we can 776 * clean up and return. Otherwise we have to re-read the 777 * media. 778 */ 779 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 780 vm_pager_free_nonreq(object, m, reqpage, count); 781 return (VM_PAGER_OK); 782 } else if (reqblock == -1) { 783 pmap_zero_page(m[reqpage]); 784 KASSERT(m[reqpage]->dirty == 0, 785 ("vnode_pager_generic_getpages: page %p is dirty", m)); 786 VM_OBJECT_WLOCK(object); 787 m[reqpage]->valid = VM_PAGE_BITS_ALL; 788 for (i = 0; i < count; i++) 789 if (i != reqpage) { 790 vm_page_lock(m[i]); 791 vm_page_free(m[i]); 792 vm_page_unlock(m[i]); 793 } 794 VM_OBJECT_WUNLOCK(object); 795 return (VM_PAGER_OK); 796 } else if (m[reqpage]->valid != 0) { 797 VM_OBJECT_WLOCK(object); 798 m[reqpage]->valid = 0; 799 VM_OBJECT_WUNLOCK(object); 800 } 801 802 /* 803 * here on direct device I/O 804 */ 805 firstaddr = -1; 806 807 /* 808 * calculate the run that includes the required page 809 */ 810 for (first = 0, i = 0; i < count; i = runend) { 811 if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr, 812 &runpg) != 0) { 813 VM_OBJECT_WLOCK(object); 814 for (; i < count; i++) 815 if (i != reqpage) { 816 vm_page_lock(m[i]); 817 vm_page_free(m[i]); 818 vm_page_unlock(m[i]); 819 } 820 VM_OBJECT_WUNLOCK(object); 821 return (VM_PAGER_ERROR); 822 } 823 if (firstaddr == -1) { 824 VM_OBJECT_WLOCK(object); 825 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 826 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx", 827 (intmax_t)firstaddr, (uintmax_t)(foff >> 32), 828 (uintmax_t)foff, 829 (uintmax_t) 830 (object->un_pager.vnp.vnp_size >> 32), 831 (uintmax_t)object->un_pager.vnp.vnp_size); 832 } 833 vm_page_lock(m[i]); 834 vm_page_free(m[i]); 835 vm_page_unlock(m[i]); 836 VM_OBJECT_WUNLOCK(object); 837 runend = i + 1; 838 first = runend; 839 continue; 840 } 841 runend = i + runpg; 842 if (runend <= reqpage) { 843 VM_OBJECT_WLOCK(object); 844 for (j = i; j < runend; j++) { 845 vm_page_lock(m[j]); 846 vm_page_free(m[j]); 847 vm_page_unlock(m[j]); 848 } 849 VM_OBJECT_WUNLOCK(object); 850 } else { 851 if (runpg < (count - first)) { 852 VM_OBJECT_WLOCK(object); 853 for (i = first + runpg; i < count; i++) { 854 vm_page_lock(m[i]); 855 vm_page_free(m[i]); 856 vm_page_unlock(m[i]); 857 } 858 VM_OBJECT_WUNLOCK(object); 859 count = first + runpg; 860 } 861 break; 862 } 863 first = runend; 864 } 865 866 /* 867 * the first and last page have been calculated now, move input pages 868 * to be zero based... 869 */ 870 if (first != 0) { 871 m += first; 872 count -= first; 873 reqpage -= first; 874 } 875 876 /* 877 * calculate the file virtual address for the transfer 878 */ 879 foff = IDX_TO_OFF(m[0]->pindex); 880 881 /* 882 * calculate the size of the transfer 883 */ 884 size = count * PAGE_SIZE; 885 KASSERT(count > 0, ("zero count")); 886 if ((foff + size) > object->un_pager.vnp.vnp_size) 887 size = object->un_pager.vnp.vnp_size - foff; 888 KASSERT(size > 0, ("zero size")); 889 890 /* 891 * round up physical size for real devices. 892 */ 893 if (1) { 894 int secmask = bo->bo_bsize - 1; 895 KASSERT(secmask < PAGE_SIZE && secmask > 0, 896 ("vnode_pager_generic_getpages: sector size %d too large", 897 secmask + 1)); 898 size = (size + secmask) & ~secmask; 899 } 900 901 bp = getpbuf(&vnode_pbuf_freecnt); 902 kva = (vm_offset_t)bp->b_data; 903 904 /* 905 * and map the pages to be read into the kva, if the filesystem 906 * requires mapped buffers. 907 */ 908 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 && 909 unmapped_buf_allowed) { 910 bp->b_data = unmapped_buf; 911 bp->b_kvabase = unmapped_buf; 912 bp->b_offset = 0; 913 bp->b_flags |= B_UNMAPPED; 914 bp->b_npages = count; 915 for (i = 0; i < count; i++) 916 bp->b_pages[i] = m[i]; 917 } else 918 pmap_qenter(kva, m, count); 919 920 /* build a minimal buffer header */ 921 bp->b_iocmd = BIO_READ; 922 bp->b_iodone = bdone; 923 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 924 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 925 bp->b_rcred = crhold(curthread->td_ucred); 926 bp->b_wcred = crhold(curthread->td_ucred); 927 bp->b_blkno = firstaddr; 928 pbgetbo(bo, bp); 929 bp->b_vp = vp; 930 bp->b_bcount = size; 931 bp->b_bufsize = size; 932 bp->b_runningbufspace = bp->b_bufsize; 933 atomic_add_long(&runningbufspace, bp->b_runningbufspace); 934 935 PCPU_INC(cnt.v_vnodein); 936 PCPU_ADD(cnt.v_vnodepgsin, count); 937 938 /* do the input */ 939 bp->b_iooffset = dbtob(bp->b_blkno); 940 bstrategy(bp); 941 942 bwait(bp, PVM, "vnread"); 943 944 if ((bp->b_ioflags & BIO_ERROR) != 0) 945 error = EIO; 946 947 if (error == 0 && size != count * PAGE_SIZE) { 948 if ((bp->b_flags & B_UNMAPPED) != 0) { 949 bp->b_flags &= ~B_UNMAPPED; 950 pmap_qenter(kva, m, count); 951 } 952 bzero((caddr_t)kva + size, PAGE_SIZE * count - size); 953 } 954 if ((bp->b_flags & B_UNMAPPED) == 0) 955 pmap_qremove(kva, count); 956 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0) { 957 bp->b_data = (caddr_t)kva; 958 bp->b_kvabase = (caddr_t)kva; 959 bp->b_flags &= ~B_UNMAPPED; 960 for (i = 0; i < count; i++) 961 bp->b_pages[i] = NULL; 962 } 963 964 /* 965 * free the buffer header back to the swap buffer pool 966 */ 967 bp->b_vp = NULL; 968 pbrelbo(bp); 969 relpbuf(bp, &vnode_pbuf_freecnt); 970 971 VM_OBJECT_WLOCK(object); 972 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 973 vm_page_t mt; 974 975 nextoff = tfoff + PAGE_SIZE; 976 mt = m[i]; 977 978 if (nextoff <= object->un_pager.vnp.vnp_size) { 979 /* 980 * Read filled up entire page. 981 */ 982 mt->valid = VM_PAGE_BITS_ALL; 983 KASSERT(mt->dirty == 0, 984 ("%s: page %p is dirty", __func__, mt)); 985 KASSERT(!pmap_page_is_mapped(mt), 986 ("%s: page %p is mapped", __func__, mt)); 987 } else { 988 /* 989 * Read did not fill up entire page. 990 * 991 * Currently we do not set the entire page valid, 992 * we just try to clear the piece that we couldn't 993 * read. 994 */ 995 vm_page_set_valid_range(mt, 0, 996 object->un_pager.vnp.vnp_size - tfoff); 997 KASSERT((mt->dirty & vm_page_bits(0, 998 object->un_pager.vnp.vnp_size - tfoff)) == 0, 999 ("%s: page %p is dirty", __func__, mt)); 1000 } 1001 1002 if (i != reqpage) 1003 vm_page_readahead_finish(mt); 1004 } 1005 VM_OBJECT_WUNLOCK(object); 1006 if (error) { 1007 printf("vnode_pager_getpages: I/O read error\n"); 1008 } 1009 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 1010 } 1011 1012 /* 1013 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 1014 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 1015 * vnode_pager_generic_putpages() to implement the previous behaviour. 1016 * 1017 * All other FS's should use the bypass to get to the local media 1018 * backing vp's VOP_PUTPAGES. 1019 */ 1020 static void 1021 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count, 1022 int flags, int *rtvals) 1023 { 1024 int rtval; 1025 struct vnode *vp; 1026 int bytes = count * PAGE_SIZE; 1027 1028 /* 1029 * Force synchronous operation if we are extremely low on memory 1030 * to prevent a low-memory deadlock. VOP operations often need to 1031 * allocate more memory to initiate the I/O ( i.e. do a BMAP 1032 * operation ). The swapper handles the case by limiting the amount 1033 * of asynchronous I/O, but that sort of solution doesn't scale well 1034 * for the vnode pager without a lot of work. 1035 * 1036 * Also, the backing vnode's iodone routine may not wake the pageout 1037 * daemon up. This should be probably be addressed XXX. 1038 */ 1039 1040 if (vm_cnt.v_free_count + vm_cnt.v_cache_count < 1041 vm_cnt.v_pageout_free_min) 1042 flags |= VM_PAGER_PUT_SYNC; 1043 1044 /* 1045 * Call device-specific putpages function 1046 */ 1047 vp = object->handle; 1048 VM_OBJECT_WUNLOCK(object); 1049 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals); 1050 KASSERT(rtval != EOPNOTSUPP, 1051 ("vnode_pager: stale FS putpages\n")); 1052 VM_OBJECT_WLOCK(object); 1053 } 1054 1055 1056 /* 1057 * This is now called from local media FS's to operate against their 1058 * own vnodes if they fail to implement VOP_PUTPAGES. 1059 * 1060 * This is typically called indirectly via the pageout daemon and 1061 * clustering has already typically occured, so in general we ask the 1062 * underlying filesystem to write the data out asynchronously rather 1063 * then delayed. 1064 */ 1065 int 1066 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount, 1067 int flags, int *rtvals) 1068 { 1069 int i; 1070 vm_object_t object; 1071 vm_page_t m; 1072 int count; 1073 1074 int maxsize, ncount; 1075 vm_ooffset_t poffset; 1076 struct uio auio; 1077 struct iovec aiov; 1078 int error; 1079 int ioflags; 1080 int ppscheck = 0; 1081 static struct timeval lastfail; 1082 static int curfail; 1083 1084 object = vp->v_object; 1085 count = bytecount / PAGE_SIZE; 1086 1087 for (i = 0; i < count; i++) 1088 rtvals[i] = VM_PAGER_ERROR; 1089 1090 if ((int64_t)ma[0]->pindex < 0) { 1091 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n", 1092 (long)ma[0]->pindex, (u_long)ma[0]->dirty); 1093 rtvals[0] = VM_PAGER_BAD; 1094 return VM_PAGER_BAD; 1095 } 1096 1097 maxsize = count * PAGE_SIZE; 1098 ncount = count; 1099 1100 poffset = IDX_TO_OFF(ma[0]->pindex); 1101 1102 /* 1103 * If the page-aligned write is larger then the actual file we 1104 * have to invalidate pages occuring beyond the file EOF. However, 1105 * there is an edge case where a file may not be page-aligned where 1106 * the last page is partially invalid. In this case the filesystem 1107 * may not properly clear the dirty bits for the entire page (which 1108 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 1109 * With the page locked we are free to fix-up the dirty bits here. 1110 * 1111 * We do not under any circumstances truncate the valid bits, as 1112 * this will screw up bogus page replacement. 1113 */ 1114 VM_OBJECT_WLOCK(object); 1115 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 1116 if (object->un_pager.vnp.vnp_size > poffset) { 1117 int pgoff; 1118 1119 maxsize = object->un_pager.vnp.vnp_size - poffset; 1120 ncount = btoc(maxsize); 1121 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1122 /* 1123 * If the object is locked and the following 1124 * conditions hold, then the page's dirty 1125 * field cannot be concurrently changed by a 1126 * pmap operation. 1127 */ 1128 m = ma[ncount - 1]; 1129 vm_page_assert_sbusied(m); 1130 KASSERT(!pmap_page_is_write_mapped(m), 1131 ("vnode_pager_generic_putpages: page %p is not read-only", m)); 1132 vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 1133 pgoff); 1134 } 1135 } else { 1136 maxsize = 0; 1137 ncount = 0; 1138 } 1139 if (ncount < count) { 1140 for (i = ncount; i < count; i++) { 1141 rtvals[i] = VM_PAGER_BAD; 1142 } 1143 } 1144 } 1145 VM_OBJECT_WUNLOCK(object); 1146 1147 /* 1148 * pageouts are already clustered, use IO_ASYNC to force a bawrite() 1149 * rather then a bdwrite() to prevent paging I/O from saturating 1150 * the buffer cache. Dummy-up the sequential heuristic to cause 1151 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 1152 * the system decides how to cluster. 1153 */ 1154 ioflags = IO_VMIO; 1155 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 1156 ioflags |= IO_SYNC; 1157 else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 1158 ioflags |= IO_ASYNC; 1159 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 1160 ioflags |= IO_SEQMAX << IO_SEQSHIFT; 1161 1162 aiov.iov_base = (caddr_t) 0; 1163 aiov.iov_len = maxsize; 1164 auio.uio_iov = &aiov; 1165 auio.uio_iovcnt = 1; 1166 auio.uio_offset = poffset; 1167 auio.uio_segflg = UIO_NOCOPY; 1168 auio.uio_rw = UIO_WRITE; 1169 auio.uio_resid = maxsize; 1170 auio.uio_td = (struct thread *) 0; 1171 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred); 1172 PCPU_INC(cnt.v_vnodeout); 1173 PCPU_ADD(cnt.v_vnodepgsout, ncount); 1174 1175 if (error) { 1176 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1))) 1177 printf("vnode_pager_putpages: I/O error %d\n", error); 1178 } 1179 if (auio.uio_resid) { 1180 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1)) 1181 printf("vnode_pager_putpages: residual I/O %zd at %lu\n", 1182 auio.uio_resid, (u_long)ma[0]->pindex); 1183 } 1184 for (i = 0; i < ncount; i++) { 1185 rtvals[i] = VM_PAGER_OK; 1186 } 1187 return rtvals[0]; 1188 } 1189 1190 void 1191 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written) 1192 { 1193 vm_object_t obj; 1194 int i, pos; 1195 1196 if (written == 0) 1197 return; 1198 obj = ma[0]->object; 1199 VM_OBJECT_WLOCK(obj); 1200 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) { 1201 if (pos < trunc_page(written)) { 1202 rtvals[i] = VM_PAGER_OK; 1203 vm_page_undirty(ma[i]); 1204 } else { 1205 /* Partially written page. */ 1206 rtvals[i] = VM_PAGER_AGAIN; 1207 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK); 1208 } 1209 } 1210 VM_OBJECT_WUNLOCK(obj); 1211 } 1212 1213 void 1214 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start, 1215 vm_offset_t end) 1216 { 1217 struct vnode *vp; 1218 vm_ooffset_t old_wm; 1219 1220 VM_OBJECT_WLOCK(object); 1221 if (object->type != OBJT_VNODE) { 1222 VM_OBJECT_WUNLOCK(object); 1223 return; 1224 } 1225 old_wm = object->un_pager.vnp.writemappings; 1226 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start; 1227 vp = object->handle; 1228 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) { 1229 ASSERT_VOP_ELOCKED(vp, "v_writecount inc"); 1230 VOP_ADD_WRITECOUNT(vp, 1); 1231 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 1232 __func__, vp, vp->v_writecount); 1233 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) { 1234 ASSERT_VOP_ELOCKED(vp, "v_writecount dec"); 1235 VOP_ADD_WRITECOUNT(vp, -1); 1236 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 1237 __func__, vp, vp->v_writecount); 1238 } 1239 VM_OBJECT_WUNLOCK(object); 1240 } 1241 1242 void 1243 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start, 1244 vm_offset_t end) 1245 { 1246 struct vnode *vp; 1247 struct mount *mp; 1248 vm_offset_t inc; 1249 1250 VM_OBJECT_WLOCK(object); 1251 1252 /* 1253 * First, recheck the object type to account for the race when 1254 * the vnode is reclaimed. 1255 */ 1256 if (object->type != OBJT_VNODE) { 1257 VM_OBJECT_WUNLOCK(object); 1258 return; 1259 } 1260 1261 /* 1262 * Optimize for the case when writemappings is not going to 1263 * zero. 1264 */ 1265 inc = end - start; 1266 if (object->un_pager.vnp.writemappings != inc) { 1267 object->un_pager.vnp.writemappings -= inc; 1268 VM_OBJECT_WUNLOCK(object); 1269 return; 1270 } 1271 1272 vp = object->handle; 1273 vhold(vp); 1274 VM_OBJECT_WUNLOCK(object); 1275 mp = NULL; 1276 vn_start_write(vp, &mp, V_WAIT); 1277 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1278 1279 /* 1280 * Decrement the object's writemappings, by swapping the start 1281 * and end arguments for vnode_pager_update_writecount(). If 1282 * there was not a race with vnode reclaimation, then the 1283 * vnode's v_writecount is decremented. 1284 */ 1285 vnode_pager_update_writecount(object, end, start); 1286 VOP_UNLOCK(vp, 0); 1287 vdrop(vp); 1288 if (mp != NULL) 1289 vn_finished_write(mp); 1290 } 1291