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