Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.37
      1 /*	$NetBSD: genfs_vnops.c,v 1.37 2001/09/15 20:36:38 chs 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 "opt_nfsserver.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/kernel.h>
     43 #include <sys/mount.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/fcntl.h>
     47 #include <sys/malloc.h>
     48 #include <sys/poll.h>
     49 #include <sys/mman.h>
     50 
     51 #include <miscfs/genfs/genfs.h>
     52 #include <miscfs/genfs/genfs_node.h>
     53 #include <miscfs/specfs/specdev.h>
     54 
     55 #include <uvm/uvm.h>
     56 #include <uvm/uvm_pager.h>
     57 
     58 #ifdef NFSSERVER
     59 #include <nfs/rpcv2.h>
     60 #include <nfs/nfsproto.h>
     61 #include <nfs/nfs.h>
     62 #include <nfs/nqnfs.h>
     63 #include <nfs/nfs_var.h>
     64 #endif
     65 
     66 int
     67 genfs_poll(v)
     68 	void *v;
     69 {
     70 	struct vop_poll_args /* {
     71 		struct vnode *a_vp;
     72 		int a_events;
     73 		struct proc *a_p;
     74 	} */ *ap = v;
     75 
     76 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
     77 }
     78 
     79 int
     80 genfs_fsync(v)
     81 	void *v;
     82 {
     83 	struct vop_fsync_args /* {
     84 		struct vnode *a_vp;
     85 		struct ucred *a_cred;
     86 		int a_flags;
     87 		off_t offlo;
     88 		off_t offhi;
     89 		struct proc *a_p;
     90 	} */ *ap = v;
     91 	struct vnode *vp = ap->a_vp;
     92 	int wait;
     93 
     94 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
     95 	vflushbuf(vp, wait);
     96 	if ((ap->a_flags & FSYNC_DATAONLY) != 0)
     97 		return (0);
     98 	else
     99 		return (VOP_UPDATE(vp, NULL, NULL, wait ? UPDATE_WAIT : 0));
    100 }
    101 
    102 int
    103 genfs_seek(v)
    104 	void *v;
    105 {
    106 	struct vop_seek_args /* {
    107 		struct vnode *a_vp;
    108 		off_t a_oldoff;
    109 		off_t a_newoff;
    110 		struct ucred *a_ucred;
    111 	} */ *ap = v;
    112 
    113 	if (ap->a_newoff < 0)
    114 		return (EINVAL);
    115 
    116 	return (0);
    117 }
    118 
    119 int
    120 genfs_abortop(v)
    121 	void *v;
    122 {
    123 	struct vop_abortop_args /* {
    124 		struct vnode *a_dvp;
    125 		struct componentname *a_cnp;
    126 	} */ *ap = v;
    127 
    128 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    129 		PNBUF_PUT(ap->a_cnp->cn_pnbuf);
    130 	return (0);
    131 }
    132 
    133 int
    134 genfs_fcntl(v)
    135 	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(v)
    155 	void *v;
    156 {
    157 
    158 	panic("genfs: bad op");
    159 }
    160 
    161 /*ARGSUSED*/
    162 int
    163 genfs_nullop(v)
    164 	void *v;
    165 {
    166 
    167 	return (0);
    168 }
    169 
    170 /*ARGSUSED*/
    171 int
    172 genfs_einval(v)
    173 	void *v;
    174 {
    175 
    176 	return (EINVAL);
    177 }
    178 
    179 /*ARGSUSED*/
    180 int
    181 genfs_eopnotsupp(v)
    182 	void *v;
    183 {
    184 
    185 	return (EOPNOTSUPP);
    186 }
    187 
    188 /*
    189  * Called when an fs doesn't support a particular vop but the vop needs to
    190  * vrele, vput, or vunlock passed in vnodes.
    191  */
    192 int
    193 genfs_eopnotsupp_rele(v)
    194 	void *v;
    195 {
    196 	struct vop_generic_args /*
    197 		struct vnodeop_desc *a_desc;
    198 		/ * other random data follows, presumably * /
    199 	} */ *ap = v;
    200 	struct vnodeop_desc *desc = ap->a_desc;
    201 	struct vnode *vp;
    202 	int flags, i, j, offset;
    203 
    204 	flags = desc->vdesc_flags;
    205 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    206 		if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    207 			break;	/* stop at end of list */
    208 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    209 			vp = *VOPARG_OFFSETTO(struct vnode**,offset,ap);
    210 			switch (j) {
    211 			case VDESC_VP0_WILLPUT:
    212 				vput(vp);
    213 				break;
    214 			case VDESC_VP0_WILLUNLOCK:
    215 				VOP_UNLOCK(vp, 0);
    216 				break;
    217 			case VDESC_VP0_WILLRELE:
    218 				vrele(vp);
    219 				break;
    220 			}
    221 		}
    222 	}
    223 
    224 	return (EOPNOTSUPP);
    225 }
    226 
    227 /*ARGSUSED*/
    228 int
    229 genfs_ebadf(v)
    230 	void *v;
    231 {
    232 
    233 	return (EBADF);
    234 }
    235 
    236 /* ARGSUSED */
    237 int
    238 genfs_enoioctl(v)
    239 	void *v;
    240 {
    241 
    242 	return (ENOTTY);
    243 }
    244 
    245 
    246 /*
    247  * Eliminate all activity associated with the requested vnode
    248  * and with all vnodes aliased to the requested vnode.
    249  */
    250 int
    251 genfs_revoke(v)
    252 	void *v;
    253 {
    254 	struct vop_revoke_args /* {
    255 		struct vnode *a_vp;
    256 		int a_flags;
    257 	} */ *ap = v;
    258 	struct vnode *vp, *vq;
    259 	struct proc *p = curproc;	/* XXX */
    260 
    261 #ifdef DIAGNOSTIC
    262 	if ((ap->a_flags & REVOKEALL) == 0)
    263 		panic("genfs_revoke: not revokeall");
    264 #endif
    265 
    266 	vp = ap->a_vp;
    267 	simple_lock(&vp->v_interlock);
    268 
    269 	if (vp->v_flag & VALIASED) {
    270 		/*
    271 		 * If a vgone (or vclean) is already in progress,
    272 		 * wait until it is done and return.
    273 		 */
    274 		if (vp->v_flag & VXLOCK) {
    275 			vp->v_flag |= VXWANT;
    276 			simple_unlock(&vp->v_interlock);
    277 			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
    278 			return (0);
    279 		}
    280 		/*
    281 		 * Ensure that vp will not be vgone'd while we
    282 		 * are eliminating its aliases.
    283 		 */
    284 		vp->v_flag |= VXLOCK;
    285 		simple_unlock(&vp->v_interlock);
    286 		while (vp->v_flag & VALIASED) {
    287 			simple_lock(&spechash_slock);
    288 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    289 				if (vq->v_rdev != vp->v_rdev ||
    290 				    vq->v_type != vp->v_type || vp == vq)
    291 					continue;
    292 				simple_unlock(&spechash_slock);
    293 				vgone(vq);
    294 				break;
    295 			}
    296 			if (vq == NULLVP)
    297 				simple_unlock(&spechash_slock);
    298 		}
    299 		/*
    300 		 * Remove the lock so that vgone below will
    301 		 * really eliminate the vnode after which time
    302 		 * vgone will awaken any sleepers.
    303 		 */
    304 		simple_lock(&vp->v_interlock);
    305 		vp->v_flag &= ~VXLOCK;
    306 	}
    307 	vgonel(vp, p);
    308 	return (0);
    309 }
    310 
    311 /*
    312  * Lock the node.
    313  */
    314 int
    315 genfs_lock(v)
    316 	void *v;
    317 {
    318 	struct vop_lock_args /* {
    319 		struct vnode *a_vp;
    320 		int a_flags;
    321 	} */ *ap = v;
    322 	struct vnode *vp = ap->a_vp;
    323 
    324 	return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock));
    325 }
    326 
    327 /*
    328  * Unlock the node.
    329  */
    330 int
    331 genfs_unlock(v)
    332 	void *v;
    333 {
    334 	struct vop_unlock_args /* {
    335 		struct vnode *a_vp;
    336 		int a_flags;
    337 	} */ *ap = v;
    338 	struct vnode *vp = ap->a_vp;
    339 
    340 	return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
    341 		&vp->v_interlock));
    342 }
    343 
    344 /*
    345  * Return whether or not the node is locked.
    346  */
    347 int
    348 genfs_islocked(v)
    349 	void *v;
    350 {
    351 	struct vop_islocked_args /* {
    352 		struct vnode *a_vp;
    353 	} */ *ap = v;
    354 	struct vnode *vp = ap->a_vp;
    355 
    356 	return (lockstatus(&vp->v_lock));
    357 }
    358 
    359 /*
    360  * Stubs to use when there is no locking to be done on the underlying object.
    361  */
    362 int
    363 genfs_nolock(v)
    364 	void *v;
    365 {
    366 	struct vop_lock_args /* {
    367 		struct vnode *a_vp;
    368 		int a_flags;
    369 		struct proc *a_p;
    370 	} */ *ap = v;
    371 
    372 	/*
    373 	 * Since we are not using the lock manager, we must clear
    374 	 * the interlock here.
    375 	 */
    376 	if (ap->a_flags & LK_INTERLOCK)
    377 		simple_unlock(&ap->a_vp->v_interlock);
    378 	return (0);
    379 }
    380 
    381 int
    382 genfs_nounlock(v)
    383 	void *v;
    384 {
    385 	return (0);
    386 }
    387 
    388 int
    389 genfs_noislocked(v)
    390 	void *v;
    391 {
    392 	return (0);
    393 }
    394 
    395 /*
    396  * Local lease check for NFS servers.  Just set up args and let
    397  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    398  * this is a null operation.
    399  */
    400 int
    401 genfs_lease_check(v)
    402 	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(v)
    425 	void *v;
    426 {
    427 	return 0;
    428 }
    429 
    430 /*
    431  * generic VM getpages routine.
    432  * Return PG_BUSY pages for the given range,
    433  * reading from backing store if necessary.
    434  */
    435 
    436 int
    437 genfs_getpages(v)
    438 	void *v;
    439 {
    440 	struct vop_getpages_args /* {
    441 		struct vnode *a_vp;
    442 		voff_t a_offset;
    443 		struct vm_page **a_m;
    444 		int *a_count;
    445 		int a_centeridx;
    446 		vm_prot_t a_access_type;
    447 		int a_advice;
    448 		int a_flags;
    449 	} */ *ap = v;
    450 
    451 	off_t newsize, diskeof, memeof;
    452 	off_t offset, origoffset, startoffset, endoffset, raoffset;
    453 	daddr_t lbn, blkno;
    454 	int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
    455 	int fs_bshift, fs_bsize, dev_bshift;
    456 	int flags = ap->a_flags;
    457 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    458 	vaddr_t kva;
    459 	struct buf *bp, *mbp;
    460 	struct vnode *vp = ap->a_vp;
    461 	struct vnode *devvp;
    462 	struct genfs_node *gp = VTOG(vp);
    463 	struct uvm_object *uobj = &vp->v_uobj;
    464 	struct vm_page *pg, *pgs[16];			/* XXXUBC 16 */
    465 	struct ucred *cred = curproc->p_ucred;		/* XXXUBC curproc */
    466 	boolean_t async = (flags & PGO_SYNCIO) == 0;
    467 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
    468 	boolean_t sawhole = FALSE;
    469 	boolean_t overwrite = (flags & PGO_OVERWRITE) != 0;
    470 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
    471 
    472 	UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d",
    473 		    vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
    474 
    475 	/* XXXUBC temp limit */
    476 	if (*ap->a_count > 16) {
    477 		panic("genfs_getpages: too many pages");
    478 	}
    479 
    480 	error = 0;
    481 	origoffset = ap->a_offset;
    482 	orignpages = *ap->a_count;
    483 	GOP_SIZE(vp, vp->v_size, &diskeof);
    484 	if (flags & PGO_PASTEOF) {
    485 		newsize = MAX(vp->v_size,
    486 			      origoffset + (orignpages << PAGE_SHIFT));
    487 		GOP_SIZE(vp, newsize, &memeof);
    488 	} else {
    489 		memeof = diskeof;
    490 	}
    491 	KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
    492 	KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
    493 	KASSERT(orignpages > 0);
    494 
    495 	/*
    496 	 * Bounds-check the request.
    497 	 */
    498 
    499 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
    500 		if ((flags & PGO_LOCKED) == 0) {
    501 			simple_unlock(&uobj->vmobjlock);
    502 		}
    503 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
    504 			    origoffset, *ap->a_count, memeof,0);
    505 		return EINVAL;
    506 	}
    507 
    508 	/*
    509 	 * For PGO_LOCKED requests, just return whatever's in memory.
    510 	 */
    511 
    512 	if (flags & PGO_LOCKED) {
    513 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
    514 			      UFP_NOWAIT|UFP_NOALLOC|UFP_NORDONLY);
    515 
    516 		return ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
    517 	}
    518 
    519 	/* vnode is VOP_LOCKed, uobj is locked */
    520 
    521 	if (write && (vp->v_flag & VONWORKLST) == 0) {
    522 		vn_syncer_add_to_worklist(vp, filedelay);
    523 	}
    524 
    525 	/*
    526 	 * find the requested pages and make some simple checks.
    527 	 * leave space in the page array for a whole block.
    528 	 */
    529 
    530 	if (vp->v_type == VREG) {
    531 		fs_bshift = vp->v_mount->mnt_fs_bshift;
    532 		dev_bshift = vp->v_mount->mnt_dev_bshift;
    533 	} else {
    534 		fs_bshift = DEV_BSHIFT;
    535 		dev_bshift = DEV_BSHIFT;
    536 	}
    537 	fs_bsize = 1 << fs_bshift;
    538 
    539 	orignpages = MIN(orignpages,
    540 	    round_page(memeof - origoffset) >> PAGE_SHIFT);
    541 	npages = orignpages;
    542 	startoffset = origoffset & ~(fs_bsize - 1);
    543 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
    544 				+ fs_bsize - 1) & ~(fs_bsize - 1));
    545 	endoffset = MIN(endoffset, round_page(memeof));
    546 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    547 
    548 	memset(pgs, 0, sizeof(pgs));
    549 	uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
    550 
    551 	/*
    552 	 * if the pages are already resident, just return them.
    553 	 */
    554 
    555 	for (i = 0; i < npages; i++) {
    556 		struct vm_page *pg = pgs[ridx + i];
    557 
    558 		if ((pg->flags & PG_FAKE) ||
    559 		    (write && (pg->flags & PG_RDONLY))) {
    560 			break;
    561 		}
    562 	}
    563 	if (i == npages) {
    564 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    565 		raoffset = origoffset + (orignpages << PAGE_SHIFT);
    566 		npages += ridx;
    567 		goto raout;
    568 	}
    569 
    570 	/*
    571 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    572 	 */
    573 
    574 	if (flags & PGO_OVERWRITE) {
    575 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    576 
    577 		for (i = 0; i < npages; i++) {
    578 			struct vm_page *pg = pgs[ridx + i];
    579 
    580 			pg->flags &= ~(PG_RDONLY|PG_CLEAN);
    581 		}
    582 		npages += ridx;
    583 		goto out;
    584 	}
    585 
    586 	/*
    587 	 * the page wasn't resident and we're not overwriting,
    588 	 * so we're going to have to do some i/o.
    589 	 * find any additional pages needed to cover the expanded range.
    590 	 */
    591 
    592 	npages = (endoffset - startoffset) >> PAGE_SHIFT;
    593 	if (startoffset != origoffset || npages != orignpages) {
    594 
    595 		/*
    596 		 * we need to avoid deadlocks caused by locking
    597 		 * additional pages at lower offsets than pages we
    598 		 * already have locked.  unlock them all and start over.
    599 		 */
    600 
    601 		for (i = 0; i < orignpages; i++) {
    602 			struct vm_page *pg = pgs[ridx + i];
    603 
    604 			if (pg->flags & PG_FAKE) {
    605 				pg->flags |= PG_RELEASED;
    606 			}
    607 		}
    608 		uvm_page_unbusy(&pgs[ridx], orignpages);
    609 		memset(pgs, 0, sizeof(pgs));
    610 
    611 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
    612 			    startoffset, endoffset, 0,0);
    613 		npgs = npages;
    614 		uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
    615 	}
    616 	simple_unlock(&uobj->vmobjlock);
    617 
    618 	/*
    619 	 * read the desired page(s).
    620 	 */
    621 
    622 	totalbytes = npages << PAGE_SHIFT;
    623 	bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
    624 	tailbytes = totalbytes - bytes;
    625 	skipbytes = 0;
    626 
    627 	kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
    628 			     UVMPAGER_MAPIN_READ);
    629 
    630 	s = splbio();
    631 	mbp = pool_get(&bufpool, PR_WAITOK);
    632 	splx(s);
    633 	mbp->b_bufsize = totalbytes;
    634 	mbp->b_data = (void *)kva;
    635 	mbp->b_resid = mbp->b_bcount = bytes;
    636 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL : 0);
    637 	mbp->b_iodone = (async ? uvm_aio_biodone : 0);
    638 	mbp->b_vp = vp;
    639 	LIST_INIT(&mbp->b_dep);
    640 
    641 	/*
    642 	 * if EOF is in the middle of the range, zero the part past EOF.
    643 	 */
    644 
    645 	if (tailbytes > 0) {
    646 		UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x",
    647 			    kva, bytes, tailbytes,0);
    648 		memset((void *)(kva + bytes), 0, tailbytes);
    649 	}
    650 
    651 	/*
    652 	 * now loop over the pages, reading as needed.
    653 	 */
    654 
    655 	if (write) {
    656 		lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    657 	} else {
    658 		lockmgr(&gp->g_glock, LK_SHARED, NULL);
    659 	}
    660 
    661 	bp = NULL;
    662 	for (offset = startoffset;
    663 	     bytes > 0;
    664 	     offset += iobytes, bytes -= iobytes) {
    665 
    666 		/*
    667 		 * skip pages which don't need to be read.
    668 		 */
    669 
    670 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    671 		while ((pgs[pidx]->flags & (PG_FAKE|PG_RDONLY)) == 0) {
    672 			size_t b;
    673 
    674 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    675 			b = MIN(PAGE_SIZE, bytes);
    676 			offset += b;
    677 			bytes -= b;
    678 			skipbytes += b;
    679 			pidx++;
    680 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    681 				    offset, 0,0,0);
    682 			if (bytes == 0) {
    683 				goto loopdone;
    684 			}
    685 		}
    686 
    687 		/*
    688 		 * bmap the file to find out the blkno to read from and
    689 		 * how much we can read in one i/o.  if bmap returns an error,
    690 		 * skip the rest of the top-level i/o.
    691 		 */
    692 
    693 		lbn = offset >> fs_bshift;
    694 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
    695 		if (error) {
    696 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    697 				    lbn, error,0,0);
    698 			skipbytes += bytes;
    699 			goto loopdone;
    700 		}
    701 
    702 		/*
    703 		 * see how many pages can be read with this i/o.
    704 		 * reduce the i/o size if necessary to avoid
    705 		 * overwriting pages with valid data.
    706 		 */
    707 
    708 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    709 		    bytes);
    710 		if (offset + iobytes > round_page(offset)) {
    711 			pcount = 1;
    712 			while (pidx + pcount < npages &&
    713 			       pgs[pidx + pcount]->flags & PG_FAKE) {
    714 				pcount++;
    715 			}
    716 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    717 				      (offset - trunc_page(offset)));
    718 		}
    719 
    720 		/*
    721 		 * if this block isn't allocated, zero it instead of reading it.
    722 		 * if this is a read access, mark the pages we zeroed PG_RDONLY.
    723 		 */
    724 
    725 		if (blkno < 0) {
    726 			int holepages = (round_page(offset + iobytes) -
    727 					 trunc_page(offset)) >> PAGE_SHIFT;
    728 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    729 
    730 			sawhole = TRUE;
    731 			memset((char *)kva + (offset - startoffset), 0,
    732 			       iobytes);
    733 			skipbytes += iobytes;
    734 
    735 			for (i = 0; i < holepages; i++) {
    736 				if (write) {
    737 					pgs[pidx + i]->flags &= ~PG_CLEAN;
    738 				} else {
    739 					pgs[pidx + i]->flags |= PG_RDONLY;
    740 				}
    741 			}
    742 			continue;
    743 		}
    744 
    745 		/*
    746 		 * allocate a sub-buf for this piece of the i/o
    747 		 * (or just use mbp if there's only 1 piece),
    748 		 * and start it going.
    749 		 */
    750 
    751 		if (offset == startoffset && iobytes == bytes) {
    752 			bp = mbp;
    753 		} else {
    754 			s = splbio();
    755 			bp = pool_get(&bufpool, PR_WAITOK);
    756 			splx(s);
    757 			bp->b_data = (char *)kva + offset - startoffset;
    758 			bp->b_resid = bp->b_bcount = iobytes;
    759 			bp->b_flags = B_BUSY|B_READ|B_CALL;
    760 			bp->b_iodone = uvm_aio_biodone1;
    761 			bp->b_vp = vp;
    762 			bp->b_proc = NULL;
    763 			LIST_INIT(&bp->b_dep);
    764 		}
    765 		bp->b_lblkno = 0;
    766 		bp->b_private = mbp;
    767 		if (devvp->v_type == VBLK) {
    768 			bp->b_dev = devvp->v_rdev;
    769 		}
    770 
    771 		/* adjust physical blkno for partial blocks */
    772 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    773 				       dev_bshift);
    774 
    775 		UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    776 			    bp, offset, iobytes, bp->b_blkno);
    777 
    778 		VOP_STRATEGY(bp);
    779 	}
    780 
    781 loopdone:
    782 	if (skipbytes) {
    783 		s = splbio();
    784 		if (error) {
    785 			mbp->b_flags |= B_ERROR;
    786 			mbp->b_error = error;
    787 		}
    788 		mbp->b_resid -= skipbytes;
    789 		if (mbp->b_resid == 0) {
    790 			biodone(mbp);
    791 		}
    792 		splx(s);
    793 	}
    794 
    795 	if (async) {
    796 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
    797 		lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    798 		return 0;
    799 	}
    800 	if (bp != NULL) {
    801 		error = biowait(mbp);
    802 	}
    803 	s = splbio();
    804 	pool_put(&bufpool, mbp);
    805 	splx(s);
    806 	uvm_pagermapout(kva, npages);
    807 	raoffset = startoffset + totalbytes;
    808 
    809 	/*
    810 	 * if this we encountered a hole then we have to do a little more work.
    811 	 * for read faults, we marked the page PG_RDONLY so that future
    812 	 * write accesses to the page will fault again.
    813 	 * for write faults, we must make sure that the backing store for
    814 	 * the page is completely allocated while the pages are locked.
    815 	 */
    816 
    817 	if (!error && sawhole && write) {
    818 		for (i = 0; i < npages; i++) {
    819 			if (pgs[i] == NULL) {
    820 				continue;
    821 			}
    822 			pgs[i]->flags &= ~PG_CLEAN;
    823 			UVMHIST_LOG(ubchist, "mark dirty pg %p", pgs[i],0,0,0);
    824 		}
    825 		error = GOP_ALLOC(vp, startoffset, npages << PAGE_SHIFT, 0,
    826 				  cred);
    827 		UVMHIST_LOG(ubchist, "gop_alloc off 0x%x/0x%x -> %d",
    828 		    startoffset, npages << PAGE_SHIFT, error,0);
    829 	}
    830 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    831 	simple_lock(&uobj->vmobjlock);
    832 
    833 	/*
    834 	 * see if we want to start any readahead.
    835 	 * XXXUBC for now, just read the next 128k on 64k boundaries.
    836 	 * this is pretty nonsensical, but it is 50% faster than reading
    837 	 * just the next 64k.
    838 	 */
    839 
    840 raout:
    841 	if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 &&
    842 	    PAGE_SHIFT <= 16) {
    843 		int racount;
    844 
    845 		racount = 1 << (16 - PAGE_SHIFT);
    846 		(void) VOP_GETPAGES(vp, raoffset, NULL, &racount, 0,
    847 				    VM_PROT_READ, 0, 0);
    848 		simple_lock(&uobj->vmobjlock);
    849 
    850 		racount = 1 << (16 - PAGE_SHIFT);
    851 		(void) VOP_GETPAGES(vp, raoffset + 0x10000, 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(v)
    980 	void *v;
    981 {
    982 	struct vop_putpages_args /* {
    983 		struct vnode *a_vp;
    984 		voff_t a_offlo;
    985 		voff_t a_offhi;
    986 		int a_flags;
    987 	} */ *ap = v;
    988 	struct vnode *vp = ap->a_vp;
    989 	struct uvm_object *uobj = &vp->v_uobj;
    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;
    999 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1000 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
   1001 
   1002 	KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
   1003 	KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
   1004 	KASSERT(startoff < endoff || endoff == 0);
   1005 
   1006 	UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
   1007 	    vp, uobj->uo_npages, startoff, endoff - startoff);
   1008 	if (uobj->uo_npages == 0) {
   1009 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
   1010 		    (vp->v_flag & VONWORKLST)) {
   1011 			vp->v_flag &= ~VONWORKLST;
   1012 			LIST_REMOVE(vp, v_synclist);
   1013 		}
   1014 		simple_unlock(&uobj->vmobjlock);
   1015 		return 0;
   1016 	}
   1017 
   1018 	/*
   1019 	 * the vnode has pages, set up to process the request.
   1020 	 */
   1021 
   1022 	error = 0;
   1023 	wasclean = TRUE;
   1024 	off = startoff;
   1025 	if (endoff == 0 || flags & PGO_ALLPAGES) {
   1026 		endoff = trunc_page(LLONG_MAX);
   1027 	}
   1028 	by_list = (uobj->uo_npages <=
   1029 	    ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
   1030 
   1031 	/*
   1032 	 * start the loop.  when scanning by list, hold the last page
   1033 	 * in the list before we start.  pages allocated after we start
   1034 	 * will be added to the end of the list, so we can stop at the
   1035 	 * current last page.
   1036 	 */
   1037 
   1038 	freeflag = (curproc == uvm.pagedaemon_proc) ? PG_PAGEOUT : PG_RELEASED;
   1039 	curmp.uobject = uobj;
   1040 	curmp.offset = (voff_t)-1;
   1041 	curmp.flags = PG_BUSY;
   1042 	endmp.uobject = uobj;
   1043 	endmp.offset = (voff_t)-1;
   1044 	endmp.flags = PG_BUSY;
   1045 	if (by_list) {
   1046 		pg = TAILQ_FIRST(&uobj->memq);
   1047 		TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq);
   1048 		PHOLD(curproc);
   1049 	} else {
   1050 		pg = uvm_pagelookup(uobj, off);
   1051 	}
   1052 	nextpg = NULL;
   1053 	while (by_list || off < endoff) {
   1054 
   1055 		/*
   1056 		 * if the current page is not interesting, move on to the next.
   1057 		 */
   1058 
   1059 		KASSERT(pg == NULL || pg->uobject == uobj);
   1060 		KASSERT(pg == NULL ||
   1061 			(pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
   1062 			(pg->flags & PG_BUSY) != 0);
   1063 		if (by_list) {
   1064 			if (pg == &endmp) {
   1065 				break;
   1066 			}
   1067 			if (pg->offset < startoff || pg->offset >= endoff ||
   1068 			    pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1069 				pg = TAILQ_NEXT(pg, listq);
   1070 				continue;
   1071 			}
   1072 			off = pg->offset;
   1073 		} else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1074 			off += PAGE_SIZE;
   1075 			if (off < endoff) {
   1076 				pg = uvm_pagelookup(uobj, off);
   1077 			}
   1078 			continue;
   1079 		}
   1080 
   1081 		/*
   1082 		 * if the current page needs to be cleaned and it's busy,
   1083 		 * wait for it to become unbusy.
   1084 		 */
   1085 
   1086 		if (flags & PGO_FREE) {
   1087 			pmap_page_protect(pg, VM_PROT_NONE);
   1088 		}
   1089 		if (flags & PGO_CLEANIT) {
   1090 			needs_clean = pmap_clear_modify(pg) ||
   1091 				(pg->flags & PG_CLEAN) == 0;
   1092 			pg->flags |= PG_CLEAN;
   1093 		} else {
   1094 			needs_clean = FALSE;
   1095 		}
   1096 		if (needs_clean && pg->flags & PG_BUSY) {
   1097 			KASSERT(curproc != uvm.pagedaemon_proc);
   1098 			UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
   1099 			if (by_list) {
   1100 				TAILQ_INSERT_BEFORE(pg, &curmp, listq);
   1101 				UVMHIST_LOG(ubchist, "curmp next %p",
   1102 					    TAILQ_NEXT(&curmp, listq), 0,0,0);
   1103 			}
   1104 			pg->flags |= PG_WANTED;
   1105 			pg->flags &= ~PG_CLEAN;
   1106 			UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
   1107 			    "genput", 0);
   1108 			simple_lock(&uobj->vmobjlock);
   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 cleaning, build a cluster.
   1122 		 * the cluster will consist of pages which are currently dirty,
   1123 		 * but they will be returned to us marked clean.
   1124 		 * if not cleaning, just operate on the one page.
   1125 		 */
   1126 
   1127 		if (needs_clean) {
   1128 			wasclean = FALSE;
   1129 			memset(pgs, 0, sizeof(pgs));
   1130 			pg->flags |= PG_BUSY;
   1131 			UVM_PAGE_OWN(pg, "genfs_putpages");
   1132 
   1133 			/*
   1134 			 * first look backward.
   1135 			 */
   1136 
   1137 			npages = MIN(n >> 1, off >> PAGE_SHIFT);
   1138 			nback = npages;
   1139 			uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
   1140 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
   1141 			if (nback) {
   1142 				memmove(&pgs[0], &pgs[npages - nback],
   1143 				    nback * sizeof(pgs[0]));
   1144 			}
   1145 			n -= nback;
   1146 
   1147 			/*
   1148 			 * then plug in our page of interest.
   1149 			 */
   1150 
   1151 			pgs[nback] = pg;
   1152 
   1153 			/*
   1154 			 * then look forward to fill in the remaining space in
   1155 			 * the array of pages.
   1156 			 */
   1157 
   1158 			npages = MIN(n, (endoff - off) >> PAGE_SHIFT) - 1;
   1159 			uvn_findpages(uobj, off + PAGE_SIZE, &npages,
   1160 			    &pgs[nback + 1],
   1161 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
   1162 			npages += nback + 1;
   1163 		} else {
   1164 			pgs[0] = pg;
   1165 			npages = 1;
   1166 		}
   1167 
   1168 		/*
   1169 		 * apply FREE or DEACTIVATE options if requested.
   1170 		 */
   1171 
   1172 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1173 			uvm_lock_pageq();
   1174 		}
   1175 		for (i = 0; i < npages; i++) {
   1176 			tpg = pgs[i];
   1177 			KASSERT(tpg->uobject == uobj);
   1178 			if (flags & PGO_DEACTIVATE &&
   1179 			    (tpg->pqflags & PQ_INACTIVE) == 0 &&
   1180 			    tpg->wire_count == 0) {
   1181 				(void) pmap_clear_reference(tpg);
   1182 				uvm_pagedeactivate(tpg);
   1183 			} else if (flags & PGO_FREE) {
   1184 				pmap_page_protect(tpg, VM_PROT_NONE);
   1185 				if (tpg->flags & PG_BUSY) {
   1186 					tpg->flags |= freeflag;
   1187 					if (freeflag == PG_PAGEOUT) {
   1188 						uvmexp.paging++;
   1189 						uvm_pagedequeue(tpg);
   1190 					}
   1191 				} else {
   1192 					nextpg = TAILQ_NEXT(tpg, listq);
   1193 					uvm_pagefree(tpg);
   1194 				}
   1195 			}
   1196 		}
   1197 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1198 			uvm_unlock_pageq();
   1199 		}
   1200 		if (needs_clean) {
   1201 
   1202 			/*
   1203 			 * start the i/o.  if we're traversing by list,
   1204 			 * keep our place in the list with a marker page.
   1205 			 */
   1206 
   1207 			if (by_list) {
   1208 				TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
   1209 				    listq);
   1210 			}
   1211 			simple_unlock(&uobj->vmobjlock);
   1212 			error = GOP_WRITE(vp, pgs, npages, flags);
   1213 			simple_lock(&uobj->vmobjlock);
   1214 			if (by_list) {
   1215 				pg = TAILQ_NEXT(&curmp, listq);
   1216 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   1217 			}
   1218 			if (error == ENOMEM) {
   1219 				for (i = 0; i < npages; i++) {
   1220 					tpg = pgs[i];
   1221 					if (tpg->flags & PG_PAGEOUT) {
   1222 						tpg->flags &= ~PG_PAGEOUT;
   1223 						uvmexp.paging--;
   1224 					}
   1225 					tpg->flags &= ~PG_CLEAN;
   1226 					uvm_pageactivate(tpg);
   1227 				}
   1228 				uvm_page_unbusy(pgs, npages);
   1229 			}
   1230 			if (error) {
   1231 				break;
   1232 			}
   1233 			if (by_list) {
   1234 				continue;
   1235 			}
   1236 		}
   1237 
   1238 		/*
   1239 		 * find the next page and continue if there was no error.
   1240 		 */
   1241 
   1242 		if (by_list) {
   1243 			if (nextpg) {
   1244 				pg = nextpg;
   1245 				nextpg = NULL;
   1246 			} else {
   1247 				pg = TAILQ_NEXT(pg, listq);
   1248 			}
   1249 		} else {
   1250 			off += PAGE_SIZE;
   1251 			if (off < endoff) {
   1252 				pg = uvm_pagelookup(uobj, off);
   1253 			}
   1254 		}
   1255 	}
   1256 	if (by_list) {
   1257 		TAILQ_REMOVE(&uobj->memq, &endmp, listq);
   1258 		PRELE(curproc);
   1259 	}
   1260 
   1261 	/*
   1262 	 * if we're cleaning and there was nothing to clean,
   1263 	 * take us off the syncer list.  if we started any i/o
   1264 	 * and we're doing sync i/o, wait for all writes to finish.
   1265 	 */
   1266 
   1267 	if ((flags & PGO_CLEANIT) && wasclean &&
   1268 	    startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
   1269 	    LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
   1270 	    (vp->v_flag & VONWORKLST)) {
   1271 		vp->v_flag &= ~VONWORKLST;
   1272 		LIST_REMOVE(vp, v_synclist);
   1273 	}
   1274 	if (!wasclean && !async) {
   1275 		s = splbio();
   1276 		while (vp->v_numoutput != 0) {
   1277 			vp->v_flag |= VBWAIT;
   1278 			UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, &uobj->vmobjlock,
   1279 					    FALSE, "genput2",0);
   1280 			simple_lock(&uobj->vmobjlock);
   1281 		}
   1282 		splx(s);
   1283 	}
   1284 	simple_unlock(&uobj->vmobjlock);
   1285 	return error;
   1286 }
   1287 
   1288 int
   1289 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
   1290 {
   1291 	int s, error, run;
   1292 	int fs_bshift, dev_bshift;
   1293 	vaddr_t kva;
   1294 	off_t eof, offset, startoffset;
   1295 	size_t bytes, iobytes, skipbytes;
   1296 	daddr_t lbn, blkno;
   1297 	struct vm_page *pg;
   1298 	struct buf *mbp, *bp;
   1299 	struct vnode *devvp;
   1300 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1301 	UVMHIST_FUNC("genfs_do_putpages"); UVMHIST_CALLED(ubchist);
   1302 
   1303 	UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
   1304 	    vp, pgs, npages, flags);
   1305 
   1306 	GOP_SIZE(vp, vp->v_size, &eof);
   1307 	if (vp->v_type == VREG) {
   1308 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1309 		dev_bshift = vp->v_mount->mnt_dev_bshift;
   1310 	} else {
   1311 		fs_bshift = DEV_BSHIFT;
   1312 		dev_bshift = DEV_BSHIFT;
   1313 	}
   1314 	error = 0;
   1315 	pg = pgs[0];
   1316 	startoffset = pg->offset;
   1317 	bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   1318 	skipbytes = 0;
   1319 	KASSERT(bytes != 0);
   1320 
   1321 	kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
   1322 			     UVMPAGER_MAPIN_WAITOK);
   1323 
   1324 	s = splbio();
   1325 	vp->v_numoutput += 2;
   1326 	mbp = pool_get(&bufpool, PR_WAITOK);
   1327 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1328 		    vp, mbp, vp->v_numoutput, bytes);
   1329 	splx(s);
   1330 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1331 	mbp->b_data = (void *)kva;
   1332 	mbp->b_resid = mbp->b_bcount = bytes;
   1333 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE| (async ? B_CALL : 0);
   1334 	mbp->b_iodone = uvm_aio_biodone;
   1335 	mbp->b_vp = vp;
   1336 	LIST_INIT(&mbp->b_dep);
   1337 
   1338 	bp = NULL;
   1339 	for (offset = startoffset;
   1340 	     bytes > 0;
   1341 	     offset += iobytes, bytes -= iobytes) {
   1342 		lbn = offset >> fs_bshift;
   1343 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
   1344 		if (error) {
   1345 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
   1346 			skipbytes += bytes;
   1347 			bytes = 0;
   1348 			break;
   1349 		}
   1350 
   1351 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1352 		    bytes);
   1353 		if (blkno == (daddr_t)-1) {
   1354 			skipbytes += iobytes;
   1355 			continue;
   1356 		}
   1357 
   1358 		/* if it's really one i/o, don't make a second buf */
   1359 		if (offset == startoffset && iobytes == bytes) {
   1360 			bp = mbp;
   1361 		} else {
   1362 			s = splbio();
   1363 			vp->v_numoutput++;
   1364 			bp = pool_get(&bufpool, PR_WAITOK);
   1365 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1366 				    vp, bp, vp->v_numoutput, 0);
   1367 			splx(s);
   1368 			bp->b_data = (char *)kva +
   1369 				(vaddr_t)(offset - pg->offset);
   1370 			bp->b_resid = bp->b_bcount = iobytes;
   1371 			bp->b_flags = B_BUSY|B_WRITE|B_CALL;
   1372 			bp->b_iodone = uvm_aio_biodone1;
   1373 			bp->b_vp = vp;
   1374 			LIST_INIT(&bp->b_dep);
   1375 		}
   1376 		bp->b_lblkno = 0;
   1377 		bp->b_private = mbp;
   1378 		if (devvp->v_type == VBLK) {
   1379 			bp->b_dev = devvp->v_rdev;
   1380 		}
   1381 
   1382 		/* adjust physical blkno for partial blocks */
   1383 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1384 				       dev_bshift);
   1385 		UVMHIST_LOG(ubchist, "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1386 			    vp, offset, bp->b_bcount, bp->b_blkno);
   1387 		VOP_STRATEGY(bp);
   1388 	}
   1389 	if (skipbytes) {
   1390 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   1391 		s = splbio();
   1392 		if (error) {
   1393 			mbp->b_flags |= B_ERROR;
   1394 			mbp->b_error = error;
   1395 		}
   1396 		mbp->b_resid -= skipbytes;
   1397 		if (mbp->b_resid == 0) {
   1398 			biodone(mbp);
   1399 		}
   1400 		splx(s);
   1401 	}
   1402 	if (async) {
   1403 		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
   1404 		return 0;
   1405 	}
   1406 	UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
   1407 	error = biowait(mbp);
   1408 	uvm_aio_aiodone(mbp);
   1409 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
   1410 	return error;
   1411 }
   1412 
   1413 void
   1414 genfs_node_init(struct vnode *vp, struct genfs_ops *ops)
   1415 {
   1416 	struct genfs_node *gp = VTOG(vp);
   1417 
   1418 	lockinit(&gp->g_glock, PINOD, "glock", 0, 0);
   1419 	gp->g_op = ops;
   1420 }
   1421 
   1422 void
   1423 genfs_size(struct vnode *vp, off_t size, off_t *eobp)
   1424 {
   1425 	int bsize;
   1426 
   1427 	bsize = 1 << vp->v_mount->mnt_fs_bshift;
   1428 	*eobp = (size + bsize - 1) & ~(bsize - 1);
   1429 }
   1430