Home | History | Annotate | Line # | Download | only in genfs
genfs_vnops.c revision 1.11.4.5
      1 /*	$NetBSD: genfs_vnops.c,v 1.11.4.5 1999/08/02 22:27:34 thorpej 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/malloc.h>
     47 #include <sys/poll.h>
     48 
     49 #include <miscfs/genfs/genfs.h>
     50 #include <miscfs/specfs/specdev.h>
     51 
     52 #include <vm/vm.h>
     53 #include <uvm/uvm.h>
     54 
     55 #ifdef NFSSERVER
     56 #include <nfs/rpcv2.h>
     57 #include <nfs/nfsproto.h>
     58 #include <nfs/nfs.h>
     59 #include <nfs/nqnfs.h>
     60 #include <nfs/nfs_var.h>
     61 #endif
     62 
     63 int
     64 genfs_poll(v)
     65 	void *v;
     66 {
     67 	struct vop_poll_args /* {
     68 		struct vnode *a_vp;
     69 		int a_events;
     70 		struct proc *a_p;
     71 	} */ *ap = v;
     72 
     73 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
     74 }
     75 
     76 int
     77 genfs_fsync(v)
     78 	void *v;
     79 {
     80 	struct vop_fsync_args /* {
     81 		struct vnode *a_vp;
     82 		struct ucred *a_cred;
     83 		int a_flags;
     84 		struct proc *a_p;
     85 	} */ *ap = v;
     86 	register struct vnode *vp = ap->a_vp;
     87 	int wait;
     88 
     89 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
     90 	vflushbuf(vp, wait);
     91 	if ((ap->a_flags & FSYNC_DATAONLY) != 0)
     92 		return (0);
     93 	else
     94 		return (VOP_UPDATE(ap->a_vp, NULL, NULL, wait));
     95 }
     96 
     97 int
     98 genfs_seek(v)
     99 	void *v;
    100 {
    101 	struct vop_seek_args /* {
    102 		struct vnode *a_vp;
    103 		off_t a_oldoff;
    104 		off_t a_newoff;
    105 		struct ucred *a_ucred;
    106 	} */ *ap = v;
    107 
    108 	if (ap->a_newoff < 0)
    109 		return (EINVAL);
    110 
    111 	return (0);
    112 }
    113 
    114 int
    115 genfs_abortop(v)
    116 	void *v;
    117 {
    118 	struct vop_abortop_args /* {
    119 		struct vnode *a_dvp;
    120 		struct componentname *a_cnp;
    121 	} */ *ap = v;
    122 
    123 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    124 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
    125 	return (0);
    126 }
    127 
    128 /*ARGSUSED*/
    129 int
    130 genfs_badop(v)
    131 	void *v;
    132 {
    133 
    134 	panic("genfs: bad op");
    135 }
    136 
    137 /*ARGSUSED*/
    138 int
    139 genfs_nullop(v)
    140 	void *v;
    141 {
    142 
    143 	return (0);
    144 }
    145 
    146 /*ARGSUSED*/
    147 int
    148 genfs_einval(v)
    149 	void *v;
    150 {
    151 
    152 	return (EINVAL);
    153 }
    154 
    155 /*ARGSUSED*/
    156 int
    157 genfs_eopnotsupp(v)
    158 	void *v;
    159 {
    160 
    161 	return (EOPNOTSUPP);
    162 }
    163 
    164 /*
    165  * Called when an fs doesn't support a particular vop but the vop needs to
    166  * vrele, vput, or vunlock passed in vnodes.
    167  */
    168 int
    169 genfs_eopnotsupp_rele(v)
    170 	void *v;
    171 {
    172 	struct vop_generic_args /*
    173 		struct vnodeop_desc *a_desc;
    174 		/ * other random data follows, presumably * /
    175 	} */ *ap = v;
    176 	struct vnodeop_desc *desc = ap->a_desc;
    177 	struct vnode *vp;
    178 	int flags, i, j, offset;
    179 
    180 	flags = desc->vdesc_flags;
    181 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
    182 		if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
    183 			break;	/* stop at end of list */
    184 		if ((j = flags & VDESC_VP0_WILLPUT)) {
    185 			vp = *VOPARG_OFFSETTO(struct vnode**,offset,ap);
    186 			switch (j) {
    187 			case VDESC_VP0_WILLPUT:
    188 				vput(vp);
    189 				break;
    190 			case VDESC_VP0_WILLUNLOCK:
    191 				VOP_UNLOCK(vp, 0);
    192 				break;
    193 			case VDESC_VP0_WILLRELE:
    194 				vrele(vp);
    195 				break;
    196 			}
    197 		}
    198 	}
    199 
    200 	return (EOPNOTSUPP);
    201 }
    202 
    203 /*ARGSUSED*/
    204 int
    205 genfs_ebadf(v)
    206 	void *v;
    207 {
    208 
    209 	return (EBADF);
    210 }
    211 
    212 /* ARGSUSED */
    213 int
    214 genfs_enoioctl(v)
    215 	void *v;
    216 {
    217 
    218 	return (ENOTTY);
    219 }
    220 
    221 
    222 /*
    223  * Eliminate all activity associated with  the requested vnode
    224  * and with all vnodes aliased to the requested vnode.
    225  */
    226 int
    227 genfs_revoke(v)
    228 	void *v;
    229 {
    230 	struct vop_revoke_args /* {
    231 		struct vnode *a_vp;
    232 		int a_flags;
    233 	} */ *ap = v;
    234 	struct vnode *vp, *vq;
    235 	struct proc *p = curproc;	/* XXX */
    236 
    237 #ifdef DIAGNOSTIC
    238 	if ((ap->a_flags & REVOKEALL) == 0)
    239 		panic("genfs_revoke: not revokeall");
    240 #endif
    241 
    242 	vp = ap->a_vp;
    243 	simple_lock(&vp->v_interlock);
    244 
    245 	if (vp->v_flag & VALIASED) {
    246 		/*
    247 		 * If a vgone (or vclean) is already in progress,
    248 		 * wait until it is done and return.
    249 		 */
    250 		if (vp->v_flag & VXLOCK) {
    251 			vp->v_flag |= VXWANT;
    252 			simple_unlock(&vp->v_interlock);
    253 			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
    254 			return (0);
    255 		}
    256 		/*
    257 		 * Ensure that vp will not be vgone'd while we
    258 		 * are eliminating its aliases.
    259 		 */
    260 		vp->v_flag |= VXLOCK;
    261 		simple_unlock(&vp->v_interlock);
    262 		while (vp->v_flag & VALIASED) {
    263 			simple_lock(&spechash_slock);
    264 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
    265 				if (vq->v_rdev != vp->v_rdev ||
    266 				    vq->v_type != vp->v_type || vp == vq)
    267 					continue;
    268 				simple_unlock(&spechash_slock);
    269 				vgone(vq);
    270 				break;
    271 			}
    272 			if (vq == NULLVP)
    273 				simple_unlock(&spechash_slock);
    274 		}
    275 		/*
    276 		 * Remove the lock so that vgone below will
    277 		 * really eliminate the vnode after which time
    278 		 * vgone will awaken any sleepers.
    279 		 */
    280 		simple_lock(&vp->v_interlock);
    281 		vp->v_flag &= ~VXLOCK;
    282 	}
    283 	vgonel(vp, p);
    284 	return (0);
    285 }
    286 
    287 /*
    288  * Lock the node.
    289  */
    290 int
    291 genfs_lock(v)
    292 	void *v;
    293 {
    294 	struct vop_lock_args /* {
    295 		struct vnode *a_vp;
    296 		int a_flags;
    297 		struct proc *a_p;
    298 	} */ *ap = v;
    299 	struct vnode *vp = ap->a_vp;
    300 
    301 	return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock));
    302 }
    303 
    304 /*
    305  * Unlock the node.
    306  */
    307 int
    308 genfs_unlock(v)
    309 	void *v;
    310 {
    311 	struct vop_unlock_args /* {
    312 		struct vnode *a_vp;
    313 		int a_flags;
    314 		struct proc *a_p;
    315 	} */ *ap = v;
    316 	struct vnode *vp = ap->a_vp;
    317 
    318 	return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
    319 		&vp->v_interlock));
    320 }
    321 
    322 /*
    323  * Return whether or not the node is locked.
    324  */
    325 int
    326 genfs_islocked(v)
    327 	void *v;
    328 {
    329 	struct vop_islocked_args /* {
    330 		struct vnode *a_vp;
    331 	} */ *ap = v;
    332 	struct vnode *vp = ap->a_vp;
    333 
    334 	return (lockstatus(&vp->v_lock));
    335 }
    336 
    337 /*
    338  * Stubs to use when there is no locking to be done on the underlying object.
    339  */
    340 int
    341 genfs_nolock(v)
    342 	void *v;
    343 {
    344 	struct vop_lock_args /* {
    345 		struct vnode *a_vp;
    346 		int a_flags;
    347 		struct proc *a_p;
    348 	} */ *ap = v;
    349 
    350 	/*
    351 	 * Since we are not using the lock manager, we must clear
    352 	 * the interlock here.
    353 	 */
    354 	if (ap->a_flags & LK_INTERLOCK)
    355 		simple_unlock(&ap->a_vp->v_interlock);
    356 	return (0);
    357 }
    358 
    359 int
    360 genfs_nounlock(v)
    361 	void *v;
    362 {
    363 	return (0);
    364 }
    365 
    366 int
    367 genfs_noislocked(v)
    368 	void *v;
    369 {
    370 	return (0);
    371 }
    372 
    373 /*
    374  * Local lease check for NFS servers.  Just set up args and let
    375  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
    376  * this is a null operation.
    377  */
    378 int
    379 genfs_lease_check(v)
    380 	void *v;
    381 {
    382 #ifdef NFSSERVER
    383 	struct vop_lease_args /* {
    384 		struct vnode *a_vp;
    385 		struct proc *a_p;
    386 		struct ucred *a_cred;
    387 		int a_flag;
    388 	} */ *ap = v;
    389 	u_int32_t duration = 0;
    390 	int cache;
    391 	u_quad_t frev;
    392 
    393 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
    394 	    NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
    395 	return (0);
    396 #else
    397 	return (0);
    398 #endif /* NFSSERVER */
    399 }
    400 
    401 /*
    402  * generic VM getpages routine.
    403  * Return PG_BUSY pages for the given range,
    404  * reading from backing store if necessary.
    405  */
    406 
    407 int
    408 genfs_getpages(v)
    409 	void *v;
    410 {
    411 	struct vop_getpages_args /* {
    412 		struct vnode *a_vp;
    413 		vaddr_t a_offset;
    414 		vm_page_t *a_m;
    415 		int *a_count;
    416 		int a_centeridx;
    417 		vm_prot_t a_access_type;
    418 		int a_advice;
    419 		int a_flags;
    420 	} */ *ap = v;
    421 
    422 	off_t eof, offset, origoffset, startoffset, endoffset;
    423 	daddr_t lbn, blkno;
    424 	int s, i, error, npages, npgs, run, ridx, pidx, pcount;
    425 	int bsize, bshift, dev_bshift, dev_bsize;
    426 	int flags = ap->a_flags;
    427 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    428 	boolean_t sawhole = FALSE;
    429 	char *kva;
    430 	struct buf *bp, *mbp;
    431 	struct vnode *vp = ap->a_vp;
    432 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
    433 	struct vm_page *pgs[16];			/* XXX 16 */
    434 	struct ucred *cred = curproc->p_ucred;		/* XXX curproc */
    435 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
    436 
    437 #ifdef DIAGNOSTIC
    438 	if (ap->a_centeridx < 0 || ap->a_centeridx > *ap->a_count) {
    439 		panic("genfs_getpages: centeridx %d out of range",
    440 		      ap->a_centeridx);
    441 	}
    442 	if (ap->a_offset & (PAGE_SIZE - 1)) {
    443 		panic("genfs_getpages: offset 0x%x", (int)ap->a_offset);
    444 	}
    445 	if (*ap->a_count < 0) {
    446 		panic("genfs_getpages: count %d < 0", *ap->a_count);
    447 	}
    448 #endif
    449 
    450 	/*
    451 	 * Bounds-check the request.
    452 	 */
    453 
    454 	eof = vp->v_uvm.u_size;
    455 	if (ap->a_offset >= eof) {
    456 		if ((flags & PGO_LOCKED) == 0) {
    457 			simple_unlock(&uobj->vmobjlock);
    458 		}
    459 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
    460 			    (int)ap->a_offset, *ap->a_count, (int)eof,0);
    461 		return EINVAL;
    462 	}
    463 
    464 	/*
    465 	 * For PGO_LOCKED requests, just return whatever's in memory.
    466 	 */
    467 
    468 	if (flags & PGO_LOCKED) {
    469 		uvn_findpages(uobj, ap->a_offset, ap->a_count, ap->a_m,
    470 			      UFP_NOWAIT|UFP_NOALLOC|UFP_NORDONLY);
    471 
    472 		return ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
    473 	}
    474 
    475 	if (ap->a_offset + ((*ap->a_count - 1) << PAGE_SHIFT) >= eof) {
    476 		panic("genfs_getpages: non LOCKED req past EOF vp %p", vp);
    477 	}
    478 
    479 	/* vnode is VOP_LOCKed, uobj is locked */
    480 
    481 	error = 0;
    482 
    483 	/*
    484 	 * find the requested pages and make some simple checks.
    485 	 * leave space in the page array for a whole block.
    486 	 */
    487 
    488 	bshift = vp->v_mount->mnt_fs_bshift;
    489 	bsize = 1 << bshift;
    490 	dev_bshift = vp->v_mount->mnt_dev_bshift;
    491 	dev_bsize = 1 << dev_bshift;
    492 
    493 	npages = *ap->a_count;
    494 	origoffset = ap->a_offset;
    495 	startoffset = origoffset & ~((off_t)bsize - 1);
    496 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
    497 				+ bsize - 1) & ~((off_t)bsize - 1));
    498 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
    499 
    500 	memset(pgs, 0, sizeof(pgs));
    501 	uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
    502 
    503 	/*
    504 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
    505 	 * PGO_OVERWRITE also means that the caller guarantees
    506 	 * that the pages already have backing store allocated.
    507 	 */
    508 
    509 	if (flags & PGO_OVERWRITE) {
    510 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
    511 
    512 		/* XXX for now, zero the page if we allocated it */
    513 		for (i = 0; i < npages; i++) {
    514 			struct vm_page *pg = pgs[ridx + i];
    515 			if (pg->flags & PG_FAKE) {
    516 				uvm_pagezero(pg);
    517 				pg->flags &= ~PG_FAKE;
    518 			}
    519 		}
    520 
    521 		simple_unlock(&uobj->vmobjlock);
    522 		goto out;
    523 	}
    524 
    525 	/*
    526 	 * if the pages are already resident, just return them.
    527 	 */
    528 
    529 	for (i = 0; i < npages; i++) {
    530 		struct vm_page *pg = pgs[ridx + i];
    531 
    532 		if ((pg->flags & PG_FAKE) != 0 ||
    533 		    ((ap->a_access_type & VM_PROT_WRITE) &&
    534 		      (pg->flags & PG_RDONLY))) {
    535 			break;
    536 		}
    537 	}
    538 	if (i == npages) {
    539 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
    540 		simple_unlock(&uobj->vmobjlock);
    541 		goto out;
    542 	}
    543 
    544 	/*
    545 	 * the page wasn't resident and we're not overwriting,
    546 	 * so we're going to have to do some i/o.
    547 	 * find any additional pages needed to cover the expanded range.
    548 	 */
    549 
    550 	if (startoffset != origoffset) {
    551 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
    552 			    (int)startoffset, (int)endoffset, 0,0);
    553 		npages = (endoffset - startoffset) >> PAGE_SHIFT;
    554 		if (npages == 0) {
    555 			panic("XXX getpages npages = 0");
    556 		}
    557 		npgs = npages;
    558 		uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
    559 	}
    560 	simple_unlock(&uobj->vmobjlock);
    561 
    562 	/*
    563 	 * read the desired page(s).
    564 	 */
    565 
    566 	totalbytes = npages << PAGE_SHIFT;
    567 	bytes = min(totalbytes,
    568 		    (vp->v_uvm.u_size - startoffset + dev_bsize - 1) &
    569 		    ~(dev_bsize - 1));
    570 	tailbytes = totalbytes - bytes;
    571 	skipbytes = 0;
    572 
    573 	kva = (void *)uvm_pagermapin(pgs, npages, M_WAITOK);
    574 
    575 	s = splbio();
    576 	mbp = pool_get(&bufpool, PR_WAITOK);
    577 	splx(s);
    578 	mbp->b_bufsize = bytes;
    579 	mbp->b_data = kva;
    580 	mbp->b_resid = mbp->b_bcount = bytes;
    581 	mbp->b_flags = B_BUSY|B_READ| (flags & PGO_SYNCIO ? 0 : B_CALL);
    582 	mbp->b_iodone = uvm_aio_biodone;
    583 	mbp->b_vp = vp;
    584 
    585 	/*
    586 	 * if EOF is in the middle of the last page, zero the part past EOF.
    587 	 */
    588 
    589 	if (tailbytes > 0) {
    590 		memset(kva + bytes, 0, tailbytes);
    591 	}
    592 
    593 	/*
    594 	 * now loop over the pages, reading as needed.
    595 	 */
    596 
    597 	bp = NULL;
    598 	offset = startoffset;
    599 	for (; bytes > 0; offset += iobytes, bytes -= iobytes) {
    600 
    601 		/*
    602 		 * skip pages which don't need to be read.
    603 		 */
    604 
    605 		pidx = (offset - startoffset) >> PAGE_SHIFT;
    606 		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
    607 			size_t b;
    608 
    609 			if (offset & (PAGE_SIZE - 1)) {
    610 				panic("genfs_getpages: skipping from middle "
    611 				      "of page");
    612 			}
    613 
    614 			b = min(PAGE_SIZE, bytes);
    615 			offset += b;
    616 			bytes -= b;
    617 			skipbytes += b;
    618 			pidx++;
    619 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
    620 				    (int)offset, 0,0,0);
    621 			if (bytes == 0) {
    622 				goto loopdone;
    623 			}
    624 		}
    625 
    626 		/*
    627 		 * bmap the file to find out the blkno to read from and
    628 		 * how much we can read in one i/o.  if bmap returns an error,
    629 		 * skip the rest of the top-level i/o.
    630 		 */
    631 
    632 		lbn = offset >> bshift;
    633 		error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
    634 		if (error) {
    635 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
    636 				    lbn, error,0,0);
    637 			skipbytes += bytes;
    638 			tailbytes = 0;
    639 			goto loopdone;
    640 		}
    641 
    642 		/*
    643 		 * see how many pages can be read with this i/o.
    644 		 * reduce the i/o size if necessary.
    645 		 */
    646 
    647 		iobytes = min(((lbn + 1 + run) << bshift) - offset, bytes);
    648 		if (offset + iobytes > round_page(offset)) {
    649 			pcount = 1;
    650 			while (pidx + pcount < npages &&
    651 			       pgs[pidx + pcount]->flags & PG_FAKE) {
    652 				pcount++;
    653 			}
    654 			iobytes = min(iobytes, (pcount << PAGE_SHIFT) -
    655 				      (offset - trunc_page(offset)));
    656 		}
    657 
    658 		/*
    659 		 * if this block isn't allocated, zero it instead of reading it.
    660 		 * if this is a read access, mark the pages we zeroed PG_RDONLY.
    661 		 */
    662 
    663 		if (blkno == (daddr_t)-1) {
    664 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
    665 
    666 			sawhole = TRUE;
    667 			memset(kva + (offset - startoffset), 0, iobytes);
    668 
    669 			if (ap->a_access_type == VM_PROT_READ) {
    670 				int holepages =
    671 					(round_page(offset + iobytes) -
    672 					 trunc_page(offset)) >> PAGE_SHIFT;
    673 				for (i = 0; i < holepages; i++) {
    674 					pgs[pidx + i]->flags |= PG_RDONLY;
    675 				}
    676 			}
    677 			continue;
    678 		}
    679 
    680 		/*
    681 		 * allocate a sub-buf for this piece of the i/o
    682 		 * (or just use mbp if there's only 1 piece),
    683 		 * and start it going.
    684 		 */
    685 
    686 		if (offset == startoffset && iobytes == bytes) {
    687 			bp = mbp;
    688 		} else {
    689 			s = splbio();
    690 			bp = pool_get(&bufpool, PR_WAITOK);
    691 			splx(s);
    692 			bp->b_data = kva + offset - startoffset;
    693 			bp->b_resid = bp->b_bcount = iobytes;
    694 			bp->b_flags = B_BUSY|B_READ|B_CALL;
    695 			bp->b_iodone = uvm_aio_biodone1;
    696 			bp->b_vp = vp;
    697 		}
    698 		bp->b_lblkno = 0;
    699 		bp->b_private = mbp;
    700 
    701 		/* adjust physical blkno for partial blocks */
    702 		bp->b_blkno = blkno + ((offset - (lbn << bshift)) >>
    703 				       dev_bshift);
    704 
    705 		UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
    706 			    bp, (int)offset, (int)iobytes, bp->b_blkno);
    707 
    708 		VOP_STRATEGY(bp);
    709 	}
    710 
    711 loopdone:
    712 	s = splbio();
    713 	if (skipbytes) {
    714 		mbp->b_resid -= skipbytes;
    715 		if (mbp->b_resid == 0) {
    716 			biodone(mbp);
    717 		}
    718 	}
    719 	splx(s);
    720 	if ((flags & PGO_SYNCIO) == 0) {
    721 		UVMHIST_LOG(ubchist, "returning PEND",0,0,0,0);
    722 		return EINPROGRESS;
    723 	}
    724 	if (bp != NULL) {
    725 		error = biowait(mbp);
    726 	}
    727 	s = splbio();
    728 	pool_put(&bufpool, mbp);
    729 	splx(s);
    730 	for (i = 0; i < npages; i++) {
    731 		UVMHIST_LOG(ubchist, "pgs[%d][0] = 0x%x",
    732 			    i, *(int *)(kva + (i << PAGE_SHIFT)), 0,0);
    733 	}
    734 	uvm_pagermapout((vaddr_t)kva, npages);
    735 
    736 	/*
    737 	 * if this we encountered a hole then we have to do a little more work.
    738 	 * for read faults, we must mark the page PG_RDONLY so that future
    739 	 * write accesses to the page will fault again.
    740 	 * for write faults, we must make sure that the backing store for
    741 	 * the page is completely allocated.
    742 	 */
    743 
    744 	if (sawhole && ap->a_access_type == VM_PROT_WRITE) {
    745 		error = VOP_BALLOC(vp, startoffset, npages << PAGE_SHIFT,
    746 				   cred, 0);
    747 		if (error) {
    748 			UVMHIST_LOG(ubchist, "balloc lbn 0x%x -> %d",
    749 				    lbn, error,0,0);
    750 			goto out;
    751 		}
    752 	}
    753 
    754 	/*
    755 	 * see if we want to start any readahead.
    756 	 * XXX writeme
    757 	 */
    758 
    759 	/*
    760 	 * we're almost done!  release the pages...
    761 	 * for errors, we free the pages.
    762 	 * otherwise we activate them and mark them as valid and clean.
    763 	 * also, unbusy all but the center page.
    764 	 */
    765 
    766 out:
    767 	if (error) {
    768 		simple_lock(&uobj->vmobjlock);
    769 		for (i = 0; i < npages; i++) {
    770 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    771 				    pgs[i], pgs[i]->flags, 0,0);
    772 			if (pgs[i]->flags & PG_FAKE) {
    773 				if (pgs[i]->flags & PG_WANTED) {
    774 					wakeup(pgs[i]);
    775 				}
    776 				uvm_pagefree(pgs[i]);
    777 			}
    778 		}
    779 		simple_unlock(&uobj->vmobjlock);
    780 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
    781 		return error;
    782 	}
    783 
    784 	UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
    785 	simple_lock(&uobj->vmobjlock);
    786 	for (i = 0; i < npages; i++) {
    787 		if (pgs[i] == NULL) {
    788 			continue;
    789 		}
    790 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
    791 			    pgs[i], pgs[i]->flags, 0,0);
    792 		if (pgs[i]->flags & PG_FAKE) {
    793 			UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
    794 				    pgs[i], (int)pgs[i]->offset,0,0);
    795 			pgs[i]->flags &= ~(PG_FAKE);
    796 			pmap_clear_modify(PMAP_PGARG(pgs[i]));
    797 			pmap_clear_reference(PMAP_PGARG(pgs[i]));
    798 		}
    799 		if (i < ridx || i >= ridx + *ap->a_count) {
    800 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
    801 				    pgs[i], (int)pgs[i]->offset,0,0);
    802 			/*
    803 			KASSERT((pgs[i]->flags & PG_RELEASED) == 0);
    804 			*/
    805 
    806 			if (pgs[i]->flags & PG_WANTED) {
    807 				wakeup(pgs[i]);
    808 			}
    809 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
    810 			UVM_PAGE_OWN(pgs[i], NULL);
    811 		}
    812 	}
    813 	simple_unlock(&uobj->vmobjlock);
    814 	memcpy(ap->a_m, &pgs[ridx], *ap->a_count * sizeof(struct vm_page *));
    815 	return 0;
    816 }
    817 
    818 /*
    819  * generic VM putpages routine.
    820  * Write the given range of pages to backing store.
    821  */
    822 int
    823 genfs_putpages(v)
    824 	void *v;
    825 {
    826 	struct vop_putpages_args /* {
    827 		struct vnode *a_vp;
    828 		struct vm_page **a_m;
    829 		int a_count;
    830 		int a_sync;
    831 		int *a_rtvals;
    832 	} */ *ap = v;
    833 
    834 	int s, error, npages, bshift, dev_bshift, dev_bsize, run;
    835 	char * kva;
    836 	off_t offset, startoffset;
    837 	size_t bytes, iobytes, skipbytes;
    838 	daddr_t lbn, blkno;
    839 	struct vm_page *pg;
    840 	struct buf *mbp, *bp;
    841 	struct vnode *vp = ap->a_vp;
    842 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
    843 
    844 	error = 0;
    845 	npages = ap->a_count;
    846 	bshift = vp->v_mount->mnt_fs_bshift;
    847 	dev_bshift = vp->v_mount->mnt_dev_bshift;
    848 	dev_bsize = 1 << dev_bshift;
    849 
    850 	pg = ap->a_m[0];
    851 	startoffset = pg->offset;
    852 	bytes = min(npages << PAGE_SHIFT,
    853 		    (vp->v_uvm.u_size - startoffset + dev_bsize - 1) &
    854 		    ~((off_t)dev_bsize - 1));
    855 	skipbytes = 0;
    856 
    857 	if (bytes == 0) {
    858 		panic("genfs_putpages: bytes == 0??? vp %p", vp);
    859 	}
    860 
    861 	kva = (void *)uvm_pagermapin(ap->a_m, npages, M_WAITOK);
    862 
    863 	s = splbio();
    864 	vp->v_numoutput++;
    865 	mbp = pool_get(&bufpool, PR_WAITOK);
    866 	UVMHIST_LOG(ubchist, "master vp %p bp %p num now %d",
    867 		    vp, mbp, vp->v_numoutput, 0);
    868 	splx(s);
    869 	mbp->b_bufsize = npages << PAGE_SHIFT;
    870 	mbp->b_data = kva;
    871 	mbp->b_resid = mbp->b_bcount = bytes;
    872 	mbp->b_flags = B_BUSY|B_WRITE| (ap->a_sync ? 0 : B_CALL) |
    873 		(curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
    874 	mbp->b_iodone = uvm_aio_biodone;
    875 	mbp->b_vp = vp;
    876 
    877 	bp = NULL;
    878 	offset = startoffset;
    879 	for (; bytes > 0; offset += iobytes, bytes -= iobytes) {
    880 		lbn = offset >> bshift;
    881 		error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
    882 		if (error) {
    883 			UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
    884 			goto errout;
    885 		}
    886 
    887 		iobytes = min(((lbn + 1 + run) << bshift) - offset, bytes);
    888 		if (blkno == (daddr_t)-1) {
    889 			skipbytes += iobytes;
    890 			continue;
    891 		}
    892 
    893 		/* if it's really one i/o, don't make a second buf */
    894 		if (offset == startoffset && iobytes == bytes) {
    895 			bp = mbp;
    896 		} else {
    897 			s = splbio();
    898 			vp->v_numoutput++;
    899 			bp = pool_get(&bufpool, PR_WAITOK);
    900 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
    901 				    vp, bp, vp->v_numoutput, 0);
    902 			splx(s);
    903 			bp->b_data = kva + offset - pg->offset;
    904 			bp->b_resid = bp->b_bcount = iobytes;
    905 			bp->b_flags = B_BUSY|B_WRITE|B_CALL;
    906 			bp->b_iodone = uvm_aio_biodone1;
    907 			bp->b_vp = vp;
    908 		}
    909 		bp->b_lblkno = 0;
    910 		bp->b_private = mbp;
    911 
    912 		/* adjust physical blkno for partial blocks */
    913 		bp->b_blkno = blkno + ((offset - (lbn << bshift)) >>
    914 				       dev_bshift);
    915 		UVMHIST_LOG(ubchist, "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
    916 			    vp, (int)offset, (int)bp->b_bcount,
    917 			    (int)bp->b_blkno);
    918 		VOP_STRATEGY(bp);
    919 	}
    920 	s = splbio();
    921 	if (skipbytes) {
    922 		mbp->b_resid -= skipbytes;
    923 		if (mbp->b_resid == 0) {
    924 			biodone(mbp);
    925 		}
    926 	}
    927 	splx(s);
    928 	if (!ap->a_sync) {
    929 		return EINPROGRESS;
    930 	}
    931 
    932 errout:
    933 	if (bp != NULL) {
    934 		error = biowait(mbp);
    935 	}
    936 	s = splbio();
    937 	pool_put(&bufpool, mbp);
    938 	splx(s);
    939 	uvm_pagermapout((vaddr_t)kva, npages);
    940 	UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
    941 	return error;
    942 }
    943