Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.36.2.3
      1 /*	$NetBSD: genfs_vnops.c,v 1.36.2.3 2001/09/27 14:48:22 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 					continue;
    300 				if ((ap->a_flags & REVOKECLONE) == 0 &&
    301 				    (vq->v_flag & VCLONED) != 0)
    302 					continue;
    303 				if ((ap->a_flags & REVOKEALIAS) == 0 &&
    304 				    (vq->v_flag & VCLONED) == 0)
    305 					continue;
    306 				simple_unlock(&spechash_slock);
    307 				vgone(vq);
    308 				break;
    309 			}
    310 			if (vq == NULLVP)
    311 				simple_unlock(&spechash_slock);
    312 		}
    313 		/*
    314 		 * Remove the lock so that vgone below will
    315 		 * really eliminate the vnode after which time
    316 		 * vgone will awaken any sleepers.
    317 		 */
    318 		simple_lock(&vp->v_interlock);
    319 		vp->v_flag &= ~VXLOCK;
    320 	}
    321 	vgonel(vp, p);
    322 	return (0);
    323 }
    324 
    325 /*
    326  * Lock the node.
    327  */
    328 int
    329 genfs_lock(v)
    330 	void *v;
    331 {
    332 	struct vop_lock_args /* {
    333 		struct vnode *a_vp;
    334 		int a_flags;
    335 	} */ *ap = v;
    336 	struct vnode *vp = ap->a_vp;
    337 
    338 	return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock));
    339 }
    340 
    341 /*
    342  * Unlock the node.
    343  */
    344 int
    345 genfs_unlock(v)
    346 	void *v;
    347 {
    348 	struct vop_unlock_args /* {
    349 		struct vnode *a_vp;
    350 		int a_flags;
    351 	} */ *ap = v;
    352 	struct vnode *vp = ap->a_vp;
    353 
    354 	return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
    355 		&vp->v_interlock));
    356 }
    357 
    358 /*
    359  * Return whether or not the node is locked.
    360  */
    361 int
    362 genfs_islocked(v)
    363 	void *v;
    364 {
    365 	struct vop_islocked_args /* {
    366 		struct vnode *a_vp;
    367 	} */ *ap = v;
    368 	struct vnode *vp = ap->a_vp;
    369 
    370 	return (lockstatus(&vp->v_lock));
    371 }
    372 
    373 /*
    374  * Stubs to use when there is no locking to be done on the underlying object.
    375  */
    376 int
    377 genfs_nolock(v)
    378 	void *v;
    379 {
    380 	struct vop_lock_args /* {
    381 		struct vnode *a_vp;
    382 		int a_flags;
    383 		struct proc *a_p;
    384 	} */ *ap = v;
    385 
    386 	/*
    387 	 * Since we are not using the lock manager, we must clear
    388 	 * the interlock here.
    389 	 */
    390 	if (ap->a_flags & LK_INTERLOCK)
    391 		simple_unlock(&ap->a_vp->v_interlock);
    392 	return (0);
    393 }
    394 
    395 int
    396 genfs_nounlock(v)
    397 	void *v;
    398 {
    399 	return (0);
    400 }
    401 
    402 int
    403 genfs_noislocked(v)
    404 	void *v;
    405 {
    406 	return (0);
    407 }
    408 
    409 /*
    410  * Local lease check for NFS servers.  Just set up args and let
    411  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    412  * this is a null operation.
    413  */
    414 int
    415 genfs_lease_check(v)
    416 	void *v;
    417 {
    418 #ifdef NFSSERVER
    419 	struct vop_lease_args /* {
    420 		struct vnode *a_vp;
    421 		struct proc *a_p;
    422 		struct ucred *a_cred;
    423 		int a_flag;
    424 	} */ *ap = v;
    425 	u_int32_t duration = 0;
    426 	int cache;
    427 	u_quad_t frev;
    428 
    429 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
    430 	    NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
    431 	return (0);
    432 #else
    433 	return (0);
    434 #endif /* NFSSERVER */
    435 }
    436 
    437 int
    438 genfs_mmap(v)
    439 	void *v;
    440 {
    441 	return 0;
    442 }
    443 
    444 /*
    445  * generic VM getpages routine.
    446  * Return PG_BUSY pages for the given range,
    447  * reading from backing store if necessary.
    448  */
    449 
    450 int
    451 genfs_getpages(v)
    452 	void *v;
    453 {
    454 	struct vop_getpages_args /* {
    455 		struct vnode *a_vp;
    456 		voff_t a_offset;
    457 		struct vm_page **a_m;
    458 		int *a_count;
    459 		int a_centeridx;
    460 		vm_prot_t a_access_type;
    461 		int a_advice;
    462 		int a_flags;
    463 	} */ *ap = v;
    464 
    465 	off_t newsize, diskeof, memeof;
    466 	off_t offset, origoffset, startoffset, endoffset, raoffset;
    467 	daddr_t lbn, blkno;
    468 	int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
    469 	int fs_bshift, fs_bsize, dev_bshift, dev_bsize;
    470 	int flags = ap->a_flags;
    471 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    472 	vaddr_t kva;
    473 	struct buf *bp, *mbp;
    474 	struct vnode *vp = ap->a_vp;
    475 	struct vnode *devvp;
    476 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
    477 	struct vm_page *pgs[16];			/* XXXUBC 16 */
    478 	struct ucred *cred = curproc->p_ucred;		/* XXXUBC curproc */
    479 	boolean_t async = (flags & PGO_SYNCIO) == 0;
    480 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
    481 	boolean_t sawhole = FALSE;
    482 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
    483 
    484 	UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d",
    485 		    vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
    486 
    487 	/* XXXUBC temp limit */
    488 	if (*ap->a_count > 16) {
    489 		return EINVAL;
    490 	}
    491 
    492 	error = 0;
    493 	origoffset = ap->a_offset;
    494 	orignpages = *ap->a_count;
    495 	error = VOP_SIZE(vp, vp->v_uvm.u_size, &diskeof);
    496 	if (error) {
    497 		return error;
    498 	}
    499 	if (flags & PGO_PASTEOF) {
    500 		newsize = MAX(vp->v_uvm.u_size,
    501 			      origoffset + (orignpages << PAGE_SHIFT));
    502 		error = VOP_SIZE(vp, newsize, &memeof);
    503 		if (error) {
    504 			return error;
    505 		}
    506 	} else {
    507 		memeof = diskeof;
    508 	}
    509 	KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
    510 	KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
    511 	KASSERT(orignpages > 0);
    512 
    513 	/*
    514 	 * Bounds-check the request.
    515 	 */
    516 
    517 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
    518 		if ((flags & PGO_LOCKED) == 0) {
    519 			simple_unlock(&uobj->vmobjlock);
    520 		}
    521 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
    522 			    origoffset, *ap->a_count, memeof,0);
    523 		return EINVAL;
    524 	}
    525 
    526 	/*
    527 	 * For PGO_LOCKED requests, just return whatever's in memory.
    528 	 */
    529 
    530 	if (flags & PGO_LOCKED) {
    531 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
    532 			      UFP_NOWAIT|UFP_NOALLOC|UFP_NORDONLY);
    533 
    534 		return ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
    535 	}
    536 
    537 	/* vnode is VOP_LOCKed, uobj is locked */
    538 
    539 	if (write && (vp->v_flag & VONWORKLST) == 0) {
    540 		vn_syncer_add_to_worklist(vp, filedelay);
    541 	}
    542 
    543 	/*
    544 	 * find the requested pages and make some simple checks.
    545 	 * leave space in the page array for a whole block.
    546 	 */
    547 
    548 	if (vp->v_type == VREG) {
    549 		fs_bshift = vp->v_mount->mnt_fs_bshift;
    550 		dev_bshift = vp->v_mount->mnt_dev_bshift;
    551 	} else {
    552 		fs_bshift = DEV_BSHIFT;
    553 		dev_bshift = DEV_BSHIFT;
    554 	}
    555 	fs_bsize = 1 << fs_bshift;
    556 	dev_bsize = 1 << dev_bshift;
    557 	KASSERT((diskeof & (dev_bsize - 1)) == 0);
    558 	KASSERT((memeof & (dev_bsize - 1)) == 0);
    559 
    560 	orignpages = MIN(orignpages,
    561 	    round_page(memeof - origoffset) >> PAGE_SHIFT);
    562 	npages = orignpages;
    563 	startoffset = origoffset & ~(fs_bsize - 1);
    564 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
    565 				+ fs_bsize - 1) & ~(fs_bsize - 1));
    566 	endoffset = MIN(endoffset, round_page(memeof));
    567 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    568 
    569 	memset(pgs, 0, sizeof(pgs));
    570 	uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
    571 
    572 	/*
    573 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    574 	 * PGO_OVERWRITE also means that the caller guarantees
    575 	 * that the pages already have backing store allocated.
    576 	 */
    577 
    578 	if (flags & PGO_OVERWRITE) {
    579 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    580 
    581 		for (i = 0; i < npages; i++) {
    582 			struct vm_page *pg = pgs[ridx + i];
    583 
    584 			if (pg->flags & PG_FAKE) {
    585 				uvm_pagezero(pg);
    586 				pg->flags &= ~(PG_FAKE);
    587 			}
    588 			pg->flags &= ~(PG_RDONLY);
    589 		}
    590 		npages += ridx;
    591 		goto out;
    592 	}
    593 
    594 	/*
    595 	 * if the pages are already resident, just return them.
    596 	 */
    597 
    598 	for (i = 0; i < npages; i++) {
    599 		struct vm_page *pg = pgs[ridx + i];
    600 
    601 		if ((pg->flags & PG_FAKE) ||
    602 		    (write && (pg->flags & PG_RDONLY))) {
    603 			break;
    604 		}
    605 	}
    606 	if (i == npages) {
    607 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    608 		raoffset = origoffset + (orignpages << PAGE_SHIFT);
    609 		npages += ridx;
    610 		goto raout;
    611 	}
    612 
    613 	/*
    614 	 * the page wasn't resident and we're not overwriting,
    615 	 * so we're going to have to do some i/o.
    616 	 * find any additional pages needed to cover the expanded range.
    617 	 */
    618 
    619 	npages = (endoffset - startoffset) >> PAGE_SHIFT;
    620 	if (startoffset != origoffset || npages != orignpages) {
    621 
    622 		/*
    623 		 * XXXUBC we need to avoid deadlocks caused by locking
    624 		 * additional pages at lower offsets than pages we
    625 		 * already have locked.  for now, unlock them all and
    626 		 * start over.
    627 		 */
    628 
    629 		for (i = 0; i < orignpages; i++) {
    630 			struct vm_page *pg = pgs[ridx + i];
    631 
    632 			if (pg->flags & PG_FAKE) {
    633 				pg->flags |= PG_RELEASED;
    634 			}
    635 		}
    636 		uvm_page_unbusy(&pgs[ridx], orignpages);
    637 		memset(pgs, 0, sizeof(pgs));
    638 
    639 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
    640 			    startoffset, endoffset, 0,0);
    641 		npgs = npages;
    642 		uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
    643 	}
    644 	simple_unlock(&uobj->vmobjlock);
    645 
    646 	/*
    647 	 * read the desired page(s).
    648 	 */
    649 
    650 	totalbytes = npages << PAGE_SHIFT;
    651 	bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
    652 	tailbytes = totalbytes - bytes;
    653 	skipbytes = 0;
    654 
    655 	kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
    656 			     UVMPAGER_MAPIN_READ);
    657 
    658 	s = splbio();
    659 	mbp = pool_get(&bufpool, PR_WAITOK);
    660 	splx(s);
    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 : 0);
    665 	mbp->b_iodone = uvm_aio_biodone;
    666 	mbp->b_vp = vp;
    667 	LIST_INIT(&mbp->b_dep);
    668 
    669 	/*
    670 	 * if EOF is in the middle of the range, zero the part past EOF.
    671 	 */
    672 
    673 	if (tailbytes > 0) {
    674 		memset((void *)(kva + bytes), 0, tailbytes);
    675 	}
    676 
    677 	/*
    678 	 * now loop over the pages, reading as needed.
    679 	 */
    680 
    681 	if (write) {
    682 		lockmgr(&vp->v_glock, LK_EXCLUSIVE, NULL);
    683 	} else {
    684 		lockmgr(&vp->v_glock, LK_SHARED, NULL);
    685 	}
    686 
    687 	bp = NULL;
    688 	for (offset = startoffset;
    689 	     bytes > 0;
    690 	     offset += iobytes, bytes -= iobytes) {
    691 
    692 		/*
    693 		 * skip pages which don't need to be read.
    694 		 */
    695 
    696 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    697 		while ((pgs[pidx]->flags & (PG_FAKE|PG_RDONLY)) == 0) {
    698 			size_t b;
    699 
    700 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
    701 			b = MIN(PAGE_SIZE, bytes);
    702 			offset += b;
    703 			bytes -= b;
    704 			skipbytes += b;
    705 			pidx++;
    706 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    707 				    offset, 0,0,0);
    708 			if (bytes == 0) {
    709 				goto loopdone;
    710 			}
    711 		}
    712 
    713 		/*
    714 		 * bmap the file to find out the blkno to read from and
    715 		 * how much we can read in one i/o.  if bmap returns an error,
    716 		 * skip the rest of the top-level i/o.
    717 		 */
    718 
    719 		lbn = offset >> fs_bshift;
    720 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
    721 		if (error) {
    722 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    723 				    lbn, error,0,0);
    724 			skipbytes += bytes;
    725 			goto loopdone;
    726 		}
    727 
    728 		/*
    729 		 * see how many pages can be read with this i/o.
    730 		 * reduce the i/o size if necessary to avoid
    731 		 * overwriting pages with valid data.
    732 		 */
    733 
    734 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
    735 		    bytes);
    736 		if (offset + iobytes > round_page(offset)) {
    737 			pcount = 1;
    738 			while (pidx + pcount < npages &&
    739 			       pgs[pidx + pcount]->flags & PG_FAKE) {
    740 				pcount++;
    741 			}
    742 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
    743 				      (offset - trunc_page(offset)));
    744 		}
    745 
    746 		/*
    747 		 * if this block isn't allocated, zero it instead of reading it.
    748 		 * if this is a read access, mark the pages we zeroed PG_RDONLY.
    749 		 */
    750 
    751 		if (blkno < 0) {
    752 			int holepages = (round_page(offset + iobytes) -
    753 					 trunc_page(offset)) >> PAGE_SHIFT;
    754 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    755 
    756 			sawhole = TRUE;
    757 			memset((char *)kva + (offset - startoffset), 0,
    758 			       iobytes);
    759 			skipbytes += iobytes;
    760 
    761 			for (i = 0; i < holepages; i++) {
    762 				if (write) {
    763 					pgs[pidx + i]->flags &= ~PG_CLEAN;
    764 				} else {
    765 					pgs[pidx + i]->flags |= PG_RDONLY;
    766 				}
    767 			}
    768 			continue;
    769 		}
    770 
    771 		/*
    772 		 * allocate a sub-buf for this piece of the i/o
    773 		 * (or just use mbp if there's only 1 piece),
    774 		 * and start it going.
    775 		 */
    776 
    777 		if (offset == startoffset && iobytes == bytes) {
    778 			bp = mbp;
    779 		} else {
    780 			s = splbio();
    781 			bp = pool_get(&bufpool, PR_WAITOK);
    782 			splx(s);
    783 			bp->b_data = (char *)kva + offset - startoffset;
    784 			bp->b_resid = bp->b_bcount = iobytes;
    785 			bp->b_flags = B_BUSY|B_READ|B_CALL;
    786 			bp->b_iodone = uvm_aio_biodone1;
    787 			bp->b_vp = vp;
    788 			LIST_INIT(&bp->b_dep);
    789 		}
    790 		bp->b_lblkno = 0;
    791 		bp->b_private = mbp;
    792 		bp->b_devvp = devvp;
    793 
    794 		/* adjust physical blkno for partial blocks */
    795 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
    796 				       dev_bshift);
    797 
    798 		UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    799 			    bp, offset, iobytes, bp->b_blkno);
    800 
    801 		VOP_STRATEGY(bp);
    802 	}
    803 
    804 loopdone:
    805 	if (skipbytes) {
    806 		s = splbio();
    807 		if (error) {
    808 			mbp->b_flags |= B_ERROR;
    809 			mbp->b_error = error;
    810 		}
    811 		mbp->b_resid -= skipbytes;
    812 		if (mbp->b_resid == 0) {
    813 			biodone(mbp);
    814 		}
    815 		splx(s);
    816 	}
    817 
    818 	if (async) {
    819 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
    820 		lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    821 		return 0;
    822 	}
    823 	if (bp != NULL) {
    824 		error = biowait(mbp);
    825 	}
    826 	s = splbio();
    827 	pool_put(&bufpool, mbp);
    828 	splx(s);
    829 	uvm_pagermapout(kva, npages);
    830 	raoffset = startoffset + totalbytes;
    831 
    832 	/*
    833 	 * if this we encountered a hole then we have to do a little more work.
    834 	 * for read faults, we marked the page PG_RDONLY so that future
    835 	 * write accesses to the page will fault again.
    836 	 * for write faults, we must make sure that the backing store for
    837 	 * the page is completely allocated while the pages are locked.
    838 	 */
    839 
    840 	if (error == 0 && sawhole && write) {
    841 		error = VOP_BALLOCN(vp, startoffset, npages << PAGE_SHIFT,
    842 				   cred, 0);
    843 		if (error) {
    844 			UVMHIST_LOG(ubchist, "balloc lbn 0x%x -> %d",
    845 				    lbn, error,0,0);
    846 			lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    847 			simple_lock(&uobj->vmobjlock);
    848 			goto out;
    849 		}
    850 	}
    851 	lockmgr(&vp->v_glock, LK_RELEASE, NULL);
    852 	simple_lock(&uobj->vmobjlock);
    853 
    854 	/*
    855 	 * see if we want to start any readahead.
    856 	 * XXXUBC for now, just read the next 128k on 64k boundaries.
    857 	 * this is pretty nonsensical, but it is 50% faster than reading
    858 	 * just the next 64k.
    859 	 */
    860 
    861 raout:
    862 	if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 &&
    863 	    PAGE_SHIFT <= 16) {
    864 		int racount;
    865 
    866 		racount = 1 << (16 - PAGE_SHIFT);
    867 		(void) VOP_GETPAGES(vp, raoffset, NULL, &racount, 0,
    868 				    VM_PROT_READ, 0, 0);
    869 		simple_lock(&uobj->vmobjlock);
    870 
    871 		racount = 1 << (16 - PAGE_SHIFT);
    872 		(void) VOP_GETPAGES(vp, raoffset + 0x10000, NULL, &racount, 0,
    873 				    VM_PROT_READ, 0, 0);
    874 		simple_lock(&uobj->vmobjlock);
    875 	}
    876 
    877 	/*
    878 	 * we're almost done!  release the pages...
    879 	 * for errors, we free the pages.
    880 	 * otherwise we activate them and mark them as valid and clean.
    881 	 * also, unbusy pages that were not actually requested.
    882 	 */
    883 
    884 out:
    885 	if (error) {
    886 		uvm_lock_pageq();
    887 		for (i = 0; i < npages; i++) {
    888 			if (pgs[i] == NULL) {
    889 				continue;
    890 			}
    891 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    892 				    pgs[i], pgs[i]->flags, 0,0);
    893 			if (pgs[i]->flags & PG_WANTED) {
    894 				wakeup(pgs[i]);
    895 			}
    896 			if (pgs[i]->flags & PG_RELEASED) {
    897 				uvm_unlock_pageq();
    898 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
    899 				uvm_lock_pageq();
    900 				continue;
    901 			}
    902 			if (pgs[i]->flags & PG_FAKE) {
    903 				uvm_pagefree(pgs[i]);
    904 				continue;
    905 			}
    906 			uvm_pageactivate(pgs[i]);
    907 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
    908 			UVM_PAGE_OWN(pgs[i], NULL);
    909 		}
    910 		uvm_unlock_pageq();
    911 		simple_unlock(&uobj->vmobjlock);
    912 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
    913 		return error;
    914 	}
    915 
    916 	UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
    917 	uvm_lock_pageq();
    918 	for (i = 0; i < npages; i++) {
    919 		if (pgs[i] == NULL) {
    920 			continue;
    921 		}
    922 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    923 			    pgs[i], pgs[i]->flags, 0,0);
    924 		if (pgs[i]->flags & PG_FAKE) {
    925 			UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
    926 				    pgs[i], pgs[i]->offset,0,0);
    927 			pgs[i]->flags &= ~(PG_FAKE);
    928 			pmap_clear_modify(pgs[i]);
    929 			pmap_clear_reference(pgs[i]);
    930 		}
    931 		if (write) {
    932 			pgs[i]->flags &= ~(PG_RDONLY);
    933 		}
    934 		if (i < ridx || i >= ridx + orignpages || async) {
    935 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
    936 				    pgs[i], pgs[i]->offset,0,0);
    937 			if (pgs[i]->flags & PG_WANTED) {
    938 				wakeup(pgs[i]);
    939 			}
    940 			if (pgs[i]->flags & PG_RELEASED) {
    941 				uvm_unlock_pageq();
    942 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
    943 				uvm_lock_pageq();
    944 				continue;
    945 			}
    946 			uvm_pageactivate(pgs[i]);
    947 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
    948 			UVM_PAGE_OWN(pgs[i], NULL);
    949 		}
    950 	}
    951 	uvm_unlock_pageq();
    952 	simple_unlock(&uobj->vmobjlock);
    953 	if (ap->a_m != NULL) {
    954 		memcpy(ap->a_m, &pgs[ridx],
    955 		       orignpages * sizeof(struct vm_page *));
    956 	}
    957 	return 0;
    958 }
    959 
    960 /*
    961  * generic VM putpages routine.
    962  * Write the given range of pages to backing store.
    963  */
    964 
    965 int
    966 genfs_putpages(v)
    967 	void *v;
    968 {
    969 	struct vop_putpages_args /* {
    970 		struct vnode *a_vp;
    971 		struct vm_page **a_m;
    972 		int a_count;
    973 		int a_flags;
    974 		int *a_rtvals;
    975 	} */ *ap = v;
    976 
    977 	int s, error, npages, run;
    978 	int fs_bshift, dev_bshift, dev_bsize;
    979 	vaddr_t kva;
    980 	off_t eof, offset, startoffset;
    981 	size_t bytes, iobytes, skipbytes;
    982 	daddr_t lbn, blkno;
    983 	struct vm_page *pg;
    984 	struct buf *mbp, *bp;
    985 	struct vnode *vp = ap->a_vp;
    986 	struct vnode *devvp;
    987 	boolean_t async = (ap->a_flags & PGO_SYNCIO) == 0;
    988 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
    989 	UVMHIST_LOG(ubchist, "vp %p offset 0x%x count %d",
    990 		    vp, ap->a_m[0]->offset, ap->a_count, 0);
    991 
    992 	simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    993 
    994 	error = VOP_SIZE(vp, vp->v_uvm.u_size, &eof);
    995 	if (error) {
    996 		return error;
    997 	}
    998 
    999 	error = 0;
   1000 	npages = ap->a_count;
   1001 	if (vp->v_type == VREG) {
   1002 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1003 		dev_bshift = vp->v_mount->mnt_dev_bshift;
   1004 	} else {
   1005 		fs_bshift = DEV_BSHIFT;
   1006 		dev_bshift = DEV_BSHIFT;
   1007 	}
   1008 	dev_bsize = 1 << dev_bshift;
   1009 	KASSERT((eof & (dev_bsize - 1)) == 0);
   1010 
   1011 	pg = ap->a_m[0];
   1012 	startoffset = pg->offset;
   1013 	bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   1014 	skipbytes = 0;
   1015 	KASSERT(bytes != 0);
   1016 
   1017 	kva = uvm_pagermapin(ap->a_m, npages, UVMPAGER_MAPIN_WAITOK);
   1018 
   1019 	s = splbio();
   1020 	vp->v_numoutput += 2;
   1021 	mbp = pool_get(&bufpool, PR_WAITOK);
   1022 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1023 		    vp, mbp, vp->v_numoutput, bytes);
   1024 	splx(s);
   1025 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1026 	mbp->b_data = (void *)kva;
   1027 	mbp->b_resid = mbp->b_bcount = bytes;
   1028 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
   1029 		(async ? B_CALL : 0) |
   1030 		(curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
   1031 	mbp->b_iodone = uvm_aio_biodone;
   1032 	mbp->b_vp = vp;
   1033 	LIST_INIT(&mbp->b_dep);
   1034 
   1035 	bp = NULL;
   1036 	for (offset = startoffset;
   1037 	     bytes > 0;
   1038 	     offset += iobytes, bytes -= iobytes) {
   1039 		lbn = offset >> fs_bshift;
   1040 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
   1041 		if (error) {
   1042 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
   1043 			skipbytes += bytes;
   1044 			bytes = 0;
   1045 			break;
   1046 		}
   1047 
   1048 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1049 		    bytes);
   1050 		if (blkno == (daddr_t)-1) {
   1051 			skipbytes += iobytes;
   1052 			continue;
   1053 		}
   1054 
   1055 		/* if it's really one i/o, don't make a second buf */
   1056 		if (offset == startoffset && iobytes == bytes) {
   1057 			bp = mbp;
   1058 		} else {
   1059 			s = splbio();
   1060 			vp->v_numoutput++;
   1061 			bp = pool_get(&bufpool, PR_WAITOK);
   1062 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1063 				    vp, bp, vp->v_numoutput, 0);
   1064 			splx(s);
   1065 			bp->b_data = (char *)kva +
   1066 				(vaddr_t)(offset - pg->offset);
   1067 			bp->b_resid = bp->b_bcount = iobytes;
   1068 			bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
   1069 			bp->b_iodone = uvm_aio_biodone1;
   1070 			bp->b_vp = vp;
   1071 			LIST_INIT(&bp->b_dep);
   1072 		}
   1073 		bp->b_lblkno = 0;
   1074 		bp->b_private = mbp;
   1075 		bp->b_devvp = devvp;
   1076 
   1077 		/* adjust physical blkno for partial blocks */
   1078 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
   1079 				       dev_bshift);
   1080 		UVMHIST_LOG(ubchist, "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1081 			    vp, offset, bp->b_bcount, bp->b_blkno);
   1082 		VOP_STRATEGY(bp);
   1083 	}
   1084 	if (skipbytes) {
   1085 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   1086 		s = splbio();
   1087 		mbp->b_resid -= skipbytes;
   1088 		if (error) {
   1089 			mbp->b_flags |= B_ERROR;
   1090 			mbp->b_error = error;
   1091 		}
   1092 		if (mbp->b_resid == 0) {
   1093 			biodone(mbp);
   1094 		}
   1095 		splx(s);
   1096 	}
   1097 	if (async) {
   1098 		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
   1099 		return 0;
   1100 	}
   1101 	if (bp != NULL) {
   1102 		UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
   1103 		error = biowait(mbp);
   1104 	}
   1105 	if (bioops.io_pageiodone) {
   1106 		(*bioops.io_pageiodone)(mbp);
   1107 	}
   1108 	s = splbio();
   1109 	vwakeup(mbp);
   1110 	pool_put(&bufpool, mbp);
   1111 	splx(s);
   1112 	uvm_pagermapout(kva, npages);
   1113 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
   1114 	return error;
   1115 }
   1116 
   1117 int
   1118 genfs_size(v)
   1119 	void *v;
   1120 {
   1121 	struct vop_size_args /* {
   1122 		struct vnode *a_vp;
   1123 		off_t a_size;
   1124 		off_t *a_eobp;
   1125 	} */ *ap = v;
   1126 	int bsize;
   1127 
   1128 	bsize = 1 << ap->a_vp->v_mount->mnt_fs_bshift;
   1129 	*ap->a_eobp = (ap->a_size + bsize - 1) & ~(bsize - 1);
   1130 	return 0;
   1131 }
   1132