Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.73
      1 /*	$NetBSD: genfs_vnops.c,v 1.73 2003/02/25 20:35:38 thorpej 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.73 2003/02/25 20:35:38 thorpej 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, GOP_SIZE_READ);
    499 	if (flags & PGO_PASTEOF) {
    500 		newsize = MAX(vp->v_size,
    501 		    origoffset + (orignpages << PAGE_SHIFT));
    502 		GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_READ);
    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 	BUF_INIT(mbp);
    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 
    664 	/*
    665 	 * if EOF is in the middle of the range, zero the part past EOF.
    666 	 * if the page including EOF is not PG_FAKE, skip over it since
    667 	 * in that case it has valid data that we need to preserve.
    668 	 */
    669 
    670 	if (tailbytes > 0) {
    671 		size_t tailstart = bytes;
    672 
    673 		if ((pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE) == 0) {
    674 			tailstart = round_page(tailstart);
    675 			tailbytes -= tailstart - bytes;
    676 		}
    677 		UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x",
    678 		    kva, tailstart, tailbytes,0);
    679 		memset((void *)(kva + tailstart), 0, tailbytes);
    680 	}
    681 
    682 	/*
    683 	 * now loop over the pages, reading as needed.
    684 	 */
    685 
    686 	if (write) {
    687 		lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    688 	} else {
    689 		lockmgr(&gp->g_glock, LK_SHARED, NULL);
    690 	}
    691 
    692 	bp = NULL;
    693 	for (offset = startoffset;
    694 	    bytes > 0;
    695 	    offset += iobytes, bytes -= iobytes) {
    696 
    697 		/*
    698 		 * skip pages which don't need to be read.
    699 		 */
    700 
    701 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    702 		while ((pgs[pidx]->flags & (PG_FAKE|PG_RDONLY)) == 0) {
    703 			size_t b;
    704 
    705 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    706 			b = MIN(PAGE_SIZE, bytes);
    707 			offset += b;
    708 			bytes -= b;
    709 			skipbytes += b;
    710 			pidx++;
    711 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    712 			    offset, 0,0,0);
    713 			if (bytes == 0) {
    714 				goto loopdone;
    715 			}
    716 		}
    717 
    718 		/*
    719 		 * bmap the file to find out the blkno to read from and
    720 		 * how much we can read in one i/o.  if bmap returns an error,
    721 		 * skip the rest of the top-level i/o.
    722 		 */
    723 
    724 		lbn = offset >> fs_bshift;
    725 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
    726 		if (error) {
    727 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    728 			    lbn, error,0,0);
    729 			skipbytes += bytes;
    730 			goto loopdone;
    731 		}
    732 
    733 		/*
    734 		 * see how many pages can be read with this i/o.
    735 		 * reduce the i/o size if necessary to avoid
    736 		 * overwriting pages with valid data.
    737 		 */
    738 
    739 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    740 		    bytes);
    741 		if (offset + iobytes > round_page(offset)) {
    742 			pcount = 1;
    743 			while (pidx + pcount < npages &&
    744 			    pgs[pidx + pcount]->flags & PG_FAKE) {
    745 				pcount++;
    746 			}
    747 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    748 			    (offset - trunc_page(offset)));
    749 		}
    750 
    751 		/*
    752 		 * if this block isn't allocated, zero it instead of
    753 		 * reading it.  if this is a read access, mark the
    754 		 * pages we zeroed PG_RDONLY.
    755 		 */
    756 
    757 		if (blkno < 0) {
    758 			int holepages = (round_page(offset + iobytes) -
    759 			    trunc_page(offset)) >> PAGE_SHIFT;
    760 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    761 
    762 			sawhole = TRUE;
    763 			memset((char *)kva + (offset - startoffset), 0,
    764 			    iobytes);
    765 			skipbytes += iobytes;
    766 
    767 			for (i = 0; i < holepages; i++) {
    768 				if (write) {
    769 					pgs[pidx + i]->flags &= ~PG_CLEAN;
    770 				} else {
    771 					pgs[pidx + i]->flags |= PG_RDONLY;
    772 				}
    773 			}
    774 			continue;
    775 		}
    776 
    777 		/*
    778 		 * allocate a sub-buf for this piece of the i/o
    779 		 * (or just use mbp if there's only 1 piece),
    780 		 * and start it going.
    781 		 */
    782 
    783 		if (offset == startoffset && iobytes == bytes) {
    784 			bp = mbp;
    785 		} else {
    786 			s = splbio();
    787 			bp = pool_get(&bufpool, PR_WAITOK);
    788 			splx(s);
    789 			BUF_INIT(bp);
    790 			bp->b_data = (char *)kva + offset - startoffset;
    791 			bp->b_resid = bp->b_bcount = iobytes;
    792 			bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
    793 			bp->b_iodone = uvm_aio_biodone1;
    794 			bp->b_vp = vp;
    795 			bp->b_proc = NULL;
    796 		}
    797 		bp->b_lblkno = 0;
    798 		bp->b_private = mbp;
    799 		if (devvp->v_type == VBLK) {
    800 			bp->b_dev = devvp->v_rdev;
    801 		}
    802 
    803 		/* adjust physical blkno for partial blocks */
    804 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    805 		    dev_bshift);
    806 
    807 		UVMHIST_LOG(ubchist,
    808 		    "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    809 		    bp, offset, iobytes, bp->b_blkno);
    810 
    811 		VOP_STRATEGY(bp);
    812 	}
    813 
    814 loopdone:
    815 	if (skipbytes) {
    816 		s = splbio();
    817 		if (error) {
    818 			mbp->b_flags |= B_ERROR;
    819 			mbp->b_error = error;
    820 		}
    821 		mbp->b_resid -= skipbytes;
    822 		if (mbp->b_resid == 0) {
    823 			biodone(mbp);
    824 		}
    825 		splx(s);
    826 	}
    827 
    828 	if (async) {
    829 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
    830 		lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    831 		return (0);
    832 	}
    833 	if (bp != NULL) {
    834 		error = biowait(mbp);
    835 	}
    836 	s = splbio();
    837 	pool_put(&bufpool, mbp);
    838 	splx(s);
    839 	uvm_pagermapout(kva, npages);
    840 	raoffset = startoffset + totalbytes;
    841 
    842 	/*
    843 	 * if this we encountered a hole then we have to do a little more work.
    844 	 * for read faults, we marked the page PG_RDONLY so that future
    845 	 * write accesses to the page will fault again.
    846 	 * for write faults, we must make sure that the backing store for
    847 	 * the page is completely allocated while the pages are locked.
    848 	 */
    849 
    850 	if (!error && sawhole && write) {
    851 		for (i = 0; i < npages; i++) {
    852 			if (pgs[i] == NULL) {
    853 				continue;
    854 			}
    855 			pgs[i]->flags &= ~PG_CLEAN;
    856 			UVMHIST_LOG(ubchist, "mark dirty pg %p", pgs[i],0,0,0);
    857 		}
    858 		error = GOP_ALLOC(vp, startoffset, npages << PAGE_SHIFT, 0,
    859 		    cred);
    860 		UVMHIST_LOG(ubchist, "gop_alloc off 0x%x/0x%x -> %d",
    861 		    startoffset, npages << PAGE_SHIFT, error,0);
    862 	}
    863 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    864 	simple_lock(&uobj->vmobjlock);
    865 
    866 	/*
    867 	 * see if we want to start any readahead.
    868 	 * XXXUBC for now, just read the next 128k on 64k boundaries.
    869 	 * this is pretty nonsensical, but it is 50% faster than reading
    870 	 * just the next 64k.
    871 	 */
    872 
    873 raout:
    874 	if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 &&
    875 	    PAGE_SHIFT <= 16) {
    876 		off_t rasize;
    877 		int rapages, err, i, skipped;
    878 
    879 		/* XXXUBC temp limit, from above */
    880 		rapages = MIN(MIN(1 << (16 - PAGE_SHIFT), MAX_READ_AHEAD),
    881 		    genfs_rapages);
    882 		rasize = rapages << PAGE_SHIFT;
    883 		for (i = skipped = 0; i < genfs_racount; i++) {
    884 			err = VOP_GETPAGES(vp, raoffset, NULL, &rapages, 0,
    885 			    VM_PROT_READ, 0, 0);
    886 			simple_lock(&uobj->vmobjlock);
    887 			if (err) {
    888 				if (err != EBUSY ||
    889 				    skipped++ == genfs_raskip)
    890 					break;
    891 			}
    892 			raoffset += rasize;
    893 			rapages = rasize >> PAGE_SHIFT;
    894 		}
    895 	}
    896 
    897 	/*
    898 	 * we're almost done!  release the pages...
    899 	 * for errors, we free the pages.
    900 	 * otherwise we activate them and mark them as valid and clean.
    901 	 * also, unbusy pages that were not actually requested.
    902 	 */
    903 
    904 	if (error) {
    905 		for (i = 0; i < npages; i++) {
    906 			if (pgs[i] == NULL) {
    907 				continue;
    908 			}
    909 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    910 			    pgs[i], pgs[i]->flags, 0,0);
    911 			if (pgs[i]->flags & PG_FAKE) {
    912 				pgs[i]->flags |= PG_RELEASED;
    913 			}
    914 		}
    915 		uvm_lock_pageq();
    916 		uvm_page_unbusy(pgs, npages);
    917 		uvm_unlock_pageq();
    918 		simple_unlock(&uobj->vmobjlock);
    919 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
    920 		return (error);
    921 	}
    922 
    923 out:
    924 	UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
    925 	uvm_lock_pageq();
    926 	for (i = 0; i < npages; i++) {
    927 		pg = pgs[i];
    928 		if (pg == NULL) {
    929 			continue;
    930 		}
    931 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    932 		    pg, pg->flags, 0,0);
    933 		if (pg->flags & PG_FAKE && !overwrite) {
    934 			pg->flags &= ~(PG_FAKE);
    935 			pmap_clear_modify(pgs[i]);
    936 		}
    937 		if (write) {
    938 			pg->flags &= ~(PG_RDONLY);
    939 		}
    940 		if (i < ridx || i >= ridx + orignpages || async) {
    941 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
    942 			    pg, pg->offset,0,0);
    943 			if (pg->flags & PG_WANTED) {
    944 				wakeup(pg);
    945 			}
    946 			if (pg->flags & PG_FAKE) {
    947 				KASSERT(overwrite);
    948 				uvm_pagezero(pg);
    949 			}
    950 			if (pg->flags & PG_RELEASED) {
    951 				uvm_pagefree(pg);
    952 				continue;
    953 			}
    954 			uvm_pageactivate(pg);
    955 			pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
    956 			UVM_PAGE_OWN(pg, NULL);
    957 		}
    958 	}
    959 	uvm_unlock_pageq();
    960 	simple_unlock(&uobj->vmobjlock);
    961 	if (ap->a_m != NULL) {
    962 		memcpy(ap->a_m, &pgs[ridx],
    963 		    orignpages * sizeof(struct vm_page *));
    964 	}
    965 	return (0);
    966 }
    967 
    968 /*
    969  * generic VM putpages routine.
    970  * Write the given range of pages to backing store.
    971  *
    972  * => "offhi == 0" means flush all pages at or after "offlo".
    973  * => object should be locked by caller.   we may _unlock_ the object
    974  *	if (and only if) we need to clean a page (PGO_CLEANIT), or
    975  *	if PGO_SYNCIO is set and there are pages busy.
    976  *	we return with the object locked.
    977  * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
    978  *	thus, a caller might want to unlock higher level resources
    979  *	(e.g. vm_map) before calling flush.
    980  * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, then we will neither
    981  *	unlock the object nor block.
    982  * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
    983  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    984  *	that new pages are inserted on the tail end of the list.   thus,
    985  *	we can make a complete pass through the object in one go by starting
    986  *	at the head and working towards the tail (new pages are put in
    987  *	front of us).
    988  * => NOTE: we are allowed to lock the page queues, so the caller
    989  *	must not be holding the page queue lock.
    990  *
    991  * note on "cleaning" object and PG_BUSY pages:
    992  *	this routine is holding the lock on the object.   the only time
    993  *	that it can run into a PG_BUSY page that it does not own is if
    994  *	some other process has started I/O on the page (e.g. either
    995  *	a pagein, or a pageout).    if the PG_BUSY page is being paged
    996  *	in, then it can not be dirty (!PG_CLEAN) because no one has
    997  *	had a chance to modify it yet.    if the PG_BUSY page is being
    998  *	paged out then it means that someone else has already started
    999  *	cleaning the page for us (how nice!).    in this case, if we
   1000  *	have syncio specified, then after we make our pass through the
   1001  *	object we need to wait for the other PG_BUSY pages to clear
   1002  *	off (i.e. we need to do an iosync).   also note that once a
   1003  *	page is PG_BUSY it must stay in its object until it is un-busyed.
   1004  *
   1005  * note on page traversal:
   1006  *	we can traverse the pages in an object either by going down the
   1007  *	linked list in "uobj->memq", or we can go over the address range
   1008  *	by page doing hash table lookups for each address.    depending
   1009  *	on how many pages are in the object it may be cheaper to do one
   1010  *	or the other.   we set "by_list" to true if we are using memq.
   1011  *	if the cost of a hash lookup was equal to the cost of the list
   1012  *	traversal we could compare the number of pages in the start->stop
   1013  *	range to the total number of pages in the object.   however, it
   1014  *	seems that a hash table lookup is more expensive than the linked
   1015  *	list traversal, so we multiply the number of pages in the
   1016  *	range by an estimate of the relatively higher cost of the hash lookup.
   1017  */
   1018 
   1019 int
   1020 genfs_putpages(void *v)
   1021 {
   1022 	struct vop_putpages_args /* {
   1023 		struct vnode *a_vp;
   1024 		voff_t a_offlo;
   1025 		voff_t a_offhi;
   1026 		int a_flags;
   1027 	} */ *ap = v;
   1028 	struct vnode *vp = ap->a_vp;
   1029 	struct uvm_object *uobj = &vp->v_uobj;
   1030 	struct simplelock *slock = &uobj->vmobjlock;
   1031 	off_t startoff = ap->a_offlo;
   1032 	off_t endoff = ap->a_offhi;
   1033 	off_t off;
   1034 	int flags = ap->a_flags;
   1035 	const int maxpages = MAXBSIZE >> PAGE_SHIFT;
   1036 	int i, s, error, npages, nback;
   1037 	int freeflag;
   1038 	struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp;
   1039 	boolean_t wasclean, by_list, needs_clean, yield;
   1040 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1041 	boolean_t pagedaemon = curproc == uvm.pagedaemon_proc;
   1042 	struct lwp *l = curlwp ? curlwp : &lwp0;
   1043 
   1044 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
   1045 
   1046 	KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
   1047 	KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
   1048 	KASSERT(startoff < endoff || endoff == 0);
   1049 
   1050 	UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
   1051 	    vp, uobj->uo_npages, startoff, endoff - startoff);
   1052 	if (uobj->uo_npages == 0) {
   1053 		s = splbio();
   1054 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
   1055 		    (vp->v_flag & VONWORKLST)) {
   1056 			vp->v_flag &= ~VONWORKLST;
   1057 			LIST_REMOVE(vp, v_synclist);
   1058 		}
   1059 		splx(s);
   1060 		simple_unlock(slock);
   1061 		return (0);
   1062 	}
   1063 
   1064 	/*
   1065 	 * the vnode has pages, set up to process the request.
   1066 	 */
   1067 
   1068 	error = 0;
   1069 	s = splbio();
   1070 	simple_lock(&global_v_numoutput_slock);
   1071 	wasclean = (vp->v_numoutput == 0);
   1072 	simple_unlock(&global_v_numoutput_slock);
   1073 	splx(s);
   1074 	off = startoff;
   1075 	if (endoff == 0 || flags & PGO_ALLPAGES) {
   1076 		endoff = trunc_page(LLONG_MAX);
   1077 	}
   1078 	by_list = (uobj->uo_npages <=
   1079 	    ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
   1080 
   1081 	/*
   1082 	 * start the loop.  when scanning by list, hold the last page
   1083 	 * in the list before we start.  pages allocated after we start
   1084 	 * will be added to the end of the list, so we can stop at the
   1085 	 * current last page.
   1086 	 */
   1087 
   1088 	freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
   1089 	curmp.uobject = uobj;
   1090 	curmp.offset = (voff_t)-1;
   1091 	curmp.flags = PG_BUSY;
   1092 	endmp.uobject = uobj;
   1093 	endmp.offset = (voff_t)-1;
   1094 	endmp.flags = PG_BUSY;
   1095 	if (by_list) {
   1096 		pg = TAILQ_FIRST(&uobj->memq);
   1097 		TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq);
   1098 		PHOLD(l);
   1099 	} else {
   1100 		pg = uvm_pagelookup(uobj, off);
   1101 	}
   1102 	nextpg = NULL;
   1103 	while (by_list || off < endoff) {
   1104 
   1105 		/*
   1106 		 * if the current page is not interesting, move on to the next.
   1107 		 */
   1108 
   1109 		KASSERT(pg == NULL || pg->uobject == uobj);
   1110 		KASSERT(pg == NULL ||
   1111 		    (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
   1112 		    (pg->flags & PG_BUSY) != 0);
   1113 		if (by_list) {
   1114 			if (pg == &endmp) {
   1115 				break;
   1116 			}
   1117 			if (pg->offset < startoff || pg->offset >= endoff ||
   1118 			    pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1119 				pg = TAILQ_NEXT(pg, listq);
   1120 				continue;
   1121 			}
   1122 			off = pg->offset;
   1123 		} else if (pg == NULL ||
   1124 		    pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
   1125 			off += PAGE_SIZE;
   1126 			if (off < endoff) {
   1127 				pg = uvm_pagelookup(uobj, off);
   1128 			}
   1129 			continue;
   1130 		}
   1131 
   1132 		/*
   1133 		 * if the current page needs to be cleaned and it's busy,
   1134 		 * wait for it to become unbusy.
   1135 		 */
   1136 
   1137 		yield = (l->l_cpu->ci_schedstate.spc_flags &
   1138 		    SPCF_SHOULDYIELD) && !pagedaemon;
   1139 		if (pg->flags & PG_BUSY || yield) {
   1140 			UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
   1141 			if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
   1142 				UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
   1143 				error = EDEADLK;
   1144 				break;
   1145 			}
   1146 			KASSERT(!pagedaemon);
   1147 			if (by_list) {
   1148 				TAILQ_INSERT_BEFORE(pg, &curmp, listq);
   1149 				UVMHIST_LOG(ubchist, "curmp next %p",
   1150 				    TAILQ_NEXT(&curmp, listq), 0,0,0);
   1151 			}
   1152 			if (yield) {
   1153 				simple_unlock(slock);
   1154 				preempt(1);
   1155 				simple_lock(slock);
   1156 			} else {
   1157 				pg->flags |= PG_WANTED;
   1158 				UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
   1159 				simple_lock(slock);
   1160 			}
   1161 			if (by_list) {
   1162 				UVMHIST_LOG(ubchist, "after next %p",
   1163 				    TAILQ_NEXT(&curmp, listq), 0,0,0);
   1164 				pg = TAILQ_NEXT(&curmp, listq);
   1165 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   1166 			} else {
   1167 				pg = uvm_pagelookup(uobj, off);
   1168 			}
   1169 			continue;
   1170 		}
   1171 
   1172 		/*
   1173 		 * if we're freeing, remove all mappings of the page now.
   1174 		 * if we're cleaning, check if the page is needs to be cleaned.
   1175 		 */
   1176 
   1177 		if (flags & PGO_FREE) {
   1178 			pmap_page_protect(pg, VM_PROT_NONE);
   1179 		}
   1180 		if (flags & PGO_CLEANIT) {
   1181 			needs_clean = pmap_clear_modify(pg) ||
   1182 			    (pg->flags & PG_CLEAN) == 0;
   1183 			pg->flags |= PG_CLEAN;
   1184 		} else {
   1185 			needs_clean = FALSE;
   1186 		}
   1187 
   1188 		/*
   1189 		 * if we're cleaning, build a cluster.
   1190 		 * the cluster will consist of pages which are currently dirty,
   1191 		 * but they will be returned to us marked clean.
   1192 		 * if not cleaning, just operate on the one page.
   1193 		 */
   1194 
   1195 		if (needs_clean) {
   1196 			wasclean = FALSE;
   1197 			memset(pgs, 0, sizeof(pgs));
   1198 			pg->flags |= PG_BUSY;
   1199 			UVM_PAGE_OWN(pg, "genfs_putpages");
   1200 
   1201 			/*
   1202 			 * first look backward.
   1203 			 */
   1204 
   1205 			npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
   1206 			nback = npages;
   1207 			uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
   1208 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
   1209 			if (nback) {
   1210 				memmove(&pgs[0], &pgs[npages - nback],
   1211 				    nback * sizeof(pgs[0]));
   1212 				if (npages - nback < nback)
   1213 					memset(&pgs[nback], 0,
   1214 					    (npages - nback) * sizeof(pgs[0]));
   1215 				else
   1216 					memset(&pgs[npages - nback], 0,
   1217 					    nback * sizeof(pgs[0]));
   1218 			}
   1219 
   1220 			/*
   1221 			 * then plug in our page of interest.
   1222 			 */
   1223 
   1224 			pgs[nback] = pg;
   1225 
   1226 			/*
   1227 			 * then look forward to fill in the remaining space in
   1228 			 * the array of pages.
   1229 			 */
   1230 
   1231 			npages = maxpages - nback - 1;
   1232 			uvn_findpages(uobj, off + PAGE_SIZE, &npages,
   1233 			    &pgs[nback + 1],
   1234 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
   1235 			npages += nback + 1;
   1236 		} else {
   1237 			pgs[0] = pg;
   1238 			npages = 1;
   1239 			nback = 0;
   1240 		}
   1241 
   1242 		/*
   1243 		 * apply FREE or DEACTIVATE options if requested.
   1244 		 */
   1245 
   1246 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1247 			uvm_lock_pageq();
   1248 		}
   1249 		for (i = 0; i < npages; i++) {
   1250 			tpg = pgs[i];
   1251 			KASSERT(tpg->uobject == uobj);
   1252 			if (by_list && tpg == TAILQ_NEXT(pg, listq))
   1253 				pg = tpg;
   1254 			if (tpg->offset < startoff || tpg->offset >= endoff)
   1255 				continue;
   1256 			if (flags & PGO_DEACTIVATE &&
   1257 			    (tpg->pqflags & PQ_INACTIVE) == 0 &&
   1258 			    tpg->wire_count == 0) {
   1259 				(void) pmap_clear_reference(tpg);
   1260 				uvm_pagedeactivate(tpg);
   1261 			} else if (flags & PGO_FREE) {
   1262 				pmap_page_protect(tpg, VM_PROT_NONE);
   1263 				if (tpg->flags & PG_BUSY) {
   1264 					tpg->flags |= freeflag;
   1265 					if (pagedaemon) {
   1266 						uvmexp.paging++;
   1267 						uvm_pagedequeue(tpg);
   1268 					}
   1269 				} else {
   1270 
   1271 					/*
   1272 					 * ``page is not busy''
   1273 					 * implies that npages is 1
   1274 					 * and needs_clean is false.
   1275 					 */
   1276 
   1277 					nextpg = TAILQ_NEXT(tpg, listq);
   1278 					uvm_pagefree(tpg);
   1279 				}
   1280 			}
   1281 		}
   1282 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   1283 			uvm_unlock_pageq();
   1284 		}
   1285 		if (needs_clean) {
   1286 
   1287 			/*
   1288 			 * start the i/o.  if we're traversing by list,
   1289 			 * keep our place in the list with a marker page.
   1290 			 */
   1291 
   1292 			if (by_list) {
   1293 				TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
   1294 				    listq);
   1295 			}
   1296 			simple_unlock(slock);
   1297 			error = GOP_WRITE(vp, pgs, npages, flags);
   1298 			simple_lock(slock);
   1299 			if (by_list) {
   1300 				pg = TAILQ_NEXT(&curmp, listq);
   1301 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   1302 			}
   1303 			if (error) {
   1304 				break;
   1305 			}
   1306 			if (by_list) {
   1307 				continue;
   1308 			}
   1309 		}
   1310 
   1311 		/*
   1312 		 * find the next page and continue if there was no error.
   1313 		 */
   1314 
   1315 		if (by_list) {
   1316 			if (nextpg) {
   1317 				pg = nextpg;
   1318 				nextpg = NULL;
   1319 			} else {
   1320 				pg = TAILQ_NEXT(pg, listq);
   1321 			}
   1322 		} else {
   1323 			off += (npages - nback) << PAGE_SHIFT;
   1324 			if (off < endoff) {
   1325 				pg = uvm_pagelookup(uobj, off);
   1326 			}
   1327 		}
   1328 	}
   1329 	if (by_list) {
   1330 		TAILQ_REMOVE(&uobj->memq, &endmp, listq);
   1331 		PRELE(l);
   1332 	}
   1333 
   1334 	/*
   1335 	 * if we're cleaning and there was nothing to clean,
   1336 	 * take us off the syncer list.  if we started any i/o
   1337 	 * and we're doing sync i/o, wait for all writes to finish.
   1338 	 */
   1339 
   1340 	s = splbio();
   1341 	if ((flags & PGO_CLEANIT) && wasclean &&
   1342 	    startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
   1343 	    LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
   1344 	    (vp->v_flag & VONWORKLST)) {
   1345 		vp->v_flag &= ~VONWORKLST;
   1346 		LIST_REMOVE(vp, v_synclist);
   1347 	}
   1348 	splx(s);
   1349 	if (!wasclean && !async) {
   1350 		s = splbio();
   1351 		/*
   1352 		 * XXX - we want simple_unlock(&global_v_numoutput_slock);
   1353 		 *	 but the slot in ltsleep() is taken!
   1354 		 * XXX - try to recover from missed wakeups with a timeout..
   1355 		 *	 must think of something better.
   1356 		 */
   1357 		while (vp->v_numoutput != 0) {
   1358 			vp->v_flag |= VBWAIT;
   1359 			UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, FALSE,
   1360 			    "genput2", hz);
   1361 			simple_lock(slock);
   1362 		}
   1363 		splx(s);
   1364 	}
   1365 	simple_unlock(&uobj->vmobjlock);
   1366 	return (error);
   1367 }
   1368 
   1369 int
   1370 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
   1371 {
   1372 	int s, error, run;
   1373 	int fs_bshift, dev_bshift;
   1374 	vaddr_t kva;
   1375 	off_t eof, offset, startoffset;
   1376 	size_t bytes, iobytes, skipbytes;
   1377 	daddr_t lbn, blkno;
   1378 	struct vm_page *pg;
   1379 	struct buf *mbp, *bp;
   1380 	struct vnode *devvp;
   1381 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1382 	UVMHIST_FUNC("genfs_gop_write"); UVMHIST_CALLED(ubchist);
   1383 
   1384 	UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
   1385 	    vp, pgs, npages, flags);
   1386 
   1387 	GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_WRITE);
   1388 	if (vp->v_type == VREG) {
   1389 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1390 		dev_bshift = vp->v_mount->mnt_dev_bshift;
   1391 	} else {
   1392 		fs_bshift = DEV_BSHIFT;
   1393 		dev_bshift = DEV_BSHIFT;
   1394 	}
   1395 	error = 0;
   1396 	pg = pgs[0];
   1397 	startoffset = pg->offset;
   1398 	bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   1399 	skipbytes = 0;
   1400 	KASSERT(bytes != 0);
   1401 
   1402 	kva = uvm_pagermapin(pgs, npages,
   1403 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1404 
   1405 	s = splbio();
   1406 	simple_lock(&global_v_numoutput_slock);
   1407 	vp->v_numoutput += 2;
   1408 	simple_unlock(&global_v_numoutput_slock);
   1409 	mbp = pool_get(&bufpool, PR_WAITOK);
   1410 	BUF_INIT(mbp);
   1411 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1412 	    vp, mbp, vp->v_numoutput, bytes);
   1413 	splx(s);
   1414 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1415 	mbp->b_data = (void *)kva;
   1416 	mbp->b_resid = mbp->b_bcount = bytes;
   1417 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE| (async ? (B_CALL|B_ASYNC) : 0);
   1418 	mbp->b_iodone = uvm_aio_biodone;
   1419 	mbp->b_vp = vp;
   1420 
   1421 	bp = NULL;
   1422 	for (offset = startoffset;
   1423 	    bytes > 0;
   1424 	    offset += iobytes, bytes -= iobytes) {
   1425 		lbn = offset >> fs_bshift;
   1426 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
   1427 		if (error) {
   1428 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
   1429 			skipbytes += bytes;
   1430 			bytes = 0;
   1431 			break;
   1432 		}
   1433 
   1434 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1435 		    bytes);
   1436 		if (blkno == (daddr_t)-1) {
   1437 			skipbytes += iobytes;
   1438 			continue;
   1439 		}
   1440 
   1441 		/* if it's really one i/o, don't make a second buf */
   1442 		if (offset == startoffset && iobytes == bytes) {
   1443 			bp = mbp;
   1444 		} else {
   1445 			s = splbio();
   1446 			V_INCR_NUMOUTPUT(vp);
   1447 			bp = pool_get(&bufpool, PR_WAITOK);
   1448 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1449 			    vp, bp, vp->v_numoutput, 0);
   1450 			splx(s);
   1451 			BUF_INIT(bp);
   1452 			bp->b_data = (char *)kva +
   1453 			    (vaddr_t)(offset - pg->offset);
   1454 			bp->b_resid = bp->b_bcount = iobytes;
   1455 			bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
   1456 			bp->b_iodone = uvm_aio_biodone1;
   1457 			bp->b_vp = vp;
   1458 		}
   1459 		bp->b_lblkno = 0;
   1460 		bp->b_private = mbp;
   1461 		if (devvp->v_type == VBLK) {
   1462 			bp->b_dev = devvp->v_rdev;
   1463 		}
   1464 
   1465 		/* adjust physical blkno for partial blocks */
   1466 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1467 		    dev_bshift);
   1468 		UVMHIST_LOG(ubchist,
   1469 		    "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1470 		    vp, offset, bp->b_bcount, bp->b_blkno);
   1471 		VOP_STRATEGY(bp);
   1472 	}
   1473 	if (skipbytes) {
   1474 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   1475 		s = splbio();
   1476 		if (error) {
   1477 			mbp->b_flags |= B_ERROR;
   1478 			mbp->b_error = error;
   1479 		}
   1480 		mbp->b_resid -= skipbytes;
   1481 		if (mbp->b_resid == 0) {
   1482 			biodone(mbp);
   1483 		}
   1484 		splx(s);
   1485 	}
   1486 	if (async) {
   1487 		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
   1488 		return (0);
   1489 	}
   1490 	UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
   1491 	error = biowait(mbp);
   1492 	uvm_aio_aiodone(mbp);
   1493 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
   1494 	return (error);
   1495 }
   1496 
   1497 /*
   1498  * VOP_PUTPAGES() for vnodes which never have pages.
   1499  */
   1500 
   1501 int
   1502 genfs_null_putpages(void *v)
   1503 {
   1504 	struct vop_putpages_args /* {
   1505 		struct vnode *a_vp;
   1506 		voff_t a_offlo;
   1507 		voff_t a_offhi;
   1508 		int a_flags;
   1509 	} */ *ap = v;
   1510 	struct vnode *vp = ap->a_vp;
   1511 
   1512 	KASSERT(vp->v_uobj.uo_npages == 0);
   1513 	simple_unlock(&vp->v_interlock);
   1514 	return (0);
   1515 }
   1516 
   1517 void
   1518 genfs_node_init(struct vnode *vp, struct genfs_ops *ops)
   1519 {
   1520 	struct genfs_node *gp = VTOG(vp);
   1521 
   1522 	lockinit(&gp->g_glock, PINOD, "glock", 0, 0);
   1523 	gp->g_op = ops;
   1524 }
   1525 
   1526 void
   1527 genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   1528 {
   1529 	int bsize;
   1530 
   1531 	bsize = 1 << vp->v_mount->mnt_fs_bshift;
   1532 	*eobp = (size + bsize - 1) & ~(bsize - 1);
   1533 }
   1534 
   1535 int
   1536 genfs_compat_getpages(void *v)
   1537 {
   1538 	struct vop_getpages_args /* {
   1539 		struct vnode *a_vp;
   1540 		voff_t a_offset;
   1541 		struct vm_page **a_m;
   1542 		int *a_count;
   1543 		int a_centeridx;
   1544 		vm_prot_t a_access_type;
   1545 		int a_advice;
   1546 		int a_flags;
   1547 	} */ *ap = v;
   1548 
   1549 	off_t origoffset;
   1550 	struct vnode *vp = ap->a_vp;
   1551 	struct uvm_object *uobj = &vp->v_uobj;
   1552 	struct vm_page *pg, **pgs;
   1553 	vaddr_t kva;
   1554 	int i, error, orignpages, npages;
   1555 	struct iovec iov;
   1556 	struct uio uio;
   1557 	struct ucred *cred = curproc->p_ucred;
   1558 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   1559 
   1560 	error = 0;
   1561 	origoffset = ap->a_offset;
   1562 	orignpages = *ap->a_count;
   1563 	pgs = ap->a_m;
   1564 
   1565 	if (write && (vp->v_flag & VONWORKLST) == 0) {
   1566 		vn_syncer_add_to_worklist(vp, filedelay);
   1567 	}
   1568 	if (ap->a_flags & PGO_LOCKED) {
   1569 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
   1570 		    UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
   1571 
   1572 		return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
   1573 	}
   1574 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
   1575 		simple_unlock(&uobj->vmobjlock);
   1576 		return (EINVAL);
   1577 	}
   1578 	npages = orignpages;
   1579 	uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
   1580 	simple_unlock(&uobj->vmobjlock);
   1581 	kva = uvm_pagermapin(pgs, npages,
   1582 	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
   1583 	for (i = 0; i < npages; i++) {
   1584 		pg = pgs[i];
   1585 		if ((pg->flags & PG_FAKE) == 0) {
   1586 			continue;
   1587 		}
   1588 		iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
   1589 		iov.iov_len = PAGE_SIZE;
   1590 		uio.uio_iov = &iov;
   1591 		uio.uio_iovcnt = 1;
   1592 		uio.uio_offset = origoffset + (i << PAGE_SHIFT);
   1593 		uio.uio_segflg = UIO_SYSSPACE;
   1594 		uio.uio_rw = UIO_READ;
   1595 		uio.uio_resid = PAGE_SIZE;
   1596 		uio.uio_procp = curproc;
   1597 		error = VOP_READ(vp, &uio, 0, cred);
   1598 		if (error) {
   1599 			break;
   1600 		}
   1601 		if (uio.uio_resid) {
   1602 			memset(iov.iov_base, 0, uio.uio_resid);
   1603 		}
   1604 	}
   1605 	uvm_pagermapout(kva, npages);
   1606 	simple_lock(&uobj->vmobjlock);
   1607 	uvm_lock_pageq();
   1608 	for (i = 0; i < npages; i++) {
   1609 		pg = pgs[i];
   1610 		if (error && (pg->flags & PG_FAKE) != 0) {
   1611 			pg->flags |= PG_RELEASED;
   1612 		} else {
   1613 			pmap_clear_modify(pg);
   1614 			uvm_pageactivate(pg);
   1615 		}
   1616 	}
   1617 	if (error) {
   1618 		uvm_page_unbusy(pgs, npages);
   1619 	}
   1620 	uvm_unlock_pageq();
   1621 	simple_unlock(&uobj->vmobjlock);
   1622 	return (error);
   1623 }
   1624 
   1625 int
   1626 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
   1627     int flags)
   1628 {
   1629 	off_t offset;
   1630 	struct iovec iov;
   1631 	struct uio uio;
   1632 	struct ucred *cred = curproc->p_ucred;
   1633 	struct buf *bp;
   1634 	vaddr_t kva;
   1635 	int s, error;
   1636 
   1637 	offset = pgs[0]->offset;
   1638 	kva = uvm_pagermapin(pgs, npages,
   1639 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1640 
   1641 	iov.iov_base = (void *)kva;
   1642 	iov.iov_len = npages << PAGE_SHIFT;
   1643 	uio.uio_iov = &iov;
   1644 	uio.uio_iovcnt = 1;
   1645 	uio.uio_offset = offset;
   1646 	uio.uio_segflg = UIO_SYSSPACE;
   1647 	uio.uio_rw = UIO_WRITE;
   1648 	uio.uio_resid = npages << PAGE_SHIFT;
   1649 	uio.uio_procp = curproc;
   1650 	error = VOP_WRITE(vp, &uio, 0, cred);
   1651 
   1652 	s = splbio();
   1653 	V_INCR_NUMOUTPUT(vp);
   1654 	bp = pool_get(&bufpool, PR_WAITOK);
   1655 	splx(s);
   1656 
   1657 	BUF_INIT(bp);
   1658 	bp->b_flags = B_BUSY | B_WRITE | B_AGE;
   1659 	bp->b_vp = vp;
   1660 	bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
   1661 	bp->b_data = (char *)kva;
   1662 	bp->b_bcount = npages << PAGE_SHIFT;
   1663 	bp->b_bufsize = npages << PAGE_SHIFT;
   1664 	bp->b_resid = 0;
   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