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