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