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