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