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