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