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