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