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