1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1990 University of Utah. 5 * Copyright (c) 1991 The Regents of the University of California. 6 * All rights reserved. 7 * Copyright (c) 1993, 1994 John S. Dyson 8 * Copyright (c) 1995, David Greenman 9 * 10 * This code is derived from software contributed to Berkeley by 11 * the Systems Programming Group of the University of Utah Computer 12 * Science Department. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the University of 25 * California, Berkeley and its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91 43 */ 44 45 /* 46 * Page to/from files (vnodes). 47 */ 48 49 /* 50 * TODO: 51 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 52 * greatly re-simplify the vnode_pager. 53 */ 54 55 #include <sys/cdefs.h> 56 __FBSDID("$FreeBSD$"); 57 58 #include "opt_vm.h" 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/proc.h> 63 #include <sys/vnode.h> 64 #include <sys/mount.h> 65 #include <sys/bio.h> 66 #include <sys/buf.h> 67 #include <sys/vmmeter.h> 68 #include <sys/limits.h> 69 #include <sys/conf.h> 70 #include <sys/rwlock.h> 71 #include <sys/sf_buf.h> 72 73 #include <machine/atomic.h> 74 75 #include <vm/vm.h> 76 #include <vm/vm_param.h> 77 #include <vm/vm_object.h> 78 #include <vm/vm_page.h> 79 #include <vm/vm_pager.h> 80 #include <vm/vm_map.h> 81 #include <vm/vnode_pager.h> 82 #include <vm/vm_extern.h> 83 84 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, 85 daddr_t *rtaddress, int *run); 86 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m); 87 static int vnode_pager_input_old(vm_object_t object, vm_page_t m); 88 static void vnode_pager_dealloc(vm_object_t); 89 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *); 90 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 91 int *, 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 umtx_shm_object_terminated(obj); 170 if (obj->ref_count == 0) { 171 /* 172 * don't double-terminate the object 173 */ 174 if ((obj->flags & OBJ_DEAD) == 0) { 175 vm_object_terminate(obj); 176 } else { 177 /* 178 * Waiters were already handled during object 179 * termination. The exclusive vnode lock hopefully 180 * prevented new waiters from referencing the dying 181 * object. 182 */ 183 KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0, 184 ("OBJ_DISCONNECTWNT set obj %p flags %x", 185 obj, obj->flags)); 186 vp->v_object = NULL; 187 VM_OBJECT_WUNLOCK(obj); 188 } 189 } else { 190 /* 191 * Woe to the process that tries to page now :-). 192 */ 193 vm_pager_deallocate(obj); 194 VM_OBJECT_WUNLOCK(obj); 195 } 196 KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object)); 197 } 198 199 200 /* 201 * Allocate (or lookup) pager for a vnode. 202 * Handle is a vnode pointer. 203 * 204 * MPSAFE 205 */ 206 vm_object_t 207 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 208 vm_ooffset_t offset, struct ucred *cred) 209 { 210 vm_object_t object; 211 struct vnode *vp; 212 213 /* 214 * Pageout to vnode, no can do yet. 215 */ 216 if (handle == NULL) 217 return (NULL); 218 219 vp = (struct vnode *) handle; 220 221 /* 222 * If the object is being terminated, wait for it to 223 * go away. 224 */ 225 retry: 226 while ((object = vp->v_object) != NULL) { 227 VM_OBJECT_WLOCK(object); 228 if ((object->flags & OBJ_DEAD) == 0) 229 break; 230 vm_object_set_flag(object, OBJ_DISCONNECTWNT); 231 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0); 232 } 233 234 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference")); 235 236 if (object == NULL) { 237 /* 238 * Add an object of the appropriate size 239 */ 240 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 241 242 object->un_pager.vnp.vnp_size = size; 243 object->un_pager.vnp.writemappings = 0; 244 245 object->handle = handle; 246 VI_LOCK(vp); 247 if (vp->v_object != NULL) { 248 /* 249 * Object has been created while we were sleeping 250 */ 251 VI_UNLOCK(vp); 252 VM_OBJECT_WLOCK(object); 253 KASSERT(object->ref_count == 1, 254 ("leaked ref %p %d", object, object->ref_count)); 255 object->type = OBJT_DEAD; 256 object->ref_count = 0; 257 VM_OBJECT_WUNLOCK(object); 258 vm_object_destroy(object); 259 goto retry; 260 } 261 vp->v_object = object; 262 VI_UNLOCK(vp); 263 } else { 264 object->ref_count++; 265 #if VM_NRESERVLEVEL > 0 266 vm_object_color(object, 0); 267 #endif 268 VM_OBJECT_WUNLOCK(object); 269 } 270 vrefact(vp); 271 return (object); 272 } 273 274 /* 275 * The object must be locked. 276 */ 277 static void 278 vnode_pager_dealloc(vm_object_t object) 279 { 280 struct vnode *vp; 281 int refs; 282 283 vp = object->handle; 284 if (vp == NULL) 285 panic("vnode_pager_dealloc: pager already dealloced"); 286 287 VM_OBJECT_ASSERT_WLOCKED(object); 288 vm_object_pip_wait(object, "vnpdea"); 289 refs = object->ref_count; 290 291 object->handle = NULL; 292 object->type = OBJT_DEAD; 293 if (object->flags & OBJ_DISCONNECTWNT) { 294 vm_object_clear_flag(object, OBJ_DISCONNECTWNT); 295 wakeup(object); 296 } 297 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc"); 298 if (object->un_pager.vnp.writemappings > 0) { 299 object->un_pager.vnp.writemappings = 0; 300 VOP_ADD_WRITECOUNT(vp, -1); 301 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 302 __func__, vp, vp->v_writecount); 303 } 304 vp->v_object = NULL; 305 VOP_UNSET_TEXT(vp); 306 VM_OBJECT_WUNLOCK(object); 307 while (refs-- > 0) 308 vunref(vp); 309 VM_OBJECT_WLOCK(object); 310 } 311 312 static boolean_t 313 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 314 int *after) 315 { 316 struct vnode *vp = object->handle; 317 daddr_t bn; 318 int err; 319 daddr_t reqblock; 320 int poff; 321 int bsize; 322 int pagesperblock, blocksperpage; 323 324 VM_OBJECT_ASSERT_WLOCKED(object); 325 /* 326 * If no vp or vp is doomed or marked transparent to VM, we do not 327 * have the page. 328 */ 329 if (vp == NULL || vp->v_iflag & VI_DOOMED) 330 return FALSE; 331 /* 332 * If the offset is beyond end of file we do 333 * not have the page. 334 */ 335 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size) 336 return FALSE; 337 338 bsize = vp->v_mount->mnt_stat.f_iosize; 339 pagesperblock = bsize / PAGE_SIZE; 340 blocksperpage = 0; 341 if (pagesperblock > 0) { 342 reqblock = pindex / pagesperblock; 343 } else { 344 blocksperpage = (PAGE_SIZE / bsize); 345 reqblock = pindex * blocksperpage; 346 } 347 VM_OBJECT_WUNLOCK(object); 348 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before); 349 VM_OBJECT_WLOCK(object); 350 if (err) 351 return TRUE; 352 if (bn == -1) 353 return FALSE; 354 if (pagesperblock > 0) { 355 poff = pindex - (reqblock * pagesperblock); 356 if (before) { 357 *before *= pagesperblock; 358 *before += poff; 359 } 360 if (after) { 361 /* 362 * The BMAP vop can report a partial block in the 363 * 'after', but must not report blocks after EOF. 364 * Assert the latter, and truncate 'after' in case 365 * of the former. 366 */ 367 KASSERT((reqblock + *after) * pagesperblock < 368 roundup2(object->size, pagesperblock), 369 ("%s: reqblock %jd after %d size %ju", __func__, 370 (intmax_t )reqblock, *after, 371 (uintmax_t )object->size)); 372 *after *= pagesperblock; 373 *after += pagesperblock - (poff + 1); 374 if (pindex + *after >= object->size) 375 *after = object->size - 1 - pindex; 376 } 377 } else { 378 if (before) { 379 *before /= blocksperpage; 380 } 381 382 if (after) { 383 *after /= blocksperpage; 384 } 385 } 386 return TRUE; 387 } 388 389 /* 390 * Lets the VM system know about a change in size for a file. 391 * We adjust our own internal size and flush any cached pages in 392 * the associated object that are affected by the size change. 393 * 394 * Note: this routine may be invoked as a result of a pager put 395 * operation (possibly at object termination time), so we must be careful. 396 */ 397 void 398 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize) 399 { 400 vm_object_t object; 401 vm_page_t m; 402 vm_pindex_t nobjsize; 403 404 if ((object = vp->v_object) == NULL) 405 return; 406 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */ 407 VM_OBJECT_WLOCK(object); 408 if (object->type == OBJT_DEAD) { 409 VM_OBJECT_WUNLOCK(object); 410 return; 411 } 412 KASSERT(object->type == OBJT_VNODE, 413 ("not vnode-backed object %p", object)); 414 if (nsize == object->un_pager.vnp.vnp_size) { 415 /* 416 * Hasn't changed size 417 */ 418 VM_OBJECT_WUNLOCK(object); 419 return; 420 } 421 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 422 if (nsize < object->un_pager.vnp.vnp_size) { 423 /* 424 * File has shrunk. Toss any cached pages beyond the new EOF. 425 */ 426 if (nobjsize < object->size) 427 vm_object_page_remove(object, nobjsize, object->size, 428 0); 429 /* 430 * this gets rid of garbage at the end of a page that is now 431 * only partially backed by the vnode. 432 * 433 * XXX for some reason (I don't know yet), if we take a 434 * completely invalid page and mark it partially valid 435 * it can screw up NFS reads, so we don't allow the case. 436 */ 437 if ((nsize & PAGE_MASK) && 438 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL && 439 m->valid != 0) { 440 int base = (int)nsize & PAGE_MASK; 441 int size = PAGE_SIZE - base; 442 443 /* 444 * Clear out partial-page garbage in case 445 * the page has been mapped. 446 */ 447 pmap_zero_page_area(m, base, size); 448 449 /* 450 * Update the valid bits to reflect the blocks that 451 * have been zeroed. Some of these valid bits may 452 * have already been set. 453 */ 454 vm_page_set_valid_range(m, base, size); 455 456 /* 457 * Round "base" to the next block boundary so that the 458 * dirty bit for a partially zeroed block is not 459 * cleared. 460 */ 461 base = roundup2(base, DEV_BSIZE); 462 463 /* 464 * Clear out partial-page dirty bits. 465 * 466 * note that we do not clear out the valid 467 * bits. This would prevent bogus_page 468 * replacement from working properly. 469 */ 470 vm_page_clear_dirty(m, base, PAGE_SIZE - base); 471 } 472 } 473 object->un_pager.vnp.vnp_size = nsize; 474 object->size = nobjsize; 475 VM_OBJECT_WUNLOCK(object); 476 } 477 478 /* 479 * calculate the linear (byte) disk address of specified virtual 480 * file address 481 */ 482 static int 483 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress, 484 int *run) 485 { 486 int bsize; 487 int err; 488 daddr_t vblock; 489 daddr_t voffset; 490 491 if (address < 0) 492 return -1; 493 494 if (vp->v_iflag & VI_DOOMED) 495 return -1; 496 497 bsize = vp->v_mount->mnt_stat.f_iosize; 498 vblock = address / bsize; 499 voffset = address % bsize; 500 501 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL); 502 if (err == 0) { 503 if (*rtaddress != -1) 504 *rtaddress += voffset / DEV_BSIZE; 505 if (run) { 506 *run += 1; 507 *run *= bsize/PAGE_SIZE; 508 *run -= voffset/PAGE_SIZE; 509 } 510 } 511 512 return (err); 513 } 514 515 /* 516 * small block filesystem vnode pager input 517 */ 518 static int 519 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m) 520 { 521 struct vnode *vp; 522 struct bufobj *bo; 523 struct buf *bp; 524 struct sf_buf *sf; 525 daddr_t fileaddr; 526 vm_offset_t bsize; 527 vm_page_bits_t bits; 528 int error, i; 529 530 error = 0; 531 vp = object->handle; 532 if (vp->v_iflag & VI_DOOMED) 533 return VM_PAGER_BAD; 534 535 bsize = vp->v_mount->mnt_stat.f_iosize; 536 537 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL); 538 539 sf = sf_buf_alloc(m, 0); 540 541 for (i = 0; i < PAGE_SIZE / bsize; i++) { 542 vm_ooffset_t address; 543 544 bits = vm_page_bits(i * bsize, bsize); 545 if (m->valid & bits) 546 continue; 547 548 address = IDX_TO_OFF(m->pindex) + i * bsize; 549 if (address >= object->un_pager.vnp.vnp_size) { 550 fileaddr = -1; 551 } else { 552 error = vnode_pager_addr(vp, address, &fileaddr, NULL); 553 if (error) 554 break; 555 } 556 if (fileaddr != -1) { 557 bp = getpbuf(&vnode_pbuf_freecnt); 558 559 /* build a minimal buffer header */ 560 bp->b_iocmd = BIO_READ; 561 bp->b_iodone = bdone; 562 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 563 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 564 bp->b_rcred = crhold(curthread->td_ucred); 565 bp->b_wcred = crhold(curthread->td_ucred); 566 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize; 567 bp->b_blkno = fileaddr; 568 pbgetbo(bo, bp); 569 bp->b_vp = vp; 570 bp->b_bcount = bsize; 571 bp->b_bufsize = bsize; 572 bp->b_runningbufspace = bp->b_bufsize; 573 atomic_add_long(&runningbufspace, bp->b_runningbufspace); 574 575 /* do the input */ 576 bp->b_iooffset = dbtob(bp->b_blkno); 577 bstrategy(bp); 578 579 bwait(bp, PVM, "vnsrd"); 580 581 if ((bp->b_ioflags & BIO_ERROR) != 0) 582 error = EIO; 583 584 /* 585 * free the buffer header back to the swap buffer pool 586 */ 587 bp->b_vp = NULL; 588 pbrelbo(bp); 589 relpbuf(bp, &vnode_pbuf_freecnt); 590 if (error) 591 break; 592 } else 593 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize); 594 KASSERT((m->dirty & bits) == 0, 595 ("vnode_pager_input_smlfs: page %p is dirty", m)); 596 VM_OBJECT_WLOCK(object); 597 m->valid |= bits; 598 VM_OBJECT_WUNLOCK(object); 599 } 600 sf_buf_free(sf); 601 if (error) { 602 return VM_PAGER_ERROR; 603 } 604 return VM_PAGER_OK; 605 } 606 607 /* 608 * old style vnode pager input routine 609 */ 610 static int 611 vnode_pager_input_old(vm_object_t object, vm_page_t m) 612 { 613 struct uio auio; 614 struct iovec aiov; 615 int error; 616 int size; 617 struct sf_buf *sf; 618 struct vnode *vp; 619 620 VM_OBJECT_ASSERT_WLOCKED(object); 621 error = 0; 622 623 /* 624 * Return failure if beyond current EOF 625 */ 626 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 627 return VM_PAGER_BAD; 628 } else { 629 size = PAGE_SIZE; 630 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 631 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 632 vp = object->handle; 633 VM_OBJECT_WUNLOCK(object); 634 635 /* 636 * Allocate a kernel virtual address and initialize so that 637 * we can use VOP_READ/WRITE routines. 638 */ 639 sf = sf_buf_alloc(m, 0); 640 641 aiov.iov_base = (caddr_t)sf_buf_kva(sf); 642 aiov.iov_len = size; 643 auio.uio_iov = &aiov; 644 auio.uio_iovcnt = 1; 645 auio.uio_offset = IDX_TO_OFF(m->pindex); 646 auio.uio_segflg = UIO_SYSSPACE; 647 auio.uio_rw = UIO_READ; 648 auio.uio_resid = size; 649 auio.uio_td = curthread; 650 651 error = VOP_READ(vp, &auio, 0, curthread->td_ucred); 652 if (!error) { 653 int count = size - auio.uio_resid; 654 655 if (count == 0) 656 error = EINVAL; 657 else if (count != PAGE_SIZE) 658 bzero((caddr_t)sf_buf_kva(sf) + count, 659 PAGE_SIZE - count); 660 } 661 sf_buf_free(sf); 662 663 VM_OBJECT_WLOCK(object); 664 } 665 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m)); 666 if (!error) 667 m->valid = VM_PAGE_BITS_ALL; 668 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 669 } 670 671 /* 672 * generic vnode pager input routine 673 */ 674 675 /* 676 * Local media VFS's that do not implement their own VOP_GETPAGES 677 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages() 678 * to implement the previous behaviour. 679 * 680 * All other FS's should use the bypass to get to the local media 681 * backing vp's VOP_GETPAGES. 682 */ 683 static int 684 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind, 685 int *rahead) 686 { 687 struct vnode *vp; 688 int rtval; 689 690 vp = object->handle; 691 VM_OBJECT_WUNLOCK(object); 692 rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead); 693 KASSERT(rtval != EOPNOTSUPP, 694 ("vnode_pager: FS getpages not implemented\n")); 695 VM_OBJECT_WLOCK(object); 696 return rtval; 697 } 698 699 static int 700 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count, 701 int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg) 702 { 703 struct vnode *vp; 704 int rtval; 705 706 vp = object->handle; 707 VM_OBJECT_WUNLOCK(object); 708 rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg); 709 KASSERT(rtval != EOPNOTSUPP, 710 ("vnode_pager: FS getpages_async not implemented\n")); 711 VM_OBJECT_WLOCK(object); 712 return (rtval); 713 } 714 715 /* 716 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for 717 * local filesystems, where partially valid pages can only occur at 718 * the end of file. 719 */ 720 int 721 vnode_pager_local_getpages(struct vop_getpages_args *ap) 722 { 723 724 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 725 ap->a_rbehind, ap->a_rahead, NULL, NULL)); 726 } 727 728 int 729 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap) 730 { 731 732 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 733 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg)); 734 } 735 736 /* 737 * This is now called from local media FS's to operate against their 738 * own vnodes if they fail to implement VOP_GETPAGES. 739 */ 740 int 741 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count, 742 int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg) 743 { 744 vm_object_t object; 745 struct bufobj *bo; 746 struct buf *bp; 747 off_t foff; 748 #ifdef INVARIANTS 749 off_t blkno0; 750 #endif 751 int bsize, pagesperblock, *freecnt; 752 int error, before, after, rbehind, rahead, poff, i; 753 int bytecount, secmask; 754 755 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 756 ("%s does not support devices", __func__)); 757 758 if (vp->v_iflag & VI_DOOMED) 759 return (VM_PAGER_BAD); 760 761 object = vp->v_object; 762 foff = IDX_TO_OFF(m[0]->pindex); 763 bsize = vp->v_mount->mnt_stat.f_iosize; 764 pagesperblock = bsize / PAGE_SIZE; 765 766 KASSERT(foff < object->un_pager.vnp.vnp_size, 767 ("%s: page %p offset beyond vp %p size", __func__, m[0], vp)); 768 KASSERT(count <= sizeof(bp->b_pages), 769 ("%s: requested %d pages", __func__, count)); 770 771 /* 772 * The last page has valid blocks. Invalid part can only 773 * exist at the end of file, and the page is made fully valid 774 * by zeroing in vm_pager_get_pages(). 775 */ 776 if (m[count - 1]->valid != 0 && --count == 0) { 777 if (iodone != NULL) 778 iodone(arg, m, 1, 0); 779 return (VM_PAGER_OK); 780 } 781 782 /* 783 * Synchronous and asynchronous paging operations use different 784 * free pbuf counters. This is done to avoid asynchronous requests 785 * to consume all pbufs. 786 * Allocate the pbuf at the very beginning of the function, so that 787 * if we are low on certain kind of pbufs don't even proceed to BMAP, 788 * but sleep. 789 */ 790 freecnt = iodone != NULL ? 791 &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt; 792 bp = getpbuf(freecnt); 793 794 /* 795 * Get the underlying device blocks for the file with VOP_BMAP(). 796 * If the file system doesn't support VOP_BMAP, use old way of 797 * getting pages via VOP_READ. 798 */ 799 error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before); 800 if (error == EOPNOTSUPP) { 801 relpbuf(bp, freecnt); 802 VM_OBJECT_WLOCK(object); 803 for (i = 0; i < count; i++) { 804 VM_CNT_INC(v_vnodein); 805 VM_CNT_INC(v_vnodepgsin); 806 error = vnode_pager_input_old(object, m[i]); 807 if (error) 808 break; 809 } 810 VM_OBJECT_WUNLOCK(object); 811 return (error); 812 } else if (error != 0) { 813 relpbuf(bp, freecnt); 814 return (VM_PAGER_ERROR); 815 } 816 817 /* 818 * If the file system supports BMAP, but blocksize is smaller 819 * than a page size, then use special small filesystem code. 820 */ 821 if (pagesperblock == 0) { 822 relpbuf(bp, freecnt); 823 for (i = 0; i < count; i++) { 824 VM_CNT_INC(v_vnodein); 825 VM_CNT_INC(v_vnodepgsin); 826 error = vnode_pager_input_smlfs(object, m[i]); 827 if (error) 828 break; 829 } 830 return (error); 831 } 832 833 /* 834 * A sparse file can be encountered only for a single page request, 835 * which may not be preceded by call to vm_pager_haspage(). 836 */ 837 if (bp->b_blkno == -1) { 838 KASSERT(count == 1, 839 ("%s: array[%d] request to a sparse file %p", __func__, 840 count, vp)); 841 relpbuf(bp, freecnt); 842 pmap_zero_page(m[0]); 843 KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty", 844 __func__, m[0])); 845 VM_OBJECT_WLOCK(object); 846 m[0]->valid = VM_PAGE_BITS_ALL; 847 VM_OBJECT_WUNLOCK(object); 848 return (VM_PAGER_OK); 849 } 850 851 #ifdef INVARIANTS 852 blkno0 = bp->b_blkno; 853 #endif 854 bp->b_blkno += (foff % bsize) / DEV_BSIZE; 855 856 /* Recalculate blocks available after/before to pages. */ 857 poff = (foff % bsize) / PAGE_SIZE; 858 before *= pagesperblock; 859 before += poff; 860 after *= pagesperblock; 861 after += pagesperblock - (poff + 1); 862 if (m[0]->pindex + after >= object->size) 863 after = object->size - 1 - m[0]->pindex; 864 KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d", 865 __func__, count, after + 1)); 866 after -= count - 1; 867 868 /* Trim requested rbehind/rahead to possible values. */ 869 rbehind = a_rbehind ? *a_rbehind : 0; 870 rahead = a_rahead ? *a_rahead : 0; 871 rbehind = min(rbehind, before); 872 rbehind = min(rbehind, m[0]->pindex); 873 rahead = min(rahead, after); 874 rahead = min(rahead, object->size - m[count - 1]->pindex); 875 /* 876 * Check that total amount of pages fit into buf. Trim rbehind and 877 * rahead evenly if not. 878 */ 879 if (rbehind + rahead + count > nitems(bp->b_pages)) { 880 int trim, sum; 881 882 trim = rbehind + rahead + count - nitems(bp->b_pages) + 1; 883 sum = rbehind + rahead; 884 if (rbehind == before) { 885 /* Roundup rbehind trim to block size. */ 886 rbehind -= roundup(trim * rbehind / sum, pagesperblock); 887 if (rbehind < 0) 888 rbehind = 0; 889 } else 890 rbehind -= trim * rbehind / sum; 891 rahead -= trim * rahead / sum; 892 } 893 KASSERT(rbehind + rahead + count <= nitems(bp->b_pages), 894 ("%s: behind %d ahead %d count %d", __func__, 895 rbehind, rahead, count)); 896 897 /* 898 * Fill in the bp->b_pages[] array with requested and optional 899 * read behind or read ahead pages. Read behind pages are looked 900 * up in a backward direction, down to a first cached page. Same 901 * for read ahead pages, but there is no need to shift the array 902 * in case of encountering a cached page. 903 */ 904 i = bp->b_npages = 0; 905 if (rbehind) { 906 vm_pindex_t startpindex, tpindex; 907 vm_page_t p; 908 909 VM_OBJECT_WLOCK(object); 910 startpindex = m[0]->pindex - rbehind; 911 if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL && 912 p->pindex >= startpindex) 913 startpindex = p->pindex + 1; 914 915 /* tpindex is unsigned; beware of numeric underflow. */ 916 for (tpindex = m[0]->pindex - 1; 917 tpindex >= startpindex && tpindex < m[0]->pindex; 918 tpindex--, i++) { 919 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 920 if (p == NULL) { 921 /* Shift the array. */ 922 for (int j = 0; j < i; j++) 923 bp->b_pages[j] = bp->b_pages[j + 924 tpindex + 1 - startpindex]; 925 break; 926 } 927 bp->b_pages[tpindex - startpindex] = p; 928 } 929 930 bp->b_pgbefore = i; 931 bp->b_npages += i; 932 bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE; 933 } else 934 bp->b_pgbefore = 0; 935 936 /* Requested pages. */ 937 for (int j = 0; j < count; j++, i++) 938 bp->b_pages[i] = m[j]; 939 bp->b_npages += count; 940 941 if (rahead) { 942 vm_pindex_t endpindex, tpindex; 943 vm_page_t p; 944 945 if (!VM_OBJECT_WOWNED(object)) 946 VM_OBJECT_WLOCK(object); 947 endpindex = m[count - 1]->pindex + rahead + 1; 948 if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL && 949 p->pindex < endpindex) 950 endpindex = p->pindex; 951 if (endpindex > object->size) 952 endpindex = object->size; 953 954 for (tpindex = m[count - 1]->pindex + 1; 955 tpindex < endpindex; i++, tpindex++) { 956 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 957 if (p == NULL) 958 break; 959 bp->b_pages[i] = p; 960 } 961 962 bp->b_pgafter = i - bp->b_npages; 963 bp->b_npages = i; 964 } else 965 bp->b_pgafter = 0; 966 967 if (VM_OBJECT_WOWNED(object)) 968 VM_OBJECT_WUNLOCK(object); 969 970 /* Report back actual behind/ahead read. */ 971 if (a_rbehind) 972 *a_rbehind = bp->b_pgbefore; 973 if (a_rahead) 974 *a_rahead = bp->b_pgafter; 975 976 #ifdef INVARIANTS 977 KASSERT(bp->b_npages <= nitems(bp->b_pages), 978 ("%s: buf %p overflowed", __func__, bp)); 979 for (int j = 1, prev = 0; j < bp->b_npages; j++) { 980 if (bp->b_pages[j] == bogus_page) 981 continue; 982 KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex == 983 j - prev, ("%s: pages array not consecutive, bp %p", 984 __func__, bp)); 985 prev = j; 986 } 987 #endif 988 989 /* 990 * Recalculate first offset and bytecount with regards to read behind. 991 * Truncate bytecount to vnode real size and round up physical size 992 * for real devices. 993 */ 994 foff = IDX_TO_OFF(bp->b_pages[0]->pindex); 995 bytecount = bp->b_npages << PAGE_SHIFT; 996 if ((foff + bytecount) > object->un_pager.vnp.vnp_size) 997 bytecount = object->un_pager.vnp.vnp_size - foff; 998 secmask = bo->bo_bsize - 1; 999 KASSERT(secmask < PAGE_SIZE && secmask > 0, 1000 ("%s: sector size %d too large", __func__, secmask + 1)); 1001 bytecount = (bytecount + secmask) & ~secmask; 1002 1003 /* 1004 * And map the pages to be read into the kva, if the filesystem 1005 * requires mapped buffers. 1006 */ 1007 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 && 1008 unmapped_buf_allowed) { 1009 bp->b_data = unmapped_buf; 1010 bp->b_offset = 0; 1011 } else { 1012 bp->b_data = bp->b_kvabase; 1013 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages); 1014 } 1015 1016 /* Build a minimal buffer header. */ 1017 bp->b_iocmd = BIO_READ; 1018 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred")); 1019 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred")); 1020 bp->b_rcred = crhold(curthread->td_ucred); 1021 bp->b_wcred = crhold(curthread->td_ucred); 1022 pbgetbo(bo, bp); 1023 bp->b_vp = vp; 1024 bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount; 1025 bp->b_iooffset = dbtob(bp->b_blkno); 1026 KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) == 1027 (blkno0 - bp->b_blkno) * DEV_BSIZE + 1028 IDX_TO_OFF(m[0]->pindex) % bsize, 1029 ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju " 1030 "blkno0 %ju b_blkno %ju", bsize, 1031 (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex, 1032 (uintmax_t)blkno0, (uintmax_t)bp->b_blkno)); 1033 1034 atomic_add_long(&runningbufspace, bp->b_runningbufspace); 1035 VM_CNT_INC(v_vnodein); 1036 VM_CNT_ADD(v_vnodepgsin, bp->b_npages); 1037 1038 if (iodone != NULL) { /* async */ 1039 bp->b_pgiodone = iodone; 1040 bp->b_caller1 = arg; 1041 bp->b_iodone = vnode_pager_generic_getpages_done_async; 1042 bp->b_flags |= B_ASYNC; 1043 BUF_KERNPROC(bp); 1044 bstrategy(bp); 1045 return (VM_PAGER_OK); 1046 } else { 1047 bp->b_iodone = bdone; 1048 bstrategy(bp); 1049 bwait(bp, PVM, "vnread"); 1050 error = vnode_pager_generic_getpages_done(bp); 1051 for (i = 0; i < bp->b_npages; i++) 1052 bp->b_pages[i] = NULL; 1053 bp->b_vp = NULL; 1054 pbrelbo(bp); 1055 relpbuf(bp, &vnode_pbuf_freecnt); 1056 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK); 1057 } 1058 } 1059 1060 static void 1061 vnode_pager_generic_getpages_done_async(struct buf *bp) 1062 { 1063 int error; 1064 1065 error = vnode_pager_generic_getpages_done(bp); 1066 /* Run the iodone upon the requested range. */ 1067 bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore, 1068 bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error); 1069 for (int i = 0; i < bp->b_npages; i++) 1070 bp->b_pages[i] = NULL; 1071 bp->b_vp = NULL; 1072 pbrelbo(bp); 1073 relpbuf(bp, &vnode_async_pbuf_freecnt); 1074 } 1075 1076 static int 1077 vnode_pager_generic_getpages_done(struct buf *bp) 1078 { 1079 vm_object_t object; 1080 off_t tfoff, nextoff; 1081 int i, error; 1082 1083 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0; 1084 object = bp->b_vp->v_object; 1085 1086 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) { 1087 if (!buf_mapped(bp)) { 1088 bp->b_data = bp->b_kvabase; 1089 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, 1090 bp->b_npages); 1091 } 1092 bzero(bp->b_data + bp->b_bcount, 1093 PAGE_SIZE * bp->b_npages - bp->b_bcount); 1094 } 1095 if (buf_mapped(bp)) { 1096 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1097 bp->b_data = unmapped_buf; 1098 } 1099 1100 VM_OBJECT_WLOCK(object); 1101 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex); 1102 i < bp->b_npages; i++, tfoff = nextoff) { 1103 vm_page_t mt; 1104 1105 nextoff = tfoff + PAGE_SIZE; 1106 mt = bp->b_pages[i]; 1107 1108 if (nextoff <= object->un_pager.vnp.vnp_size) { 1109 /* 1110 * Read filled up entire page. 1111 */ 1112 mt->valid = VM_PAGE_BITS_ALL; 1113 KASSERT(mt->dirty == 0, 1114 ("%s: page %p is dirty", __func__, mt)); 1115 KASSERT(!pmap_page_is_mapped(mt), 1116 ("%s: page %p is mapped", __func__, mt)); 1117 } else { 1118 /* 1119 * Read did not fill up entire page. 1120 * 1121 * Currently we do not set the entire page valid, 1122 * we just try to clear the piece that we couldn't 1123 * read. 1124 */ 1125 vm_page_set_valid_range(mt, 0, 1126 object->un_pager.vnp.vnp_size - tfoff); 1127 KASSERT((mt->dirty & vm_page_bits(0, 1128 object->un_pager.vnp.vnp_size - tfoff)) == 0, 1129 ("%s: page %p is dirty", __func__, mt)); 1130 } 1131 1132 if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter) 1133 vm_page_readahead_finish(mt); 1134 } 1135 VM_OBJECT_WUNLOCK(object); 1136 if (error != 0) 1137 printf("%s: I/O read error %d\n", __func__, error); 1138 1139 return (error); 1140 } 1141 1142 /* 1143 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 1144 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 1145 * vnode_pager_generic_putpages() to implement the previous behaviour. 1146 * 1147 * All other FS's should use the bypass to get to the local media 1148 * backing vp's VOP_PUTPAGES. 1149 */ 1150 static void 1151 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count, 1152 int flags, int *rtvals) 1153 { 1154 int rtval; 1155 struct vnode *vp; 1156 int bytes = count * PAGE_SIZE; 1157 1158 /* 1159 * Force synchronous operation if we are extremely low on memory 1160 * to prevent a low-memory deadlock. VOP operations often need to 1161 * allocate more memory to initiate the I/O ( i.e. do a BMAP 1162 * operation ). The swapper handles the case by limiting the amount 1163 * of asynchronous I/O, but that sort of solution doesn't scale well 1164 * for the vnode pager without a lot of work. 1165 * 1166 * Also, the backing vnode's iodone routine may not wake the pageout 1167 * daemon up. This should be probably be addressed XXX. 1168 */ 1169 1170 if (vm_page_count_min()) 1171 flags |= VM_PAGER_PUT_SYNC; 1172 1173 /* 1174 * Call device-specific putpages function 1175 */ 1176 vp = object->handle; 1177 VM_OBJECT_WUNLOCK(object); 1178 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals); 1179 KASSERT(rtval != EOPNOTSUPP, 1180 ("vnode_pager: stale FS putpages\n")); 1181 VM_OBJECT_WLOCK(object); 1182 } 1183 1184 static int 1185 vn_off2bidx(vm_ooffset_t offset) 1186 { 1187 1188 return ((offset & PAGE_MASK) / DEV_BSIZE); 1189 } 1190 1191 static bool 1192 vn_dirty_blk(vm_page_t m, vm_ooffset_t offset) 1193 { 1194 1195 KASSERT(IDX_TO_OFF(m->pindex) <= offset && 1196 offset < IDX_TO_OFF(m->pindex + 1), 1197 ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex, 1198 (uintmax_t)offset)); 1199 return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0); 1200 } 1201 1202 /* 1203 * This is now called from local media FS's to operate against their 1204 * own vnodes if they fail to implement VOP_PUTPAGES. 1205 * 1206 * This is typically called indirectly via the pageout daemon and 1207 * clustering has already typically occurred, so in general we ask the 1208 * underlying filesystem to write the data out asynchronously rather 1209 * then delayed. 1210 */ 1211 int 1212 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount, 1213 int flags, int *rtvals) 1214 { 1215 vm_object_t object; 1216 vm_page_t m; 1217 vm_ooffset_t maxblksz, next_offset, poffset, prev_offset; 1218 struct uio auio; 1219 struct iovec aiov; 1220 off_t prev_resid, wrsz; 1221 int count, error, i, maxsize, ncount, pgoff, ppscheck; 1222 bool in_hole; 1223 static struct timeval lastfail; 1224 static int curfail; 1225 1226 object = vp->v_object; 1227 count = bytecount / PAGE_SIZE; 1228 1229 for (i = 0; i < count; i++) 1230 rtvals[i] = VM_PAGER_ERROR; 1231 1232 if ((int64_t)ma[0]->pindex < 0) { 1233 printf("vnode_pager_generic_putpages: " 1234 "attempt to write meta-data 0x%jx(%lx)\n", 1235 (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty); 1236 rtvals[0] = VM_PAGER_BAD; 1237 return (VM_PAGER_BAD); 1238 } 1239 1240 maxsize = count * PAGE_SIZE; 1241 ncount = count; 1242 1243 poffset = IDX_TO_OFF(ma[0]->pindex); 1244 1245 /* 1246 * If the page-aligned write is larger then the actual file we 1247 * have to invalidate pages occurring beyond the file EOF. However, 1248 * there is an edge case where a file may not be page-aligned where 1249 * the last page is partially invalid. In this case the filesystem 1250 * may not properly clear the dirty bits for the entire page (which 1251 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 1252 * With the page locked we are free to fix-up the dirty bits here. 1253 * 1254 * We do not under any circumstances truncate the valid bits, as 1255 * this will screw up bogus page replacement. 1256 */ 1257 VM_OBJECT_RLOCK(object); 1258 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 1259 if (!VM_OBJECT_TRYUPGRADE(object)) { 1260 VM_OBJECT_RUNLOCK(object); 1261 VM_OBJECT_WLOCK(object); 1262 if (maxsize + poffset <= object->un_pager.vnp.vnp_size) 1263 goto downgrade; 1264 } 1265 if (object->un_pager.vnp.vnp_size > poffset) { 1266 maxsize = object->un_pager.vnp.vnp_size - poffset; 1267 ncount = btoc(maxsize); 1268 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 1269 pgoff = roundup2(pgoff, DEV_BSIZE); 1270 1271 /* 1272 * If the object is locked and the following 1273 * conditions hold, then the page's dirty 1274 * field cannot be concurrently changed by a 1275 * pmap operation. 1276 */ 1277 m = ma[ncount - 1]; 1278 vm_page_assert_sbusied(m); 1279 KASSERT(!pmap_page_is_write_mapped(m), 1280 ("vnode_pager_generic_putpages: page %p is not read-only", m)); 1281 MPASS(m->dirty != 0); 1282 vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 1283 pgoff); 1284 } 1285 } else { 1286 maxsize = 0; 1287 ncount = 0; 1288 } 1289 for (i = ncount; i < count; i++) 1290 rtvals[i] = VM_PAGER_BAD; 1291 downgrade: 1292 VM_OBJECT_LOCK_DOWNGRADE(object); 1293 } 1294 1295 auio.uio_iov = &aiov; 1296 auio.uio_segflg = UIO_NOCOPY; 1297 auio.uio_rw = UIO_WRITE; 1298 auio.uio_td = NULL; 1299 maxblksz = roundup2(poffset + maxsize, DEV_BSIZE); 1300 1301 for (prev_offset = poffset; prev_offset < maxblksz;) { 1302 /* Skip clean blocks. */ 1303 for (in_hole = true; in_hole && prev_offset < maxblksz;) { 1304 m = ma[OFF_TO_IDX(prev_offset - poffset)]; 1305 for (i = vn_off2bidx(prev_offset); 1306 i < sizeof(vm_page_bits_t) * NBBY && 1307 prev_offset < maxblksz; i++) { 1308 if (vn_dirty_blk(m, prev_offset)) { 1309 in_hole = false; 1310 break; 1311 } 1312 prev_offset += DEV_BSIZE; 1313 } 1314 } 1315 if (in_hole) 1316 goto write_done; 1317 1318 /* Find longest run of dirty blocks. */ 1319 for (next_offset = prev_offset; next_offset < maxblksz;) { 1320 m = ma[OFF_TO_IDX(next_offset - poffset)]; 1321 for (i = vn_off2bidx(next_offset); 1322 i < sizeof(vm_page_bits_t) * NBBY && 1323 next_offset < maxblksz; i++) { 1324 if (!vn_dirty_blk(m, next_offset)) 1325 goto start_write; 1326 next_offset += DEV_BSIZE; 1327 } 1328 } 1329 start_write: 1330 if (next_offset > poffset + maxsize) 1331 next_offset = poffset + maxsize; 1332 1333 /* 1334 * Getting here requires finding a dirty block in the 1335 * 'skip clean blocks' loop. 1336 */ 1337 MPASS(prev_offset < next_offset); 1338 1339 VM_OBJECT_RUNLOCK(object); 1340 aiov.iov_base = NULL; 1341 auio.uio_iovcnt = 1; 1342 auio.uio_offset = prev_offset; 1343 prev_resid = auio.uio_resid = aiov.iov_len = next_offset - 1344 prev_offset; 1345 error = VOP_WRITE(vp, &auio, 1346 vnode_pager_putpages_ioflags(flags), curthread->td_ucred); 1347 1348 wrsz = prev_resid - auio.uio_resid; 1349 if (wrsz == 0) { 1350 if (ppsratecheck(&lastfail, &curfail, 1) != 0) { 1351 vn_printf(vp, "vnode_pager_putpages: " 1352 "zero-length write at %ju resid %zd\n", 1353 auio.uio_offset, auio.uio_resid); 1354 } 1355 VM_OBJECT_RLOCK(object); 1356 break; 1357 } 1358 1359 /* Adjust the starting offset for next iteration. */ 1360 prev_offset += wrsz; 1361 MPASS(auio.uio_offset == prev_offset); 1362 1363 ppscheck = 0; 1364 if (error != 0 && (ppscheck = ppsratecheck(&lastfail, 1365 &curfail, 1)) != 0) 1366 vn_printf(vp, "vnode_pager_putpages: I/O error %d\n", 1367 error); 1368 if (auio.uio_resid != 0 && (ppscheck != 0 || 1369 ppsratecheck(&lastfail, &curfail, 1) != 0)) 1370 vn_printf(vp, "vnode_pager_putpages: residual I/O %zd " 1371 "at %ju\n", auio.uio_resid, 1372 (uintmax_t)ma[0]->pindex); 1373 VM_OBJECT_RLOCK(object); 1374 if (error != 0 || auio.uio_resid != 0) 1375 break; 1376 } 1377 write_done: 1378 /* Mark completely processed pages. */ 1379 for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++) 1380 rtvals[i] = VM_PAGER_OK; 1381 /* Mark partial EOF page. */ 1382 if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0) 1383 rtvals[i++] = VM_PAGER_OK; 1384 /* Unwritten pages in range, free bonus if the page is clean. */ 1385 for (; i < ncount; i++) 1386 rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR; 1387 VM_OBJECT_RUNLOCK(object); 1388 VM_CNT_ADD(v_vnodepgsout, i); 1389 VM_CNT_INC(v_vnodeout); 1390 return (rtvals[0]); 1391 } 1392 1393 int 1394 vnode_pager_putpages_ioflags(int pager_flags) 1395 { 1396 int ioflags; 1397 1398 /* 1399 * Pageouts are already clustered, use IO_ASYNC to force a 1400 * bawrite() rather then a bdwrite() to prevent paging I/O 1401 * from saturating the buffer cache. Dummy-up the sequential 1402 * heuristic to cause large ranges to cluster. If neither 1403 * IO_SYNC or IO_ASYNC is set, the system decides how to 1404 * cluster. 1405 */ 1406 ioflags = IO_VMIO; 1407 if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0) 1408 ioflags |= IO_SYNC; 1409 else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0) 1410 ioflags |= IO_ASYNC; 1411 ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0; 1412 ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0; 1413 ioflags |= IO_SEQMAX << IO_SEQSHIFT; 1414 return (ioflags); 1415 } 1416 1417 /* 1418 * vnode_pager_undirty_pages(). 1419 * 1420 * A helper to mark pages as clean after pageout that was possibly 1421 * done with a short write. The lpos argument specifies the page run 1422 * length in bytes, and the written argument specifies how many bytes 1423 * were actually written. eof is the offset past the last valid byte 1424 * in the vnode using the absolute file position of the first byte in 1425 * the run as the base from which it is computed. 1426 */ 1427 void 1428 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof, 1429 int lpos) 1430 { 1431 vm_object_t obj; 1432 int i, pos, pos_devb; 1433 1434 if (written == 0 && eof >= lpos) 1435 return; 1436 obj = ma[0]->object; 1437 VM_OBJECT_WLOCK(obj); 1438 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) { 1439 if (pos < trunc_page(written)) { 1440 rtvals[i] = VM_PAGER_OK; 1441 vm_page_undirty(ma[i]); 1442 } else { 1443 /* Partially written page. */ 1444 rtvals[i] = VM_PAGER_AGAIN; 1445 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK); 1446 } 1447 } 1448 if (eof >= lpos) /* avoid truncation */ 1449 goto done; 1450 for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) { 1451 if (pos != trunc_page(pos)) { 1452 /* 1453 * The page contains the last valid byte in 1454 * the vnode, mark the rest of the page as 1455 * clean, potentially making the whole page 1456 * clean. 1457 */ 1458 pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE); 1459 vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE - 1460 pos_devb); 1461 1462 /* 1463 * If the page was cleaned, report the pageout 1464 * on it as successful. msync() no longer 1465 * needs to write out the page, endlessly 1466 * creating write requests and dirty buffers. 1467 */ 1468 if (ma[i]->dirty == 0) 1469 rtvals[i] = VM_PAGER_OK; 1470 1471 pos = round_page(pos); 1472 } else { 1473 /* vm_pageout_flush() clears dirty */ 1474 rtvals[i] = VM_PAGER_BAD; 1475 pos += PAGE_SIZE; 1476 } 1477 } 1478 done: 1479 VM_OBJECT_WUNLOCK(obj); 1480 } 1481 1482 void 1483 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start, 1484 vm_offset_t end) 1485 { 1486 struct vnode *vp; 1487 vm_ooffset_t old_wm; 1488 1489 VM_OBJECT_WLOCK(object); 1490 if (object->type != OBJT_VNODE) { 1491 VM_OBJECT_WUNLOCK(object); 1492 return; 1493 } 1494 old_wm = object->un_pager.vnp.writemappings; 1495 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start; 1496 vp = object->handle; 1497 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) { 1498 ASSERT_VOP_ELOCKED(vp, "v_writecount inc"); 1499 VOP_ADD_WRITECOUNT(vp, 1); 1500 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 1501 __func__, vp, vp->v_writecount); 1502 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) { 1503 ASSERT_VOP_ELOCKED(vp, "v_writecount dec"); 1504 VOP_ADD_WRITECOUNT(vp, -1); 1505 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 1506 __func__, vp, vp->v_writecount); 1507 } 1508 VM_OBJECT_WUNLOCK(object); 1509 } 1510 1511 void 1512 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start, 1513 vm_offset_t end) 1514 { 1515 struct vnode *vp; 1516 struct mount *mp; 1517 vm_offset_t inc; 1518 1519 VM_OBJECT_WLOCK(object); 1520 1521 /* 1522 * First, recheck the object type to account for the race when 1523 * the vnode is reclaimed. 1524 */ 1525 if (object->type != OBJT_VNODE) { 1526 VM_OBJECT_WUNLOCK(object); 1527 return; 1528 } 1529 1530 /* 1531 * Optimize for the case when writemappings is not going to 1532 * zero. 1533 */ 1534 inc = end - start; 1535 if (object->un_pager.vnp.writemappings != inc) { 1536 object->un_pager.vnp.writemappings -= inc; 1537 VM_OBJECT_WUNLOCK(object); 1538 return; 1539 } 1540 1541 vp = object->handle; 1542 vhold(vp); 1543 VM_OBJECT_WUNLOCK(object); 1544 mp = NULL; 1545 vn_start_write(vp, &mp, V_WAIT); 1546 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1547 1548 /* 1549 * Decrement the object's writemappings, by swapping the start 1550 * and end arguments for vnode_pager_update_writecount(). If 1551 * there was not a race with vnode reclaimation, then the 1552 * vnode's v_writecount is decremented. 1553 */ 1554 vnode_pager_update_writecount(object, end, start); 1555 VOP_UNLOCK(vp, 0); 1556 vdrop(vp); 1557 if (mp != NULL) 1558 vn_finished_write(mp); 1559 } 1560