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