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