genfs_io.c revision 1.102
1/*	$NetBSD: genfs_io.c,v 1.102 2022/01/14 21:59:50 riastradh 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_io.c,v 1.102 2022/01/14 21:59:50 riastradh Exp $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/proc.h>
39#include <sys/kernel.h>
40#include <sys/mount.h>
41#include <sys/vnode.h>
42#include <sys/kmem.h>
43#include <sys/kauth.h>
44#include <sys/fstrans.h>
45#include <sys/buf.h>
46#include <sys/atomic.h>
47
48#include <miscfs/genfs/genfs.h>
49#include <miscfs/genfs/genfs_node.h>
50#include <miscfs/specfs/specdev.h>
51
52#include <uvm/uvm.h>
53#include <uvm/uvm_pager.h>
54#include <uvm/uvm_page_array.h>
55
56static int genfs_do_directio(struct vmspace *, vaddr_t, size_t, struct vnode *,
57    off_t, enum uio_rw);
58static void genfs_dio_iodone(struct buf *);
59
60static int genfs_getpages_read(struct vnode *, struct vm_page **, int, off_t,
61    off_t, bool, bool, bool, bool);
62static int genfs_do_io(struct vnode *, off_t, vaddr_t, size_t, int, enum uio_rw,
63    void (*)(struct buf *));
64static void genfs_rel_pages(struct vm_page **, unsigned int);
65
66int genfs_maxdio = MAXPHYS;
67
68static void
69genfs_rel_pages(struct vm_page **pgs, unsigned int npages)
70{
71	unsigned int i;
72
73	for (i = 0; i < npages; i++) {
74		struct vm_page *pg = pgs[i];
75
76		if (pg == NULL || pg == PGO_DONTCARE)
77			continue;
78		KASSERT(uvm_page_owner_locked_p(pg, true));
79		if (pg->flags & PG_FAKE) {
80			pg->flags |= PG_RELEASED;
81		}
82	}
83	uvm_page_unbusy(pgs, npages);
84}
85
86/*
87 * generic VM getpages routine.
88 * Return PG_BUSY pages for the given range,
89 * reading from backing store if necessary.
90 */
91
92int
93genfs_getpages(void *v)
94{
95	struct vop_getpages_args /* {
96		struct vnode *a_vp;
97		voff_t a_offset;
98		struct vm_page **a_m;
99		int *a_count;
100		int a_centeridx;
101		vm_prot_t a_access_type;
102		int a_advice;
103		int a_flags;
104	} */ * const ap = v;
105
106	off_t diskeof, memeof;
107	int i, error, npages, iflag;
108	const int flags = ap->a_flags;
109	struct vnode * const vp = ap->a_vp;
110	struct uvm_object * const uobj = &vp->v_uobj;
111	const bool async = (flags & PGO_SYNCIO) == 0;
112	const bool memwrite = (ap->a_access_type & VM_PROT_WRITE) != 0;
113	const bool overwrite = (flags & PGO_OVERWRITE) != 0;
114	const bool blockalloc = memwrite && (flags & PGO_NOBLOCKALLOC) == 0;
115	const bool need_wapbl = (vp->v_mount->mnt_wapbl &&
116			(flags & PGO_JOURNALLOCKED) == 0);
117	const bool glocked = (flags & PGO_GLOCKHELD) != 0;
118	bool holds_wapbl = false;
119	struct mount *trans_mount = NULL;
120	UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
121
122	UVMHIST_LOG(ubchist, "vp %#jx off 0x%jx/%jx count %jd",
123	    (uintptr_t)vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
124
125	KASSERT(memwrite >= overwrite);
126	KASSERT(vp->v_type == VREG || vp->v_type == VDIR ||
127	    vp->v_type == VLNK || vp->v_type == VBLK);
128
129	/*
130	 * the object must be locked.  it can only be a read lock when
131	 * processing a read fault with PGO_LOCKED.
132	 */
133
134	KASSERT(rw_lock_held(uobj->vmobjlock));
135	KASSERT(rw_write_held(uobj->vmobjlock) ||
136	   ((flags & PGO_LOCKED) != 0 && !memwrite));
137
138#ifdef DIAGNOSTIC
139	if ((flags & PGO_JOURNALLOCKED) && vp->v_mount->mnt_wapbl)
140                WAPBL_JLOCK_ASSERT(vp->v_mount);
141#endif
142
143	/*
144	 * check for reclaimed vnode.  v_interlock is not held here, but
145	 * VI_DEADCHECK is set with vmobjlock held.
146	 */
147
148	iflag = atomic_load_relaxed(&vp->v_iflag);
149	if (__predict_false((iflag & VI_DEADCHECK) != 0)) {
150		mutex_enter(vp->v_interlock);
151		error = vdead_check(vp, VDEAD_NOWAIT);
152		mutex_exit(vp->v_interlock);
153		if (error) {
154			if ((flags & PGO_LOCKED) == 0)
155				rw_exit(uobj->vmobjlock);
156			return error;
157		}
158	}
159
160startover:
161	error = 0;
162	const voff_t origvsize = vp->v_size;
163	const off_t origoffset = ap->a_offset;
164	const int orignpages = *ap->a_count;
165
166	GOP_SIZE(vp, origvsize, &diskeof, 0);
167	if (flags & PGO_PASTEOF) {
168		off_t newsize;
169#if defined(DIAGNOSTIC)
170		off_t writeeof;
171#endif /* defined(DIAGNOSTIC) */
172
173		newsize = MAX(origvsize,
174		    origoffset + (orignpages << PAGE_SHIFT));
175		GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_MEM);
176#if defined(DIAGNOSTIC)
177		GOP_SIZE(vp, vp->v_writesize, &writeeof, GOP_SIZE_MEM);
178		if (newsize > round_page(writeeof)) {
179			panic("%s: past eof: %" PRId64 " vs. %" PRId64,
180			    __func__, newsize, round_page(writeeof));
181		}
182#endif /* defined(DIAGNOSTIC) */
183	} else {
184		GOP_SIZE(vp, origvsize, &memeof, GOP_SIZE_MEM);
185	}
186	KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
187	KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
188	KASSERT(orignpages > 0);
189
190	/*
191	 * Bounds-check the request.
192	 */
193
194	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
195		if ((flags & PGO_LOCKED) == 0) {
196			rw_exit(uobj->vmobjlock);
197		}
198		UVMHIST_LOG(ubchist, "off 0x%jx count %jd goes past EOF 0x%jx",
199		    origoffset, *ap->a_count, memeof,0);
200		error = EINVAL;
201		goto out_err;
202	}
203
204	/* uobj is locked */
205
206	if ((flags & PGO_NOTIMESTAMP) == 0 &&
207	    (vp->v_type != VBLK ||
208	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
209		int updflags = 0;
210
211		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) {
212			updflags = GOP_UPDATE_ACCESSED;
213		}
214		if (memwrite) {
215			updflags |= GOP_UPDATE_MODIFIED;
216		}
217		if (updflags != 0) {
218			GOP_MARKUPDATE(vp, updflags);
219		}
220	}
221
222	/*
223	 * For PGO_LOCKED requests, just return whatever's in memory.
224	 */
225
226	if (flags & PGO_LOCKED) {
227		int nfound;
228		struct vm_page *pg;
229
230		KASSERT(!glocked);
231		npages = *ap->a_count;
232#if defined(DEBUG)
233		for (i = 0; i < npages; i++) {
234			pg = ap->a_m[i];
235			KASSERT(pg == NULL || pg == PGO_DONTCARE);
236		}
237#endif /* defined(DEBUG) */
238 		nfound = uvn_findpages(uobj, origoffset, &npages,
239		    ap->a_m, NULL,
240		    UFP_NOWAIT | UFP_NOALLOC | UFP_NOBUSY |
241		    (memwrite ? UFP_NORDONLY : 0));
242		KASSERT(npages == *ap->a_count);
243		if (nfound == 0) {
244			error = EBUSY;
245			goto out_err;
246		}
247		/*
248		 * lock and unlock g_glock to ensure that no one is truncating
249		 * the file behind us.
250		 */
251		if (!genfs_node_rdtrylock(vp)) {
252			/*
253			 * restore the array.
254			 */
255
256			for (i = 0; i < npages; i++) {
257				pg = ap->a_m[i];
258
259				if (pg != NULL && pg != PGO_DONTCARE) {
260					ap->a_m[i] = NULL;
261				}
262				KASSERT(ap->a_m[i] == NULL ||
263				    ap->a_m[i] == PGO_DONTCARE);
264			}
265		} else {
266			genfs_node_unlock(vp);
267		}
268		error = (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
269		if (error == 0 && memwrite) {
270			for (i = 0; i < npages; i++) {
271				pg = ap->a_m[i];
272				if (pg == NULL || pg == PGO_DONTCARE) {
273					continue;
274				}
275				if (uvm_pagegetdirty(pg) ==
276				    UVM_PAGE_STATUS_CLEAN) {
277					uvm_pagemarkdirty(pg,
278					    UVM_PAGE_STATUS_UNKNOWN);
279				}
280			}
281		}
282		goto out_err;
283	}
284	rw_exit(uobj->vmobjlock);
285
286	/*
287	 * find the requested pages and make some simple checks.
288	 * leave space in the page array for a whole block.
289	 */
290
291	const int fs_bshift = (vp->v_type != VBLK) ?
292	    vp->v_mount->mnt_fs_bshift : DEV_BSHIFT;
293	const int fs_bsize = 1 << fs_bshift;
294#define	blk_mask	(fs_bsize - 1)
295#define	trunc_blk(x)	((x) & ~blk_mask)
296#define	round_blk(x)	(((x) + blk_mask) & ~blk_mask)
297
298	const int orignmempages = MIN(orignpages,
299	    round_page(memeof - origoffset) >> PAGE_SHIFT);
300	npages = orignmempages;
301	const off_t startoffset = trunc_blk(origoffset);
302	const off_t endoffset = MIN(
303	    round_page(round_blk(origoffset + (npages << PAGE_SHIFT))),
304	    round_page(memeof));
305	const int ridx = (origoffset - startoffset) >> PAGE_SHIFT;
306
307	const int pgs_size = sizeof(struct vm_page *) *
308	    ((endoffset - startoffset) >> PAGE_SHIFT);
309	struct vm_page **pgs, *pgs_onstack[UBC_MAX_PAGES];
310
311	if (pgs_size > sizeof(pgs_onstack)) {
312		pgs = kmem_zalloc(pgs_size, async ? KM_NOSLEEP : KM_SLEEP);
313		if (pgs == NULL) {
314			pgs = pgs_onstack;
315			error = ENOMEM;
316			goto out_err;
317		}
318	} else {
319		pgs = pgs_onstack;
320		(void)memset(pgs, 0, pgs_size);
321	}
322
323	UVMHIST_LOG(ubchist, "ridx %jd npages %jd startoff %#jx endoff %#jx",
324	    ridx, npages, startoffset, endoffset);
325
326	if (trans_mount == NULL) {
327		trans_mount = vp->v_mount;
328		fstrans_start(trans_mount);
329		/*
330		 * check if this vnode is still valid.
331		 */
332		mutex_enter(vp->v_interlock);
333		error = vdead_check(vp, 0);
334		mutex_exit(vp->v_interlock);
335		if (error)
336			goto out_err_free;
337		/*
338		 * XXX: This assumes that we come here only via
339		 * the mmio path
340		 */
341		if (blockalloc && need_wapbl) {
342			error = WAPBL_BEGIN(trans_mount);
343			if (error)
344				goto out_err_free;
345			holds_wapbl = true;
346		}
347	}
348
349	/*
350	 * hold g_glock to prevent a race with truncate.
351	 *
352	 * check if our idea of v_size is still valid.
353	 */
354
355	KASSERT(!glocked || genfs_node_wrlocked(vp));
356	if (!glocked) {
357		if (blockalloc) {
358			genfs_node_wrlock(vp);
359		} else {
360			genfs_node_rdlock(vp);
361		}
362	}
363	rw_enter(uobj->vmobjlock, RW_WRITER);
364	if (vp->v_size < origvsize) {
365		if (!glocked) {
366			genfs_node_unlock(vp);
367		}
368		if (pgs != pgs_onstack)
369			kmem_free(pgs, pgs_size);
370		goto startover;
371	}
372
373	if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], NULL,
374	    async ? UFP_NOWAIT : UFP_ALL) != orignmempages) {
375		if (!glocked) {
376			genfs_node_unlock(vp);
377		}
378		KASSERT(async != 0);
379		genfs_rel_pages(&pgs[ridx], orignmempages);
380		rw_exit(uobj->vmobjlock);
381		error = EBUSY;
382		goto out_err_free;
383	}
384
385	/*
386	 * if PGO_OVERWRITE is set, don't bother reading the pages.
387	 */
388
389	if (overwrite) {
390		if (!glocked) {
391			genfs_node_unlock(vp);
392		}
393		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
394
395		for (i = 0; i < npages; i++) {
396			struct vm_page *pg = pgs[ridx + i];
397
398			/*
399			 * it's caller's responsibility to allocate blocks
400			 * beforehand for the overwrite case.
401			 */
402
403			KASSERT((pg->flags & PG_RDONLY) == 0 || !blockalloc);
404			pg->flags &= ~PG_RDONLY;
405
406			/*
407			 * mark the page DIRTY.
408			 * otherwise another thread can do putpages and pull
409			 * our vnode from syncer's queue before our caller does
410			 * ubc_release.  note that putpages won't see CLEAN
411			 * pages even if they are BUSY.
412			 */
413
414			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
415		}
416		npages += ridx;
417		goto out;
418	}
419
420	/*
421	 * if the pages are already resident, just return them.
422	 */
423
424	for (i = 0; i < npages; i++) {
425		struct vm_page *pg = pgs[ridx + i];
426
427		if ((pg->flags & PG_FAKE) ||
428		    (blockalloc && (pg->flags & PG_RDONLY) != 0)) {
429			break;
430		}
431	}
432	if (i == npages) {
433		if (!glocked) {
434			genfs_node_unlock(vp);
435		}
436		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
437		npages += ridx;
438		goto out;
439	}
440
441	/*
442	 * the page wasn't resident and we're not overwriting,
443	 * so we're going to have to do some i/o.
444	 * find any additional pages needed to cover the expanded range.
445	 */
446
447	npages = (endoffset - startoffset) >> PAGE_SHIFT;
448	if (startoffset != origoffset || npages != orignmempages) {
449		int npgs;
450
451		/*
452		 * we need to avoid deadlocks caused by locking
453		 * additional pages at lower offsets than pages we
454		 * already have locked.  unlock them all and start over.
455		 */
456
457		genfs_rel_pages(&pgs[ridx], orignmempages);
458		memset(pgs, 0, pgs_size);
459
460		UVMHIST_LOG(ubchist, "reset npages start 0x%jx end 0x%jx",
461		    startoffset, endoffset, 0,0);
462		npgs = npages;
463		if (uvn_findpages(uobj, startoffset, &npgs, pgs, NULL,
464		    async ? UFP_NOWAIT : UFP_ALL) != npages) {
465			if (!glocked) {
466				genfs_node_unlock(vp);
467			}
468			KASSERT(async != 0);
469			genfs_rel_pages(pgs, npages);
470			rw_exit(uobj->vmobjlock);
471			error = EBUSY;
472			goto out_err_free;
473		}
474	}
475
476	rw_exit(uobj->vmobjlock);
477	error = genfs_getpages_read(vp, pgs, npages, startoffset, diskeof,
478	    async, memwrite, blockalloc, glocked);
479	if (!glocked) {
480		genfs_node_unlock(vp);
481	}
482	if (error == 0 && async)
483		goto out_err_free;
484	rw_enter(uobj->vmobjlock, RW_WRITER);
485
486	/*
487	 * we're almost done!  release the pages...
488	 * for errors, we free the pages.
489	 * otherwise we activate them and mark them as valid and clean.
490	 * also, unbusy pages that were not actually requested.
491	 */
492
493	if (error) {
494		genfs_rel_pages(pgs, npages);
495		rw_exit(uobj->vmobjlock);
496		UVMHIST_LOG(ubchist, "returning error %jd", error,0,0,0);
497		goto out_err_free;
498	}
499
500out:
501	UVMHIST_LOG(ubchist, "succeeding, npages %jd", npages,0,0,0);
502	error = 0;
503	for (i = 0; i < npages; i++) {
504		struct vm_page *pg = pgs[i];
505		if (pg == NULL) {
506			continue;
507		}
508		UVMHIST_LOG(ubchist, "examining pg %#jx flags 0x%jx",
509		    (uintptr_t)pg, pg->flags, 0,0);
510		if (pg->flags & PG_FAKE && !overwrite) {
511			/*
512			 * we've read page's contents from the backing storage.
513			 *
514			 * for a read fault, we keep them CLEAN;  if we
515			 * encountered a hole while reading, the pages can
516			 * already been dirtied with zeros.
517			 */
518			KASSERTMSG(blockalloc || uvm_pagegetdirty(pg) ==
519			    UVM_PAGE_STATUS_CLEAN, "page %p not clean", pg);
520			pg->flags &= ~PG_FAKE;
521		}
522		KASSERT(!memwrite || !blockalloc || (pg->flags & PG_RDONLY) == 0);
523		if (i < ridx || i >= ridx + orignmempages || async) {
524			UVMHIST_LOG(ubchist, "unbusy pg %#jx offset 0x%jx",
525			    (uintptr_t)pg, pg->offset,0,0);
526			if (pg->flags & PG_FAKE) {
527				KASSERT(overwrite);
528				uvm_pagezero(pg);
529			}
530			if (pg->flags & PG_RELEASED) {
531				uvm_pagefree(pg);
532				continue;
533			}
534			uvm_pagelock(pg);
535			uvm_pageenqueue(pg);
536			uvm_pagewakeup(pg);
537			uvm_pageunlock(pg);
538			pg->flags &= ~(PG_BUSY|PG_FAKE);
539			UVM_PAGE_OWN(pg, NULL);
540		} else if (memwrite && !overwrite &&
541		    uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_CLEAN) {
542			/*
543			 * for a write fault, start dirtiness tracking of
544			 * requested pages.
545			 */
546			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_UNKNOWN);
547		}
548	}
549	rw_exit(uobj->vmobjlock);
550	if (ap->a_m != NULL) {
551		memcpy(ap->a_m, &pgs[ridx],
552		    orignmempages * sizeof(struct vm_page *));
553	}
554
555out_err_free:
556	if (pgs != NULL && pgs != pgs_onstack)
557		kmem_free(pgs, pgs_size);
558out_err:
559	if (trans_mount != NULL) {
560		if (holds_wapbl)
561			WAPBL_END(trans_mount);
562		fstrans_done(trans_mount);
563	}
564	return error;
565}
566
567/*
568 * genfs_getpages_read: Read the pages in with VOP_BMAP/VOP_STRATEGY.
569 *
570 * "glocked" (which is currently not actually used) tells us not whether
571 * the genfs_node is locked on entry (it always is) but whether it was
572 * locked on entry to genfs_getpages.
573 */
574static int
575genfs_getpages_read(struct vnode *vp, struct vm_page **pgs, int npages,
576    off_t startoffset, off_t diskeof,
577    bool async, bool memwrite, bool blockalloc, bool glocked)
578{
579	struct uvm_object * const uobj = &vp->v_uobj;
580	const int fs_bshift = (vp->v_type != VBLK) ?
581	    vp->v_mount->mnt_fs_bshift : DEV_BSHIFT;
582	const int dev_bshift = (vp->v_type != VBLK) ?
583	    vp->v_mount->mnt_dev_bshift : DEV_BSHIFT;
584	kauth_cred_t const cred = curlwp->l_cred;		/* XXXUBC curlwp */
585	size_t bytes, iobytes, tailstart, tailbytes, totalbytes, skipbytes;
586	vaddr_t kva;
587	struct buf *bp, *mbp;
588	bool sawhole = false;
589	int i;
590	int error = 0;
591
592	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
593
594	/*
595	 * read the desired page(s).
596	 */
597
598	totalbytes = npages << PAGE_SHIFT;
599	bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
600	tailbytes = totalbytes - bytes;
601	skipbytes = 0;
602
603	kva = uvm_pagermapin(pgs, npages,
604	    UVMPAGER_MAPIN_READ | (async ? 0 : UVMPAGER_MAPIN_WAITOK));
605	if (kva == 0)
606		return EBUSY;
607
608	mbp = getiobuf(vp, true);
609	mbp->b_bufsize = totalbytes;
610	mbp->b_data = (void *)kva;
611	mbp->b_resid = mbp->b_bcount = bytes;
612	mbp->b_cflags |= BC_BUSY;
613	if (async) {
614		mbp->b_flags = B_READ | B_ASYNC;
615		mbp->b_iodone = uvm_aio_aiodone;
616	} else {
617		mbp->b_flags = B_READ;
618		mbp->b_iodone = NULL;
619	}
620	if (async)
621		BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
622	else
623		BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
624
625	/*
626	 * if EOF is in the middle of the range, zero the part past EOF.
627	 * skip over pages which are not PG_FAKE since in that case they have
628	 * valid data that we need to preserve.
629	 */
630
631	tailstart = bytes;
632	while (tailbytes > 0) {
633		const int len = PAGE_SIZE - (tailstart & PAGE_MASK);
634
635		KASSERT(len <= tailbytes);
636		if ((pgs[tailstart >> PAGE_SHIFT]->flags & PG_FAKE) != 0) {
637			memset((void *)(kva + tailstart), 0, len);
638			UVMHIST_LOG(ubchist, "tailbytes %#jx 0x%jx 0x%jx",
639			    (uintptr_t)kva, tailstart, len, 0);
640		}
641		tailstart += len;
642		tailbytes -= len;
643	}
644
645	/*
646	 * now loop over the pages, reading as needed.
647	 */
648
649	bp = NULL;
650	off_t offset;
651	for (offset = startoffset;
652	    bytes > 0;
653	    offset += iobytes, bytes -= iobytes) {
654		int run;
655		daddr_t lbn, blkno;
656		int pidx;
657		struct vnode *devvp;
658
659		/*
660		 * skip pages which don't need to be read.
661		 */
662
663		pidx = (offset - startoffset) >> PAGE_SHIFT;
664		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
665			size_t b;
666
667			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
668			if ((pgs[pidx]->flags & PG_RDONLY)) {
669				sawhole = true;
670			}
671			b = MIN(PAGE_SIZE, bytes);
672			offset += b;
673			bytes -= b;
674			skipbytes += b;
675			pidx++;
676			UVMHIST_LOG(ubchist, "skipping, new offset 0x%jx",
677			    offset, 0,0,0);
678			if (bytes == 0) {
679				goto loopdone;
680			}
681		}
682
683		/*
684		 * bmap the file to find out the blkno to read from and
685		 * how much we can read in one i/o.  if bmap returns an error,
686		 * skip the rest of the top-level i/o.
687		 */
688
689		lbn = offset >> fs_bshift;
690		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
691		if (error) {
692			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%jx -> %jd",
693			    lbn,error,0,0);
694			skipbytes += bytes;
695			bytes = 0;
696			goto loopdone;
697		}
698
699		/*
700		 * see how many pages can be read with this i/o.
701		 * reduce the i/o size if necessary to avoid
702		 * overwriting pages with valid data.
703		 */
704
705		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
706		    bytes);
707		if (offset + iobytes > round_page(offset)) {
708			int pcount;
709
710			pcount = 1;
711			while (pidx + pcount < npages &&
712			    pgs[pidx + pcount]->flags & PG_FAKE) {
713				pcount++;
714			}
715			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
716			    (offset - trunc_page(offset)));
717		}
718
719		/*
720		 * if this block isn't allocated, zero it instead of
721		 * reading it.  unless we are going to allocate blocks,
722		 * mark the pages we zeroed PG_RDONLY.
723		 */
724
725		if (blkno == (daddr_t)-1) {
726			int holepages = (round_page(offset + iobytes) -
727			    trunc_page(offset)) >> PAGE_SHIFT;
728			UVMHIST_LOG(ubchist, "lbn 0x%jx -> HOLE", lbn,0,0,0);
729
730			sawhole = true;
731			memset((char *)kva + (offset - startoffset), 0,
732			    iobytes);
733			skipbytes += iobytes;
734
735			if (!blockalloc) {
736				rw_enter(uobj->vmobjlock, RW_WRITER);
737				for (i = 0; i < holepages; i++) {
738					pgs[pidx + i]->flags |= PG_RDONLY;
739				}
740				rw_exit(uobj->vmobjlock);
741			}
742			continue;
743		}
744
745		/*
746		 * allocate a sub-buf for this piece of the i/o
747		 * (or just use mbp if there's only 1 piece),
748		 * and start it going.
749		 */
750
751		if (offset == startoffset && iobytes == bytes) {
752			bp = mbp;
753		} else {
754			UVMHIST_LOG(ubchist, "vp %#jx bp %#jx num now %jd",
755			    (uintptr_t)vp, (uintptr_t)bp, vp->v_numoutput, 0);
756			bp = getiobuf(vp, true);
757			nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
758		}
759		bp->b_lblkno = 0;
760
761		/* adjust physical blkno for partial blocks */
762		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
763		    dev_bshift);
764
765		UVMHIST_LOG(ubchist,
766		    "bp %#jx offset 0x%x bcount 0x%x blkno 0x%x",
767		    (uintptr_t)bp, offset, bp->b_bcount, bp->b_blkno);
768
769		VOP_STRATEGY(devvp, bp);
770	}
771
772loopdone:
773	nestiobuf_done(mbp, skipbytes, error);
774	if (async) {
775		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
776		return 0;
777	}
778	if (bp != NULL) {
779		error = biowait(mbp);
780	}
781
782	/* Remove the mapping (make KVA available as soon as possible) */
783	uvm_pagermapout(kva, npages);
784
785	/*
786	 * if this we encountered a hole then we have to do a little more work.
787	 * for read faults, we marked the page PG_RDONLY so that future
788	 * write accesses to the page will fault again.
789	 * for write faults, we must make sure that the backing store for
790	 * the page is completely allocated while the pages are locked.
791	 */
792
793	if (!error && sawhole && blockalloc) {
794		error = GOP_ALLOC(vp, startoffset,
795		    npages << PAGE_SHIFT, 0, cred);
796		UVMHIST_LOG(ubchist, "gop_alloc off 0x%jx/0x%jx -> %jd",
797		    startoffset, npages << PAGE_SHIFT, error,0);
798		if (!error) {
799			rw_enter(uobj->vmobjlock, RW_WRITER);
800			for (i = 0; i < npages; i++) {
801				struct vm_page *pg = pgs[i];
802
803				if (pg == NULL) {
804					continue;
805				}
806				pg->flags &= ~PG_RDONLY;
807				uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
808				UVMHIST_LOG(ubchist, "mark dirty pg %#jx",
809				    (uintptr_t)pg, 0, 0, 0);
810			}
811			rw_exit(uobj->vmobjlock);
812		}
813	}
814
815	putiobuf(mbp);
816	return error;
817}
818
819/*
820 * generic VM putpages routine.
821 * Write the given range of pages to backing store.
822 *
823 * => "offhi == 0" means flush all pages at or after "offlo".
824 * => object should be locked by caller.  we return with the
825 *      object unlocked.
826 * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
827 *	thus, a caller might want to unlock higher level resources
828 *	(e.g. vm_map) before calling flush.
829 * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, we will not block
830 * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
831 *
832 * note on "cleaning" object and PG_BUSY pages:
833 *	this routine is holding the lock on the object.   the only time
834 *	that it can run into a PG_BUSY page that it does not own is if
835 *	some other process has started I/O on the page (e.g. either
836 *	a pagein, or a pageout).  if the PG_BUSY page is being paged
837 *	in, then it can not be dirty (!UVM_PAGE_STATUS_CLEAN) because no
838 *	one has	had a chance to modify it yet.  if the PG_BUSY page is
839 *	being paged out then it means that someone else has already started
840 *	cleaning the page for us (how nice!).  in this case, if we
841 *	have syncio specified, then after we make our pass through the
842 *	object we need to wait for the other PG_BUSY pages to clear
843 *	off (i.e. we need to do an iosync).   also note that once a
844 *	page is PG_BUSY it must stay in its object until it is un-busyed.
845 */
846
847int
848genfs_putpages(void *v)
849{
850	struct vop_putpages_args /* {
851		struct vnode *a_vp;
852		voff_t a_offlo;
853		voff_t a_offhi;
854		int a_flags;
855	} */ * const ap = v;
856
857	return genfs_do_putpages(ap->a_vp, ap->a_offlo, ap->a_offhi,
858	    ap->a_flags, NULL);
859}
860
861int
862genfs_do_putpages(struct vnode *vp, off_t startoff, off_t endoff,
863    int origflags, struct vm_page **busypg)
864{
865	struct uvm_object * const uobj = &vp->v_uobj;
866	krwlock_t * const slock = uobj->vmobjlock;
867	off_t nextoff;
868	int i, error, npages, nback;
869	int freeflag;
870	/*
871	 * This array is larger than it should so that it's size is constant.
872	 * The right size is MAXPAGES.
873	 */
874	struct vm_page *pgs[MAXPHYS / MIN_PAGE_SIZE];
875#define MAXPAGES (MAXPHYS / PAGE_SIZE)
876	struct vm_page *pg, *tpg;
877	struct uvm_page_array a;
878	bool wasclean, needs_clean;
879	bool async = (origflags & PGO_SYNCIO) == 0;
880	bool pagedaemon = curlwp == uvm.pagedaemon_lwp;
881	struct mount *trans_mp;
882	int flags;
883	bool modified;		/* if we write out any pages */
884	bool holds_wapbl;
885	bool cleanall;		/* try to pull off from the syncer's list */
886	bool onworklst;
887	bool nodirty;
888	const bool dirtyonly = (origflags & (PGO_DEACTIVATE|PGO_FREE)) == 0;
889
890	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
891
892	KASSERT(origflags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
893	KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
894	KASSERT(startoff < endoff || endoff == 0);
895	KASSERT(rw_write_held(slock));
896
897	UVMHIST_LOG(ubchist, "vp %#jx pages %jd off 0x%jx len 0x%jx",
898	    (uintptr_t)vp, uobj->uo_npages, startoff, endoff - startoff);
899
900#ifdef DIAGNOSTIC
901	if ((origflags & PGO_JOURNALLOCKED) && vp->v_mount->mnt_wapbl)
902                WAPBL_JLOCK_ASSERT(vp->v_mount);
903#endif
904
905	trans_mp = NULL;
906	holds_wapbl = false;
907
908retry:
909	modified = false;
910	flags = origflags;
911
912	/*
913	 * shortcut if we have no pages to process.
914	 */
915
916	nodirty = uvm_obj_clean_p(uobj);
917#ifdef DIAGNOSTIC
918	mutex_enter(vp->v_interlock);
919	KASSERT((vp->v_iflag & VI_ONWORKLST) != 0 || nodirty);
920	mutex_exit(vp->v_interlock);
921#endif
922	if (uobj->uo_npages == 0 || (dirtyonly && nodirty)) {
923		mutex_enter(vp->v_interlock);
924		if (vp->v_iflag & VI_ONWORKLST && LIST_EMPTY(&vp->v_dirtyblkhd)) {
925			vn_syncer_remove_from_worklist(vp);
926		}
927		mutex_exit(vp->v_interlock);
928		if (trans_mp) {
929			if (holds_wapbl)
930				WAPBL_END(trans_mp);
931			fstrans_done(trans_mp);
932		}
933		rw_exit(slock);
934		return (0);
935	}
936
937	/*
938	 * the vnode has pages, set up to process the request.
939	 */
940
941	if (trans_mp == NULL && (flags & PGO_CLEANIT) != 0) {
942		if (pagedaemon) {
943			/* Pagedaemon must not sleep here. */
944			trans_mp = vp->v_mount;
945			error = fstrans_start_nowait(trans_mp);
946			if (error) {
947				rw_exit(slock);
948				return error;
949			}
950		} else {
951			/*
952			 * Cannot use vdeadcheck() here as this operation
953			 * usually gets used from VOP_RECLAIM().  Test for
954			 * change of v_mount instead and retry on change.
955			 */
956			rw_exit(slock);
957			trans_mp = vp->v_mount;
958			fstrans_start(trans_mp);
959			if (vp->v_mount != trans_mp) {
960				fstrans_done(trans_mp);
961				trans_mp = NULL;
962			} else {
963				holds_wapbl = (trans_mp->mnt_wapbl &&
964				    (origflags & PGO_JOURNALLOCKED) == 0);
965				if (holds_wapbl) {
966					error = WAPBL_BEGIN(trans_mp);
967					if (error) {
968						fstrans_done(trans_mp);
969						return error;
970					}
971				}
972			}
973			rw_enter(slock, RW_WRITER);
974			goto retry;
975		}
976	}
977
978	error = 0;
979	wasclean = uvm_obj_nowriteback_p(uobj);
980	nextoff = startoff;
981	if (endoff == 0 || flags & PGO_ALLPAGES) {
982		endoff = trunc_page(LLONG_MAX);
983	}
984
985	/*
986	 * if this vnode is known not to have dirty pages,
987	 * don't bother to clean it out.
988	 */
989
990	if (nodirty) {
991		/* We handled the dirtyonly && nodirty case above.  */
992		KASSERT(!dirtyonly);
993		flags &= ~PGO_CLEANIT;
994	}
995
996	/*
997	 * start the loop to scan pages.
998	 */
999
1000	cleanall = true;
1001	freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
1002	uvm_page_array_init(&a, uobj, dirtyonly ? (UVM_PAGE_ARRAY_FILL_DIRTY |
1003	    (!async ? UVM_PAGE_ARRAY_FILL_WRITEBACK : 0)) : 0);
1004	for (;;) {
1005		bool pgprotected;
1006
1007		/*
1008		 * if !dirtyonly, iterate over all resident pages in the range.
1009		 *
1010		 * if dirtyonly, only possibly dirty pages are interesting.
1011		 * however, if we are asked to sync for integrity, we should
1012		 * wait on pages being written back by other threads as well.
1013		 */
1014
1015		pg = uvm_page_array_fill_and_peek(&a, nextoff, 0);
1016		if (pg == NULL) {
1017			break;
1018		}
1019
1020		KASSERT(pg->uobject == uobj);
1021		KASSERT((pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
1022		    (pg->flags & (PG_BUSY)) != 0);
1023		KASSERT(pg->offset >= startoff);
1024		KASSERT(pg->offset >= nextoff);
1025		KASSERT(!dirtyonly ||
1026		    uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN ||
1027		    uvm_obj_page_writeback_p(pg));
1028
1029		if (pg->offset >= endoff) {
1030			break;
1031		}
1032
1033		/*
1034		 * a preempt point.
1035		 */
1036
1037		if (preempt_needed()) {
1038			nextoff = pg->offset; /* visit this page again */
1039			rw_exit(slock);
1040			preempt();
1041			/*
1042			 * as we dropped the object lock, our cached pages can
1043			 * be stale.
1044			 */
1045			uvm_page_array_clear(&a);
1046			rw_enter(slock, RW_WRITER);
1047			continue;
1048		}
1049
1050		/*
1051		 * if the current page is busy, wait for it to become unbusy.
1052		 */
1053
1054		if ((pg->flags & PG_BUSY) != 0) {
1055			UVMHIST_LOG(ubchist, "busy %#jx", (uintptr_t)pg,
1056			   0, 0, 0);
1057			if ((pg->flags & (PG_RELEASED|PG_PAGEOUT)) != 0
1058			    && (flags & PGO_BUSYFAIL) != 0) {
1059				UVMHIST_LOG(ubchist, "busyfail %#jx",
1060				    (uintptr_t)pg, 0, 0, 0);
1061				error = EDEADLK;
1062				if (busypg != NULL)
1063					*busypg = pg;
1064				break;
1065			}
1066			if (pagedaemon) {
1067				/*
1068				 * someone has taken the page while we
1069				 * dropped the lock for fstrans_start.
1070				 */
1071				break;
1072			}
1073			/*
1074			 * don't bother to wait on other's activities
1075			 * unless we are asked to sync for integrity.
1076			 */
1077			if (!async && (flags & PGO_RECLAIM) == 0) {
1078				wasclean = false;
1079				nextoff = pg->offset + PAGE_SIZE;
1080				uvm_page_array_advance(&a);
1081				continue;
1082			}
1083			nextoff = pg->offset; /* visit this page again */
1084			uvm_pagewait(pg, slock, "genput");
1085			/*
1086			 * as we dropped the object lock, our cached pages can
1087			 * be stale.
1088			 */
1089			uvm_page_array_clear(&a);
1090			rw_enter(slock, RW_WRITER);
1091			continue;
1092		}
1093
1094		nextoff = pg->offset + PAGE_SIZE;
1095		uvm_page_array_advance(&a);
1096
1097		/*
1098		 * if we're freeing, remove all mappings of the page now.
1099		 * if we're cleaning, check if the page is needs to be cleaned.
1100		 */
1101
1102		pgprotected = false;
1103		if (flags & PGO_FREE) {
1104			pmap_page_protect(pg, VM_PROT_NONE);
1105			pgprotected = true;
1106		} else if (flags & PGO_CLEANIT) {
1107
1108			/*
1109			 * if we still have some hope to pull this vnode off
1110			 * from the syncer queue, write-protect the page.
1111			 */
1112
1113			if (cleanall && wasclean) {
1114
1115				/*
1116				 * uobj pages get wired only by uvm_fault
1117				 * where uobj is locked.
1118				 */
1119
1120				if (pg->wire_count == 0) {
1121					pmap_page_protect(pg,
1122					    VM_PROT_READ|VM_PROT_EXECUTE);
1123					pgprotected = true;
1124				} else {
1125					cleanall = false;
1126				}
1127			}
1128		}
1129
1130		if (flags & PGO_CLEANIT) {
1131			needs_clean = uvm_pagecheckdirty(pg, pgprotected);
1132		} else {
1133			needs_clean = false;
1134		}
1135
1136		/*
1137		 * if we're cleaning, build a cluster.
1138		 * the cluster will consist of pages which are currently dirty.
1139		 * if not cleaning, just operate on the one page.
1140		 */
1141
1142		if (needs_clean) {
1143			wasclean = false;
1144			memset(pgs, 0, sizeof(pgs));
1145			pg->flags |= PG_BUSY;
1146			UVM_PAGE_OWN(pg, "genfs_putpages");
1147
1148			/*
1149			 * let the fs constrain the offset range of the cluster.
1150			 * we additionally constrain the range here such that
1151			 * it fits in the "pgs" pages array.
1152			 */
1153
1154			off_t fslo, fshi, genlo, lo, off = pg->offset;
1155			GOP_PUTRANGE(vp, off, &fslo, &fshi);
1156			KASSERT(fslo == trunc_page(fslo));
1157			KASSERT(fslo <= off);
1158			KASSERT(fshi == trunc_page(fshi));
1159			KASSERT(fshi == 0 || off < fshi);
1160
1161			if (off > MAXPHYS / 2)
1162				genlo = trunc_page(off - (MAXPHYS / 2));
1163			else
1164				genlo = 0;
1165			lo = MAX(fslo, genlo);
1166
1167			/*
1168			 * first look backward.
1169			 */
1170
1171			npages = (off - lo) >> PAGE_SHIFT;
1172			nback = npages;
1173			uvn_findpages(uobj, off - PAGE_SIZE, &nback,
1174			    &pgs[0], NULL,
1175			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
1176			if (nback) {
1177				memmove(&pgs[0], &pgs[npages - nback],
1178				    nback * sizeof(pgs[0]));
1179				if (npages - nback < nback)
1180					memset(&pgs[nback], 0,
1181					    (npages - nback) * sizeof(pgs[0]));
1182				else
1183					memset(&pgs[npages - nback], 0,
1184					    nback * sizeof(pgs[0]));
1185			}
1186
1187			/*
1188			 * then plug in our page of interest.
1189			 */
1190
1191			pgs[nback] = pg;
1192
1193			/*
1194			 * then look forward to fill in the remaining space in
1195			 * the array of pages.
1196			 *
1197			 * pass our cached array of pages so that hopefully
1198			 * uvn_findpages can find some good pages in it.
1199			 * the array a was filled above with the one of
1200			 * following sets of flags:
1201			 *	0
1202			 *	UVM_PAGE_ARRAY_FILL_DIRTY
1203			 *	UVM_PAGE_ARRAY_FILL_DIRTY|WRITEBACK
1204			 *
1205			 * XXX this is fragile but it'll work: the array
1206			 * was earlier filled sparsely, but UFP_DIRTYONLY
1207			 * implies dense.  see corresponding comment in
1208			 * uvn_findpages().
1209			 */
1210
1211			npages = MAXPAGES - nback - 1;
1212			if (fshi)
1213				npages = MIN(npages,
1214					     (fshi - off - 1) >> PAGE_SHIFT);
1215			uvn_findpages(uobj, off + PAGE_SIZE, &npages,
1216			    &pgs[nback + 1], &a,
1217			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
1218			npages += nback + 1;
1219		} else {
1220			pgs[0] = pg;
1221			npages = 1;
1222			nback = 0;
1223		}
1224
1225		/*
1226		 * apply FREE or DEACTIVATE options if requested.
1227		 */
1228
1229		for (i = 0; i < npages; i++) {
1230			tpg = pgs[i];
1231			KASSERT(tpg->uobject == uobj);
1232			KASSERT(i == 0 ||
1233			    pgs[i-1]->offset + PAGE_SIZE == tpg->offset);
1234			KASSERT(!needs_clean || uvm_pagegetdirty(pgs[i]) !=
1235			    UVM_PAGE_STATUS_DIRTY);
1236			if (needs_clean) {
1237				/*
1238				 * mark pages as WRITEBACK so that concurrent
1239				 * fsync can find and wait for our activities.
1240				 */
1241				uvm_obj_page_set_writeback(pgs[i]);
1242			}
1243			if (tpg->offset < startoff || tpg->offset >= endoff)
1244				continue;
1245			if (flags & PGO_DEACTIVATE && tpg->wire_count == 0) {
1246				uvm_pagelock(tpg);
1247				uvm_pagedeactivate(tpg);
1248				uvm_pageunlock(tpg);
1249			} else if (flags & PGO_FREE) {
1250				pmap_page_protect(tpg, VM_PROT_NONE);
1251				if (tpg->flags & PG_BUSY) {
1252					tpg->flags |= freeflag;
1253					if (pagedaemon) {
1254						uvm_pageout_start(1);
1255						uvm_pagelock(tpg);
1256						uvm_pagedequeue(tpg);
1257						uvm_pageunlock(tpg);
1258					}
1259				} else {
1260
1261					/*
1262					 * ``page is not busy''
1263					 * implies that npages is 1
1264					 * and needs_clean is false.
1265					 */
1266
1267					KASSERT(npages == 1);
1268					KASSERT(!needs_clean);
1269					KASSERT(pg == tpg);
1270					KASSERT(nextoff ==
1271					    tpg->offset + PAGE_SIZE);
1272					uvm_pagefree(tpg);
1273					if (pagedaemon)
1274						uvmexp.pdfreed++;
1275				}
1276			}
1277		}
1278		if (needs_clean) {
1279			modified = true;
1280			KASSERT(nextoff == pg->offset + PAGE_SIZE);
1281			KASSERT(nback < npages);
1282			nextoff = pg->offset + ((npages - nback) << PAGE_SHIFT);
1283			KASSERT(pgs[nback] == pg);
1284			KASSERT(nextoff == pgs[npages - 1]->offset + PAGE_SIZE);
1285
1286			/*
1287			 * start the i/o.
1288			 */
1289			rw_exit(slock);
1290			error = GOP_WRITE(vp, pgs, npages, flags);
1291			/*
1292			 * as we dropped the object lock, our cached pages can
1293			 * be stale.
1294			 */
1295			uvm_page_array_clear(&a);
1296			rw_enter(slock, RW_WRITER);
1297			if (error) {
1298				break;
1299			}
1300		}
1301	}
1302	uvm_page_array_fini(&a);
1303
1304	/*
1305	 * update ctime/mtime if the modification we started writing out might
1306	 * be from mmap'ed write.
1307	 *
1308	 * this is necessary when an application keeps a file mmaped and
1309	 * repeatedly modifies it via the window.  note that, because we
1310	 * don't always write-protect pages when cleaning, such modifications
1311	 * might not involve any page faults.
1312	 */
1313
1314	mutex_enter(vp->v_interlock);
1315	if (modified && (vp->v_iflag & VI_WRMAP) != 0 &&
1316	    (vp->v_type != VBLK ||
1317	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
1318		GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
1319	}
1320
1321	/*
1322	 * if we no longer have any possibly dirty pages, take us off the
1323	 * syncer list.
1324	 */
1325
1326	if ((vp->v_iflag & VI_ONWORKLST) != 0 && uvm_obj_clean_p(uobj) &&
1327	    LIST_EMPTY(&vp->v_dirtyblkhd)) {
1328		vn_syncer_remove_from_worklist(vp);
1329	}
1330
1331	/* Wait for output to complete. */
1332	rw_exit(slock);
1333	if (!wasclean && !async && vp->v_numoutput != 0) {
1334		while (vp->v_numoutput != 0)
1335			cv_wait(&vp->v_cv, vp->v_interlock);
1336	}
1337	onworklst = (vp->v_iflag & VI_ONWORKLST) != 0;
1338	mutex_exit(vp->v_interlock);
1339
1340	if ((flags & PGO_RECLAIM) != 0 && onworklst) {
1341		/*
1342		 * in the case of PGO_RECLAIM, ensure to make the vnode clean.
1343		 * retrying is not a big deal because, in many cases,
1344		 * uobj->uo_npages is already 0 here.
1345		 */
1346		rw_enter(slock, RW_WRITER);
1347		goto retry;
1348	}
1349
1350	if (trans_mp) {
1351		if (holds_wapbl)
1352			WAPBL_END(trans_mp);
1353		fstrans_done(trans_mp);
1354	}
1355
1356	return (error);
1357}
1358
1359/*
1360 * Default putrange method for file systems that do not care
1361 * how many pages are given to one GOP_WRITE() call.
1362 */
1363void
1364genfs_gop_putrange(struct vnode *vp, off_t off, off_t *lop, off_t *hip)
1365{
1366
1367	*lop = 0;
1368	*hip = 0;
1369}
1370
1371int
1372genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1373{
1374	off_t off;
1375	vaddr_t kva;
1376	size_t len;
1377	int error;
1378	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1379
1380	UVMHIST_LOG(ubchist, "vp %#jx pgs %#jx npages %jd flags 0x%jx",
1381	    (uintptr_t)vp, (uintptr_t)pgs, npages, flags);
1382
1383	off = pgs[0]->offset;
1384	kva = uvm_pagermapin(pgs, npages,
1385	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1386	len = npages << PAGE_SHIFT;
1387
1388	error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
1389			    uvm_aio_aiodone);
1390
1391	return error;
1392}
1393
1394/*
1395 * genfs_gop_write_rwmap:
1396 *
1397 * a variant of genfs_gop_write.  it's used by UDF for its directory buffers.
1398 * this maps pages with PROT_WRITE so that VOP_STRATEGY can modifies
1399 * the contents before writing it out to the underlying storage.
1400 */
1401
1402int
1403genfs_gop_write_rwmap(struct vnode *vp, struct vm_page **pgs, int npages,
1404    int flags)
1405{
1406	off_t off;
1407	vaddr_t kva;
1408	size_t len;
1409	int error;
1410	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1411
1412	UVMHIST_LOG(ubchist, "vp %#jx pgs %#jx npages %jd flags 0x%jx",
1413	    (uintptr_t)vp, (uintptr_t)pgs, npages, flags);
1414
1415	off = pgs[0]->offset;
1416	kva = uvm_pagermapin(pgs, npages,
1417	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1418	len = npages << PAGE_SHIFT;
1419
1420	error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
1421			    uvm_aio_aiodone);
1422
1423	return error;
1424}
1425
1426/*
1427 * Backend routine for doing I/O to vnode pages.  Pages are already locked
1428 * and mapped into kernel memory.  Here we just look up the underlying
1429 * device block addresses and call the strategy routine.
1430 */
1431
1432static int
1433genfs_do_io(struct vnode *vp, off_t off, vaddr_t kva, size_t len, int flags,
1434    enum uio_rw rw, void (*iodone)(struct buf *))
1435{
1436	int s, error;
1437	int fs_bshift, dev_bshift;
1438	off_t eof, offset, startoffset;
1439	size_t bytes, iobytes, skipbytes;
1440	struct buf *mbp, *bp;
1441	const bool async = (flags & PGO_SYNCIO) == 0;
1442	const bool lazy = (flags & PGO_LAZY) == 0;
1443	const bool iowrite = rw == UIO_WRITE;
1444	const int brw = iowrite ? B_WRITE : B_READ;
1445	UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1446
1447	UVMHIST_LOG(ubchist, "vp %#jx kva %#jx len 0x%jx flags 0x%jx",
1448	    (uintptr_t)vp, (uintptr_t)kva, len, flags);
1449
1450	KASSERT(vp->v_size <= vp->v_writesize);
1451	GOP_SIZE(vp, vp->v_writesize, &eof, 0);
1452	if (vp->v_type != VBLK) {
1453		fs_bshift = vp->v_mount->mnt_fs_bshift;
1454		dev_bshift = vp->v_mount->mnt_dev_bshift;
1455	} else {
1456		fs_bshift = DEV_BSHIFT;
1457		dev_bshift = DEV_BSHIFT;
1458	}
1459	error = 0;
1460	startoffset = off;
1461	bytes = MIN(len, eof - startoffset);
1462	skipbytes = 0;
1463	KASSERT(bytes != 0);
1464
1465	if (iowrite) {
1466		/*
1467		 * why += 2?
1468		 * 1 for biodone, 1 for uvm_aio_aiodone.
1469		 */
1470		mutex_enter(vp->v_interlock);
1471		vp->v_numoutput += 2;
1472		mutex_exit(vp->v_interlock);
1473	}
1474	mbp = getiobuf(vp, true);
1475	UVMHIST_LOG(ubchist, "vp %#jx mbp %#jx num now %jd bytes 0x%jx",
1476	    (uintptr_t)vp, (uintptr_t)mbp, vp->v_numoutput, bytes);
1477	mbp->b_bufsize = len;
1478	mbp->b_data = (void *)kva;
1479	mbp->b_resid = mbp->b_bcount = bytes;
1480	mbp->b_cflags |= BC_BUSY | BC_AGE;
1481	if (async) {
1482		mbp->b_flags = brw | B_ASYNC;
1483		mbp->b_iodone = iodone;
1484	} else {
1485		mbp->b_flags = brw;
1486		mbp->b_iodone = NULL;
1487	}
1488	if (curlwp == uvm.pagedaemon_lwp)
1489		BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
1490	else if (async || lazy)
1491		BIO_SETPRIO(mbp, BPRIO_TIMENONCRITICAL);
1492	else
1493		BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
1494
1495	bp = NULL;
1496	for (offset = startoffset;
1497	    bytes > 0;
1498	    offset += iobytes, bytes -= iobytes) {
1499		int run;
1500		daddr_t lbn, blkno;
1501		struct vnode *devvp;
1502
1503		/*
1504		 * bmap the file to find out the blkno to read from and
1505		 * how much we can read in one i/o.  if bmap returns an error,
1506		 * skip the rest of the top-level i/o.
1507		 */
1508
1509		lbn = offset >> fs_bshift;
1510		error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1511		if (error) {
1512			UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%jx -> %jd",
1513			    lbn, error, 0, 0);
1514			skipbytes += bytes;
1515			bytes = 0;
1516			goto loopdone;
1517		}
1518
1519		/*
1520		 * see how many pages can be read with this i/o.
1521		 * reduce the i/o size if necessary to avoid
1522		 * overwriting pages with valid data.
1523		 */
1524
1525		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1526		    bytes);
1527
1528		/*
1529		 * if this block isn't allocated, zero it instead of
1530		 * reading it.  unless we are going to allocate blocks,
1531		 * mark the pages we zeroed PG_RDONLY.
1532		 */
1533
1534		if (blkno == (daddr_t)-1) {
1535			if (!iowrite) {
1536				memset((char *)kva + (offset - startoffset), 0,
1537				    iobytes);
1538			}
1539			skipbytes += iobytes;
1540			continue;
1541		}
1542
1543		/*
1544		 * allocate a sub-buf for this piece of the i/o
1545		 * (or just use mbp if there's only 1 piece),
1546		 * and start it going.
1547		 */
1548
1549		if (offset == startoffset && iobytes == bytes) {
1550			bp = mbp;
1551		} else {
1552			UVMHIST_LOG(ubchist, "vp %#jx bp %#jx num now %jd",
1553			    (uintptr_t)vp, (uintptr_t)bp, vp->v_numoutput, 0);
1554			bp = getiobuf(vp, true);
1555			nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
1556		}
1557		bp->b_lblkno = 0;
1558
1559		/* adjust physical blkno for partial blocks */
1560		bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
1561		    dev_bshift);
1562
1563		UVMHIST_LOG(ubchist,
1564		    "bp %#jx offset 0x%jx bcount 0x%jx blkno 0x%jx",
1565		    (uintptr_t)bp, offset, bp->b_bcount, bp->b_blkno);
1566
1567		VOP_STRATEGY(devvp, bp);
1568	}
1569
1570loopdone:
1571	if (skipbytes) {
1572		UVMHIST_LOG(ubchist, "skipbytes %jd", skipbytes, 0,0,0);
1573	}
1574	nestiobuf_done(mbp, skipbytes, error);
1575	if (async) {
1576		UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
1577		return (0);
1578	}
1579	UVMHIST_LOG(ubchist, "waiting for mbp %#jx", (uintptr_t)mbp, 0, 0, 0);
1580	error = biowait(mbp);
1581	s = splbio();
1582	(*iodone)(mbp);
1583	splx(s);
1584	UVMHIST_LOG(ubchist, "returning, error %jd", error, 0, 0, 0);
1585	return (error);
1586}
1587
1588int
1589genfs_compat_getpages(void *v)
1590{
1591	struct vop_getpages_args /* {
1592		struct vnode *a_vp;
1593		voff_t a_offset;
1594		struct vm_page **a_m;
1595		int *a_count;
1596		int a_centeridx;
1597		vm_prot_t a_access_type;
1598		int a_advice;
1599		int a_flags;
1600	} */ *ap = v;
1601
1602	off_t origoffset;
1603	struct vnode *vp = ap->a_vp;
1604	struct uvm_object *uobj = &vp->v_uobj;
1605	struct vm_page *pg, **pgs;
1606	vaddr_t kva;
1607	int i, error, orignpages, npages;
1608	struct iovec iov;
1609	struct uio uio;
1610	kauth_cred_t cred = curlwp->l_cred;
1611	const bool memwrite = (ap->a_access_type & VM_PROT_WRITE) != 0;
1612
1613	error = 0;
1614	origoffset = ap->a_offset;
1615	orignpages = *ap->a_count;
1616	pgs = ap->a_m;
1617
1618	if (ap->a_flags & PGO_LOCKED) {
1619		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m, NULL,
1620		    UFP_NOWAIT|UFP_NOALLOC| (memwrite ? UFP_NORDONLY : 0));
1621
1622		error = ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
1623		return error;
1624	}
1625	if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
1626		rw_exit(uobj->vmobjlock);
1627		return EINVAL;
1628	}
1629	if ((ap->a_flags & PGO_SYNCIO) == 0) {
1630		rw_exit(uobj->vmobjlock);
1631		return 0;
1632	}
1633	npages = orignpages;
1634	uvn_findpages(uobj, origoffset, &npages, pgs, NULL, UFP_ALL);
1635	rw_exit(uobj->vmobjlock);
1636	kva = uvm_pagermapin(pgs, npages,
1637	    UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1638	for (i = 0; i < npages; i++) {
1639		pg = pgs[i];
1640		if ((pg->flags & PG_FAKE) == 0) {
1641			continue;
1642		}
1643		iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
1644		iov.iov_len = PAGE_SIZE;
1645		uio.uio_iov = &iov;
1646		uio.uio_iovcnt = 1;
1647		uio.uio_offset = origoffset + (i << PAGE_SHIFT);
1648		uio.uio_rw = UIO_READ;
1649		uio.uio_resid = PAGE_SIZE;
1650		UIO_SETUP_SYSSPACE(&uio);
1651		/* XXX vn_lock */
1652		error = VOP_READ(vp, &uio, 0, cred);
1653		if (error) {
1654			break;
1655		}
1656		if (uio.uio_resid) {
1657			memset(iov.iov_base, 0, uio.uio_resid);
1658		}
1659	}
1660	uvm_pagermapout(kva, npages);
1661	rw_enter(uobj->vmobjlock, RW_WRITER);
1662	for (i = 0; i < npages; i++) {
1663		pg = pgs[i];
1664		if (error && (pg->flags & PG_FAKE) != 0) {
1665			pg->flags |= PG_RELEASED;
1666		} else {
1667			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_UNKNOWN);
1668			uvm_pagelock(pg);
1669			uvm_pageactivate(pg);
1670			uvm_pageunlock(pg);
1671		}
1672	}
1673	if (error) {
1674		uvm_page_unbusy(pgs, npages);
1675	}
1676	rw_exit(uobj->vmobjlock);
1677	return error;
1678}
1679
1680int
1681genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1682    int flags)
1683{
1684	off_t offset;
1685	struct iovec iov;
1686	struct uio uio;
1687	kauth_cred_t cred = curlwp->l_cred;
1688	struct buf *bp;
1689	vaddr_t kva;
1690	int error;
1691
1692	offset = pgs[0]->offset;
1693	kva = uvm_pagermapin(pgs, npages,
1694	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1695
1696	iov.iov_base = (void *)kva;
1697	iov.iov_len = npages << PAGE_SHIFT;
1698	uio.uio_iov = &iov;
1699	uio.uio_iovcnt = 1;
1700	uio.uio_offset = offset;
1701	uio.uio_rw = UIO_WRITE;
1702	uio.uio_resid = npages << PAGE_SHIFT;
1703	UIO_SETUP_SYSSPACE(&uio);
1704	/* XXX vn_lock */
1705	error = VOP_WRITE(vp, &uio, 0, cred);
1706
1707	mutex_enter(vp->v_interlock);
1708	vp->v_numoutput++;
1709	mutex_exit(vp->v_interlock);
1710
1711	bp = getiobuf(vp, true);
1712	bp->b_cflags |= BC_BUSY | BC_AGE;
1713	bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1714	bp->b_data = (char *)kva;
1715	bp->b_bcount = npages << PAGE_SHIFT;
1716	bp->b_bufsize = npages << PAGE_SHIFT;
1717	bp->b_resid = 0;
1718	bp->b_error = error;
1719	uvm_aio_aiodone(bp);
1720	return (error);
1721}
1722
1723/*
1724 * Process a uio using direct I/O.  If we reach a part of the request
1725 * which cannot be processed in this fashion for some reason, just return.
1726 * The caller must handle some additional part of the request using
1727 * buffered I/O before trying direct I/O again.
1728 */
1729
1730void
1731genfs_directio(struct vnode *vp, struct uio *uio, int ioflag)
1732{
1733	struct vmspace *vs;
1734	struct iovec *iov;
1735	vaddr_t va;
1736	size_t len;
1737	const int mask = DEV_BSIZE - 1;
1738	int error;
1739	bool need_wapbl = (vp->v_mount && vp->v_mount->mnt_wapbl &&
1740	    (ioflag & IO_JOURNALLOCKED) == 0);
1741
1742#ifdef DIAGNOSTIC
1743	if ((ioflag & IO_JOURNALLOCKED) && vp->v_mount->mnt_wapbl)
1744                WAPBL_JLOCK_ASSERT(vp->v_mount);
1745#endif
1746
1747	/*
1748	 * We only support direct I/O to user space for now.
1749	 */
1750
1751	if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
1752		return;
1753	}
1754
1755	/*
1756	 * If the vnode is mapped, we would need to get the getpages lock
1757	 * to stabilize the bmap, but then we would get into trouble while
1758	 * locking the pages if the pages belong to this same vnode (or a
1759	 * multi-vnode cascade to the same effect).  Just fall back to
1760	 * buffered I/O if the vnode is mapped to avoid this mess.
1761	 */
1762
1763	if (vp->v_vflag & VV_MAPPED) {
1764		return;
1765	}
1766
1767	if (need_wapbl) {
1768		error = WAPBL_BEGIN(vp->v_mount);
1769		if (error)
1770			return;
1771	}
1772
1773	/*
1774	 * Do as much of the uio as possible with direct I/O.
1775	 */
1776
1777	vs = uio->uio_vmspace;
1778	while (uio->uio_resid) {
1779		iov = uio->uio_iov;
1780		if (iov->iov_len == 0) {
1781			uio->uio_iov++;
1782			uio->uio_iovcnt--;
1783			continue;
1784		}
1785		va = (vaddr_t)iov->iov_base;
1786		len = MIN(iov->iov_len, genfs_maxdio);
1787		len &= ~mask;
1788
1789		/*
1790		 * If the next chunk is smaller than DEV_BSIZE or extends past
1791		 * the current EOF, then fall back to buffered I/O.
1792		 */
1793
1794		if (len == 0 || uio->uio_offset + len > vp->v_size) {
1795			break;
1796		}
1797
1798		/*
1799		 * Check alignment.  The file offset must be at least
1800		 * sector-aligned.  The exact constraint on memory alignment
1801		 * is very hardware-dependent, but requiring sector-aligned
1802		 * addresses there too is safe.
1803		 */
1804
1805		if (uio->uio_offset & mask || va & mask) {
1806			break;
1807		}
1808		error = genfs_do_directio(vs, va, len, vp, uio->uio_offset,
1809					  uio->uio_rw);
1810		if (error) {
1811			break;
1812		}
1813		iov->iov_base = (char *)iov->iov_base + len;
1814		iov->iov_len -= len;
1815		uio->uio_offset += len;
1816		uio->uio_resid -= len;
1817	}
1818
1819	if (need_wapbl)
1820		WAPBL_END(vp->v_mount);
1821}
1822
1823/*
1824 * Iodone routine for direct I/O.  We don't do much here since the request is
1825 * always synchronous, so the caller will do most of the work after biowait().
1826 */
1827
1828static void
1829genfs_dio_iodone(struct buf *bp)
1830{
1831
1832	KASSERT((bp->b_flags & B_ASYNC) == 0);
1833	if ((bp->b_flags & B_READ) == 0 && (bp->b_cflags & BC_AGE) != 0) {
1834		mutex_enter(bp->b_objlock);
1835		vwakeup(bp);
1836		mutex_exit(bp->b_objlock);
1837	}
1838	putiobuf(bp);
1839}
1840
1841/*
1842 * Process one chunk of a direct I/O request.
1843 */
1844
1845static int
1846genfs_do_directio(struct vmspace *vs, vaddr_t uva, size_t len, struct vnode *vp,
1847    off_t off, enum uio_rw rw)
1848{
1849	struct vm_map *map;
1850	struct pmap *upm, *kpm __unused;
1851	size_t klen = round_page(uva + len) - trunc_page(uva);
1852	off_t spoff, epoff;
1853	vaddr_t kva, puva;
1854	paddr_t pa;
1855	vm_prot_t prot;
1856	int error, rv __diagused, poff, koff;
1857	const int pgoflags = PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED |
1858		(rw == UIO_WRITE ? PGO_FREE : 0);
1859
1860	/*
1861	 * For writes, verify that this range of the file already has fully
1862	 * allocated backing store.  If there are any holes, just punt and
1863	 * make the caller take the buffered write path.
1864	 */
1865
1866	if (rw == UIO_WRITE) {
1867		daddr_t lbn, elbn, blkno;
1868		int bsize, bshift, run;
1869
1870		bshift = vp->v_mount->mnt_fs_bshift;
1871		bsize = 1 << bshift;
1872		lbn = off >> bshift;
1873		elbn = (off + len + bsize - 1) >> bshift;
1874		while (lbn < elbn) {
1875			error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
1876			if (error) {
1877				return error;
1878			}
1879			if (blkno == (daddr_t)-1) {
1880				return ENOSPC;
1881			}
1882			lbn += 1 + run;
1883		}
1884	}
1885
1886	/*
1887	 * Flush any cached pages for parts of the file that we're about to
1888	 * access.  If we're writing, invalidate pages as well.
1889	 */
1890
1891	spoff = trunc_page(off);
1892	epoff = round_page(off + len);
1893	rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
1894	error = VOP_PUTPAGES(vp, spoff, epoff, pgoflags);
1895	if (error) {
1896		return error;
1897	}
1898
1899	/*
1900	 * Wire the user pages and remap them into kernel memory.
1901	 */
1902
1903	prot = rw == UIO_READ ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
1904	error = uvm_vslock(vs, (void *)uva, len, prot);
1905	if (error) {
1906		return error;
1907	}
1908
1909	map = &vs->vm_map;
1910	upm = vm_map_pmap(map);
1911	kpm = vm_map_pmap(kernel_map);
1912	puva = trunc_page(uva);
1913	kva = uvm_km_alloc(kernel_map, klen, atop(puva) & uvmexp.colormask,
1914	    UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_COLORMATCH);
1915	for (poff = 0; poff < klen; poff += PAGE_SIZE) {
1916		rv = pmap_extract(upm, puva + poff, &pa);
1917		KASSERT(rv);
1918		pmap_kenter_pa(kva + poff, pa, prot, PMAP_WIRED);
1919	}
1920	pmap_update(kpm);
1921
1922	/*
1923	 * Do the I/O.
1924	 */
1925
1926	koff = uva - trunc_page(uva);
1927	error = genfs_do_io(vp, off, kva + koff, len, PGO_SYNCIO, rw,
1928			    genfs_dio_iodone);
1929
1930	/*
1931	 * Tear down the kernel mapping.
1932	 */
1933
1934	pmap_kremove(kva, klen);
1935	pmap_update(kpm);
1936	uvm_km_free(kernel_map, kva, klen, UVM_KMF_VAONLY);
1937
1938	/*
1939	 * Unwire the user pages.
1940	 */
1941
1942	uvm_vsunlock(vs, (void *)uva, len);
1943	return error;
1944}
1945