Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.36.2.2
      1 /*	$NetBSD: genfs_vnops.c,v 1.36.2.2 2001/09/26 15:28:23 fvdl 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 "opt_nfsserver.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/kernel.h>
     43 #include <sys/mount.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/fcntl.h>
     47 #include <sys/malloc.h>
     48 #include <sys/poll.h>
     49 
     50 #include <miscfs/genfs/genfs.h>
     51 #include <miscfs/specfs/specdev.h>
     52 
     53 #include <uvm/uvm.h>
     54 #include <uvm/uvm_pager.h>
     55 
     56 #ifdef NFSSERVER
     57 #include <nfs/rpcv2.h>
     58 #include <nfs/nfsproto.h>
     59 #include <nfs/nfs.h>
     60 #include <nfs/nqnfs.h>
     61 #include <nfs/nfs_var.h>
     62 #endif
     63 
     64 int
     65 genfs_poll(v)
     66 	void *v;
     67 {
     68 	struct vop_poll_args /* {
     69 		struct vnode *a_vp;
     70 		int a_events;
     71 		struct proc *a_p;
     72 	} */ *ap = v;
     73 
     74 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
     75 }
     76 
     77 int
     78 genfs_fsync(v)
     79 	void *v;
     80 {
     81 	struct vop_fsync_args /* {
     82 		struct vnode *a_vp;
     83 		struct ucred *a_cred;
     84 		int a_flags;
     85 		off_t offlo;
     86 		off_t offhi;
     87 		struct proc *a_p;
     88 	} */ *ap = v;
     89 	struct vnode *vp = ap->a_vp;
     90 	int wait;
     91 
     92 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
     93 	vflushbuf(vp, wait);
     94 	if ((ap->a_flags & FSYNC_DATAONLY) != 0)
     95 		return (0);
     96 	else
     97 		return (VOP_UPDATE(vp, NULL, NULL, wait ? UPDATE_WAIT : 0));
     98 }
     99 
    100 int
    101 genfs_seek(v)
    102 	void *v;
    103 {
    104 	struct vop_seek_args /* {
    105 		struct vnode *a_vp;
    106 		off_t a_oldoff;
    107 		off_t a_newoff;
    108 		struct ucred *a_ucred;
    109 	} */ *ap = v;
    110 
    111 	if (ap->a_newoff < 0)
    112 		return (EINVAL);
    113 
    114 	return (0);
    115 }
    116 
    117 int
    118 genfs_abortop(v)
    119 	void *v;
    120 {
    121 	struct vop_abortop_args /* {
    122 		struct vnode *a_dvp;
    123 		struct componentname *a_cnp;
    124 	} */ *ap = v;
    125 
    126 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    127 		PNBUF_PUT(ap->a_cnp->cn_pnbuf);
    128 	return (0);
    129 }
    130 
    131 int
    132 genfs_fcntl(v)
    133 	void *v;
    134 {
    135 	struct vop_fcntl_args /* {
    136 		struct vnode *a_vp;
    137 		u_int a_command;
    138 		caddr_t a_data;
    139 		int a_fflag;
    140 		struct ucred *a_cred;
    141 		struct proc *a_p;
    142 	} */ *ap = v;
    143 
    144 	if (ap->a_command == F_SETFL)
    145 		return (0);
    146 	else
    147 		return (EOPNOTSUPP);
    148 }
    149 
    150 /*ARGSUSED*/
    151 int
    152 genfs_badop(v)
    153 	void *v;
    154 {
    155 
    156 	panic("genfs: bad op");
    157 }
    158 
    159 /*ARGSUSED*/
    160 int
    161 genfs_nullop(v)
    162 	void *v;
    163 {
    164 
    165 	return (0);
    166 }
    167 
    168 /*ARGSUSED*/
    169 int
    170 genfs_einval(v)
    171 	void *v;
    172 {
    173 
    174 	return (EINVAL);
    175 }
    176 
    177 /*ARGSUSED*/
    178 int
    179 genfs_eopnotsupp(v)
    180 	void *v;
    181 {
    182 
    183 	return (EOPNOTSUPP);
    184 }
    185 
    186 /*
    187  * Called when an fs doesn't support a particular vop but the vop needs to
    188  * vrele, vput, or vunlock passed in vnodes.
    189  */
    190 int
    191 genfs_eopnotsupp_rele(v)
    192 	void *v;
    193 {
    194 	struct vop_generic_args /*
    195 		struct vnodeop_desc *a_desc;
    196 		/ * other random data follows, presumably * /
    197 	} */ *ap = v;
    198 	struct vnodeop_desc *desc = ap->a_desc;
    199 	struct vnode *vp;
    200 	int flags, i, j, offset;
    201 
    202 	flags = desc->vdesc_flags;
    203 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    204 		if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    205 			break;	/* stop at end of list */
    206 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    207 			vp = *VOPARG_OFFSETTO(struct vnode**,offset,ap);
    208 			switch (j) {
    209 			case VDESC_VP0_WILLPUT:
    210 				vput(vp);
    211 				break;
    212 			case VDESC_VP0_WILLUNLOCK:
    213 				VOP_UNLOCK(vp, 0);
    214 				break;
    215 			case VDESC_VP0_WILLRELE:
    216 				vrele(vp);
    217 				break;
    218 			}
    219 		}
    220 	}
    221 
    222 	return (EOPNOTSUPP);
    223 }
    224 
    225 /*ARGSUSED*/
    226 int
    227 genfs_ebadf(v)
    228 	void *v;
    229 {
    230 
    231 	return (EBADF);
    232 }
    233 
    234 /* ARGSUSED */
    235 int
    236 genfs_enoioctl(v)
    237 	void *v;
    238 {
    239 
    240 	return (ENOTTY);
    241 }
    242 
    243 
    244 /*
    245  * Eliminate all activity associated with the requested vnode
    246  * and with all vnodes aliased to the requested vnode.
    247  */
    248 int
    249 genfs_revoke(v)
    250 	void *v;
    251 {
    252 	struct vop_revoke_args /* {
    253 		struct vnode *a_vp;
    254 		int a_flags;
    255 	} */ *ap = v;
    256 	struct vnode *vp, *vq;
    257 	struct proc *p = curproc;	/* XXX */
    258 
    259 #ifdef DIAGNOSTIC
    260 	if ((ap->a_flags & (REVOKEALIAS | REVOKECLONE)) == 0)
    261 		panic("genfs_revoke: not revokealias or revokeclone");
    262 #endif
    263 
    264 	vp = ap->a_vp;
    265 	simple_lock(&vp->v_interlock);
    266 
    267 	/*
    268 	 * Rules:
    269 	 * - a cloned vnode has both VALIASED and VCLONED set
    270 	 * - an aliased vnode only has VALIASED set.
    271 	 * - revoking a cloned vnode will only revoke the vnode itself.
    272 	 * - revoking a normal vnode will revoke all aliased vnodes
    273 	 *   for the same device if REVOKEALIAS is set in the flags argument
    274 	 * - revoking a normal vnode will revoke cloned vnodes for the same
    275 	 *   device if REVOKECLONE is set.
    276 	 */
    277 	if ((vp->v_flag & (VALIASED | VCLONED)) == VALIASED) {
    278 		/*
    279 		 * If a vgone (or vclean) is already in progress,
    280 		 * wait until it is done and return.
    281 		 */
    282 		if (vp->v_flag & VXLOCK) {
    283 			vp->v_flag |= VXWANT;
    284 			simple_unlock(&vp->v_interlock);
    285 			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
    286 			return (0);
    287 		}
    288 		/*
    289 		 * Ensure that vp will not be vgone'd while we
    290 		 * are eliminating its aliases.
    291 		 */
    292 		vp->v_flag |= VXLOCK;
    293 		simple_unlock(&vp->v_interlock);
    294 		while (vp->v_flag & VALIASED) {
    295 			simple_lock(&spechash_slock);
    296 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    297 				if (vq->v_rdev != vp->v_rdev ||
    298 				    vq->v_type != vp->v_type || vp == vq)
    299 				if ((ap->a_flags & REVOKECLONE) == 0 &&
    300 				    (vq->v_flag & VCLONED) != 0)
    301 					continue;
    302 				if ((ap->a_flags & REVOKEALIAS) == 0 &&
    303 				    (vq->v_flag & VCLONED) == 0)
    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(v)
    329 	void *v;
    330 {
    331 	struct vop_lock_args /* {
    332 		struct vnode *a_vp;
    333 		int a_flags;
    334 	} */ *ap = v;
    335 	struct vnode *vp = ap->a_vp;
    336 
    337 	return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock));
    338 }
    339 
    340 /*
    341  * Unlock the node.
    342  */
    343 int
    344 genfs_unlock(v)
    345 	void *v;
    346 {
    347 	struct vop_unlock_args /* {
    348 		struct vnode *a_vp;
    349 		int a_flags;
    350 	} */ *ap = v;
    351 	struct vnode *vp = ap->a_vp;
    352 
    353 	return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
    354 		&vp->v_interlock));
    355 }
    356 
    357 /*
    358  * Return whether or not the node is locked.
    359  */
    360 int
    361 genfs_islocked(v)
    362 	void *v;
    363 {
    364 	struct vop_islocked_args /* {
    365 		struct vnode *a_vp;
    366 	} */ *ap = v;
    367 	struct vnode *vp = ap->a_vp;
    368 
    369 	return (lockstatus(&vp->v_lock));
    370 }
    371 
    372 /*
    373  * Stubs to use when there is no locking to be done on the underlying object.
    374  */
    375 int
    376 genfs_nolock(v)
    377 	void *v;
    378 {
    379 	struct vop_lock_args /* {
    380 		struct vnode *a_vp;
    381 		int a_flags;
    382 		struct proc *a_p;
    383 	} */ *ap = v;
    384 
    385 	/*
    386 	 * Since we are not using the lock manager, we must clear
    387 	 * the interlock here.
    388 	 */
    389 	if (ap->a_flags & LK_INTERLOCK)
    390 		simple_unlock(&ap->a_vp->v_interlock);
    391 	return (0);
    392 }
    393 
    394 int
    395 genfs_nounlock(v)
    396 	void *v;
    397 {
    398 	return (0);
    399 }
    400 
    401 int
    402 genfs_noislocked(v)
    403 	void *v;
    404 {
    405 	return (0);
    406 }
    407 
    408 /*
    409  * Local lease check for NFS servers.  Just set up args and let
    410  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    411  * this is a null operation.
    412  */
    413 int
    414 genfs_lease_check(v)
    415 	void *v;
    416 {
    417 #ifdef NFSSERVER
    418 	struct vop_lease_args /* {
    419 		struct vnode *a_vp;
    420 		struct proc *a_p;
    421 		struct ucred *a_cred;
    422 		int a_flag;
    423 	} */ *ap = v;
    424 	u_int32_t duration = 0;
    425 	int cache;
    426 	u_quad_t frev;
    427 
    428 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
    429 	    NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
    430 	return (0);
    431 #else
    432 	return (0);
    433 #endif /* NFSSERVER */
    434 }
    435 
    436 int
    437 genfs_mmap(v)
    438 	void *v;
    439 {
    440 	return 0;
    441 }
    442 
    443 /*
    444  * generic VM getpages routine.
    445  * Return PG_BUSY pages for the given range,
    446  * reading from backing store if necessary.
    447  */
    448 
    449 int
    450 genfs_getpages(v)
    451 	void *v;
    452 {
    453 	struct vop_getpages_args /* {
    454 		struct vnode *a_vp;
    455 		voff_t a_offset;
    456 		struct vm_page **a_m;
    457 		int *a_count;
    458 		int a_centeridx;
    459 		vm_prot_t a_access_type;
    460 		int a_advice;
    461 		int a_flags;
    462 	} */ *ap = v;
    463 
    464 	off_t newsize, diskeof, memeof;
    465 	off_t offset, origoffset, startoffset, endoffset, raoffset;
    466 	daddr_t lbn, blkno;
    467 	int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
    468 	int fs_bshift, fs_bsize, dev_bshift, dev_bsize;
    469 	int flags = ap->a_flags;
    470 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    471 	vaddr_t kva;
    472 	struct buf *bp, *mbp;
    473 	struct vnode *vp = ap->a_vp;
    474 	struct vnode *devvp;
    475 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
    476 	struct vm_page *pgs[16];			/* XXXUBC 16 */
    477 	struct ucred *cred = curproc->p_ucred;		/* XXXUBC curproc */
    478 	boolean_t async = (flags & PGO_SYNCIO) == 0;
    479 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
    480 	boolean_t sawhole = FALSE;
    481 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
    482 
    483 	UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d",
    484 		    vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
    485 
    486 	/* XXXUBC temp limit */
    487 	if (*ap->a_count > 16) {
    488 		return EINVAL;
    489 	}
    490 
    491 	error = 0;
    492 	origoffset = ap->a_offset;
    493 	orignpages = *ap->a_count;
    494 	error = VOP_SIZE(vp, vp->v_uvm.u_size, &diskeof);
    495 	if (error) {
    496 		return error;
    497 	}
    498 	if (flags & PGO_PASTEOF) {
    499 		newsize = MAX(vp->v_uvm.u_size,
    500 			      origoffset + (orignpages << PAGE_SHIFT));
    501 		error = VOP_SIZE(vp, newsize, &memeof);
    502 		if (error) {
    503 			return error;
    504 		}
    505 	} else {
    506 		memeof = diskeof;
    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 
    512 	/*
    513 	 * Bounds-check the request.
    514 	 */
    515 
    516 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
    517 		if ((flags & PGO_LOCKED) == 0) {
    518 			simple_unlock(&uobj->vmobjlock);
    519 		}
    520 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
    521 			    origoffset, *ap->a_count, memeof,0);
    522 		return EINVAL;
    523 	}
    524 
    525 	/*
    526 	 * For PGO_LOCKED requests, just return whatever's in memory.
    527 	 */
    528 
    529 	if (flags & PGO_LOCKED) {
    530 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
    531 			      UFP_NOWAIT|UFP_NOALLOC|UFP_NORDONLY);
    532 
    533 		return ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
    534 	}
    535 
    536 	/* vnode is VOP_LOCKed, uobj is locked */
    537 
    538 	if (write && (vp->v_flag & VONWORKLST) == 0) {
    539 		vn_syncer_add_to_worklist(vp, filedelay);
    540 	}
    541 
    542 	/*
    543 	 * find the requested pages and make some simple checks.
    544 	 * leave space in the page array for a whole block.
    545 	 */
    546 
    547 	if (vp->v_type == VREG) {
    548 		fs_bshift = vp->v_mount->mnt_fs_bshift;
    549 		dev_bshift = vp->v_mount->mnt_dev_bshift;
    550 	} else {
    551 		fs_bshift = DEV_BSHIFT;
    552 		dev_bshift = DEV_BSHIFT;
    553 	}
    554 	fs_bsize = 1 << fs_bshift;
    555 	dev_bsize = 1 << dev_bshift;
    556 	KASSERT((diskeof & (dev_bsize - 1)) == 0);
    557 	KASSERT((memeof & (dev_bsize - 1)) == 0);
    558 
    559 	orignpages = MIN(orignpages,
    560 	    round_page(memeof - origoffset) >> PAGE_SHIFT);
    561 	npages = orignpages;
    562 	startoffset = origoffset & ~(fs_bsize - 1);
    563 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
    564 				+ fs_bsize - 1) & ~(fs_bsize - 1));
    565 	endoffset = MIN(endoffset, round_page(memeof));
    566 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    567 
    568 	memset(pgs, 0, sizeof(pgs));
    569 	uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
    570 
    571 	/*
    572 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    573 	 * PGO_OVERWRITE also means that the caller guarantees
    574 	 * that the pages already have backing store allocated.
    575 	 */
    576 
    577 	if (flags & PGO_OVERWRITE) {
    578 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    579 
    580 		for (i = 0; i < npages; i++) {
    581 			struct vm_page *pg = pgs[ridx + i];
    582 
    583 			if (pg->flags & PG_FAKE) {
    584 				uvm_pagezero(pg);
    585 				pg->flags &= ~(PG_FAKE);
    586 			}
    587 			pg->flags &= ~(PG_RDONLY);
    588 		}
    589 		npages += ridx;
    590 		goto out;
    591 	}
    592 
    593 	/*
    594 	 * if the pages are already resident, just return them.
    595 	 */
    596 
    597 	for (i = 0; i < npages; i++) {
    598 		struct vm_page *pg = pgs[ridx + i];
    599 
    600 		if ((pg->flags & PG_FAKE) ||
    601 		    (write && (pg->flags & PG_RDONLY))) {
    602 			break;
    603 		}
    604 	}
    605 	if (i == npages) {
    606 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    607 		raoffset = origoffset + (orignpages << PAGE_SHIFT);
    608 		npages += ridx;
    609 		goto raout;
    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 		 * XXXUBC we need to avoid deadlocks caused by locking
    623 		 * additional pages at lower offsets than pages we
    624 		 * already have locked.  for now, unlock them all and
    625 		 * start over.
    626 		 */
    627 
    628 		for (i = 0; i < orignpages; i++) {
    629 			struct vm_page *pg = pgs[ridx + i];
    630 
    631 			if (pg->flags & PG_FAKE) {
    632 				pg->flags |= PG_RELEASED;
    633 			}
    634 		}
    635 		uvm_page_unbusy(&pgs[ridx], orignpages);
    636 		memset(pgs, 0, sizeof(pgs));
    637 
    638 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
    639 			    startoffset, endoffset, 0,0);
    640 		npgs = npages;
    641 		uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
    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, UVMPAGER_MAPIN_WAITOK |
    655 			     UVMPAGER_MAPIN_READ);
    656 
    657 	s = splbio();
    658 	mbp = pool_get(&bufpool, PR_WAITOK);
    659 	splx(s);
    660 	mbp->b_bufsize = totalbytes;
    661 	mbp->b_data = (void *)kva;
    662 	mbp->b_resid = mbp->b_bcount = bytes;
    663 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL : 0);
    664 	mbp->b_iodone = uvm_aio_biodone;
    665 	mbp->b_vp = vp;
    666 	LIST_INIT(&mbp->b_dep);
    667 
    668 	/*
    669 	 * if EOF is in the middle of the range, zero the part past EOF.
    670 	 */
    671 
    672 	if (tailbytes > 0) {
    673 		memset((void *)(kva + bytes), 0, tailbytes);
    674 	}
    675 
    676 	/*
    677 	 * now loop over the pages, reading as needed.
    678 	 */
    679 
    680 	if (write) {
    681 		lockmgr(&vp->v_glock, LK_EXCLUSIVE, NULL);
    682 	} else {
    683 		lockmgr(&vp->v_glock, LK_SHARED, NULL);
    684 	}
    685 
    686 	bp = NULL;
    687 	for (offset = startoffset;
    688 	     bytes > 0;
    689 	     offset += iobytes, bytes -= iobytes) {
    690 
    691 		/*
    692 		 * skip pages which don't need to be read.
    693 		 */
    694 
    695 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    696 		while ((pgs[pidx]->flags & (PG_FAKE|PG_RDONLY)) == 0) {
    697 			size_t b;
    698 
    699 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    700 			b = MIN(PAGE_SIZE, bytes);
    701 			offset += b;
    702 			bytes -= b;
    703 			skipbytes += b;
    704 			pidx++;
    705 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    706 				    offset, 0,0,0);
    707 			if (bytes == 0) {
    708 				goto loopdone;
    709 			}
    710 		}
    711 
    712 		/*
    713 		 * bmap the file to find out the blkno to read from and
    714 		 * how much we can read in one i/o.  if bmap returns an error,
    715 		 * skip the rest of the top-level i/o.
    716 		 */
    717 
    718 		lbn = offset >> fs_bshift;
    719 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
    720 		if (error) {
    721 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    722 				    lbn, error,0,0);
    723 			skipbytes += bytes;
    724 			goto loopdone;
    725 		}
    726 
    727 		/*
    728 		 * see how many pages can be read with this i/o.
    729 		 * reduce the i/o size if necessary to avoid
    730 		 * overwriting pages with valid data.
    731 		 */
    732 
    733 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    734 		    bytes);
    735 		if (offset + iobytes > round_page(offset)) {
    736 			pcount = 1;
    737 			while (pidx + pcount < npages &&
    738 			       pgs[pidx + pcount]->flags & PG_FAKE) {
    739 				pcount++;
    740 			}
    741 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    742 				      (offset - trunc_page(offset)));
    743 		}
    744 
    745 		/*
    746 		 * if this block isn't allocated, zero it instead of reading it.
    747 		 * if this is a read access, mark the pages we zeroed PG_RDONLY.
    748 		 */
    749 
    750 		if (blkno < 0) {
    751 			int holepages = (round_page(offset + iobytes) -
    752 					 trunc_page(offset)) >> PAGE_SHIFT;
    753 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    754 
    755 			sawhole = TRUE;
    756 			memset((char *)kva + (offset - startoffset), 0,
    757 			       iobytes);
    758 			skipbytes += iobytes;
    759 
    760 			for (i = 0; i < holepages; i++) {
    761 				if (write) {
    762 					pgs[pidx + i]->flags &= ~PG_CLEAN;
    763 				} else {
    764 					pgs[pidx + i]->flags |= PG_RDONLY;
    765 				}
    766 			}
    767 			continue;
    768 		}
    769 
    770 		/*
    771 		 * allocate a sub-buf for this piece of the i/o
    772 		 * (or just use mbp if there's only 1 piece),
    773 		 * and start it going.
    774 		 */
    775 
    776 		if (offset == startoffset && iobytes == bytes) {
    777 			bp = mbp;
    778 		} else {
    779 			s = splbio();
    780 			bp = pool_get(&bufpool, PR_WAITOK);
    781 			splx(s);
    782 			bp->b_data = (char *)kva + offset - startoffset;
    783 			bp->b_resid = bp->b_bcount = iobytes;
    784 			bp->b_flags = B_BUSY|B_READ|B_CALL;
    785 			bp->b_iodone = uvm_aio_biodone1;
    786 			bp->b_vp = vp;
    787 			LIST_INIT(&bp->b_dep);
    788 		}
    789 		bp->b_lblkno = 0;
    790 		bp->b_private = mbp;
    791 		bp->b_devvp = devvp;
    792 
    793 		/* adjust physical blkno for partial blocks */
    794 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    795 				       dev_bshift);
    796 
    797 		UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    798 			    bp, offset, iobytes, bp->b_blkno);
    799 
    800 		VOP_STRATEGY(bp);
    801 	}
    802 
    803 loopdone:
    804 	if (skipbytes) {
    805 		s = splbio();
    806 		if (error) {
    807 			mbp->b_flags |= B_ERROR;
    808 			mbp->b_error = error;
    809 		}
    810 		mbp->b_resid -= skipbytes;
    811 		if (mbp->b_resid == 0) {
    812 			biodone(mbp);
    813 		}
    814 		splx(s);
    815 	}
    816 
    817 	if (async) {
    818 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
    819 		lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    820 		return 0;
    821 	}
    822 	if (bp != NULL) {
    823 		error = biowait(mbp);
    824 	}
    825 	s = splbio();
    826 	pool_put(&bufpool, mbp);
    827 	splx(s);
    828 	uvm_pagermapout(kva, npages);
    829 	raoffset = startoffset + totalbytes;
    830 
    831 	/*
    832 	 * if this we encountered a hole then we have to do a little more work.
    833 	 * for read faults, we marked the page PG_RDONLY so that future
    834 	 * write accesses to the page will fault again.
    835 	 * for write faults, we must make sure that the backing store for
    836 	 * the page is completely allocated while the pages are locked.
    837 	 */
    838 
    839 	if (error == 0 && sawhole && write) {
    840 		error = VOP_BALLOCN(vp, startoffset, npages << PAGE_SHIFT,
    841 				   cred, 0);
    842 		if (error) {
    843 			UVMHIST_LOG(ubchist, "balloc lbn 0x%x -> %d",
    844 				    lbn, error,0,0);
    845 			lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    846 			simple_lock(&uobj->vmobjlock);
    847 			goto out;
    848 		}
    849 	}
    850 	lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    851 	simple_lock(&uobj->vmobjlock);
    852 
    853 	/*
    854 	 * see if we want to start any readahead.
    855 	 * XXXUBC for now, just read the next 128k on 64k boundaries.
    856 	 * this is pretty nonsensical, but it is 50% faster than reading
    857 	 * just the next 64k.
    858 	 */
    859 
    860 raout:
    861 	if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 &&
    862 	    PAGE_SHIFT <= 16) {
    863 		int racount;
    864 
    865 		racount = 1 << (16 - PAGE_SHIFT);
    866 		(void) VOP_GETPAGES(vp, raoffset, NULL, &racount, 0,
    867 				    VM_PROT_READ, 0, 0);
    868 		simple_lock(&uobj->vmobjlock);
    869 
    870 		racount = 1 << (16 - PAGE_SHIFT);
    871 		(void) VOP_GETPAGES(vp, raoffset + 0x10000, NULL, &racount, 0,
    872 				    VM_PROT_READ, 0, 0);
    873 		simple_lock(&uobj->vmobjlock);
    874 	}
    875 
    876 	/*
    877 	 * we're almost done!  release the pages...
    878 	 * for errors, we free the pages.
    879 	 * otherwise we activate them and mark them as valid and clean.
    880 	 * also, unbusy pages that were not actually requested.
    881 	 */
    882 
    883 out:
    884 	if (error) {
    885 		uvm_lock_pageq();
    886 		for (i = 0; i < npages; i++) {
    887 			if (pgs[i] == NULL) {
    888 				continue;
    889 			}
    890 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    891 				    pgs[i], pgs[i]->flags, 0,0);
    892 			if (pgs[i]->flags & PG_WANTED) {
    893 				wakeup(pgs[i]);
    894 			}
    895 			if (pgs[i]->flags & PG_RELEASED) {
    896 				uvm_unlock_pageq();
    897 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
    898 				uvm_lock_pageq();
    899 				continue;
    900 			}
    901 			if (pgs[i]->flags & PG_FAKE) {
    902 				uvm_pagefree(pgs[i]);
    903 				continue;
    904 			}
    905 			uvm_pageactivate(pgs[i]);
    906 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
    907 			UVM_PAGE_OWN(pgs[i], NULL);
    908 		}
    909 		uvm_unlock_pageq();
    910 		simple_unlock(&uobj->vmobjlock);
    911 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
    912 		return error;
    913 	}
    914 
    915 	UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
    916 	uvm_lock_pageq();
    917 	for (i = 0; i < npages; i++) {
    918 		if (pgs[i] == NULL) {
    919 			continue;
    920 		}
    921 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    922 			    pgs[i], pgs[i]->flags, 0,0);
    923 		if (pgs[i]->flags & PG_FAKE) {
    924 			UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
    925 				    pgs[i], pgs[i]->offset,0,0);
    926 			pgs[i]->flags &= ~(PG_FAKE);
    927 			pmap_clear_modify(pgs[i]);
    928 			pmap_clear_reference(pgs[i]);
    929 		}
    930 		if (write) {
    931 			pgs[i]->flags &= ~(PG_RDONLY);
    932 		}
    933 		if (i < ridx || i >= ridx + orignpages || async) {
    934 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
    935 				    pgs[i], pgs[i]->offset,0,0);
    936 			if (pgs[i]->flags & PG_WANTED) {
    937 				wakeup(pgs[i]);
    938 			}
    939 			if (pgs[i]->flags & PG_RELEASED) {
    940 				uvm_unlock_pageq();
    941 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
    942 				uvm_lock_pageq();
    943 				continue;
    944 			}
    945 			uvm_pageactivate(pgs[i]);
    946 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
    947 			UVM_PAGE_OWN(pgs[i], NULL);
    948 		}
    949 	}
    950 	uvm_unlock_pageq();
    951 	simple_unlock(&uobj->vmobjlock);
    952 	if (ap->a_m != NULL) {
    953 		memcpy(ap->a_m, &pgs[ridx],
    954 		       orignpages * sizeof(struct vm_page *));
    955 	}
    956 	return 0;
    957 }
    958 
    959 /*
    960  * generic VM putpages routine.
    961  * Write the given range of pages to backing store.
    962  */
    963 
    964 int
    965 genfs_putpages(v)
    966 	void *v;
    967 {
    968 	struct vop_putpages_args /* {
    969 		struct vnode *a_vp;
    970 		struct vm_page **a_m;
    971 		int a_count;
    972 		int a_flags;
    973 		int *a_rtvals;
    974 	} */ *ap = v;
    975 
    976 	int s, error, npages, run;
    977 	int fs_bshift, dev_bshift, dev_bsize;
    978 	vaddr_t kva;
    979 	off_t eof, offset, startoffset;
    980 	size_t bytes, iobytes, skipbytes;
    981 	daddr_t lbn, blkno;
    982 	struct vm_page *pg;
    983 	struct buf *mbp, *bp;
    984 	struct vnode *vp = ap->a_vp;
    985 	struct vnode *devvp;
    986 	boolean_t async = (ap->a_flags & PGO_SYNCIO) == 0;
    987 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
    988 	UVMHIST_LOG(ubchist, "vp %p offset 0x%x count %d",
    989 		    vp, ap->a_m[0]->offset, ap->a_count, 0);
    990 
    991 	simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    992 
    993 	error = VOP_SIZE(vp, vp->v_uvm.u_size, &eof);
    994 	if (error) {
    995 		return error;
    996 	}
    997 
    998 	error = 0;
    999 	npages = ap->a_count;
   1000 	if (vp->v_type == VREG) {
   1001 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1002 		dev_bshift = vp->v_mount->mnt_dev_bshift;
   1003 	} else {
   1004 		fs_bshift = DEV_BSHIFT;
   1005 		dev_bshift = DEV_BSHIFT;
   1006 	}
   1007 	dev_bsize = 1 << dev_bshift;
   1008 	KASSERT((eof & (dev_bsize - 1)) == 0);
   1009 
   1010 	pg = ap->a_m[0];
   1011 	startoffset = pg->offset;
   1012 	bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   1013 	skipbytes = 0;
   1014 	KASSERT(bytes != 0);
   1015 
   1016 	kva = uvm_pagermapin(ap->a_m, npages, UVMPAGER_MAPIN_WAITOK);
   1017 
   1018 	s = splbio();
   1019 	vp->v_numoutput += 2;
   1020 	mbp = pool_get(&bufpool, PR_WAITOK);
   1021 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1022 		    vp, mbp, vp->v_numoutput, bytes);
   1023 	splx(s);
   1024 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1025 	mbp->b_data = (void *)kva;
   1026 	mbp->b_resid = mbp->b_bcount = bytes;
   1027 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
   1028 		(async ? B_CALL : 0) |
   1029 		(curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
   1030 	mbp->b_iodone = uvm_aio_biodone;
   1031 	mbp->b_vp = vp;
   1032 	LIST_INIT(&mbp->b_dep);
   1033 
   1034 	bp = NULL;
   1035 	for (offset = startoffset;
   1036 	     bytes > 0;
   1037 	     offset += iobytes, bytes -= iobytes) {
   1038 		lbn = offset >> fs_bshift;
   1039 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
   1040 		if (error) {
   1041 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
   1042 			skipbytes += bytes;
   1043 			bytes = 0;
   1044 			break;
   1045 		}
   1046 
   1047 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1048 		    bytes);
   1049 		if (blkno == (daddr_t)-1) {
   1050 			skipbytes += iobytes;
   1051 			continue;
   1052 		}
   1053 
   1054 		/* if it's really one i/o, don't make a second buf */
   1055 		if (offset == startoffset && iobytes == bytes) {
   1056 			bp = mbp;
   1057 		} else {
   1058 			s = splbio();
   1059 			vp->v_numoutput++;
   1060 			bp = pool_get(&bufpool, PR_WAITOK);
   1061 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1062 				    vp, bp, vp->v_numoutput, 0);
   1063 			splx(s);
   1064 			bp->b_data = (char *)kva +
   1065 				(vaddr_t)(offset - pg->offset);
   1066 			bp->b_resid = bp->b_bcount = iobytes;
   1067 			bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
   1068 			bp->b_iodone = uvm_aio_biodone1;
   1069 			bp->b_vp = vp;
   1070 			LIST_INIT(&bp->b_dep);
   1071 		}
   1072 		bp->b_lblkno = 0;
   1073 		bp->b_private = mbp;
   1074 		bp->b_devvp = devvp;
   1075 
   1076 		/* adjust physical blkno for partial blocks */
   1077 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1078 				       dev_bshift);
   1079 		UVMHIST_LOG(ubchist, "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1080 			    vp, offset, bp->b_bcount, bp->b_blkno);
   1081 		VOP_STRATEGY(bp);
   1082 	}
   1083 	if (skipbytes) {
   1084 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   1085 		s = splbio();
   1086 		mbp->b_resid -= skipbytes;
   1087 		if (error) {
   1088 			mbp->b_flags |= B_ERROR;
   1089 			mbp->b_error = error;
   1090 		}
   1091 		if (mbp->b_resid == 0) {
   1092 			biodone(mbp);
   1093 		}
   1094 		splx(s);
   1095 	}
   1096 	if (async) {
   1097 		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
   1098 		return 0;
   1099 	}
   1100 	if (bp != NULL) {
   1101 		UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
   1102 		error = biowait(mbp);
   1103 	}
   1104 	if (bioops.io_pageiodone) {
   1105 		(*bioops.io_pageiodone)(mbp);
   1106 	}
   1107 	s = splbio();
   1108 	vwakeup(mbp);
   1109 	pool_put(&bufpool, mbp);
   1110 	splx(s);
   1111 	uvm_pagermapout(kva, npages);
   1112 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
   1113 	return error;
   1114 }
   1115 
   1116 int
   1117 genfs_size(v)
   1118 	void *v;
   1119 {
   1120 	struct vop_size_args /* {
   1121 		struct vnode *a_vp;
   1122 		off_t a_size;
   1123 		off_t *a_eobp;
   1124 	} */ *ap = v;
   1125 	int bsize;
   1126 
   1127 	bsize = 1 << ap->a_vp->v_mount->mnt_fs_bshift;
   1128 	*ap->a_eobp = (ap->a_size + bsize - 1) & ~(bsize - 1);
   1129 	return 0;
   1130 }
   1131