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