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