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