1 /* 2 * Copyright (c) 1990 University of Utah. 3 * Copyright (c) 1991 The Regents of the University of California. 4 * All rights reserved. 5 * Copyright (c) 1993, 1994 John S. Dyson 6 * Copyright (c) 1995, David Greenman 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91 41 * $FreeBSD: src/sys/vm/vnode_pager.c,v 1.116.2.7 2002/12/31 09:34:51 dillon Exp $ 42 * $DragonFly: src/sys/vm/vnode_pager.c,v 1.19 2005/06/02 20:57:21 swildner Exp $ 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/param.h> 56 #include <sys/systm.h> 57 #include <sys/kernel.h> 58 #include <sys/proc.h> 59 #include <sys/vnode.h> 60 #include <sys/mount.h> 61 #include <sys/buf.h> 62 #include <sys/vmmeter.h> 63 #include <sys/conf.h> 64 #include <sys/sfbuf.h> 65 #include <sys/thread2.h> 66 67 #include <vm/vm.h> 68 #include <vm/vm_object.h> 69 #include <vm/vm_page.h> 70 #include <vm/vm_pager.h> 71 #include <vm/vm_map.h> 72 #include <vm/vnode_pager.h> 73 #include <vm/vm_extern.h> 74 75 static vm_offset_t vnode_pager_addr (struct vnode *vp, vm_ooffset_t address, 76 int *run); 77 static void vnode_pager_iodone (struct buf *bp); 78 static int vnode_pager_input_smlfs (vm_object_t object, vm_page_t m); 79 static int vnode_pager_input_old (vm_object_t object, vm_page_t m); 80 static void vnode_pager_dealloc (vm_object_t); 81 static int vnode_pager_getpages (vm_object_t, vm_page_t *, int, int); 82 static void vnode_pager_putpages (vm_object_t, vm_page_t *, int, boolean_t, int *); 83 static boolean_t vnode_pager_haspage (vm_object_t, vm_pindex_t, int *, int *); 84 85 struct pagerops vnodepagerops = { 86 NULL, 87 vnode_pager_alloc, 88 vnode_pager_dealloc, 89 vnode_pager_getpages, 90 vnode_pager_putpages, 91 vnode_pager_haspage, 92 NULL 93 }; 94 95 int vnode_pbuf_freecnt = -1; /* start out unlimited */ 96 97 /* 98 * Allocate (or lookup) pager for a vnode. 99 * Handle is a vnode pointer. 100 */ 101 vm_object_t 102 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 103 vm_ooffset_t offset) 104 { 105 vm_object_t object; 106 struct vnode *vp; 107 108 /* 109 * Pageout to vnode, no can do yet. 110 */ 111 if (handle == NULL) 112 return (NULL); 113 114 /* 115 * XXX hack - This initialization should be put somewhere else. 116 */ 117 if (vnode_pbuf_freecnt < 0) { 118 vnode_pbuf_freecnt = nswbuf / 2 + 1; 119 } 120 121 vp = (struct vnode *) handle; 122 123 /* 124 * Prevent race condition when allocating the object. This 125 * can happen with NFS vnodes since the nfsnode isn't locked. 126 */ 127 while (vp->v_flag & VOLOCK) { 128 vp->v_flag |= VOWANT; 129 tsleep(vp, 0, "vnpobj", 0); 130 } 131 vp->v_flag |= VOLOCK; 132 133 /* 134 * If the object is being terminated, wait for it to 135 * go away. 136 */ 137 while (((object = vp->v_object) != NULL) && 138 (object->flags & OBJ_DEAD)) { 139 tsleep(object, 0, "vadead", 0); 140 } 141 142 if (vp->v_usecount == 0) 143 panic("vnode_pager_alloc: no vnode reference"); 144 145 if (object == NULL) { 146 /* 147 * And an object of the appropriate size 148 */ 149 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 150 object->flags = 0; 151 152 object->un_pager.vnp.vnp_size = size; 153 154 object->handle = handle; 155 vp->v_object = object; 156 vp->v_usecount++; 157 } else { 158 object->ref_count++; 159 vp->v_usecount++; 160 } 161 162 vp->v_flag &= ~VOLOCK; 163 if (vp->v_flag & VOWANT) { 164 vp->v_flag &= ~VOWANT; 165 wakeup(vp); 166 } 167 return (object); 168 } 169 170 static void 171 vnode_pager_dealloc(vm_object_t object) 172 { 173 struct vnode *vp = object->handle; 174 175 if (vp == NULL) 176 panic("vnode_pager_dealloc: pager already dealloced"); 177 178 vm_object_pip_wait(object, "vnpdea"); 179 180 object->handle = NULL; 181 object->type = OBJT_DEAD; 182 vp->v_object = NULL; 183 vp->v_flag &= ~(VTEXT | VOBJBUF); 184 } 185 186 static boolean_t 187 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 188 int *after) 189 { 190 struct vnode *vp = object->handle; 191 daddr_t bn; 192 int err; 193 daddr_t reqblock; 194 int poff; 195 int bsize; 196 int pagesperblock, blocksperpage; 197 198 /* 199 * If no vp or vp is doomed or marked transparent to VM, we do not 200 * have the page. 201 */ 202 if ((vp == NULL) || (vp->v_flag & VRECLAIMED)) 203 return FALSE; 204 205 /* 206 * If filesystem no longer mounted or offset beyond end of file we do 207 * not have the page. 208 */ 209 if ((vp->v_mount == NULL) || 210 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 211 return FALSE; 212 213 bsize = vp->v_mount->mnt_stat.f_iosize; 214 pagesperblock = bsize / PAGE_SIZE; 215 blocksperpage = 0; 216 if (pagesperblock > 0) { 217 reqblock = pindex / pagesperblock; 218 } else { 219 blocksperpage = (PAGE_SIZE / bsize); 220 reqblock = pindex * blocksperpage; 221 } 222 err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 223 after, before); 224 if (err) 225 return TRUE; 226 if ( bn == -1) 227 return FALSE; 228 if (pagesperblock > 0) { 229 poff = pindex - (reqblock * pagesperblock); 230 if (before) { 231 *before *= pagesperblock; 232 *before += poff; 233 } 234 if (after) { 235 int numafter; 236 *after *= pagesperblock; 237 numafter = pagesperblock - (poff + 1); 238 if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 239 numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 240 } 241 *after += numafter; 242 } 243 } else { 244 if (before) { 245 *before /= blocksperpage; 246 } 247 248 if (after) { 249 *after /= blocksperpage; 250 } 251 } 252 return TRUE; 253 } 254 255 /* 256 * Lets the VM system know about a change in size for a file. 257 * We adjust our own internal size and flush any cached pages in 258 * the associated object that are affected by the size change. 259 * 260 * Note: this routine may be invoked as a result of a pager put 261 * operation (possibly at object termination time), so we must be careful. 262 */ 263 void 264 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize) 265 { 266 vm_pindex_t nobjsize; 267 vm_object_t object = vp->v_object; 268 269 if (object == NULL) 270 return; 271 272 /* 273 * Hasn't changed size 274 */ 275 if (nsize == object->un_pager.vnp.vnp_size) 276 return; 277 278 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 279 280 /* 281 * File has shrunk. Toss any cached pages beyond the new EOF. 282 */ 283 if (nsize < object->un_pager.vnp.vnp_size) { 284 vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size); 285 if (nobjsize < object->size) { 286 vm_object_page_remove(object, nobjsize, object->size, 287 FALSE); 288 } 289 /* 290 * This gets rid of garbage at the end of a page that is now 291 * only partially backed by the vnode. Since we are setting 292 * the entire page valid & clean after we are done we have 293 * to be sure that the portion of the page within the file 294 * bounds is already valid. If it isn't then making it 295 * valid would create a corrupt block. 296 */ 297 if (nsize & PAGE_MASK) { 298 vm_offset_t kva; 299 vm_page_t m; 300 301 m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 302 if (m && m->valid) { 303 int base = (int)nsize & PAGE_MASK; 304 int size = PAGE_SIZE - base; 305 struct sf_buf *sf; 306 307 /* 308 * Clear out partial-page garbage in case 309 * the page has been mapped. 310 */ 311 sf = sf_buf_alloc(m, SFB_CPUPRIVATE); 312 kva = sf_buf_kva(sf); 313 bzero((caddr_t)kva + base, size); 314 sf_buf_free(sf); 315 316 /* 317 * XXX work around SMP data integrity race 318 * by unmapping the page from user processes. 319 * The garbage we just cleared may be mapped 320 * to a user process running on another cpu 321 * and this code is not running through normal 322 * I/O channels which handle SMP issues for 323 * us, so unmap page to synchronize all cpus. 324 * 325 * XXX should vm_pager_unmap_page() have 326 * dealt with this? 327 */ 328 vm_page_protect(m, VM_PROT_NONE); 329 330 /* 331 * Clear out partial-page dirty bits. This 332 * has the side effect of setting the valid 333 * bits, but that is ok. There are a bunch 334 * of places in the VM system where we expected 335 * m->dirty == VM_PAGE_BITS_ALL. The file EOF 336 * case is one of them. If the page is still 337 * partially dirty, make it fully dirty. 338 * 339 * note that we do not clear out the valid 340 * bits. This would prevent bogus_page 341 * replacement from working properly. 342 */ 343 vm_page_set_validclean(m, base, size); 344 if (m->dirty != 0) 345 m->dirty = VM_PAGE_BITS_ALL; 346 } 347 } 348 } 349 object->un_pager.vnp.vnp_size = nsize; 350 object->size = nobjsize; 351 } 352 353 void 354 vnode_pager_freepage(vm_page_t m) 355 { 356 vm_page_free(m); 357 } 358 359 /* 360 * calculate the linear (byte) disk address of specified virtual 361 * file address 362 */ 363 static vm_offset_t 364 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, int *run) 365 { 366 int rtaddress; 367 int bsize; 368 daddr_t block; 369 struct vnode *rtvp; 370 int err; 371 daddr_t vblock; 372 int voffset; 373 374 if (address < 0) 375 return -1; 376 377 if (vp->v_mount == NULL) 378 return -1; 379 380 bsize = vp->v_mount->mnt_stat.f_iosize; 381 vblock = address / bsize; 382 voffset = address % bsize; 383 384 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 385 386 if (err || (block == -1)) 387 rtaddress = -1; 388 else { 389 rtaddress = block + voffset / DEV_BSIZE; 390 if( run) { 391 *run += 1; 392 *run *= bsize/PAGE_SIZE; 393 *run -= voffset/PAGE_SIZE; 394 } 395 } 396 397 return rtaddress; 398 } 399 400 /* 401 * interrupt routine for I/O completion 402 */ 403 static void 404 vnode_pager_iodone(struct buf *bp) 405 { 406 bp->b_flags |= B_DONE; 407 wakeup(bp); 408 } 409 410 /* 411 * small block file system vnode pager input 412 */ 413 static int 414 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m) 415 { 416 int i; 417 struct vnode *dp, *vp; 418 struct buf *bp; 419 vm_offset_t kva; 420 struct sf_buf *sf; 421 int fileaddr; 422 vm_offset_t bsize; 423 int error = 0; 424 425 vp = object->handle; 426 if (vp->v_mount == NULL) 427 return VM_PAGER_BAD; 428 429 bsize = vp->v_mount->mnt_stat.f_iosize; 430 431 432 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 433 434 sf = sf_buf_alloc(m, 0); 435 kva = sf_buf_kva(sf); 436 437 for (i = 0; i < PAGE_SIZE / bsize; i++) { 438 vm_ooffset_t address; 439 440 if (vm_page_bits(i * bsize, bsize) & m->valid) 441 continue; 442 443 address = IDX_TO_OFF(m->pindex) + i * bsize; 444 if (address >= object->un_pager.vnp.vnp_size) { 445 fileaddr = -1; 446 } else { 447 fileaddr = vnode_pager_addr(vp, address, NULL); 448 } 449 if (fileaddr != -1) { 450 bp = getpbuf(&vnode_pbuf_freecnt); 451 452 /* build a minimal buffer header */ 453 bp->b_flags = B_READ | B_CALL; 454 bp->b_iodone = vnode_pager_iodone; 455 bp->b_data = (caddr_t) kva + i * bsize; 456 bp->b_blkno = fileaddr; 457 pbgetvp(dp, bp); 458 bp->b_bcount = bsize; 459 bp->b_bufsize = bsize; 460 bp->b_runningbufspace = bp->b_bufsize; 461 runningbufspace += bp->b_runningbufspace; 462 463 /* do the input */ 464 VOP_STRATEGY(bp->b_vp, bp); 465 466 /* we definitely need to be at splvm here */ 467 468 crit_enter(); 469 while ((bp->b_flags & B_DONE) == 0) { 470 tsleep(bp, 0, "vnsrd", 0); 471 } 472 crit_exit(); 473 if ((bp->b_flags & B_ERROR) != 0) 474 error = EIO; 475 476 /* 477 * free the buffer header back to the swap buffer pool 478 */ 479 relpbuf(bp, &vnode_pbuf_freecnt); 480 if (error) 481 break; 482 483 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 484 } else { 485 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 486 bzero((caddr_t) kva + i * bsize, bsize); 487 } 488 } 489 sf_buf_free(sf); 490 pmap_clear_modify(m); 491 vm_page_flag_clear(m, PG_ZERO); 492 if (error) { 493 return VM_PAGER_ERROR; 494 } 495 return VM_PAGER_OK; 496 497 } 498 499 500 /* 501 * old style vnode pager output routine 502 */ 503 static int 504 vnode_pager_input_old(vm_object_t object, vm_page_t m) 505 { 506 struct uio auio; 507 struct iovec aiov; 508 int error; 509 int size; 510 vm_offset_t kva; 511 struct sf_buf *sf; 512 513 error = 0; 514 515 /* 516 * Return failure if beyond current EOF 517 */ 518 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 519 return VM_PAGER_BAD; 520 } else { 521 size = PAGE_SIZE; 522 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 523 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 524 525 /* 526 * Allocate a kernel virtual address and initialize so that 527 * we can use VOP_READ/WRITE routines. 528 */ 529 sf = sf_buf_alloc(m, 0); 530 kva = sf_buf_kva(sf); 531 532 aiov.iov_base = (caddr_t) kva; 533 aiov.iov_len = size; 534 auio.uio_iov = &aiov; 535 auio.uio_iovcnt = 1; 536 auio.uio_offset = IDX_TO_OFF(m->pindex); 537 auio.uio_segflg = UIO_SYSSPACE; 538 auio.uio_rw = UIO_READ; 539 auio.uio_resid = size; 540 auio.uio_td = curthread; 541 542 error = VOP_READ(((struct vnode *)object->handle), 543 &auio, 0, proc0.p_ucred); 544 if (!error) { 545 int count = size - auio.uio_resid; 546 547 if (count == 0) 548 error = EINVAL; 549 else if (count != PAGE_SIZE) 550 bzero((caddr_t) kva + count, PAGE_SIZE - count); 551 } 552 sf_buf_free(sf); 553 } 554 pmap_clear_modify(m); 555 vm_page_undirty(m); 556 vm_page_flag_clear(m, PG_ZERO); 557 if (!error) 558 m->valid = VM_PAGE_BITS_ALL; 559 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 560 } 561 562 /* 563 * generic vnode pager input routine 564 */ 565 566 /* 567 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 568 * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to 569 * vnode_pager_generic_getpages() to implement the previous behaviour. 570 * 571 * All other FS's should use the bypass to get to the local media 572 * backing vp's VOP_GETPAGES. 573 */ 574 static int 575 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage) 576 { 577 int rtval; 578 struct vnode *vp; 579 int bytes = count * PAGE_SIZE; 580 581 vp = object->handle; 582 /* 583 * XXX temporary diagnostic message to help track stale FS code, 584 * Returning EOPNOTSUPP from here may make things unhappy. 585 */ 586 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 587 if (rtval == EOPNOTSUPP) { 588 printf("vnode_pager: *** WARNING *** stale FS getpages\n"); 589 rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage); 590 } 591 return rtval; 592 } 593 594 595 /* 596 * This is now called from local media FS's to operate against their 597 * own vnodes if they fail to implement VOP_GETPAGES. 598 */ 599 int 600 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount, 601 int reqpage) 602 { 603 vm_object_t object; 604 vm_offset_t kva; 605 off_t foff, tfoff, nextoff; 606 int i, size, bsize, first, firstaddr; 607 struct vnode *dp; 608 int runpg; 609 int runend; 610 struct buf *bp; 611 int count; 612 int error = 0; 613 614 object = vp->v_object; 615 count = bytecount / PAGE_SIZE; 616 617 if (vp->v_mount == NULL) 618 return VM_PAGER_BAD; 619 620 bsize = vp->v_mount->mnt_stat.f_iosize; 621 622 /* get the UNDERLYING device for the file with VOP_BMAP() */ 623 624 /* 625 * originally, we did not check for an error return value -- assuming 626 * an fs always has a bmap entry point -- that assumption is wrong!!! 627 */ 628 foff = IDX_TO_OFF(m[reqpage]->pindex); 629 630 /* 631 * if we can't bmap, use old VOP code 632 */ 633 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 634 for (i = 0; i < count; i++) { 635 if (i != reqpage) { 636 vnode_pager_freepage(m[i]); 637 } 638 } 639 mycpu->gd_cnt.v_vnodein++; 640 mycpu->gd_cnt.v_vnodepgsin++; 641 return vnode_pager_input_old(object, m[reqpage]); 642 643 /* 644 * if the blocksize is smaller than a page size, then use 645 * special small filesystem code. NFS sometimes has a small 646 * blocksize, but it can handle large reads itself. 647 */ 648 } else if ((PAGE_SIZE / bsize) > 1 && 649 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 650 for (i = 0; i < count; i++) { 651 if (i != reqpage) { 652 vnode_pager_freepage(m[i]); 653 } 654 } 655 mycpu->gd_cnt.v_vnodein++; 656 mycpu->gd_cnt.v_vnodepgsin++; 657 return vnode_pager_input_smlfs(object, m[reqpage]); 658 } 659 660 /* 661 * If we have a completely valid page available to us, we can 662 * clean up and return. Otherwise we have to re-read the 663 * media. 664 * 665 * Note that this does not work with NFS, so NFS has its own 666 * getpages routine. The problem is that NFS can have partially 667 * valid pages associated with the buffer cache due to the piecemeal 668 * write support. If we were to fall through and re-read the media 669 * as we do here, dirty data could be lost. 670 */ 671 672 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 673 for (i = 0; i < count; i++) { 674 if (i != reqpage) 675 vnode_pager_freepage(m[i]); 676 } 677 return VM_PAGER_OK; 678 } 679 m[reqpage]->valid = 0; 680 681 /* 682 * here on direct device I/O 683 */ 684 685 firstaddr = -1; 686 /* 687 * calculate the run that includes the required page 688 */ 689 for(first = 0, i = 0; i < count; i = runend) { 690 firstaddr = vnode_pager_addr(vp, 691 IDX_TO_OFF(m[i]->pindex), &runpg); 692 if (firstaddr == -1) { 693 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 694 /* XXX no %qd in kernel. */ 695 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx", 696 firstaddr, (u_long)(foff >> 32), 697 (u_long)(u_int32_t)foff, 698 (u_long)(u_int32_t) 699 (object->un_pager.vnp.vnp_size >> 32), 700 (u_long)(u_int32_t) 701 object->un_pager.vnp.vnp_size); 702 } 703 vnode_pager_freepage(m[i]); 704 runend = i + 1; 705 first = runend; 706 continue; 707 } 708 runend = i + runpg; 709 if (runend <= reqpage) { 710 int j; 711 for (j = i; j < runend; j++) { 712 vnode_pager_freepage(m[j]); 713 } 714 } else { 715 if (runpg < (count - first)) { 716 for (i = first + runpg; i < count; i++) 717 vnode_pager_freepage(m[i]); 718 count = first + runpg; 719 } 720 break; 721 } 722 first = runend; 723 } 724 725 /* 726 * the first and last page have been calculated now, move input pages 727 * to be zero based... 728 */ 729 if (first != 0) { 730 for (i = first; i < count; i++) { 731 m[i - first] = m[i]; 732 } 733 count -= first; 734 reqpage -= first; 735 } 736 737 /* 738 * calculate the file virtual address for the transfer 739 */ 740 foff = IDX_TO_OFF(m[0]->pindex); 741 742 /* 743 * calculate the size of the transfer 744 */ 745 size = count * PAGE_SIZE; 746 if ((foff + size) > object->un_pager.vnp.vnp_size) 747 size = object->un_pager.vnp.vnp_size - foff; 748 749 /* 750 * round up physical size for real devices. 751 */ 752 if (dp->v_type == VBLK || dp->v_type == VCHR) { 753 int secmask = dp->v_rdev->si_bsize_phys - 1; 754 KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1)); 755 size = (size + secmask) & ~secmask; 756 } 757 758 bp = getpbuf(&vnode_pbuf_freecnt); 759 kva = (vm_offset_t) bp->b_data; 760 761 /* 762 * and map the pages to be read into the kva 763 */ 764 pmap_qenter(kva, m, count); 765 766 /* build a minimal buffer header */ 767 bp->b_flags = B_READ | B_CALL; 768 bp->b_iodone = vnode_pager_iodone; 769 /* B_PHYS is not set, but it is nice to fill this in */ 770 bp->b_blkno = firstaddr; 771 pbgetvp(dp, bp); 772 bp->b_bcount = size; 773 bp->b_bufsize = size; 774 bp->b_runningbufspace = bp->b_bufsize; 775 runningbufspace += bp->b_runningbufspace; 776 777 mycpu->gd_cnt.v_vnodein++; 778 mycpu->gd_cnt.v_vnodepgsin += count; 779 780 /* do the input */ 781 VOP_STRATEGY(bp->b_vp, bp); 782 783 crit_enter(); 784 /* we definitely need to be at splvm here */ 785 786 while ((bp->b_flags & B_DONE) == 0) { 787 tsleep(bp, 0, "vnread", 0); 788 } 789 crit_exit(); 790 if ((bp->b_flags & B_ERROR) != 0) 791 error = EIO; 792 793 if (!error) { 794 if (size != count * PAGE_SIZE) 795 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 796 } 797 pmap_qremove(kva, count); 798 799 /* 800 * free the buffer header back to the swap buffer pool 801 */ 802 relpbuf(bp, &vnode_pbuf_freecnt); 803 804 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 805 vm_page_t mt; 806 807 nextoff = tfoff + PAGE_SIZE; 808 mt = m[i]; 809 810 if (nextoff <= object->un_pager.vnp.vnp_size) { 811 /* 812 * Read filled up entire page. 813 */ 814 mt->valid = VM_PAGE_BITS_ALL; 815 vm_page_undirty(mt); /* should be an assert? XXX */ 816 pmap_clear_modify(mt); 817 } else { 818 /* 819 * Read did not fill up entire page. Since this 820 * is getpages, the page may be mapped, so we have 821 * to zero the invalid portions of the page even 822 * though we aren't setting them valid. 823 * 824 * Currently we do not set the entire page valid, 825 * we just try to clear the piece that we couldn't 826 * read. 827 */ 828 vm_page_set_validclean(mt, 0, 829 object->un_pager.vnp.vnp_size - tfoff); 830 /* handled by vm_fault now */ 831 /* vm_page_zero_invalid(mt, FALSE); */ 832 } 833 834 vm_page_flag_clear(mt, PG_ZERO); 835 if (i != reqpage) { 836 837 /* 838 * whether or not to leave the page activated is up in 839 * the air, but we should put the page on a page queue 840 * somewhere. (it already is in the object). Result: 841 * It appears that empirical results show that 842 * deactivating pages is best. 843 */ 844 845 /* 846 * just in case someone was asking for this page we 847 * now tell them that it is ok to use 848 */ 849 if (!error) { 850 if (mt->flags & PG_WANTED) 851 vm_page_activate(mt); 852 else 853 vm_page_deactivate(mt); 854 vm_page_wakeup(mt); 855 } else { 856 vnode_pager_freepage(mt); 857 } 858 } 859 } 860 if (error) { 861 printf("vnode_pager_getpages: I/O read error\n"); 862 } 863 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 864 } 865 866 /* 867 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 868 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 869 * vnode_pager_generic_putpages() to implement the previous behaviour. 870 * 871 * All other FS's should use the bypass to get to the local media 872 * backing vp's VOP_PUTPAGES. 873 */ 874 static void 875 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count, 876 boolean_t sync, int *rtvals) 877 { 878 int rtval; 879 struct vnode *vp; 880 int bytes = count * PAGE_SIZE; 881 882 /* 883 * Force synchronous operation if we are extremely low on memory 884 * to prevent a low-memory deadlock. VOP operations often need to 885 * allocate more memory to initiate the I/O ( i.e. do a BMAP 886 * operation ). The swapper handles the case by limiting the amount 887 * of asynchronous I/O, but that sort of solution doesn't scale well 888 * for the vnode pager without a lot of work. 889 * 890 * Also, the backing vnode's iodone routine may not wake the pageout 891 * daemon up. This should be probably be addressed XXX. 892 */ 893 894 if ((vmstats.v_free_count + vmstats.v_cache_count) < vmstats.v_pageout_free_min) 895 sync |= OBJPC_SYNC; 896 897 /* 898 * Call device-specific putpages function 899 */ 900 901 vp = object->handle; 902 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 903 if (rtval == EOPNOTSUPP) { 904 printf("vnode_pager: *** WARNING *** stale FS putpages\n"); 905 rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals); 906 } 907 } 908 909 910 /* 911 * This is now called from local media FS's to operate against their 912 * own vnodes if they fail to implement VOP_PUTPAGES. 913 * 914 * This is typically called indirectly via the pageout daemon and 915 * clustering has already typically occured, so in general we ask the 916 * underlying filesystem to write the data out asynchronously rather 917 * then delayed. 918 */ 919 int 920 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *m, int bytecount, 921 int flags, int *rtvals) 922 { 923 int i; 924 vm_object_t object; 925 int count; 926 927 int maxsize, ncount; 928 vm_ooffset_t poffset; 929 struct uio auio; 930 struct iovec aiov; 931 int error; 932 int ioflags; 933 934 object = vp->v_object; 935 count = bytecount / PAGE_SIZE; 936 937 for (i = 0; i < count; i++) 938 rtvals[i] = VM_PAGER_AGAIN; 939 940 if ((int) m[0]->pindex < 0) { 941 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n", 942 (long)m[0]->pindex, m[0]->dirty); 943 rtvals[0] = VM_PAGER_BAD; 944 return VM_PAGER_BAD; 945 } 946 947 maxsize = count * PAGE_SIZE; 948 ncount = count; 949 950 poffset = IDX_TO_OFF(m[0]->pindex); 951 952 /* 953 * If the page-aligned write is larger then the actual file we 954 * have to invalidate pages occuring beyond the file EOF. However, 955 * there is an edge case where a file may not be page-aligned where 956 * the last page is partially invalid. In this case the filesystem 957 * may not properly clear the dirty bits for the entire page (which 958 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d). 959 * With the page locked we are free to fix-up the dirty bits here. 960 * 961 * We do not under any circumstances truncate the valid bits, as 962 * this will screw up bogus page replacement. 963 */ 964 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 965 if (object->un_pager.vnp.vnp_size > poffset) { 966 int pgoff; 967 968 maxsize = object->un_pager.vnp.vnp_size - poffset; 969 ncount = btoc(maxsize); 970 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) { 971 vm_page_clear_dirty(m[ncount - 1], pgoff, 972 PAGE_SIZE - pgoff); 973 } 974 } else { 975 maxsize = 0; 976 ncount = 0; 977 } 978 if (ncount < count) { 979 for (i = ncount; i < count; i++) { 980 rtvals[i] = VM_PAGER_BAD; 981 } 982 } 983 } 984 985 /* 986 * pageouts are already clustered, use IO_ASYNC to force a bawrite() 987 * rather then a bdwrite() to prevent paging I/O from saturating 988 * the buffer cache. Dummy-up the sequential heuristic to cause 989 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set, 990 * the system decides how to cluster. 991 */ 992 ioflags = IO_VMIO; 993 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) 994 ioflags |= IO_SYNC; 995 else if ((flags & VM_PAGER_CLUSTER_OK) == 0) 996 ioflags |= IO_ASYNC; 997 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 998 ioflags |= IO_SEQMAX << IO_SEQSHIFT; 999 1000 aiov.iov_base = (caddr_t) 0; 1001 aiov.iov_len = maxsize; 1002 auio.uio_iov = &aiov; 1003 auio.uio_iovcnt = 1; 1004 auio.uio_offset = poffset; 1005 auio.uio_segflg = UIO_NOCOPY; 1006 auio.uio_rw = UIO_WRITE; 1007 auio.uio_resid = maxsize; 1008 auio.uio_td = NULL; 1009 error = VOP_WRITE(vp, &auio, ioflags, proc0.p_ucred); 1010 mycpu->gd_cnt.v_vnodeout++; 1011 mycpu->gd_cnt.v_vnodepgsout += ncount; 1012 1013 if (error) { 1014 printf("vnode_pager_putpages: I/O error %d\n", error); 1015 } 1016 if (auio.uio_resid) { 1017 printf("vnode_pager_putpages: residual I/O %d at %lu\n", 1018 auio.uio_resid, (u_long)m[0]->pindex); 1019 } 1020 for (i = 0; i < ncount; i++) { 1021 rtvals[i] = VM_PAGER_OK; 1022 } 1023 return rtvals[0]; 1024 } 1025 1026 struct vnode * 1027 vnode_pager_lock(vm_object_t object) 1028 { 1029 struct thread *td = curthread; /* XXX */ 1030 int error; 1031 1032 for (; object != NULL; object = object->backing_object) { 1033 if (object->type != OBJT_VNODE) 1034 continue; 1035 if (object->flags & OBJ_DEAD) 1036 return NULL; 1037 1038 for (;;) { 1039 struct vnode *vp = object->handle; 1040 error = vget(vp, LK_NOPAUSE | LK_SHARED | 1041 LK_RETRY | LK_CANRECURSE, td); 1042 if (error == 0) { 1043 if (object->handle != vp) { 1044 vput(vp); 1045 continue; 1046 } 1047 return (vp); 1048 } 1049 if ((object->flags & OBJ_DEAD) || 1050 (object->type != OBJT_VNODE)) { 1051 return NULL; 1052 } 1053 printf("vnode_pager_lock: vp %p error %d lockstatus %d, retrying\n", vp, error, lockstatus(&vp->v_lock, td)); 1054 tsleep(object->handle, 0, "vnpgrl", hz); 1055 } 1056 } 1057 return NULL; 1058 } 1059