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