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