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