xref: /netbsd-src/sys/miscfs/genfs/genfs_vnops.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: genfs_vnops.c,v 1.125 2006/05/14 21:31:52 elad 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.125 2006/05/14 21:31:52 elad 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 #include <sys/kauth.h>
53 
54 #include <miscfs/genfs/genfs.h>
55 #include <miscfs/genfs/genfs_node.h>
56 #include <miscfs/specfs/specdev.h>
57 
58 #include <uvm/uvm.h>
59 #include <uvm/uvm_pager.h>
60 
61 #ifdef NFSSERVER
62 #include <nfs/rpcv2.h>
63 #include <nfs/nfsproto.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nqnfs.h>
66 #include <nfs/nfs_var.h>
67 #endif
68 
69 static inline void genfs_rel_pages(struct vm_page **, int);
70 static void filt_genfsdetach(struct knote *);
71 static int filt_genfsread(struct knote *, long);
72 static int filt_genfsvnode(struct knote *, long);
73 
74 #define MAX_READ_PAGES	16 	/* XXXUBC 16 */
75 
76 int
77 genfs_poll(void *v)
78 {
79 	struct vop_poll_args /* {
80 		struct vnode *a_vp;
81 		int a_events;
82 		struct lwp *a_l;
83 	} */ *ap = v;
84 
85 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
86 }
87 
88 int
89 genfs_seek(void *v)
90 {
91 	struct vop_seek_args /* {
92 		struct vnode *a_vp;
93 		off_t a_oldoff;
94 		off_t a_newoff;
95 		kauth_cred_t cred;
96 	} */ *ap = v;
97 
98 	if (ap->a_newoff < 0)
99 		return (EINVAL);
100 
101 	return (0);
102 }
103 
104 int
105 genfs_abortop(void *v)
106 {
107 	struct vop_abortop_args /* {
108 		struct vnode *a_dvp;
109 		struct componentname *a_cnp;
110 	} */ *ap = v;
111 
112 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
113 		PNBUF_PUT(ap->a_cnp->cn_pnbuf);
114 	return (0);
115 }
116 
117 int
118 genfs_fcntl(void *v)
119 {
120 	struct vop_fcntl_args /* {
121 		struct vnode *a_vp;
122 		u_int a_command;
123 		caddr_t a_data;
124 		int a_fflag;
125 		kauth_cred_t a_cred;
126 		struct lwp *a_l;
127 	} */ *ap = v;
128 
129 	if (ap->a_command == F_SETFL)
130 		return (0);
131 	else
132 		return (EOPNOTSUPP);
133 }
134 
135 /*ARGSUSED*/
136 int
137 genfs_badop(void *v)
138 {
139 
140 	panic("genfs: bad op");
141 }
142 
143 /*ARGSUSED*/
144 int
145 genfs_nullop(void *v)
146 {
147 
148 	return (0);
149 }
150 
151 /*ARGSUSED*/
152 int
153 genfs_einval(void *v)
154 {
155 
156 	return (EINVAL);
157 }
158 
159 /*
160  * Called when an fs doesn't support a particular vop.
161  * This takes care to vrele, vput, or vunlock passed in vnodes.
162  */
163 int
164 genfs_eopnotsupp(void *v)
165 {
166 	struct vop_generic_args /*
167 		struct vnodeop_desc *a_desc;
168 		/ * other random data follows, presumably * /
169 	} */ *ap = v;
170 	struct vnodeop_desc *desc = ap->a_desc;
171 	struct vnode *vp, *vp_last = NULL;
172 	int flags, i, j, offset;
173 
174 	flags = desc->vdesc_flags;
175 	for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
176 		if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
177 			break;	/* stop at end of list */
178 		if ((j = flags & VDESC_VP0_WILLPUT)) {
179 			vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
180 
181 			/* Skip if NULL */
182 			if (!vp)
183 				continue;
184 
185 			switch (j) {
186 			case VDESC_VP0_WILLPUT:
187 				/* Check for dvp == vp cases */
188 				if (vp == vp_last)
189 					vrele(vp);
190 				else {
191 					vput(vp);
192 					vp_last = vp;
193 				}
194 				break;
195 			case VDESC_VP0_WILLUNLOCK:
196 				VOP_UNLOCK(vp, 0);
197 				break;
198 			case VDESC_VP0_WILLRELE:
199 				vrele(vp);
200 				break;
201 			}
202 		}
203 	}
204 
205 	return (EOPNOTSUPP);
206 }
207 
208 /*ARGSUSED*/
209 int
210 genfs_ebadf(void *v)
211 {
212 
213 	return (EBADF);
214 }
215 
216 /* ARGSUSED */
217 int
218 genfs_enoioctl(void *v)
219 {
220 
221 	return (EPASSTHROUGH);
222 }
223 
224 
225 /*
226  * Eliminate all activity associated with the requested vnode
227  * and with all vnodes aliased to the requested vnode.
228  */
229 int
230 genfs_revoke(void *v)
231 {
232 	struct vop_revoke_args /* {
233 		struct vnode *a_vp;
234 		int a_flags;
235 	} */ *ap = v;
236 	struct vnode *vp, *vq;
237 	struct lwp *l = curlwp;		/* XXX */
238 
239 #ifdef DIAGNOSTIC
240 	if ((ap->a_flags & REVOKEALL) == 0)
241 		panic("genfs_revoke: not revokeall");
242 #endif
243 
244 	vp = ap->a_vp;
245 	simple_lock(&vp->v_interlock);
246 
247 	if (vp->v_flag & VALIASED) {
248 		/*
249 		 * If a vgone (or vclean) is already in progress,
250 		 * wait until it is done and return.
251 		 */
252 		if (vp->v_flag & VXLOCK) {
253 			vp->v_flag |= VXWANT;
254 			ltsleep(vp, PINOD|PNORELOCK, "vop_revokeall", 0,
255 				&vp->v_interlock);
256 			return (0);
257 		}
258 		/*
259 		 * Ensure that vp will not be vgone'd while we
260 		 * are eliminating its aliases.
261 		 */
262 		vp->v_flag |= VXLOCK;
263 		simple_unlock(&vp->v_interlock);
264 		while (vp->v_flag & VALIASED) {
265 			simple_lock(&spechash_slock);
266 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
267 				if (vq->v_rdev != vp->v_rdev ||
268 				    vq->v_type != vp->v_type || vp == vq)
269 					continue;
270 				simple_unlock(&spechash_slock);
271 				vgone(vq);
272 				break;
273 			}
274 			if (vq == NULLVP)
275 				simple_unlock(&spechash_slock);
276 		}
277 		/*
278 		 * Remove the lock so that vgone below will
279 		 * really eliminate the vnode after which time
280 		 * vgone will awaken any sleepers.
281 		 */
282 		simple_lock(&vp->v_interlock);
283 		vp->v_flag &= ~VXLOCK;
284 	}
285 	vgonel(vp, l);
286 	return (0);
287 }
288 
289 /*
290  * Lock the node.
291  */
292 int
293 genfs_lock(void *v)
294 {
295 	struct vop_lock_args /* {
296 		struct vnode *a_vp;
297 		int a_flags;
298 	} */ *ap = v;
299 	struct vnode *vp = ap->a_vp;
300 
301 	return (lockmgr(vp->v_vnlock, ap->a_flags, &vp->v_interlock));
302 }
303 
304 /*
305  * Unlock the node.
306  */
307 int
308 genfs_unlock(void *v)
309 {
310 	struct vop_unlock_args /* {
311 		struct vnode *a_vp;
312 		int a_flags;
313 	} */ *ap = v;
314 	struct vnode *vp = ap->a_vp;
315 
316 	return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE,
317 	    &vp->v_interlock));
318 }
319 
320 /*
321  * Return whether or not the node is locked.
322  */
323 int
324 genfs_islocked(void *v)
325 {
326 	struct vop_islocked_args /* {
327 		struct vnode *a_vp;
328 	} */ *ap = v;
329 	struct vnode *vp = ap->a_vp;
330 
331 	return (lockstatus(vp->v_vnlock));
332 }
333 
334 /*
335  * Stubs to use when there is no locking to be done on the underlying object.
336  */
337 int
338 genfs_nolock(void *v)
339 {
340 	struct vop_lock_args /* {
341 		struct vnode *a_vp;
342 		int a_flags;
343 		struct lwp *a_l;
344 	} */ *ap = v;
345 
346 	/*
347 	 * Since we are not using the lock manager, we must clear
348 	 * the interlock here.
349 	 */
350 	if (ap->a_flags & LK_INTERLOCK)
351 		simple_unlock(&ap->a_vp->v_interlock);
352 	return (0);
353 }
354 
355 int
356 genfs_nounlock(void *v)
357 {
358 
359 	return (0);
360 }
361 
362 int
363 genfs_noislocked(void *v)
364 {
365 
366 	return (0);
367 }
368 
369 /*
370  * Local lease check for NFS servers.  Just set up args and let
371  * nqsrv_getlease() do the rest.  If NFSSERVER is not in the kernel,
372  * this is a null operation.
373  */
374 int
375 genfs_lease_check(void *v)
376 {
377 #ifdef NFSSERVER
378 	struct vop_lease_args /* {
379 		struct vnode *a_vp;
380 		struct lwp *a_l;
381 		kauth_cred_t a_cred;
382 		int a_flag;
383 	} */ *ap = v;
384 	u_int32_t duration = 0;
385 	int cache;
386 	u_quad_t frev;
387 
388 	(void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
389 	    NQLOCALSLP, ap->a_l, (struct mbuf *)0, &cache, &frev, ap->a_cred);
390 	return (0);
391 #else
392 	return (0);
393 #endif /* NFSSERVER */
394 }
395 
396 int
397 genfs_mmap(void *v)
398 {
399 
400 	return (0);
401 }
402 
403 static inline void
404 genfs_rel_pages(struct vm_page **pgs, int npages)
405 {
406 	int i;
407 
408 	for (i = 0; i < npages; i++) {
409 		struct vm_page *pg = pgs[i];
410 
411 		if (pg == NULL)
412 			continue;
413 		if (pg->flags & PG_FAKE) {
414 			pg->flags |= PG_RELEASED;
415 		}
416 	}
417 	uvm_lock_pageq();
418 	uvm_page_unbusy(pgs, npages);
419 	uvm_unlock_pageq();
420 }
421 
422 /*
423  * generic VM getpages routine.
424  * Return PG_BUSY pages for the given range,
425  * reading from backing store if necessary.
426  */
427 
428 int
429 genfs_getpages(void *v)
430 {
431 	struct vop_getpages_args /* {
432 		struct vnode *a_vp;
433 		voff_t a_offset;
434 		struct vm_page **a_m;
435 		int *a_count;
436 		int a_centeridx;
437 		vm_prot_t a_access_type;
438 		int a_advice;
439 		int a_flags;
440 	} */ *ap = v;
441 
442 	off_t newsize, diskeof, memeof;
443 	off_t offset, origoffset, startoffset, endoffset;
444 	daddr_t lbn, blkno;
445 	int i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
446 	int fs_bshift, fs_bsize, dev_bshift;
447 	int flags = ap->a_flags;
448 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
449 	vaddr_t kva;
450 	struct buf *bp, *mbp;
451 	struct vnode *vp = ap->a_vp;
452 	struct vnode *devvp;
453 	struct genfs_node *gp = VTOG(vp);
454 	struct uvm_object *uobj = &vp->v_uobj;
455 	struct vm_page *pg, **pgs, *pgs_onstack[MAX_READ_PAGES];
456 	int pgs_size;
457 	kauth_cred_t cred = curproc->p_cred;		/* XXXUBC curlwp */
458 	boolean_t async = (flags & PGO_SYNCIO) == 0;
459 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
460 	boolean_t sawhole = FALSE;
461 	boolean_t overwrite = (flags & PGO_OVERWRITE) != 0;
462 	boolean_t blockalloc = write && (flags & PGO_NOBLOCKALLOC) == 0;
463 	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
464 
465 	UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d",
466 	    vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
467 
468 	KASSERT(vp->v_type == VREG || vp->v_type == VDIR ||
469 	    vp->v_type == VLNK || vp->v_type == VBLK);
470 
471 	/* XXXUBC temp limit */
472 	if (*ap->a_count > MAX_READ_PAGES) {
473 		panic("genfs_getpages: too many pages");
474 	}
475 
476 	error = 0;
477 	origoffset = ap->a_offset;
478 	orignpages = *ap->a_count;
479 	GOP_SIZE(vp, vp->v_size, &diskeof, 0);
480 	if (flags & PGO_PASTEOF) {
481 		newsize = MAX(vp->v_size,
482 		    origoffset + (orignpages << PAGE_SHIFT));
483 		GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_MEM);
484 	} else {
485 		GOP_SIZE(vp, vp->v_size, &memeof, GOP_SIZE_MEM);
486 	}
487 	KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
488 	KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
489 	KASSERT(orignpages > 0);
490 
491 	/*
492 	 * Bounds-check the request.
493 	 */
494 
495 	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
496 		if ((flags & PGO_LOCKED) == 0) {
497 			simple_unlock(&uobj->vmobjlock);
498 		}
499 		UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
500 		    origoffset, *ap->a_count, memeof,0);
501 		return (EINVAL);
502 	}
503 
504 	/* uobj is locked */
505 
506 	if ((flags & PGO_NOTIMESTAMP) == 0 &&
507 	    (vp->v_type != VBLK ||
508 	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
509 		int updflags = 0;
510 
511 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) {
512 			updflags = GOP_UPDATE_ACCESSED;
513 		}
514 		if (write) {
515 			updflags |= GOP_UPDATE_MODIFIED;
516 		}
517 		if (updflags != 0) {
518 			GOP_MARKUPDATE(vp, updflags);
519 		}
520 	}
521 
522 	if (write) {
523 		gp->g_dirtygen++;
524 		if ((vp->v_flag & VONWORKLST) == 0) {
525 			vn_syncer_add_to_worklist(vp, filedelay);
526 		}
527 		if ((vp->v_flag & (VWRITEMAP|VWRITEMAPDIRTY)) == VWRITEMAP) {
528 			vp->v_flag |= VWRITEMAPDIRTY;
529 		}
530 	}
531 
532 	/*
533 	 * For PGO_LOCKED requests, just return whatever's in memory.
534 	 */
535 
536 	if (flags & PGO_LOCKED) {
537 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
538 		    UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
539 
540 		return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
541 	}
542 
543 	/*
544 	 * find the requested pages and make some simple checks.
545 	 * leave space in the page array for a whole block.
546 	 */
547 
548 	if (vp->v_type != VBLK) {
549 		fs_bshift = vp->v_mount->mnt_fs_bshift;
550 		dev_bshift = vp->v_mount->mnt_dev_bshift;
551 	} else {
552 		fs_bshift = DEV_BSHIFT;
553 		dev_bshift = DEV_BSHIFT;
554 	}
555 	fs_bsize = 1 << fs_bshift;
556 
557 	orignpages = MIN(orignpages,
558 	    round_page(memeof - origoffset) >> PAGE_SHIFT);
559 	npages = orignpages;
560 	startoffset = origoffset & ~(fs_bsize - 1);
561 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT) +
562 	    fs_bsize - 1) & ~(fs_bsize - 1));
563 	endoffset = MIN(endoffset, round_page(memeof));
564 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
565 
566 	pgs_size = sizeof(struct vm_page *) *
567 	    ((endoffset - startoffset) >> PAGE_SHIFT);
568 	if (pgs_size > sizeof(pgs_onstack)) {
569 		pgs = malloc(pgs_size, M_DEVBUF, M_NOWAIT | M_ZERO);
570 		if (pgs == NULL) {
571 			simple_unlock(&uobj->vmobjlock);
572 			return (ENOMEM);
573 		}
574 	} else {
575 		pgs = pgs_onstack;
576 		memset(pgs, 0, pgs_size);
577 	}
578 	UVMHIST_LOG(ubchist, "ridx %d npages %d startoff %ld endoff %ld",
579 	    ridx, npages, startoffset, endoffset);
580 	if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx],
581 	    async ? UFP_NOWAIT : UFP_ALL) != orignpages) {
582 		KASSERT(async != 0);
583 		genfs_rel_pages(&pgs[ridx], orignpages);
584 		simple_unlock(&uobj->vmobjlock);
585 		if (pgs != pgs_onstack)
586 			free(pgs, M_DEVBUF);
587 		return (EBUSY);
588 	}
589 
590 	/*
591 	 * if the pages are already resident, just return them.
592 	 */
593 
594 	for (i = 0; i < npages; i++) {
595 		struct vm_page *pg1 = pgs[ridx + i];
596 
597 		if ((pg1->flags & PG_FAKE) ||
598 		    (blockalloc && (pg1->flags & PG_RDONLY))) {
599 			break;
600 		}
601 	}
602 	if (i == npages) {
603 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
604 		npages += ridx;
605 		goto out;
606 	}
607 
608 	/*
609 	 * if PGO_OVERWRITE is set, don't bother reading the pages.
610 	 */
611 
612 	if (overwrite) {
613 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
614 
615 		for (i = 0; i < npages; i++) {
616 			struct vm_page *pg1 = pgs[ridx + i];
617 
618 			pg1->flags &= ~(PG_RDONLY|PG_CLEAN);
619 		}
620 		npages += ridx;
621 		goto out;
622 	}
623 
624 	/*
625 	 * the page wasn't resident and we're not overwriting,
626 	 * so we're going to have to do some i/o.
627 	 * find any additional pages needed to cover the expanded range.
628 	 */
629 
630 	npages = (endoffset - startoffset) >> PAGE_SHIFT;
631 	if (startoffset != origoffset || npages != orignpages) {
632 
633 		/*
634 		 * we need to avoid deadlocks caused by locking
635 		 * additional pages at lower offsets than pages we
636 		 * already have locked.  unlock them all and start over.
637 		 */
638 
639 		genfs_rel_pages(&pgs[ridx], orignpages);
640 		memset(pgs, 0, pgs_size);
641 
642 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
643 		    startoffset, endoffset, 0,0);
644 		npgs = npages;
645 		if (uvn_findpages(uobj, startoffset, &npgs, pgs,
646 		    async ? UFP_NOWAIT : UFP_ALL) != npages) {
647 			KASSERT(async != 0);
648 			genfs_rel_pages(pgs, npages);
649 			simple_unlock(&uobj->vmobjlock);
650 			if (pgs != pgs_onstack)
651 				free(pgs, M_DEVBUF);
652 			return (EBUSY);
653 		}
654 	}
655 	simple_unlock(&uobj->vmobjlock);
656 
657 	/*
658 	 * read the desired page(s).
659 	 */
660 
661 	totalbytes = npages << PAGE_SHIFT;
662 	bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
663 	tailbytes = totalbytes - bytes;
664 	skipbytes = 0;
665 
666 	kva = uvm_pagermapin(pgs, npages,
667 	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
668 
669 	mbp = getiobuf();
670 	mbp->b_bufsize = totalbytes;
671 	mbp->b_data = (void *)kva;
672 	mbp->b_resid = mbp->b_bcount = bytes;
673 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
674 	mbp->b_iodone = (async ? uvm_aio_biodone : 0);
675 	mbp->b_vp = vp;
676 	if (async)
677 		BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
678 	else
679 		BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
680 
681 	/*
682 	 * if EOF is in the middle of the range, zero the part past EOF.
683 	 * if the page including EOF is not PG_FAKE, skip over it since
684 	 * in that case it has valid data that we need to preserve.
685 	 */
686 
687 	if (tailbytes > 0) {
688 		size_t tailstart = bytes;
689 
690 		if ((pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE) == 0) {
691 			tailstart = round_page(tailstart);
692 			tailbytes -= tailstart - bytes;
693 		}
694 		UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x",
695 		    kva, tailstart, tailbytes,0);
696 		memset((void *)(kva + tailstart), 0, tailbytes);
697 	}
698 
699 	/*
700 	 * now loop over the pages, reading as needed.
701 	 */
702 
703 	if (blockalloc) {
704 		lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
705 	} else {
706 		lockmgr(&gp->g_glock, LK_SHARED, NULL);
707 	}
708 
709 	bp = NULL;
710 	for (offset = startoffset;
711 	    bytes > 0;
712 	    offset += iobytes, bytes -= iobytes) {
713 
714 		/*
715 		 * skip pages which don't need to be read.
716 		 */
717 
718 		pidx = (offset - startoffset) >> PAGE_SHIFT;
719 		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
720 			size_t b;
721 
722 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
723 			if ((pgs[pidx]->flags & PG_RDONLY)) {
724 				sawhole = TRUE;
725 			}
726 			b = MIN(PAGE_SIZE, bytes);
727 			offset += b;
728 			bytes -= b;
729 			skipbytes += b;
730 			pidx++;
731 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
732 			    offset, 0,0,0);
733 			if (bytes == 0) {
734 				goto loopdone;
735 			}
736 		}
737 
738 		/*
739 		 * bmap the file to find out the blkno to read from and
740 		 * how much we can read in one i/o.  if bmap returns an error,
741 		 * skip the rest of the top-level i/o.
742 		 */
743 
744 		lbn = offset >> fs_bshift;
745 		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
746 		if (error) {
747 			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
748 			    lbn, error,0,0);
749 			skipbytes += bytes;
750 			goto loopdone;
751 		}
752 
753 		/*
754 		 * see how many pages can be read with this i/o.
755 		 * reduce the i/o size if necessary to avoid
756 		 * overwriting pages with valid data.
757 		 */
758 
759 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
760 		    bytes);
761 		if (offset + iobytes > round_page(offset)) {
762 			pcount = 1;
763 			while (pidx + pcount < npages &&
764 			    pgs[pidx + pcount]->flags & PG_FAKE) {
765 				pcount++;
766 			}
767 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
768 			    (offset - trunc_page(offset)));
769 		}
770 
771 		/*
772 		 * if this block isn't allocated, zero it instead of
773 		 * reading it.  unless we are going to allocate blocks,
774 		 * mark the pages we zeroed PG_RDONLY.
775 		 */
776 
777 		if (blkno < 0) {
778 			int holepages = (round_page(offset + iobytes) -
779 			    trunc_page(offset)) >> PAGE_SHIFT;
780 			UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
781 
782 			sawhole = TRUE;
783 			memset((char *)kva + (offset - startoffset), 0,
784 			    iobytes);
785 			skipbytes += iobytes;
786 
787 			for (i = 0; i < holepages; i++) {
788 				if (write) {
789 					pgs[pidx + i]->flags &= ~PG_CLEAN;
790 				}
791 				if (!blockalloc) {
792 					pgs[pidx + i]->flags |= PG_RDONLY;
793 				}
794 			}
795 			continue;
796 		}
797 
798 		/*
799 		 * allocate a sub-buf for this piece of the i/o
800 		 * (or just use mbp if there's only 1 piece),
801 		 * and start it going.
802 		 */
803 
804 		if (offset == startoffset && iobytes == bytes) {
805 			bp = mbp;
806 		} else {
807 			bp = getiobuf();
808 			nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
809 		}
810 		bp->b_lblkno = 0;
811 
812 		/* adjust physical blkno for partial blocks */
813 		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
814 		    dev_bshift);
815 
816 		UVMHIST_LOG(ubchist,
817 		    "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
818 		    bp, offset, iobytes, bp->b_blkno);
819 
820 		VOP_STRATEGY(devvp, bp);
821 	}
822 
823 loopdone:
824 	nestiobuf_done(mbp, skipbytes, error);
825 	if (async) {
826 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
827 		lockmgr(&gp->g_glock, LK_RELEASE, NULL);
828 		if (pgs != pgs_onstack)
829 			free(pgs, M_DEVBUF);
830 		return (0);
831 	}
832 	if (bp != NULL) {
833 		error = biowait(mbp);
834 	}
835 	putiobuf(mbp);
836 	uvm_pagermapout(kva, npages);
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 != VBLK ||
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, 0);
1429 	if (vp->v_type != VBLK) {
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 	kauth_cred_t cred = curproc->p_cred;
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_rw = UIO_READ;
1623 		uio.uio_resid = PAGE_SIZE;
1624 		UIO_SETUP_SYSSPACE(&uio);
1625 		/* XXX vn_lock */
1626 		error = VOP_READ(vp, &uio, 0, cred);
1627 		if (error) {
1628 			break;
1629 		}
1630 		if (uio.uio_resid) {
1631 			memset(iov.iov_base, 0, uio.uio_resid);
1632 		}
1633 	}
1634 	uvm_pagermapout(kva, npages);
1635 	simple_lock(&uobj->vmobjlock);
1636 	uvm_lock_pageq();
1637 	for (i = 0; i < npages; i++) {
1638 		pg = pgs[i];
1639 		if (error && (pg->flags & PG_FAKE) != 0) {
1640 			pg->flags |= PG_RELEASED;
1641 		} else {
1642 			pmap_clear_modify(pg);
1643 			uvm_pageactivate(pg);
1644 		}
1645 	}
1646 	if (error) {
1647 		uvm_page_unbusy(pgs, npages);
1648 	}
1649 	uvm_unlock_pageq();
1650 	simple_unlock(&uobj->vmobjlock);
1651 	return (error);
1652 }
1653 
1654 int
1655 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1656     int flags)
1657 {
1658 	off_t offset;
1659 	struct iovec iov;
1660 	struct uio uio;
1661 	kauth_cred_t cred = curproc->p_cred;
1662 	struct buf *bp;
1663 	vaddr_t kva;
1664 	int s, error;
1665 
1666 	offset = pgs[0]->offset;
1667 	kva = uvm_pagermapin(pgs, npages,
1668 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1669 
1670 	iov.iov_base = (void *)kva;
1671 	iov.iov_len = npages << PAGE_SHIFT;
1672 	uio.uio_iov = &iov;
1673 	uio.uio_iovcnt = 1;
1674 	uio.uio_offset = offset;
1675 	uio.uio_rw = UIO_WRITE;
1676 	uio.uio_resid = npages << PAGE_SHIFT;
1677 	UIO_SETUP_SYSSPACE(&uio);
1678 	/* XXX vn_lock */
1679 	error = VOP_WRITE(vp, &uio, 0, cred);
1680 
1681 	s = splbio();
1682 	V_INCR_NUMOUTPUT(vp);
1683 	splx(s);
1684 
1685 	bp = getiobuf();
1686 	bp->b_flags = B_BUSY | B_WRITE | B_AGE;
1687 	bp->b_vp = vp;
1688 	bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1689 	bp->b_data = (char *)kva;
1690 	bp->b_bcount = npages << PAGE_SHIFT;
1691 	bp->b_bufsize = npages << PAGE_SHIFT;
1692 	bp->b_resid = 0;
1693 	if (error) {
1694 		bp->b_flags |= B_ERROR;
1695 		bp->b_error = error;
1696 	}
1697 	uvm_aio_aiodone(bp);
1698 	return (error);
1699 }
1700 
1701 static void
1702 filt_genfsdetach(struct knote *kn)
1703 {
1704 	struct vnode *vp = (struct vnode *)kn->kn_hook;
1705 
1706 	/* XXXLUKEM lock the struct? */
1707 	SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
1708 }
1709 
1710 static int
1711 filt_genfsread(struct knote *kn, long hint)
1712 {
1713 	struct vnode *vp = (struct vnode *)kn->kn_hook;
1714 
1715 	/*
1716 	 * filesystem is gone, so set the EOF flag and schedule
1717 	 * the knote for deletion.
1718 	 */
1719 	if (hint == NOTE_REVOKE) {
1720 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1721 		return (1);
1722 	}
1723 
1724 	/* XXXLUKEM lock the struct? */
1725 	kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
1726         return (kn->kn_data != 0);
1727 }
1728 
1729 static int
1730 filt_genfsvnode(struct knote *kn, long hint)
1731 {
1732 
1733 	if (kn->kn_sfflags & hint)
1734 		kn->kn_fflags |= hint;
1735 	if (hint == NOTE_REVOKE) {
1736 		kn->kn_flags |= EV_EOF;
1737 		return (1);
1738 	}
1739 	return (kn->kn_fflags != 0);
1740 }
1741 
1742 static const struct filterops genfsread_filtops =
1743 	{ 1, NULL, filt_genfsdetach, filt_genfsread };
1744 static const struct filterops genfsvnode_filtops =
1745 	{ 1, NULL, filt_genfsdetach, filt_genfsvnode };
1746 
1747 int
1748 genfs_kqfilter(void *v)
1749 {
1750 	struct vop_kqfilter_args /* {
1751 		struct vnode	*a_vp;
1752 		struct knote	*a_kn;
1753 	} */ *ap = v;
1754 	struct vnode *vp;
1755 	struct knote *kn;
1756 
1757 	vp = ap->a_vp;
1758 	kn = ap->a_kn;
1759 	switch (kn->kn_filter) {
1760 	case EVFILT_READ:
1761 		kn->kn_fop = &genfsread_filtops;
1762 		break;
1763 	case EVFILT_VNODE:
1764 		kn->kn_fop = &genfsvnode_filtops;
1765 		break;
1766 	default:
1767 		return (1);
1768 	}
1769 
1770 	kn->kn_hook = vp;
1771 
1772 	/* XXXLUKEM lock the struct? */
1773 	SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
1774 
1775 	return (0);
1776 }
1777