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