1 /* $NetBSD: genfs_vnops.c,v 1.76 2003/04/23 00:55:19 tls Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.76 2003/04/23 00:55:19 tls Exp $"); 39 40 #include "opt_nfsserver.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/proc.h> 45 #include <sys/kernel.h> 46 #include <sys/mount.h> 47 #include <sys/namei.h> 48 #include <sys/vnode.h> 49 #include <sys/fcntl.h> 50 #include <sys/malloc.h> 51 #include <sys/poll.h> 52 #include <sys/mman.h> 53 #include <sys/file.h> 54 55 #include <miscfs/genfs/genfs.h> 56 #include <miscfs/genfs/genfs_node.h> 57 #include <miscfs/specfs/specdev.h> 58 59 #include <uvm/uvm.h> 60 #include <uvm/uvm_pager.h> 61 62 #ifdef NFSSERVER 63 #include <nfs/rpcv2.h> 64 #include <nfs/nfsproto.h> 65 #include <nfs/nfs.h> 66 #include <nfs/nqnfs.h> 67 #include <nfs/nfs_var.h> 68 #endif 69 70 static __inline void genfs_rel_pages(struct vm_page **, int); 71 static void filt_genfsdetach(struct knote *); 72 static int filt_genfsread(struct knote *, long); 73 static int filt_genfsvnode(struct knote *, long); 74 75 76 #define MAX_READ_AHEAD 16 /* XXXUBC 16 */ 77 int genfs_rapages = MAX_READ_AHEAD; /* # of pages in each chunk of readahead */ 78 int genfs_racount = 2; /* # of page chunks to readahead */ 79 int genfs_raskip = 2; /* # of busy page chunks allowed to skip */ 80 81 int 82 genfs_poll(void *v) 83 { 84 struct vop_poll_args /* { 85 struct vnode *a_vp; 86 int a_events; 87 struct proc *a_p; 88 } */ *ap = v; 89 90 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 91 } 92 93 int 94 genfs_fsync(void *v) 95 { 96 struct vop_fsync_args /* { 97 struct vnode *a_vp; 98 struct ucred *a_cred; 99 int a_flags; 100 off_t offlo; 101 off_t offhi; 102 struct proc *a_p; 103 } */ *ap = v; 104 struct vnode *vp = ap->a_vp; 105 int wait; 106 107 wait = (ap->a_flags & FSYNC_WAIT) != 0; 108 vflushbuf(vp, wait); 109 if ((ap->a_flags & FSYNC_DATAONLY) != 0) 110 return (0); 111 else 112 return (VOP_UPDATE(vp, NULL, NULL, wait ? UPDATE_WAIT : 0)); 113 } 114 115 int 116 genfs_seek(void *v) 117 { 118 struct vop_seek_args /* { 119 struct vnode *a_vp; 120 off_t a_oldoff; 121 off_t a_newoff; 122 struct ucred *a_ucred; 123 } */ *ap = v; 124 125 if (ap->a_newoff < 0) 126 return (EINVAL); 127 128 return (0); 129 } 130 131 int 132 genfs_abortop(void *v) 133 { 134 struct vop_abortop_args /* { 135 struct vnode *a_dvp; 136 struct componentname *a_cnp; 137 } */ *ap = v; 138 139 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) 140 PNBUF_PUT(ap->a_cnp->cn_pnbuf); 141 return (0); 142 } 143 144 int 145 genfs_fcntl(void *v) 146 { 147 struct vop_fcntl_args /* { 148 struct vnode *a_vp; 149 u_int a_command; 150 caddr_t a_data; 151 int a_fflag; 152 struct ucred *a_cred; 153 struct proc *a_p; 154 } */ *ap = v; 155 156 if (ap->a_command == F_SETFL) 157 return (0); 158 else 159 return (EOPNOTSUPP); 160 } 161 162 /*ARGSUSED*/ 163 int 164 genfs_badop(void *v) 165 { 166 167 panic("genfs: bad op"); 168 } 169 170 /*ARGSUSED*/ 171 int 172 genfs_nullop(void *v) 173 { 174 175 return (0); 176 } 177 178 /*ARGSUSED*/ 179 int 180 genfs_einval(void *v) 181 { 182 183 return (EINVAL); 184 } 185 186 /* 187 * Called when an fs doesn't support a particular vop. 188 * This takes care to vrele, vput, or vunlock passed in vnodes. 189 */ 190 int 191 genfs_eopnotsupp(void *v) 192 { 193 struct vop_generic_args /* 194 struct vnodeop_desc *a_desc; 195 / * other random data follows, presumably * / 196 } */ *ap = v; 197 struct vnodeop_desc *desc = ap->a_desc; 198 struct vnode *vp, *vp_last = NULL; 199 int flags, i, j, offset; 200 201 flags = desc->vdesc_flags; 202 for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) { 203 if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET) 204 break; /* stop at end of list */ 205 if ((j = flags & VDESC_VP0_WILLPUT)) { 206 vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap); 207 208 /* Skip if NULL */ 209 if (!vp) 210 continue; 211 212 switch (j) { 213 case VDESC_VP0_WILLPUT: 214 /* Check for dvp == vp cases */ 215 if (vp == vp_last) 216 vrele(vp); 217 else { 218 vput(vp); 219 vp_last = vp; 220 } 221 break; 222 case VDESC_VP0_WILLUNLOCK: 223 VOP_UNLOCK(vp, 0); 224 break; 225 case VDESC_VP0_WILLRELE: 226 vrele(vp); 227 break; 228 } 229 } 230 } 231 232 return (EOPNOTSUPP); 233 } 234 235 /*ARGSUSED*/ 236 int 237 genfs_ebadf(void *v) 238 { 239 240 return (EBADF); 241 } 242 243 /* ARGSUSED */ 244 int 245 genfs_enoioctl(void *v) 246 { 247 248 return (EPASSTHROUGH); 249 } 250 251 252 /* 253 * Eliminate all activity associated with the requested vnode 254 * and with all vnodes aliased to the requested vnode. 255 */ 256 int 257 genfs_revoke(void *v) 258 { 259 struct vop_revoke_args /* { 260 struct vnode *a_vp; 261 int a_flags; 262 } */ *ap = v; 263 struct vnode *vp, *vq; 264 struct proc *p = curproc; /* XXX */ 265 266 #ifdef DIAGNOSTIC 267 if ((ap->a_flags & REVOKEALL) == 0) 268 panic("genfs_revoke: not revokeall"); 269 #endif 270 271 vp = ap->a_vp; 272 simple_lock(&vp->v_interlock); 273 274 if (vp->v_flag & VALIASED) { 275 /* 276 * If a vgone (or vclean) is already in progress, 277 * wait until it is done and return. 278 */ 279 if (vp->v_flag & VXLOCK) { 280 vp->v_flag |= VXWANT; 281 simple_unlock(&vp->v_interlock); 282 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0); 283 return (0); 284 } 285 /* 286 * Ensure that vp will not be vgone'd while we 287 * are eliminating its aliases. 288 */ 289 vp->v_flag |= VXLOCK; 290 simple_unlock(&vp->v_interlock); 291 while (vp->v_flag & VALIASED) { 292 simple_lock(&spechash_slock); 293 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 294 if (vq->v_rdev != vp->v_rdev || 295 vq->v_type != vp->v_type || vp == vq) 296 continue; 297 simple_unlock(&spechash_slock); 298 vgone(vq); 299 break; 300 } 301 if (vq == NULLVP) 302 simple_unlock(&spechash_slock); 303 } 304 /* 305 * Remove the lock so that vgone below will 306 * really eliminate the vnode after which time 307 * vgone will awaken any sleepers. 308 */ 309 simple_lock(&vp->v_interlock); 310 vp->v_flag &= ~VXLOCK; 311 } 312 vgonel(vp, p); 313 return (0); 314 } 315 316 /* 317 * Lock the node. 318 */ 319 int 320 genfs_lock(void *v) 321 { 322 struct vop_lock_args /* { 323 struct vnode *a_vp; 324 int a_flags; 325 } */ *ap = v; 326 struct vnode *vp = ap->a_vp; 327 328 return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock)); 329 } 330 331 /* 332 * Unlock the node. 333 */ 334 int 335 genfs_unlock(void *v) 336 { 337 struct vop_unlock_args /* { 338 struct vnode *a_vp; 339 int a_flags; 340 } */ *ap = v; 341 struct vnode *vp = ap->a_vp; 342 343 return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE, 344 &vp->v_interlock)); 345 } 346 347 /* 348 * Return whether or not the node is locked. 349 */ 350 int 351 genfs_islocked(void *v) 352 { 353 struct vop_islocked_args /* { 354 struct vnode *a_vp; 355 } */ *ap = v; 356 struct vnode *vp = ap->a_vp; 357 358 return (lockstatus(&vp->v_lock)); 359 } 360 361 /* 362 * Stubs to use when there is no locking to be done on the underlying object. 363 */ 364 int 365 genfs_nolock(void *v) 366 { 367 struct vop_lock_args /* { 368 struct vnode *a_vp; 369 int a_flags; 370 struct proc *a_p; 371 } */ *ap = v; 372 373 /* 374 * Since we are not using the lock manager, we must clear 375 * the interlock here. 376 */ 377 if (ap->a_flags & LK_INTERLOCK) 378 simple_unlock(&ap->a_vp->v_interlock); 379 return (0); 380 } 381 382 int 383 genfs_nounlock(void *v) 384 { 385 386 return (0); 387 } 388 389 int 390 genfs_noislocked(void *v) 391 { 392 393 return (0); 394 } 395 396 /* 397 * Local lease check for NFS servers. Just set up args and let 398 * nqsrv_getlease() do the rest. If NFSSERVER is not in the kernel, 399 * this is a null operation. 400 */ 401 int 402 genfs_lease_check(void *v) 403 { 404 #ifdef NFSSERVER 405 struct vop_lease_args /* { 406 struct vnode *a_vp; 407 struct proc *a_p; 408 struct ucred *a_cred; 409 int a_flag; 410 } */ *ap = v; 411 u_int32_t duration = 0; 412 int cache; 413 u_quad_t frev; 414 415 (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag, 416 NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred); 417 return (0); 418 #else 419 return (0); 420 #endif /* NFSSERVER */ 421 } 422 423 int 424 genfs_mmap(void *v) 425 { 426 427 return (0); 428 } 429 430 static __inline void 431 genfs_rel_pages(struct vm_page **pgs, int npages) 432 { 433 int i; 434 435 for (i = 0; i < npages; i++) { 436 struct vm_page *pg = pgs[i]; 437 438 if (pg == NULL) 439 continue; 440 if (pg->flags & PG_FAKE) { 441 pg->flags |= PG_RELEASED; 442 } 443 } 444 uvm_lock_pageq(); 445 uvm_page_unbusy(pgs, npages); 446 uvm_unlock_pageq(); 447 } 448 449 /* 450 * generic VM getpages routine. 451 * Return PG_BUSY pages for the given range, 452 * reading from backing store if necessary. 453 */ 454 455 int 456 genfs_getpages(void *v) 457 { 458 struct vop_getpages_args /* { 459 struct vnode *a_vp; 460 voff_t a_offset; 461 struct vm_page **a_m; 462 int *a_count; 463 int a_centeridx; 464 vm_prot_t a_access_type; 465 int a_advice; 466 int a_flags; 467 } */ *ap = v; 468 469 off_t newsize, diskeof, memeof; 470 off_t offset, origoffset, startoffset, endoffset, raoffset; 471 daddr_t lbn, blkno; 472 int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount; 473 int fs_bshift, fs_bsize, dev_bshift; 474 int flags = ap->a_flags; 475 size_t bytes, iobytes, tailbytes, totalbytes, skipbytes; 476 vaddr_t kva; 477 struct buf *bp, *mbp; 478 struct vnode *vp = ap->a_vp; 479 struct vnode *devvp; 480 struct genfs_node *gp = VTOG(vp); 481 struct uvm_object *uobj = &vp->v_uobj; 482 struct vm_page *pg, *pgs[MAX_READ_AHEAD]; 483 struct ucred *cred = curproc->p_ucred; /* XXXUBC curlwp */ 484 boolean_t async = (flags & PGO_SYNCIO) == 0; 485 boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0; 486 boolean_t sawhole = FALSE; 487 boolean_t overwrite = (flags & PGO_OVERWRITE) != 0; 488 UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist); 489 490 UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d", 491 vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count); 492 493 /* XXXUBC temp limit */ 494 if (*ap->a_count > MAX_READ_AHEAD) { 495 panic("genfs_getpages: too many pages"); 496 } 497 498 error = 0; 499 origoffset = ap->a_offset; 500 orignpages = *ap->a_count; 501 GOP_SIZE(vp, vp->v_size, &diskeof, GOP_SIZE_READ); 502 if (flags & PGO_PASTEOF) { 503 newsize = MAX(vp->v_size, 504 origoffset + (orignpages << PAGE_SHIFT)); 505 GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_READ); 506 } else { 507 memeof = diskeof; 508 } 509 KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages); 510 KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0); 511 KASSERT(orignpages > 0); 512 513 /* 514 * Bounds-check the request. 515 */ 516 517 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) { 518 if ((flags & PGO_LOCKED) == 0) { 519 simple_unlock(&uobj->vmobjlock); 520 } 521 UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x", 522 origoffset, *ap->a_count, memeof,0); 523 return (EINVAL); 524 } 525 526 /* 527 * For PGO_LOCKED requests, just return whatever's in memory. 528 */ 529 530 if (flags & PGO_LOCKED) { 531 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m, 532 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0)); 533 534 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0); 535 } 536 537 /* vnode is VOP_LOCKed, uobj is locked */ 538 539 if (write && (vp->v_flag & VONWORKLST) == 0) { 540 vn_syncer_add_to_worklist(vp, filedelay); 541 } 542 543 /* 544 * find the requested pages and make some simple checks. 545 * leave space in the page array for a whole block. 546 */ 547 548 if (vp->v_type == VREG) { 549 fs_bshift = vp->v_mount->mnt_fs_bshift; 550 dev_bshift = vp->v_mount->mnt_dev_bshift; 551 } else { 552 fs_bshift = DEV_BSHIFT; 553 dev_bshift = DEV_BSHIFT; 554 } 555 fs_bsize = 1 << fs_bshift; 556 557 orignpages = MIN(orignpages, 558 round_page(memeof - origoffset) >> PAGE_SHIFT); 559 npages = orignpages; 560 startoffset = origoffset & ~(fs_bsize - 1); 561 endoffset = round_page((origoffset + (npages << PAGE_SHIFT) + 562 fs_bsize - 1) & ~(fs_bsize - 1)); 563 endoffset = MIN(endoffset, round_page(memeof)); 564 ridx = (origoffset - startoffset) >> PAGE_SHIFT; 565 566 memset(pgs, 0, sizeof(pgs)); 567 UVMHIST_LOG(ubchist, "ridx %d npages %d startoff %ld endoff %ld", 568 ridx, npages, startoffset, endoffset); 569 KASSERT(&pgs[ridx + npages] <= &pgs[MAX_READ_AHEAD]); 570 if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], 571 async ? UFP_NOWAIT : UFP_ALL) != orignpages) { 572 KASSERT(async != 0); 573 genfs_rel_pages(&pgs[ridx], orignpages); 574 simple_unlock(&uobj->vmobjlock); 575 return (EBUSY); 576 } 577 578 /* 579 * if the pages are already resident, just return them. 580 */ 581 582 for (i = 0; i < npages; i++) { 583 struct vm_page *pg = pgs[ridx + i]; 584 585 if ((pg->flags & PG_FAKE) || 586 (write && (pg->flags & PG_RDONLY))) { 587 break; 588 } 589 } 590 if (i == npages) { 591 UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0); 592 raoffset = origoffset + (orignpages << PAGE_SHIFT); 593 npages += ridx; 594 goto raout; 595 } 596 597 /* 598 * if PGO_OVERWRITE is set, don't bother reading the pages. 599 */ 600 601 if (flags & PGO_OVERWRITE) { 602 UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0); 603 604 for (i = 0; i < npages; i++) { 605 struct vm_page *pg = pgs[ridx + i]; 606 607 pg->flags &= ~(PG_RDONLY|PG_CLEAN); 608 } 609 npages += ridx; 610 goto out; 611 } 612 613 /* 614 * the page wasn't resident and we're not overwriting, 615 * so we're going to have to do some i/o. 616 * find any additional pages needed to cover the expanded range. 617 */ 618 619 npages = (endoffset - startoffset) >> PAGE_SHIFT; 620 if (startoffset != origoffset || npages != orignpages) { 621 622 /* 623 * we need to avoid deadlocks caused by locking 624 * additional pages at lower offsets than pages we 625 * already have locked. unlock them all and start over. 626 */ 627 628 genfs_rel_pages(&pgs[ridx], orignpages); 629 memset(pgs, 0, sizeof(pgs)); 630 631 UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x", 632 startoffset, endoffset, 0,0); 633 npgs = npages; 634 if (uvn_findpages(uobj, startoffset, &npgs, pgs, 635 async ? UFP_NOWAIT : UFP_ALL) != npages) { 636 KASSERT(async != 0); 637 genfs_rel_pages(pgs, npages); 638 simple_unlock(&uobj->vmobjlock); 639 return (EBUSY); 640 } 641 } 642 simple_unlock(&uobj->vmobjlock); 643 644 /* 645 * read the desired page(s). 646 */ 647 648 totalbytes = npages << PAGE_SHIFT; 649 bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0)); 650 tailbytes = totalbytes - bytes; 651 skipbytes = 0; 652 653 kva = uvm_pagermapin(pgs, npages, 654 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK); 655 656 s = splbio(); 657 mbp = pool_get(&bufpool, PR_WAITOK); 658 splx(s); 659 BUF_INIT(mbp); 660 mbp->b_bufsize = totalbytes; 661 mbp->b_data = (void *)kva; 662 mbp->b_resid = mbp->b_bcount = bytes; 663 mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0); 664 mbp->b_iodone = (async ? uvm_aio_biodone : 0); 665 mbp->b_vp = vp; 666 667 /* 668 * if EOF is in the middle of the range, zero the part past EOF. 669 * if the page including EOF is not PG_FAKE, skip over it since 670 * in that case it has valid data that we need to preserve. 671 */ 672 673 if (tailbytes > 0) { 674 size_t tailstart = bytes; 675 676 if ((pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE) == 0) { 677 tailstart = round_page(tailstart); 678 tailbytes -= tailstart - bytes; 679 } 680 UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x", 681 kva, tailstart, tailbytes,0); 682 memset((void *)(kva + tailstart), 0, tailbytes); 683 } 684 685 /* 686 * now loop over the pages, reading as needed. 687 */ 688 689 if (write) { 690 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL); 691 } else { 692 lockmgr(&gp->g_glock, LK_SHARED, NULL); 693 } 694 695 bp = NULL; 696 for (offset = startoffset; 697 bytes > 0; 698 offset += iobytes, bytes -= iobytes) { 699 700 /* 701 * skip pages which don't need to be read. 702 */ 703 704 pidx = (offset - startoffset) >> PAGE_SHIFT; 705 while ((pgs[pidx]->flags & (PG_FAKE|PG_RDONLY)) == 0) { 706 size_t b; 707 708 KASSERT((offset & (PAGE_SIZE - 1)) == 0); 709 b = MIN(PAGE_SIZE, bytes); 710 offset += b; 711 bytes -= b; 712 skipbytes += b; 713 pidx++; 714 UVMHIST_LOG(ubchist, "skipping, new offset 0x%x", 715 offset, 0,0,0); 716 if (bytes == 0) { 717 goto loopdone; 718 } 719 } 720 721 /* 722 * bmap the file to find out the blkno to read from and 723 * how much we can read in one i/o. if bmap returns an error, 724 * skip the rest of the top-level i/o. 725 */ 726 727 lbn = offset >> fs_bshift; 728 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run); 729 if (error) { 730 UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n", 731 lbn, error,0,0); 732 skipbytes += bytes; 733 goto loopdone; 734 } 735 736 /* 737 * see how many pages can be read with this i/o. 738 * reduce the i/o size if necessary to avoid 739 * overwriting pages with valid data. 740 */ 741 742 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset, 743 bytes); 744 if (offset + iobytes > round_page(offset)) { 745 pcount = 1; 746 while (pidx + pcount < npages && 747 pgs[pidx + pcount]->flags & PG_FAKE) { 748 pcount++; 749 } 750 iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) - 751 (offset - trunc_page(offset))); 752 } 753 754 /* 755 * if this block isn't allocated, zero it instead of 756 * reading it. if this is a read access, mark the 757 * pages we zeroed PG_RDONLY. 758 */ 759 760 if (blkno < 0) { 761 int holepages = (round_page(offset + iobytes) - 762 trunc_page(offset)) >> PAGE_SHIFT; 763 UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0); 764 765 sawhole = TRUE; 766 memset((char *)kva + (offset - startoffset), 0, 767 iobytes); 768 skipbytes += iobytes; 769 770 for (i = 0; i < holepages; i++) { 771 if (write) { 772 pgs[pidx + i]->flags &= ~PG_CLEAN; 773 } else { 774 pgs[pidx + i]->flags |= PG_RDONLY; 775 } 776 } 777 continue; 778 } 779 780 /* 781 * allocate a sub-buf for this piece of the i/o 782 * (or just use mbp if there's only 1 piece), 783 * and start it going. 784 */ 785 786 if (offset == startoffset && iobytes == bytes) { 787 bp = mbp; 788 } else { 789 s = splbio(); 790 bp = pool_get(&bufpool, PR_WAITOK); 791 splx(s); 792 BUF_INIT(bp); 793 bp->b_data = (char *)kva + offset - startoffset; 794 bp->b_resid = bp->b_bcount = iobytes; 795 bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC; 796 bp->b_iodone = uvm_aio_biodone1; 797 bp->b_vp = vp; 798 bp->b_proc = NULL; 799 } 800 bp->b_lblkno = 0; 801 bp->b_private = mbp; 802 if (devvp->v_type == VBLK) { 803 bp->b_dev = devvp->v_rdev; 804 } 805 806 /* adjust physical blkno for partial blocks */ 807 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >> 808 dev_bshift); 809 810 UVMHIST_LOG(ubchist, 811 "bp %p offset 0x%x bcount 0x%x blkno 0x%x", 812 bp, offset, iobytes, bp->b_blkno); 813 814 VOP_STRATEGY(bp); 815 } 816 817 loopdone: 818 if (skipbytes) { 819 s = splbio(); 820 if (error) { 821 mbp->b_flags |= B_ERROR; 822 mbp->b_error = error; 823 } 824 mbp->b_resid -= skipbytes; 825 if (mbp->b_resid == 0) { 826 biodone(mbp); 827 } 828 splx(s); 829 } 830 831 if (async) { 832 UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0); 833 lockmgr(&gp->g_glock, LK_RELEASE, NULL); 834 return (0); 835 } 836 if (bp != NULL) { 837 error = biowait(mbp); 838 } 839 s = splbio(); 840 pool_put(&bufpool, mbp); 841 splx(s); 842 uvm_pagermapout(kva, npages); 843 raoffset = startoffset + totalbytes; 844 845 /* 846 * if this we encountered a hole then we have to do a little more work. 847 * for read faults, we marked the page PG_RDONLY so that future 848 * write accesses to the page will fault again. 849 * for write faults, we must make sure that the backing store for 850 * the page is completely allocated while the pages are locked. 851 */ 852 853 if (!error && sawhole && write) { 854 for (i = 0; i < npages; i++) { 855 if (pgs[i] == NULL) { 856 continue; 857 } 858 pgs[i]->flags &= ~PG_CLEAN; 859 UVMHIST_LOG(ubchist, "mark dirty pg %p", pgs[i],0,0,0); 860 } 861 error = GOP_ALLOC(vp, startoffset, npages << PAGE_SHIFT, 0, 862 cred); 863 UVMHIST_LOG(ubchist, "gop_alloc off 0x%x/0x%x -> %d", 864 startoffset, npages << PAGE_SHIFT, error,0); 865 } 866 lockmgr(&gp->g_glock, LK_RELEASE, NULL); 867 simple_lock(&uobj->vmobjlock); 868 869 /* 870 * see if we want to start any readahead. 871 * XXXUBC for now, just read the next 128k on 64k boundaries. 872 * this is pretty nonsensical, but it is 50% faster than reading 873 * just the next 64k. 874 */ 875 876 raout: 877 if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 && 878 PAGE_SHIFT <= 16) { 879 off_t rasize; 880 int rapages, err, i, skipped; 881 882 /* XXXUBC temp limit, from above */ 883 rapages = MIN(MIN(1 << (16 - PAGE_SHIFT), MAX_READ_AHEAD), 884 genfs_rapages); 885 rasize = rapages << PAGE_SHIFT; 886 for (i = skipped = 0; i < genfs_racount; i++) { 887 err = VOP_GETPAGES(vp, raoffset, NULL, &rapages, 0, 888 VM_PROT_READ, 0, 0); 889 simple_lock(&uobj->vmobjlock); 890 if (err) { 891 if (err != EBUSY || 892 skipped++ == genfs_raskip) 893 break; 894 } 895 raoffset += rasize; 896 rapages = rasize >> PAGE_SHIFT; 897 } 898 } 899 900 /* 901 * we're almost done! release the pages... 902 * for errors, we free the pages. 903 * otherwise we activate them and mark them as valid and clean. 904 * also, unbusy pages that were not actually requested. 905 */ 906 907 if (error) { 908 for (i = 0; i < npages; i++) { 909 if (pgs[i] == NULL) { 910 continue; 911 } 912 UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x", 913 pgs[i], pgs[i]->flags, 0,0); 914 if (pgs[i]->flags & PG_FAKE) { 915 pgs[i]->flags |= PG_RELEASED; 916 } 917 } 918 uvm_lock_pageq(); 919 uvm_page_unbusy(pgs, npages); 920 uvm_unlock_pageq(); 921 simple_unlock(&uobj->vmobjlock); 922 UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0); 923 return (error); 924 } 925 926 out: 927 UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0); 928 uvm_lock_pageq(); 929 for (i = 0; i < npages; i++) { 930 pg = pgs[i]; 931 if (pg == NULL) { 932 continue; 933 } 934 UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x", 935 pg, pg->flags, 0,0); 936 if (pg->flags & PG_FAKE && !overwrite) { 937 pg->flags &= ~(PG_FAKE); 938 pmap_clear_modify(pgs[i]); 939 } 940 if (write) { 941 pg->flags &= ~(PG_RDONLY); 942 } 943 if (i < ridx || i >= ridx + orignpages || async) { 944 UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x", 945 pg, pg->offset,0,0); 946 if (pg->flags & PG_WANTED) { 947 wakeup(pg); 948 } 949 if (pg->flags & PG_FAKE) { 950 KASSERT(overwrite); 951 uvm_pagezero(pg); 952 } 953 if (pg->flags & PG_RELEASED) { 954 uvm_pagefree(pg); 955 continue; 956 } 957 uvm_pageactivate(pg); 958 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE); 959 UVM_PAGE_OWN(pg, NULL); 960 } 961 } 962 uvm_unlock_pageq(); 963 simple_unlock(&uobj->vmobjlock); 964 if (ap->a_m != NULL) { 965 memcpy(ap->a_m, &pgs[ridx], 966 orignpages * sizeof(struct vm_page *)); 967 } 968 return (0); 969 } 970 971 /* 972 * generic VM putpages routine. 973 * Write the given range of pages to backing store. 974 * 975 * => "offhi == 0" means flush all pages at or after "offlo". 976 * => object should be locked by caller. we may _unlock_ the object 977 * if (and only if) we need to clean a page (PGO_CLEANIT), or 978 * if PGO_SYNCIO is set and there are pages busy. 979 * we return with the object locked. 980 * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O). 981 * thus, a caller might want to unlock higher level resources 982 * (e.g. vm_map) before calling flush. 983 * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, then we will neither 984 * unlock the object nor block. 985 * => if PGO_ALLPAGES is set, then all pages in the object will be processed. 986 * => NOTE: we rely on the fact that the object's memq is a TAILQ and 987 * that new pages are inserted on the tail end of the list. thus, 988 * we can make a complete pass through the object in one go by starting 989 * at the head and working towards the tail (new pages are put in 990 * front of us). 991 * => NOTE: we are allowed to lock the page queues, so the caller 992 * must not be holding the page queue lock. 993 * 994 * note on "cleaning" object and PG_BUSY pages: 995 * this routine is holding the lock on the object. the only time 996 * that it can run into a PG_BUSY page that it does not own is if 997 * some other process has started I/O on the page (e.g. either 998 * a pagein, or a pageout). if the PG_BUSY page is being paged 999 * in, then it can not be dirty (!PG_CLEAN) because no one has 1000 * had a chance to modify it yet. if the PG_BUSY page is being 1001 * paged out then it means that someone else has already started 1002 * cleaning the page for us (how nice!). in this case, if we 1003 * have syncio specified, then after we make our pass through the 1004 * object we need to wait for the other PG_BUSY pages to clear 1005 * off (i.e. we need to do an iosync). also note that once a 1006 * page is PG_BUSY it must stay in its object until it is un-busyed. 1007 * 1008 * note on page traversal: 1009 * we can traverse the pages in an object either by going down the 1010 * linked list in "uobj->memq", or we can go over the address range 1011 * by page doing hash table lookups for each address. depending 1012 * on how many pages are in the object it may be cheaper to do one 1013 * or the other. we set "by_list" to true if we are using memq. 1014 * if the cost of a hash lookup was equal to the cost of the list 1015 * traversal we could compare the number of pages in the start->stop 1016 * range to the total number of pages in the object. however, it 1017 * seems that a hash table lookup is more expensive than the linked 1018 * list traversal, so we multiply the number of pages in the 1019 * range by an estimate of the relatively higher cost of the hash lookup. 1020 */ 1021 1022 int 1023 genfs_putpages(void *v) 1024 { 1025 struct vop_putpages_args /* { 1026 struct vnode *a_vp; 1027 voff_t a_offlo; 1028 voff_t a_offhi; 1029 int a_flags; 1030 } */ *ap = v; 1031 struct vnode *vp = ap->a_vp; 1032 struct uvm_object *uobj = &vp->v_uobj; 1033 struct simplelock *slock = &uobj->vmobjlock; 1034 off_t startoff = ap->a_offlo; 1035 off_t endoff = ap->a_offhi; 1036 off_t off; 1037 int flags = ap->a_flags; 1038 /* Even for strange MAXPHYS, the shift rounds down to a page */ 1039 const int maxpages = MAXPHYS >> PAGE_SHIFT; 1040 int i, s, error, npages, nback; 1041 int freeflag; 1042 struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp; 1043 boolean_t wasclean, by_list, needs_clean, yield; 1044 boolean_t async = (flags & PGO_SYNCIO) == 0; 1045 boolean_t pagedaemon = curproc == uvm.pagedaemon_proc; 1046 struct lwp *l = curlwp ? curlwp : &lwp0; 1047 1048 UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist); 1049 1050 KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)); 1051 KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0); 1052 KASSERT(startoff < endoff || endoff == 0); 1053 1054 UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x", 1055 vp, uobj->uo_npages, startoff, endoff - startoff); 1056 if (uobj->uo_npages == 0) { 1057 s = splbio(); 1058 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL && 1059 (vp->v_flag & VONWORKLST)) { 1060 vp->v_flag &= ~VONWORKLST; 1061 LIST_REMOVE(vp, v_synclist); 1062 } 1063 splx(s); 1064 simple_unlock(slock); 1065 return (0); 1066 } 1067 1068 /* 1069 * the vnode has pages, set up to process the request. 1070 */ 1071 1072 error = 0; 1073 s = splbio(); 1074 simple_lock(&global_v_numoutput_slock); 1075 wasclean = (vp->v_numoutput == 0); 1076 simple_unlock(&global_v_numoutput_slock); 1077 splx(s); 1078 off = startoff; 1079 if (endoff == 0 || flags & PGO_ALLPAGES) { 1080 endoff = trunc_page(LLONG_MAX); 1081 } 1082 by_list = (uobj->uo_npages <= 1083 ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY); 1084 1085 /* 1086 * start the loop. when scanning by list, hold the last page 1087 * in the list before we start. pages allocated after we start 1088 * will be added to the end of the list, so we can stop at the 1089 * current last page. 1090 */ 1091 1092 freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED; 1093 curmp.uobject = uobj; 1094 curmp.offset = (voff_t)-1; 1095 curmp.flags = PG_BUSY; 1096 endmp.uobject = uobj; 1097 endmp.offset = (voff_t)-1; 1098 endmp.flags = PG_BUSY; 1099 if (by_list) { 1100 pg = TAILQ_FIRST(&uobj->memq); 1101 TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq); 1102 PHOLD(l); 1103 } else { 1104 pg = uvm_pagelookup(uobj, off); 1105 } 1106 nextpg = NULL; 1107 while (by_list || off < endoff) { 1108 1109 /* 1110 * if the current page is not interesting, move on to the next. 1111 */ 1112 1113 KASSERT(pg == NULL || pg->uobject == uobj); 1114 KASSERT(pg == NULL || 1115 (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 || 1116 (pg->flags & PG_BUSY) != 0); 1117 if (by_list) { 1118 if (pg == &endmp) { 1119 break; 1120 } 1121 if (pg->offset < startoff || pg->offset >= endoff || 1122 pg->flags & (PG_RELEASED|PG_PAGEOUT)) { 1123 pg = TAILQ_NEXT(pg, listq); 1124 continue; 1125 } 1126 off = pg->offset; 1127 } else if (pg == NULL || 1128 pg->flags & (PG_RELEASED|PG_PAGEOUT)) { 1129 off += PAGE_SIZE; 1130 if (off < endoff) { 1131 pg = uvm_pagelookup(uobj, off); 1132 } 1133 continue; 1134 } 1135 1136 /* 1137 * if the current page needs to be cleaned and it's busy, 1138 * wait for it to become unbusy. 1139 */ 1140 1141 yield = (l->l_cpu->ci_schedstate.spc_flags & 1142 SPCF_SHOULDYIELD) && !pagedaemon; 1143 if (pg->flags & PG_BUSY || yield) { 1144 UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0); 1145 if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) { 1146 UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0); 1147 error = EDEADLK; 1148 break; 1149 } 1150 KASSERT(!pagedaemon); 1151 if (by_list) { 1152 TAILQ_INSERT_BEFORE(pg, &curmp, listq); 1153 UVMHIST_LOG(ubchist, "curmp next %p", 1154 TAILQ_NEXT(&curmp, listq), 0,0,0); 1155 } 1156 if (yield) { 1157 simple_unlock(slock); 1158 preempt(1); 1159 simple_lock(slock); 1160 } else { 1161 pg->flags |= PG_WANTED; 1162 UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0); 1163 simple_lock(slock); 1164 } 1165 if (by_list) { 1166 UVMHIST_LOG(ubchist, "after next %p", 1167 TAILQ_NEXT(&curmp, listq), 0,0,0); 1168 pg = TAILQ_NEXT(&curmp, listq); 1169 TAILQ_REMOVE(&uobj->memq, &curmp, listq); 1170 } else { 1171 pg = uvm_pagelookup(uobj, off); 1172 } 1173 continue; 1174 } 1175 1176 /* 1177 * if we're freeing, remove all mappings of the page now. 1178 * if we're cleaning, check if the page is needs to be cleaned. 1179 */ 1180 1181 if (flags & PGO_FREE) { 1182 pmap_page_protect(pg, VM_PROT_NONE); 1183 } 1184 if (flags & PGO_CLEANIT) { 1185 needs_clean = pmap_clear_modify(pg) || 1186 (pg->flags & PG_CLEAN) == 0; 1187 pg->flags |= PG_CLEAN; 1188 } else { 1189 needs_clean = FALSE; 1190 } 1191 1192 /* 1193 * if we're cleaning, build a cluster. 1194 * the cluster will consist of pages which are currently dirty, 1195 * but they will be returned to us marked clean. 1196 * if not cleaning, just operate on the one page. 1197 */ 1198 1199 if (needs_clean) { 1200 wasclean = FALSE; 1201 memset(pgs, 0, sizeof(pgs)); 1202 pg->flags |= PG_BUSY; 1203 UVM_PAGE_OWN(pg, "genfs_putpages"); 1204 1205 /* 1206 * first look backward. 1207 */ 1208 1209 npages = MIN(maxpages >> 1, off >> PAGE_SHIFT); 1210 nback = npages; 1211 uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0], 1212 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD); 1213 if (nback) { 1214 memmove(&pgs[0], &pgs[npages - nback], 1215 nback * sizeof(pgs[0])); 1216 if (npages - nback < nback) 1217 memset(&pgs[nback], 0, 1218 (npages - nback) * sizeof(pgs[0])); 1219 else 1220 memset(&pgs[npages - nback], 0, 1221 nback * sizeof(pgs[0])); 1222 } 1223 1224 /* 1225 * then plug in our page of interest. 1226 */ 1227 1228 pgs[nback] = pg; 1229 1230 /* 1231 * then look forward to fill in the remaining space in 1232 * the array of pages. 1233 */ 1234 1235 npages = maxpages - nback - 1; 1236 uvn_findpages(uobj, off + PAGE_SIZE, &npages, 1237 &pgs[nback + 1], 1238 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY); 1239 npages += nback + 1; 1240 } else { 1241 pgs[0] = pg; 1242 npages = 1; 1243 nback = 0; 1244 } 1245 1246 /* 1247 * apply FREE or DEACTIVATE options if requested. 1248 */ 1249 1250 if (flags & (PGO_DEACTIVATE|PGO_FREE)) { 1251 uvm_lock_pageq(); 1252 } 1253 for (i = 0; i < npages; i++) { 1254 tpg = pgs[i]; 1255 KASSERT(tpg->uobject == uobj); 1256 if (by_list && tpg == TAILQ_NEXT(pg, listq)) 1257 pg = tpg; 1258 if (tpg->offset < startoff || tpg->offset >= endoff) 1259 continue; 1260 if (flags & PGO_DEACTIVATE && 1261 (tpg->pqflags & PQ_INACTIVE) == 0 && 1262 tpg->wire_count == 0) { 1263 (void) pmap_clear_reference(tpg); 1264 uvm_pagedeactivate(tpg); 1265 } else if (flags & PGO_FREE) { 1266 pmap_page_protect(tpg, VM_PROT_NONE); 1267 if (tpg->flags & PG_BUSY) { 1268 tpg->flags |= freeflag; 1269 if (pagedaemon) { 1270 uvmexp.paging++; 1271 uvm_pagedequeue(tpg); 1272 } 1273 } else { 1274 1275 /* 1276 * ``page is not busy'' 1277 * implies that npages is 1 1278 * and needs_clean is false. 1279 */ 1280 1281 nextpg = TAILQ_NEXT(tpg, listq); 1282 uvm_pagefree(tpg); 1283 } 1284 } 1285 } 1286 if (flags & (PGO_DEACTIVATE|PGO_FREE)) { 1287 uvm_unlock_pageq(); 1288 } 1289 if (needs_clean) { 1290 1291 /* 1292 * start the i/o. if we're traversing by list, 1293 * keep our place in the list with a marker page. 1294 */ 1295 1296 if (by_list) { 1297 TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp, 1298 listq); 1299 } 1300 simple_unlock(slock); 1301 error = GOP_WRITE(vp, pgs, npages, flags); 1302 simple_lock(slock); 1303 if (by_list) { 1304 pg = TAILQ_NEXT(&curmp, listq); 1305 TAILQ_REMOVE(&uobj->memq, &curmp, listq); 1306 } 1307 if (error) { 1308 break; 1309 } 1310 if (by_list) { 1311 continue; 1312 } 1313 } 1314 1315 /* 1316 * find the next page and continue if there was no error. 1317 */ 1318 1319 if (by_list) { 1320 if (nextpg) { 1321 pg = nextpg; 1322 nextpg = NULL; 1323 } else { 1324 pg = TAILQ_NEXT(pg, listq); 1325 } 1326 } else { 1327 off += (npages - nback) << PAGE_SHIFT; 1328 if (off < endoff) { 1329 pg = uvm_pagelookup(uobj, off); 1330 } 1331 } 1332 } 1333 if (by_list) { 1334 TAILQ_REMOVE(&uobj->memq, &endmp, listq); 1335 PRELE(l); 1336 } 1337 1338 /* 1339 * if we're cleaning and there was nothing to clean, 1340 * take us off the syncer list. if we started any i/o 1341 * and we're doing sync i/o, wait for all writes to finish. 1342 */ 1343 1344 s = splbio(); 1345 if ((flags & PGO_CLEANIT) && wasclean && 1346 startoff == 0 && endoff == trunc_page(LLONG_MAX) && 1347 LIST_FIRST(&vp->v_dirtyblkhd) == NULL && 1348 (vp->v_flag & VONWORKLST)) { 1349 vp->v_flag &= ~VONWORKLST; 1350 LIST_REMOVE(vp, v_synclist); 1351 } 1352 splx(s); 1353 if (!wasclean && !async) { 1354 s = splbio(); 1355 /* 1356 * XXX - we want simple_unlock(&global_v_numoutput_slock); 1357 * but the slot in ltsleep() is taken! 1358 * XXX - try to recover from missed wakeups with a timeout.. 1359 * must think of something better. 1360 */ 1361 while (vp->v_numoutput != 0) { 1362 vp->v_flag |= VBWAIT; 1363 UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, FALSE, 1364 "genput2", hz); 1365 simple_lock(slock); 1366 } 1367 splx(s); 1368 } 1369 simple_unlock(&uobj->vmobjlock); 1370 return (error); 1371 } 1372 1373 int 1374 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags) 1375 { 1376 int s, error, run; 1377 int fs_bshift, dev_bshift; 1378 vaddr_t kva; 1379 off_t eof, offset, startoffset; 1380 size_t bytes, iobytes, skipbytes; 1381 daddr_t lbn, blkno; 1382 struct vm_page *pg; 1383 struct buf *mbp, *bp; 1384 struct vnode *devvp; 1385 boolean_t async = (flags & PGO_SYNCIO) == 0; 1386 UVMHIST_FUNC("genfs_gop_write"); UVMHIST_CALLED(ubchist); 1387 1388 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x", 1389 vp, pgs, npages, flags); 1390 1391 GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_WRITE); 1392 if (vp->v_type == VREG) { 1393 fs_bshift = vp->v_mount->mnt_fs_bshift; 1394 dev_bshift = vp->v_mount->mnt_dev_bshift; 1395 } else { 1396 fs_bshift = DEV_BSHIFT; 1397 dev_bshift = DEV_BSHIFT; 1398 } 1399 error = 0; 1400 pg = pgs[0]; 1401 startoffset = pg->offset; 1402 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset); 1403 skipbytes = 0; 1404 KASSERT(bytes != 0); 1405 1406 kva = uvm_pagermapin(pgs, npages, 1407 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK); 1408 1409 s = splbio(); 1410 simple_lock(&global_v_numoutput_slock); 1411 vp->v_numoutput += 2; 1412 simple_unlock(&global_v_numoutput_slock); 1413 mbp = pool_get(&bufpool, PR_WAITOK); 1414 BUF_INIT(mbp); 1415 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x", 1416 vp, mbp, vp->v_numoutput, bytes); 1417 splx(s); 1418 mbp->b_bufsize = npages << PAGE_SHIFT; 1419 mbp->b_data = (void *)kva; 1420 mbp->b_resid = mbp->b_bcount = bytes; 1421 mbp->b_flags = B_BUSY|B_WRITE|B_AGE| (async ? (B_CALL|B_ASYNC) : 0); 1422 mbp->b_iodone = uvm_aio_biodone; 1423 mbp->b_vp = vp; 1424 1425 bp = NULL; 1426 for (offset = startoffset; 1427 bytes > 0; 1428 offset += iobytes, bytes -= iobytes) { 1429 lbn = offset >> fs_bshift; 1430 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run); 1431 if (error) { 1432 UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0); 1433 skipbytes += bytes; 1434 bytes = 0; 1435 break; 1436 } 1437 1438 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset, 1439 bytes); 1440 if (blkno == (daddr_t)-1) { 1441 skipbytes += iobytes; 1442 continue; 1443 } 1444 1445 /* if it's really one i/o, don't make a second buf */ 1446 if (offset == startoffset && iobytes == bytes) { 1447 bp = mbp; 1448 } else { 1449 s = splbio(); 1450 V_INCR_NUMOUTPUT(vp); 1451 bp = pool_get(&bufpool, PR_WAITOK); 1452 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d", 1453 vp, bp, vp->v_numoutput, 0); 1454 splx(s); 1455 BUF_INIT(bp); 1456 bp->b_data = (char *)kva + 1457 (vaddr_t)(offset - pg->offset); 1458 bp->b_resid = bp->b_bcount = iobytes; 1459 bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC; 1460 bp->b_iodone = uvm_aio_biodone1; 1461 bp->b_vp = vp; 1462 } 1463 bp->b_lblkno = 0; 1464 bp->b_private = mbp; 1465 if (devvp->v_type == VBLK) { 1466 bp->b_dev = devvp->v_rdev; 1467 } 1468 1469 /* adjust physical blkno for partial blocks */ 1470 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >> 1471 dev_bshift); 1472 UVMHIST_LOG(ubchist, 1473 "vp %p offset 0x%x bcount 0x%x blkno 0x%x", 1474 vp, offset, bp->b_bcount, bp->b_blkno); 1475 VOP_STRATEGY(bp); 1476 } 1477 if (skipbytes) { 1478 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0); 1479 s = splbio(); 1480 if (error) { 1481 mbp->b_flags |= B_ERROR; 1482 mbp->b_error = error; 1483 } 1484 mbp->b_resid -= skipbytes; 1485 if (mbp->b_resid == 0) { 1486 biodone(mbp); 1487 } 1488 splx(s); 1489 } 1490 if (async) { 1491 UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0); 1492 return (0); 1493 } 1494 UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0); 1495 error = biowait(mbp); 1496 uvm_aio_aiodone(mbp); 1497 UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0); 1498 return (error); 1499 } 1500 1501 /* 1502 * VOP_PUTPAGES() for vnodes which never have pages. 1503 */ 1504 1505 int 1506 genfs_null_putpages(void *v) 1507 { 1508 struct vop_putpages_args /* { 1509 struct vnode *a_vp; 1510 voff_t a_offlo; 1511 voff_t a_offhi; 1512 int a_flags; 1513 } */ *ap = v; 1514 struct vnode *vp = ap->a_vp; 1515 1516 KASSERT(vp->v_uobj.uo_npages == 0); 1517 simple_unlock(&vp->v_interlock); 1518 return (0); 1519 } 1520 1521 void 1522 genfs_node_init(struct vnode *vp, struct genfs_ops *ops) 1523 { 1524 struct genfs_node *gp = VTOG(vp); 1525 1526 lockinit(&gp->g_glock, PINOD, "glock", 0, 0); 1527 gp->g_op = ops; 1528 } 1529 1530 void 1531 genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags) 1532 { 1533 int bsize; 1534 1535 bsize = 1 << vp->v_mount->mnt_fs_bshift; 1536 *eobp = (size + bsize - 1) & ~(bsize - 1); 1537 } 1538 1539 int 1540 genfs_compat_getpages(void *v) 1541 { 1542 struct vop_getpages_args /* { 1543 struct vnode *a_vp; 1544 voff_t a_offset; 1545 struct vm_page **a_m; 1546 int *a_count; 1547 int a_centeridx; 1548 vm_prot_t a_access_type; 1549 int a_advice; 1550 int a_flags; 1551 } */ *ap = v; 1552 1553 off_t origoffset; 1554 struct vnode *vp = ap->a_vp; 1555 struct uvm_object *uobj = &vp->v_uobj; 1556 struct vm_page *pg, **pgs; 1557 vaddr_t kva; 1558 int i, error, orignpages, npages; 1559 struct iovec iov; 1560 struct uio uio; 1561 struct ucred *cred = curproc->p_ucred; 1562 boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0; 1563 1564 error = 0; 1565 origoffset = ap->a_offset; 1566 orignpages = *ap->a_count; 1567 pgs = ap->a_m; 1568 1569 if (write && (vp->v_flag & VONWORKLST) == 0) { 1570 vn_syncer_add_to_worklist(vp, filedelay); 1571 } 1572 if (ap->a_flags & PGO_LOCKED) { 1573 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m, 1574 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0)); 1575 1576 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0); 1577 } 1578 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) { 1579 simple_unlock(&uobj->vmobjlock); 1580 return (EINVAL); 1581 } 1582 npages = orignpages; 1583 uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL); 1584 simple_unlock(&uobj->vmobjlock); 1585 kva = uvm_pagermapin(pgs, npages, 1586 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK); 1587 for (i = 0; i < npages; i++) { 1588 pg = pgs[i]; 1589 if ((pg->flags & PG_FAKE) == 0) { 1590 continue; 1591 } 1592 iov.iov_base = (char *)kva + (i << PAGE_SHIFT); 1593 iov.iov_len = PAGE_SIZE; 1594 uio.uio_iov = &iov; 1595 uio.uio_iovcnt = 1; 1596 uio.uio_offset = origoffset + (i << PAGE_SHIFT); 1597 uio.uio_segflg = UIO_SYSSPACE; 1598 uio.uio_rw = UIO_READ; 1599 uio.uio_resid = PAGE_SIZE; 1600 uio.uio_procp = curproc; 1601 error = VOP_READ(vp, &uio, 0, cred); 1602 if (error) { 1603 break; 1604 } 1605 if (uio.uio_resid) { 1606 memset(iov.iov_base, 0, uio.uio_resid); 1607 } 1608 } 1609 uvm_pagermapout(kva, npages); 1610 simple_lock(&uobj->vmobjlock); 1611 uvm_lock_pageq(); 1612 for (i = 0; i < npages; i++) { 1613 pg = pgs[i]; 1614 if (error && (pg->flags & PG_FAKE) != 0) { 1615 pg->flags |= PG_RELEASED; 1616 } else { 1617 pmap_clear_modify(pg); 1618 uvm_pageactivate(pg); 1619 } 1620 } 1621 if (error) { 1622 uvm_page_unbusy(pgs, npages); 1623 } 1624 uvm_unlock_pageq(); 1625 simple_unlock(&uobj->vmobjlock); 1626 return (error); 1627 } 1628 1629 int 1630 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, 1631 int flags) 1632 { 1633 off_t offset; 1634 struct iovec iov; 1635 struct uio uio; 1636 struct ucred *cred = curproc->p_ucred; 1637 struct buf *bp; 1638 vaddr_t kva; 1639 int s, error; 1640 1641 offset = pgs[0]->offset; 1642 kva = uvm_pagermapin(pgs, npages, 1643 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK); 1644 1645 iov.iov_base = (void *)kva; 1646 iov.iov_len = npages << PAGE_SHIFT; 1647 uio.uio_iov = &iov; 1648 uio.uio_iovcnt = 1; 1649 uio.uio_offset = offset; 1650 uio.uio_segflg = UIO_SYSSPACE; 1651 uio.uio_rw = UIO_WRITE; 1652 uio.uio_resid = npages << PAGE_SHIFT; 1653 uio.uio_procp = curproc; 1654 error = VOP_WRITE(vp, &uio, 0, cred); 1655 1656 s = splbio(); 1657 V_INCR_NUMOUTPUT(vp); 1658 bp = pool_get(&bufpool, PR_WAITOK); 1659 splx(s); 1660 1661 BUF_INIT(bp); 1662 bp->b_flags = B_BUSY | B_WRITE | B_AGE; 1663 bp->b_vp = vp; 1664 bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift; 1665 bp->b_data = (char *)kva; 1666 bp->b_bcount = npages << PAGE_SHIFT; 1667 bp->b_bufsize = npages << PAGE_SHIFT; 1668 bp->b_resid = 0; 1669 if (error) { 1670 bp->b_flags |= B_ERROR; 1671 bp->b_error = error; 1672 } 1673 uvm_aio_aiodone(bp); 1674 return (error); 1675 } 1676 1677 static void 1678 filt_genfsdetach(struct knote *kn) 1679 { 1680 struct vnode *vp = (struct vnode *)kn->kn_hook; 1681 1682 /* XXXLUKEM lock the struct? */ 1683 SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext); 1684 } 1685 1686 static int 1687 filt_genfsread(struct knote *kn, long hint) 1688 { 1689 struct vnode *vp = (struct vnode *)kn->kn_hook; 1690 1691 /* 1692 * filesystem is gone, so set the EOF flag and schedule 1693 * the knote for deletion. 1694 */ 1695 if (hint == NOTE_REVOKE) { 1696 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 1697 return (1); 1698 } 1699 1700 /* XXXLUKEM lock the struct? */ 1701 kn->kn_data = vp->v_size - kn->kn_fp->f_offset; 1702 return (kn->kn_data != 0); 1703 } 1704 1705 static int 1706 filt_genfsvnode(struct knote *kn, long hint) 1707 { 1708 1709 if (kn->kn_sfflags & hint) 1710 kn->kn_fflags |= hint; 1711 if (hint == NOTE_REVOKE) { 1712 kn->kn_flags |= EV_EOF; 1713 return (1); 1714 } 1715 return (kn->kn_fflags != 0); 1716 } 1717 1718 static const struct filterops genfsread_filtops = 1719 { 1, NULL, filt_genfsdetach, filt_genfsread }; 1720 static const struct filterops genfsvnode_filtops = 1721 { 1, NULL, filt_genfsdetach, filt_genfsvnode }; 1722 1723 int 1724 genfs_kqfilter(void *v) 1725 { 1726 struct vop_kqfilter_args /* { 1727 struct vnode *a_vp; 1728 struct knote *a_kn; 1729 } */ *ap = v; 1730 struct vnode *vp; 1731 struct knote *kn; 1732 1733 vp = ap->a_vp; 1734 kn = ap->a_kn; 1735 switch (kn->kn_filter) { 1736 case EVFILT_READ: 1737 kn->kn_fop = &genfsread_filtops; 1738 break; 1739 case EVFILT_VNODE: 1740 kn->kn_fop = &genfsvnode_filtops; 1741 break; 1742 default: 1743 return (1); 1744 } 1745 1746 kn->kn_hook = vp; 1747 1748 /* XXXLUKEM lock the struct? */ 1749 SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext); 1750 1751 return (0); 1752 } 1753