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