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