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