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