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