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