genfs_io.c revision 1.36.2.11 1 /* $NetBSD: genfs_io.c,v 1.36.2.11 2010/07/06 07:20:27 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.11 2010/07/06 07:20:27 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 #include <sys/once.h>
55
56 #include <miscfs/genfs/genfs.h>
57 #include <miscfs/genfs/genfs_node.h>
58 #include <miscfs/specfs/specdev.h>
59
60 #include <uvm/uvm.h>
61 #include <uvm/uvm_pager.h>
62
63 static int genfs_do_getpages(void *);
64 #ifdef XIP
65 static int genfs_do_getpages_xip(void *);
66 #endif
67 static int genfs_do_directio(struct vmspace *, vaddr_t, size_t, struct vnode *,
68 off_t, enum uio_rw);
69 static void genfs_dio_iodone(struct buf *);
70
71 static int genfs_do_io(struct vnode *, off_t, vaddr_t, size_t, int, enum uio_rw,
72 void (*)(struct buf *));
73 static inline void genfs_rel_pages(struct vm_page **, int);
74
75 int genfs_maxdio = MAXPHYS;
76
77 static inline void
78 genfs_rel_pages(struct vm_page **pgs, int npages)
79 {
80 int i;
81
82 for (i = 0; i < npages; i++) {
83 struct vm_page *pg = pgs[i];
84
85 if (pg == NULL || pg == PGO_DONTCARE)
86 continue;
87 if (pg->flags & PG_FAKE) {
88 pg->flags |= PG_RELEASED;
89 }
90 }
91 mutex_enter(&uvm_pageqlock);
92 uvm_page_unbusy(pgs, npages);
93 mutex_exit(&uvm_pageqlock);
94 }
95
96 /*
97 * generic VM getpages routine.
98 * Return PG_BUSY pages for the given range,
99 * reading from backing store if necessary.
100 */
101
102 int
103 genfs_getpages(void *v)
104 {
105 #ifdef XIP
106 struct vop_getpages_args /* {
107 struct vnode *a_vp;
108 voff_t a_offset;
109 struct vm_page **a_m;
110 int *a_count;
111 int a_centeridx;
112 vm_prot_t a_access_type;
113 int a_advice;
114 int a_flags;
115 } */ * const ap = v;
116 struct vnode * const vp = ap->a_vp;
117
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 static struct uvm_object xip_zero_obj;
747 static struct vm_page *xip_zero_page;
748
749 static int
750 xip_zero_page_init(void)
751 {
752
753 UVM_OBJ_INIT(&xip_zero_obj, NULL, 0);
754 xip_zero_page = uvm_pagealloc(&xip_zero_obj, 0, NULL, UVM_PGA_ZERO);
755 KASSERT(xip_zero_page != NULL);
756 uvm_pagewire(xip_zero_page);
757 return 0;
758 }
759
760 /*
761 * genfs_do_getpages_xip
762 * Return "direct pages" of XIP vnode. The block addresses of XIP
763 * vnode pages are returned back to the VM fault handler as the
764 * actually mapped physical addresses.
765 *
766 * XXX Should be merged into genfs_do_getpages() after
767 * XXX genfs_do_getpages() and genfs_do_io() are merged.
768 */
769 static int
770 genfs_do_getpages_xip(void *v)
771 {
772 struct vop_getpages_args /* {
773 struct vnode *a_vp;
774 voff_t a_offset;
775 struct vm_page **a_m;
776 int *a_count;
777 int a_centeridx;
778 vm_prot_t a_access_type;
779 int a_advice;
780 int a_flags;
781 } */ * const ap = v;
782
783 struct vnode * const vp = ap->a_vp;
784 int *npagesp = ap->a_count;
785 const off_t offset = ap->a_offset;
786 struct vm_page **pps = ap->a_m;
787 struct uvm_object * const uobj = &vp->v_uobj;
788 const int flags = ap->a_flags;
789
790 int error;
791 off_t eof, sbkoff, ebkoff, off;
792 int npages;
793 int fs_bshift, fs_bsize, dev_bshift, dev_bsize;
794 int i;
795 paddr_t phys_addr;
796
797 UVMHIST_FUNC("genfs_do_getpages_xip"); UVMHIST_CALLED(ubchist);
798
799 KASSERT((vp->v_vflag & VV_XIP) != 0);
800
801 GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_MEM);
802 npages = MIN(*npagesp, round_page(eof - offset) >> PAGE_SHIFT);
803
804 fs_bshift = vp->v_mount->mnt_fs_bshift;
805 fs_bsize = 1 << fs_bshift;
806 dev_bshift = vp->v_mount->mnt_dev_bshift;
807 dev_bsize = 1 << dev_bshift;
808
809 sbkoff = offset & ~(fs_bsize - 1);
810 ebkoff = ((offset + PAGE_SIZE * npages) + (fs_bsize - 1)) & ~(fs_bsize - 1);
811
812 UVMHIST_LOG(ubchist, "xip npages=%d sbkoff=%lx ebkoff=%lx", npages, (long)sbkoff, (long)ebkoff, 0);
813
814 if ((flags & PGO_LOCKED) == 0)
815 mutex_exit(&uobj->vmobjlock);
816
817 /* XXX optimize */
818 off = offset;
819 i = 0;
820 while (i < npages) {
821 daddr_t lbn, blkno;
822 int run;
823 struct vnode *devvp;
824
825 lbn = (off & ~(fs_bsize - 1)) >> fs_bshift;
826
827 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
828 KASSERT(error == 0);
829 UVMHIST_LOG(ubchist, "xip VOP_BMAP: lbn=%ld blkno=%ld run=%d", (long)lbn, (long)blkno, run, 0);
830
831 if (blkno < 0) {
832 static ONCE_DECL(xip_zero_page_inited);
833
834 RUN_ONCE(&xip_zero_page_inited, xip_zero_page_init);
835 phys_addr = xip_zero_page->phys_addr;
836 } else {
837 struct vm_physseg *seg;
838
839 seg = devvp->v_physseg;
840 KASSERT(seg != NULL);
841 /* bus_space_mmap cookie -> paddr_t */
842 phys_addr = pmap_phys_address(seg->start) +
843 (blkno << dev_bshift) +
844 (off - (lbn << fs_bshift));
845 }
846
847 pps[i] = uvm_phys_to_vm_page_direct(phys_addr);
848
849 UVMHIST_LOG(ubchist, "xip pgs %d => phys_addr=0x%lx (%p)",
850 i,
851 (long)phys_addr,
852 pps[i],
853 0);
854
855 off += PAGE_SIZE;
856 i++;
857 }
858
859 *npagesp = i;
860
861 return 0;
862 }
863 #endif
864
865 /*
866 * generic VM putpages routine.
867 * Write the given range of pages to backing store.
868 *
869 * => "offhi == 0" means flush all pages at or after "offlo".
870 * => object should be locked by caller. we return with the
871 * object unlocked.
872 * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
873 * thus, a caller might want to unlock higher level resources
874 * (e.g. vm_map) before calling flush.
875 * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, we will not block
876 * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
877 * => NOTE: we rely on the fact that the object's memq is a TAILQ and
878 * that new pages are inserted on the tail end of the list. thus,
879 * we can make a complete pass through the object in one go by starting
880 * at the head and working towards the tail (new pages are put in
881 * front of us).
882 * => NOTE: we are allowed to lock the page queues, so the caller
883 * must not be holding the page queue lock.
884 *
885 * note on "cleaning" object and PG_BUSY pages:
886 * this routine is holding the lock on the object. the only time
887 * that it can run into a PG_BUSY page that it does not own is if
888 * some other process has started I/O on the page (e.g. either
889 * a pagein, or a pageout). if the PG_BUSY page is being paged
890 * in, then it can not be dirty (!PG_CLEAN) because no one has
891 * had a chance to modify it yet. if the PG_BUSY page is being
892 * paged out then it means that someone else has already started
893 * cleaning the page for us (how nice!). in this case, if we
894 * have syncio specified, then after we make our pass through the
895 * object we need to wait for the other PG_BUSY pages to clear
896 * off (i.e. we need to do an iosync). also note that once a
897 * page is PG_BUSY it must stay in its object until it is un-busyed.
898 *
899 * note on page traversal:
900 * we can traverse the pages in an object either by going down the
901 * linked list in "uobj->memq", or we can go over the address range
902 * by page doing hash table lookups for each address. depending
903 * on how many pages are in the object it may be cheaper to do one
904 * or the other. we set "by_list" to true if we are using memq.
905 * if the cost of a hash lookup was equal to the cost of the list
906 * traversal we could compare the number of pages in the start->stop
907 * range to the total number of pages in the object. however, it
908 * seems that a hash table lookup is more expensive than the linked
909 * list traversal, so we multiply the number of pages in the
910 * range by an estimate of the relatively higher cost of the hash lookup.
911 */
912
913 int
914 genfs_putpages(void *v)
915 {
916 struct vop_putpages_args /* {
917 struct vnode *a_vp;
918 voff_t a_offlo;
919 voff_t a_offhi;
920 int a_flags;
921 } */ * const ap = v;
922
923 return genfs_do_putpages(ap->a_vp, ap->a_offlo, ap->a_offhi,
924 ap->a_flags, NULL);
925 }
926
927 int
928 genfs_do_putpages(struct vnode *vp, off_t startoff, off_t endoff,
929 int origflags, struct vm_page **busypg)
930 {
931 struct uvm_object * const uobj = &vp->v_uobj;
932 kmutex_t * const slock = &uobj->vmobjlock;
933 off_t off;
934 /* Even for strange MAXPHYS, the shift rounds down to a page */
935 #define maxpages (MAXPHYS >> PAGE_SHIFT)
936 int i, error, npages, nback;
937 int freeflag;
938 struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp;
939 bool wasclean, by_list, needs_clean, yld;
940 bool async = (origflags & PGO_SYNCIO) == 0;
941 bool pagedaemon = curlwp == uvm.pagedaemon_lwp;
942 struct lwp * const l = curlwp ? curlwp : &lwp0;
943 struct genfs_node * const gp = VTOG(vp);
944 int flags;
945 int dirtygen;
946 bool modified;
947 bool need_wapbl;
948 bool has_trans;
949 bool cleanall;
950 bool onworklst;
951
952 UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
953
954 KASSERT(origflags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
955 KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
956 KASSERT(startoff < endoff || endoff == 0);
957
958 UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
959 vp, uobj->uo_npages, startoff, endoff - startoff);
960
961 has_trans = false;
962 need_wapbl = (!pagedaemon && vp->v_mount && vp->v_mount->mnt_wapbl &&
963 (origflags & PGO_JOURNALLOCKED) == 0);
964
965 retry:
966 modified = false;
967 flags = origflags;
968 KASSERT((vp->v_iflag & VI_ONWORKLST) != 0 ||
969 (vp->v_iflag & VI_WRMAPDIRTY) == 0);
970 if (uobj->uo_npages == 0) {
971 if (vp->v_iflag & VI_ONWORKLST) {
972 vp->v_iflag &= ~VI_WRMAPDIRTY;
973 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
974 vn_syncer_remove_from_worklist(vp);
975 }
976 if (has_trans) {
977 if (need_wapbl)
978 WAPBL_END(vp->v_mount);
979 fstrans_done(vp->v_mount);
980 }
981 mutex_exit(slock);
982 return (0);
983 }
984
985 /*
986 * the vnode has pages, set up to process the request.
987 */
988
989 if (!has_trans && (flags & PGO_CLEANIT) != 0) {
990 mutex_exit(slock);
991 if (pagedaemon) {
992 error = fstrans_start_nowait(vp->v_mount, FSTRANS_LAZY);
993 if (error)
994 return error;
995 } else
996 fstrans_start(vp->v_mount, FSTRANS_LAZY);
997 if (need_wapbl) {
998 error = WAPBL_BEGIN(vp->v_mount);
999 if (error) {
1000 fstrans_done(vp->v_mount);
1001 return error;
1002 }
1003 }
1004 has_trans = true;
1005 mutex_enter(slock);
1006 goto retry;
1007 }
1008
1009 error = 0;
1010 wasclean = (vp->v_numoutput == 0);
1011 off = startoff;
1012 if (endoff == 0 || flags & PGO_ALLPAGES) {
1013 endoff = trunc_page(LLONG_MAX);
1014 }
1015 by_list = (uobj->uo_npages <=
1016 ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_TREE_PENALTY);
1017
1018 #if !defined(DEBUG)
1019 /*
1020 * if this vnode is known not to have dirty pages,
1021 * don't bother to clean it out.
1022 */
1023
1024 if ((vp->v_iflag & VI_ONWORKLST) == 0) {
1025 if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == 0) {
1026 goto skip_scan;
1027 }
1028 flags &= ~PGO_CLEANIT;
1029 }
1030 #endif /* !defined(DEBUG) */
1031
1032 /*
1033 * start the loop. when scanning by list, hold the last page
1034 * in the list before we start. pages allocated after we start
1035 * will be added to the end of the list, so we can stop at the
1036 * current last page.
1037 */
1038
1039 cleanall = (flags & PGO_CLEANIT) != 0 && wasclean &&
1040 startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
1041 (vp->v_iflag & VI_ONWORKLST) != 0;
1042 dirtygen = gp->g_dirtygen;
1043 freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
1044 if (by_list) {
1045 curmp.uobject = uobj;
1046 curmp.offset = (voff_t)-1;
1047 curmp.flags = PG_BUSY;
1048 endmp.uobject = uobj;
1049 endmp.offset = (voff_t)-1;
1050 endmp.flags = PG_BUSY;
1051 pg = TAILQ_FIRST(&uobj->memq);
1052 TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq.queue);
1053 } else {
1054 pg = uvm_pagelookup(uobj, off);
1055 }
1056 nextpg = NULL;
1057 while (by_list || off < endoff) {
1058
1059 /*
1060 * if the current page is not interesting, move on to the next.
1061 */
1062
1063 KASSERT(pg == NULL || pg->uobject == uobj);
1064 KASSERT(pg == NULL ||
1065 (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
1066 (pg->flags & PG_BUSY) != 0);
1067 if (by_list) {
1068 if (pg == &endmp) {
1069 break;
1070 }
1071 if (pg->offset < startoff || pg->offset >= endoff ||
1072 pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1073 if (pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1074 wasclean = false;
1075 }
1076 pg = TAILQ_NEXT(pg, listq.queue);
1077 continue;
1078 }
1079 off = pg->offset;
1080 } else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1081 if (pg != NULL) {
1082 wasclean = false;
1083 }
1084 off += PAGE_SIZE;
1085 if (off < endoff) {
1086 pg = uvm_pagelookup(uobj, off);
1087 }
1088 continue;
1089 }
1090
1091 /*
1092 * if the current page needs to be cleaned and it's busy,
1093 * wait for it to become unbusy.
1094 */
1095
1096 yld = (l->l_cpu->ci_schedstate.spc_flags &
1097 SPCF_SHOULDYIELD) && !pagedaemon;
1098 if (pg->flags & PG_BUSY || yld) {
1099 UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
1100 if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
1101 UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
1102 error = EDEADLK;
1103 if (busypg != NULL)
1104 *busypg = pg;
1105 break;
1106 }
1107 if (pagedaemon) {
1108 /*
1109 * someone has taken the page while we
1110 * dropped the lock for fstrans_start.
1111 */
1112 break;
1113 }
1114 if (by_list) {
1115 TAILQ_INSERT_BEFORE(pg, &curmp, listq.queue);
1116 UVMHIST_LOG(ubchist, "curmp next %p",
1117 TAILQ_NEXT(&curmp, listq.queue), 0,0,0);
1118 }
1119 if (yld) {
1120 mutex_exit(slock);
1121 preempt();
1122 mutex_enter(slock);
1123 } else {
1124 pg->flags |= PG_WANTED;
1125 UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
1126 mutex_enter(slock);
1127 }
1128 if (by_list) {
1129 UVMHIST_LOG(ubchist, "after next %p",
1130 TAILQ_NEXT(&curmp, listq.queue), 0,0,0);
1131 pg = TAILQ_NEXT(&curmp, listq.queue);
1132 TAILQ_REMOVE(&uobj->memq, &curmp, listq.queue);
1133 } else {
1134 pg = uvm_pagelookup(uobj, off);
1135 }
1136 continue;
1137 }
1138
1139 /*
1140 * if we're freeing, remove all mappings of the page now.
1141 * if we're cleaning, check if the page is needs to be cleaned.
1142 */
1143
1144 if (flags & PGO_FREE) {
1145 pmap_page_protect(pg, VM_PROT_NONE);
1146 } else if (flags & PGO_CLEANIT) {
1147
1148 /*
1149 * if we still have some hope to pull this vnode off
1150 * from the syncer queue, write-protect the page.
1151 */
1152
1153 if (cleanall && wasclean &&
1154 gp->g_dirtygen == dirtygen) {
1155
1156 /*
1157 * uobj pages get wired only by uvm_fault
1158 * where uobj is locked.
1159 */
1160
1161 if (pg->wire_count == 0) {
1162 pmap_page_protect(pg,
1163 VM_PROT_READ|VM_PROT_EXECUTE);
1164 } else {
1165 cleanall = false;
1166 }
1167 }
1168 }
1169
1170 if (flags & PGO_CLEANIT) {
1171 needs_clean = pmap_clear_modify(pg) ||
1172 (pg->flags & PG_CLEAN) == 0;
1173 pg->flags |= PG_CLEAN;
1174 } else {
1175 needs_clean = false;
1176 }
1177
1178 /*
1179 * if we're cleaning, build a cluster.
1180 * the cluster will consist of pages which are currently dirty,
1181 * but they will be returned to us marked clean.
1182 * if not cleaning, just operate on the one page.
1183 */
1184
1185 if (needs_clean) {
1186 KDASSERT((vp->v_iflag & VI_ONWORKLST));
1187 wasclean = false;
1188 memset(pgs, 0, sizeof(pgs));
1189 pg->flags |= PG_BUSY;
1190 UVM_PAGE_OWN(pg, "genfs_putpages");
1191
1192 /*
1193 * first look backward.
1194 */
1195
1196 npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
1197 nback = npages;
1198 uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
1199 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
1200 if (nback) {
1201 memmove(&pgs[0], &pgs[npages - nback],
1202 nback * sizeof(pgs[0]));
1203 if (npages - nback < nback)
1204 memset(&pgs[nback], 0,
1205 (npages - nback) * sizeof(pgs[0]));
1206 else
1207 memset(&pgs[npages - nback], 0,
1208 nback * sizeof(pgs[0]));
1209 }
1210
1211 /*
1212 * then plug in our page of interest.
1213 */
1214
1215 pgs[nback] = pg;
1216
1217 /*
1218 * then look forward to fill in the remaining space in
1219 * the array of pages.
1220 */
1221
1222 npages = maxpages - nback - 1;
1223 uvn_findpages(uobj, off + PAGE_SIZE, &npages,
1224 &pgs[nback + 1],
1225 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
1226 npages += nback + 1;
1227 } else {
1228 pgs[0] = pg;
1229 npages = 1;
1230 nback = 0;
1231 }
1232
1233 /*
1234 * apply FREE or DEACTIVATE options if requested.
1235 */
1236
1237 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1238 mutex_enter(&uvm_pageqlock);
1239 }
1240 for (i = 0; i < npages; i++) {
1241 tpg = pgs[i];
1242 KASSERT(tpg->uobject == uobj);
1243 if (by_list && tpg == TAILQ_NEXT(pg, listq.queue))
1244 pg = tpg;
1245 if (tpg->offset < startoff || tpg->offset >= endoff)
1246 continue;
1247 if (flags & PGO_DEACTIVATE && tpg->wire_count == 0) {
1248 uvm_pagedeactivate(tpg);
1249 } else if (flags & PGO_FREE) {
1250 pmap_page_protect(tpg, VM_PROT_NONE);
1251 if (tpg->flags & PG_BUSY) {
1252 tpg->flags |= freeflag;
1253 if (pagedaemon) {
1254 uvm_pageout_start(1);
1255 uvm_pagedequeue(tpg);
1256 }
1257 } else {
1258
1259 /*
1260 * ``page is not busy''
1261 * implies that npages is 1
1262 * and needs_clean is false.
1263 */
1264
1265 nextpg = TAILQ_NEXT(tpg, listq.queue);
1266 uvm_pagefree(tpg);
1267 if (pagedaemon)
1268 uvmexp.pdfreed++;
1269 }
1270 }
1271 }
1272 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1273 mutex_exit(&uvm_pageqlock);
1274 }
1275 if (needs_clean) {
1276 modified = true;
1277
1278 /*
1279 * start the i/o. if we're traversing by list,
1280 * keep our place in the list with a marker page.
1281 */
1282
1283 if (by_list) {
1284 TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
1285 listq.queue);
1286 }
1287 mutex_exit(slock);
1288 error = GOP_WRITE(vp, pgs, npages, flags);
1289 mutex_enter(slock);
1290 if (by_list) {
1291 pg = TAILQ_NEXT(&curmp, listq.queue);
1292 TAILQ_REMOVE(&uobj->memq, &curmp, listq.queue);
1293 }
1294 if (error) {
1295 break;
1296 }
1297 if (by_list) {
1298 continue;
1299 }
1300 }
1301
1302 /*
1303 * find the next page and continue if there was no error.
1304 */
1305
1306 if (by_list) {
1307 if (nextpg) {
1308 pg = nextpg;
1309 nextpg = NULL;
1310 } else {
1311 pg = TAILQ_NEXT(pg, listq.queue);
1312 }
1313 } else {
1314 off += (npages - nback) << PAGE_SHIFT;
1315 if (off < endoff) {
1316 pg = uvm_pagelookup(uobj, off);
1317 }
1318 }
1319 }
1320 if (by_list) {
1321 TAILQ_REMOVE(&uobj->memq, &endmp, listq.queue);
1322 }
1323
1324 if (modified && (vp->v_iflag & VI_WRMAPDIRTY) != 0 &&
1325 (vp->v_type != VBLK ||
1326 (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
1327 GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
1328 }
1329
1330 /*
1331 * if we're cleaning and there was nothing to clean,
1332 * take us off the syncer list. if we started any i/o
1333 * and we're doing sync i/o, wait for all writes to finish.
1334 */
1335
1336 if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
1337 (vp->v_iflag & VI_ONWORKLST) != 0) {
1338 #if defined(DEBUG)
1339 TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
1340 if ((pg->flags & PG_CLEAN) == 0) {
1341 printf("%s: %p: !CLEAN\n", __func__, pg);
1342 }
1343 if (pmap_is_modified(pg)) {
1344 printf("%s: %p: modified\n", __func__, pg);
1345 }
1346 }
1347 #endif /* defined(DEBUG) */
1348 vp->v_iflag &= ~VI_WRMAPDIRTY;
1349 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
1350 vn_syncer_remove_from_worklist(vp);
1351 }
1352
1353 #if !defined(DEBUG)
1354 skip_scan:
1355 #endif /* !defined(DEBUG) */
1356
1357 /* Wait for output to complete. */
1358 if (!wasclean && !async && vp->v_numoutput != 0) {
1359 while (vp->v_numoutput != 0)
1360 cv_wait(&vp->v_cv, slock);
1361 }
1362 onworklst = (vp->v_iflag & VI_ONWORKLST) != 0;
1363 mutex_exit(slock);
1364
1365 if ((flags & PGO_RECLAIM) != 0 && onworklst) {
1366 /*
1367 * in the case of PGO_RECLAIM, ensure to make the vnode clean.
1368 * retrying is not a big deal because, in many cases,
1369 * uobj->uo_npages is already 0 here.
1370 */
1371 mutex_enter(slock);
1372 goto retry;
1373 }
1374
1375 if (has_trans) {
1376 if (need_wapbl)
1377 WAPBL_END(vp->v_mount);
1378 fstrans_done(vp->v_mount);
1379 }
1380
1381 return (error);
1382 }
1383
1384 int
1385 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1386 {
1387 off_t off;
1388 vaddr_t kva;
1389 size_t len;
1390 int error;
1391 UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1392
1393 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1394 vp, pgs, npages, flags);
1395
1396 off = pgs[0]->offset;
1397 kva = uvm_pagermapin(pgs, npages,
1398 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1399 len = npages << PAGE_SHIFT;
1400
1401 error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
1402 uvm_aio_biodone);
1403
1404 return error;
1405 }
1406
1407 int
1408 genfs_gop_write_rwmap(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1409 {
1410 off_t off;
1411 vaddr_t kva;
1412 size_t len;
1413 int error;
1414 UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1415
1416 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1417 vp, pgs, npages, flags);
1418
1419 off = pgs[0]->offset;
1420 kva = uvm_pagermapin(pgs, npages,
1421 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1422 len = npages << PAGE_SHIFT;
1423
1424 error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
1425 uvm_aio_biodone);
1426
1427 return error;
1428 }
1429
1430 /*
1431 * Backend routine for doing I/O to vnode pages. Pages are already locked
1432 * and mapped into kernel memory. Here we just look up the underlying
1433 * device block addresses and call the strategy routine.
1434 */
1435
1436 static int
1437 genfs_do_io(struct vnode *vp, off_t off, vaddr_t kva, size_t len, int flags,
1438 enum uio_rw rw, void (*iodone)(struct buf *))
1439 {
1440 int s, error;
1441 int fs_bshift, dev_bshift;
1442 off_t eof, offset, startoffset;
1443 size_t bytes, iobytes, skipbytes;
1444 struct buf *mbp, *bp;
1445 const bool async = (flags & PGO_SYNCIO) == 0;
1446 const bool iowrite = rw == UIO_WRITE;
1447 const int brw = iowrite ? B_WRITE : B_READ;
1448 UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1449
1450 UVMHIST_LOG(ubchist, "vp %p kva %p len 0x%x flags 0x%x",
1451 vp, kva, len, flags);
1452
1453 KASSERT(vp->v_size <= vp->v_writesize);
1454 GOP_SIZE(vp, vp->v_writesize, &eof, 0);
1455 if (vp->v_type != VBLK) {
1456 fs_bshift = vp->v_mount->mnt_fs_bshift;
1457 dev_bshift = vp->v_mount->mnt_dev_bshift;
1458 } else {
1459 fs_bshift = DEV_BSHIFT;
1460 dev_bshift = DEV_BSHIFT;
1461 }
1462 error = 0;
1463 startoffset = off;
1464 bytes = MIN(len, eof - startoffset);
1465 skipbytes = 0;
1466 KASSERT(bytes != 0);
1467
1468 if (iowrite) {
1469 mutex_enter(&vp->v_interlock);
1470 vp->v_numoutput += 2;
1471 mutex_exit(&vp->v_interlock);
1472 }
1473 mbp = getiobuf(vp, true);
1474 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1475 vp, mbp, vp->v_numoutput, bytes);
1476 mbp->b_bufsize = len;
1477 mbp->b_data = (void *)kva;
1478 mbp->b_resid = mbp->b_bcount = bytes;
1479 mbp->b_cflags = BC_BUSY | BC_AGE;
1480 if (async) {
1481 mbp->b_flags = brw | B_ASYNC;
1482 mbp->b_iodone = iodone;
1483 } else {
1484 mbp->b_flags = brw;
1485 mbp->b_iodone = NULL;
1486 }
1487 if (curlwp == uvm.pagedaemon_lwp)
1488 BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
1489 else if (async)
1490 BIO_SETPRIO(mbp, BPRIO_TIMENONCRITICAL);
1491 else
1492 BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
1493
1494 bp = NULL;
1495 for (offset = startoffset;
1496 bytes > 0;
1497 offset += iobytes, bytes -= iobytes) {
1498 int run;
1499 daddr_t lbn, blkno;
1500 struct vnode *devvp;
1501
1502 /*
1503 * bmap the file to find out the blkno to read from and
1504 * how much we can read in one i/o. if bmap returns an error,
1505 * skip the rest of the top-level i/o.
1506 */
1507
1508 lbn = offset >> fs_bshift;
1509 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1510 if (error) {
1511 UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
1512 lbn,error,0,0);
1513 skipbytes += bytes;
1514 bytes = 0;
1515 goto loopdone;
1516 }
1517
1518 /*
1519 * see how many pages can be read with this i/o.
1520 * reduce the i/o size if necessary to avoid
1521 * overwriting pages with valid data.
1522 */
1523
1524 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1525 bytes);
1526
1527 /*
1528 * if this block isn't allocated, zero it instead of
1529 * reading it. unless we are going to allocate blocks,
1530 * mark the pages we zeroed PG_RDONLY.
1531 */
1532
1533 if (blkno == (daddr_t)-1) {
1534 if (!iowrite) {
1535 memset((char *)kva + (offset - startoffset), 0,
1536 iobytes);
1537 }
1538 skipbytes += iobytes;
1539 continue;
1540 }
1541
1542 /*
1543 * allocate a sub-buf for this piece of the i/o
1544 * (or just use mbp if there's only 1 piece),
1545 * and start it going.
1546 */
1547
1548 if (offset == startoffset && iobytes == bytes) {
1549 bp = mbp;
1550 } else {
1551 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1552 vp, bp, vp->v_numoutput, 0);
1553 bp = getiobuf(vp, true);
1554 nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
1555 }
1556 bp->b_lblkno = 0;
1557
1558 /* adjust physical blkno for partial blocks */
1559 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
1560 dev_bshift);
1561
1562 UVMHIST_LOG(ubchist,
1563 "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
1564 bp, offset, bp->b_bcount, bp->b_blkno);
1565
1566 VOP_STRATEGY(devvp, bp);
1567 }
1568
1569 loopdone:
1570 if (skipbytes) {
1571 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1572 }
1573 nestiobuf_done(mbp, skipbytes, error);
1574 if (async) {
1575 UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
1576 return (0);
1577 }
1578 UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1579 error = biowait(mbp);
1580 s = splbio();
1581 (*iodone)(mbp);
1582 splx(s);
1583 UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1584 return (error);
1585 }
1586
1587 int
1588 genfs_compat_getpages(void *v)
1589 {
1590 struct vop_getpages_args /* {
1591 struct vnode *a_vp;
1592 voff_t a_offset;
1593 struct vm_page **a_m;
1594 int *a_count;
1595 int a_centeridx;
1596 vm_prot_t a_access_type;
1597 int a_advice;
1598 int a_flags;
1599 } */ *ap = v;
1600
1601 off_t origoffset;
1602 struct vnode *vp = ap->a_vp;
1603 struct uvm_object *uobj = &vp->v_uobj;
1604 struct vm_page *pg, **pgs;
1605 vaddr_t kva;
1606 int i, error, orignpages, npages;
1607 struct iovec iov;
1608 struct uio uio;
1609 kauth_cred_t cred = curlwp->l_cred;
1610 const bool memwrite = (ap->a_access_type & VM_PROT_WRITE) != 0;
1611
1612 error = 0;
1613 origoffset = ap->a_offset;
1614 orignpages = *ap->a_count;
1615 pgs = ap->a_m;
1616
1617 if (memwrite && (vp->v_iflag & VI_ONWORKLST) == 0) {
1618 vn_syncer_add_to_worklist(vp, filedelay);
1619 }
1620 if (ap->a_flags & PGO_LOCKED) {
1621 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1622 UFP_NOWAIT|UFP_NOALLOC| (memwrite ? UFP_NORDONLY : 0));
1623
1624 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
1625 }
1626 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
1627 mutex_exit(&uobj->vmobjlock);
1628 return (EINVAL);
1629 }
1630 if ((ap->a_flags & PGO_SYNCIO) == 0) {
1631 mutex_exit(&uobj->vmobjlock);
1632 return 0;
1633 }
1634 npages = orignpages;
1635 uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
1636 mutex_exit(&uobj->vmobjlock);
1637 kva = uvm_pagermapin(pgs, npages,
1638 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1639 for (i = 0; i < npages; i++) {
1640 pg = pgs[i];
1641 if ((pg->flags & PG_FAKE) == 0) {
1642 continue;
1643 }
1644 iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
1645 iov.iov_len = PAGE_SIZE;
1646 uio.uio_iov = &iov;
1647 uio.uio_iovcnt = 1;
1648 uio.uio_offset = origoffset + (i << PAGE_SHIFT);
1649 uio.uio_rw = UIO_READ;
1650 uio.uio_resid = PAGE_SIZE;
1651 UIO_SETUP_SYSSPACE(&uio);
1652 /* XXX vn_lock */
1653 error = VOP_READ(vp, &uio, 0, cred);
1654 if (error) {
1655 break;
1656 }
1657 if (uio.uio_resid) {
1658 memset(iov.iov_base, 0, uio.uio_resid);
1659 }
1660 }
1661 uvm_pagermapout(kva, npages);
1662 mutex_enter(&uobj->vmobjlock);
1663 mutex_enter(&uvm_pageqlock);
1664 for (i = 0; i < npages; i++) {
1665 pg = pgs[i];
1666 if (error && (pg->flags & PG_FAKE) != 0) {
1667 pg->flags |= PG_RELEASED;
1668 } else {
1669 pmap_clear_modify(pg);
1670 uvm_pageactivate(pg);
1671 }
1672 }
1673 if (error) {
1674 uvm_page_unbusy(pgs, npages);
1675 }
1676 mutex_exit(&uvm_pageqlock);
1677 mutex_exit(&uobj->vmobjlock);
1678 return (error);
1679 }
1680
1681 int
1682 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1683 int flags)
1684 {
1685 off_t offset;
1686 struct iovec iov;
1687 struct uio uio;
1688 kauth_cred_t cred = curlwp->l_cred;
1689 struct buf *bp;
1690 vaddr_t kva;
1691 int error;
1692
1693 offset = pgs[0]->offset;
1694 kva = uvm_pagermapin(pgs, npages,
1695 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1696
1697 iov.iov_base = (void *)kva;
1698 iov.iov_len = npages << PAGE_SHIFT;
1699 uio.uio_iov = &iov;
1700 uio.uio_iovcnt = 1;
1701 uio.uio_offset = offset;
1702 uio.uio_rw = UIO_WRITE;
1703 uio.uio_resid = npages << PAGE_SHIFT;
1704 UIO_SETUP_SYSSPACE(&uio);
1705 /* XXX vn_lock */
1706 error = VOP_WRITE(vp, &uio, 0, cred);
1707
1708 mutex_enter(&vp->v_interlock);
1709 vp->v_numoutput++;
1710 mutex_exit(&vp->v_interlock);
1711
1712 bp = getiobuf(vp, true);
1713 bp->b_cflags = BC_BUSY | BC_AGE;
1714 bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1715 bp->b_data = (char *)kva;
1716 bp->b_bcount = npages << PAGE_SHIFT;
1717 bp->b_bufsize = npages << PAGE_SHIFT;
1718 bp->b_resid = 0;
1719 bp->b_error = error;
1720 uvm_aio_aiodone(bp);
1721 return (error);
1722 }
1723
1724 /*
1725 * Process a uio using direct I/O. If we reach a part of the request
1726 * which cannot be processed in this fashion for some reason, just return.
1727 * The caller must handle some additional part of the request using
1728 * buffered I/O before trying direct I/O again.
1729 */
1730
1731 void
1732 genfs_directio(struct vnode *vp, struct uio *uio, int ioflag)
1733 {
1734 struct vmspace *vs;
1735 struct iovec *iov;
1736 vaddr_t va;
1737 size_t len;
1738 const int mask = DEV_BSIZE - 1;
1739 int error;
1740 bool need_wapbl = (vp->v_mount && vp->v_mount->mnt_wapbl &&
1741 (ioflag & IO_JOURNALLOCKED) == 0);
1742
1743 /*
1744 * We only support direct I/O to user space for now.
1745 */
1746
1747 if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
1748 return;
1749 }
1750
1751 /*
1752 * If the vnode is mapped, we would need to get the getpages lock
1753 * to stabilize the bmap, but then we would get into trouble whil e
1754 * locking the pages if the pages belong to this same vnode (or a
1755 * multi-vnode cascade to the same effect). Just fall back to
1756 * buffered I/O if the vnode is mapped to avoid this mess.
1757 */
1758
1759 if (vp->v_vflag & VV_MAPPED) {
1760 return;
1761 }
1762
1763 if (need_wapbl) {
1764 error = WAPBL_BEGIN(vp->v_mount);
1765 if (error)
1766 return;
1767 }
1768
1769 /*
1770 * Do as much of the uio as possible with direct I/O.
1771 */
1772
1773 vs = uio->uio_vmspace;
1774 while (uio->uio_resid) {
1775 iov = uio->uio_iov;
1776 if (iov->iov_len == 0) {
1777 uio->uio_iov++;
1778 uio->uio_iovcnt--;
1779 continue;
1780 }
1781 va = (vaddr_t)iov->iov_base;
1782 len = MIN(iov->iov_len, genfs_maxdio);
1783 len &= ~mask;
1784
1785 /*
1786 * If the next chunk is smaller than DEV_BSIZE or extends past
1787 * the current EOF, then fall back to buffered I/O.
1788 */
1789
1790 if (len == 0 || uio->uio_offset + len > vp->v_size) {
1791 break;
1792 }
1793
1794 /*
1795 * Check alignment. The file offset must be at least
1796 * sector-aligned. The exact constraint on memory alignment
1797 * is very hardware-dependent, but requiring sector-aligned
1798 * addresses there too is safe.
1799 */
1800
1801 if (uio->uio_offset & mask || va & mask) {
1802 break;
1803 }
1804 error = genfs_do_directio(vs, va, len, vp, uio->uio_offset,
1805 uio->uio_rw);
1806 if (error) {
1807 break;
1808 }
1809 iov->iov_base = (char *)iov->iov_base + len;
1810 iov->iov_len -= len;
1811 uio->uio_offset += len;
1812 uio->uio_resid -= len;
1813 }
1814
1815 if (need_wapbl)
1816 WAPBL_END(vp->v_mount);
1817 }
1818
1819 /*
1820 * Iodone routine for direct I/O. We don't do much here since the request is
1821 * always synchronous, so the caller will do most of the work after biowait().
1822 */
1823
1824 static void
1825 genfs_dio_iodone(struct buf *bp)
1826 {
1827
1828 KASSERT((bp->b_flags & B_ASYNC) == 0);
1829 if ((bp->b_flags & B_READ) == 0 && (bp->b_cflags & BC_AGE) != 0) {
1830 mutex_enter(bp->b_objlock);
1831 vwakeup(bp);
1832 mutex_exit(bp->b_objlock);
1833 }
1834 putiobuf(bp);
1835 }
1836
1837 /*
1838 * Process one chunk of a direct I/O request.
1839 */
1840
1841 static int
1842 genfs_do_directio(struct vmspace *vs, vaddr_t uva, size_t len, struct vnode *vp,
1843 off_t off, enum uio_rw rw)
1844 {
1845 struct vm_map *map;
1846 struct pmap *upm, *kpm;
1847 size_t klen = round_page(uva + len) - trunc_page(uva);
1848 off_t spoff, epoff;
1849 vaddr_t kva, puva;
1850 paddr_t pa;
1851 vm_prot_t prot;
1852 int error, rv, poff, koff;
1853 const int pgoflags = PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED |
1854 (rw == UIO_WRITE ? PGO_FREE : 0);
1855
1856 /*
1857 * For writes, verify that this range of the file already has fully
1858 * allocated backing store. If there are any holes, just punt and
1859 * make the caller take the buffered write path.
1860 */
1861
1862 if (rw == UIO_WRITE) {
1863 daddr_t lbn, elbn, blkno;
1864 int bsize, bshift, run;
1865
1866 bshift = vp->v_mount->mnt_fs_bshift;
1867 bsize = 1 << bshift;
1868 lbn = off >> bshift;
1869 elbn = (off + len + bsize - 1) >> bshift;
1870 while (lbn < elbn) {
1871 error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
1872 if (error) {
1873 return error;
1874 }
1875 if (blkno == (daddr_t)-1) {
1876 return ENOSPC;
1877 }
1878 lbn += 1 + run;
1879 }
1880 }
1881
1882 /*
1883 * Flush any cached pages for parts of the file that we're about to
1884 * access. If we're writing, invalidate pages as well.
1885 */
1886
1887 spoff = trunc_page(off);
1888 epoff = round_page(off + len);
1889 mutex_enter(&vp->v_interlock);
1890 error = VOP_PUTPAGES(vp, spoff, epoff, pgoflags);
1891 if (error) {
1892 return error;
1893 }
1894
1895 /*
1896 * Wire the user pages and remap them into kernel memory.
1897 */
1898
1899 prot = rw == UIO_READ ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
1900 error = uvm_vslock(vs, (void *)uva, len, prot);
1901 if (error) {
1902 return error;
1903 }
1904
1905 map = &vs->vm_map;
1906 upm = vm_map_pmap(map);
1907 kpm = vm_map_pmap(kernel_map);
1908 kva = uvm_km_alloc(kernel_map, klen, 0,
1909 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
1910 puva = trunc_page(uva);
1911 for (poff = 0; poff < klen; poff += PAGE_SIZE) {
1912 rv = pmap_extract(upm, puva + poff, &pa);
1913 KASSERT(rv);
1914 pmap_enter(kpm, kva + poff, pa, prot, prot | PMAP_WIRED);
1915 }
1916 pmap_update(kpm);
1917
1918 /*
1919 * Do the I/O.
1920 */
1921
1922 koff = uva - trunc_page(uva);
1923 error = genfs_do_io(vp, off, kva + koff, len, PGO_SYNCIO, rw,
1924 genfs_dio_iodone);
1925
1926 /*
1927 * Tear down the kernel mapping.
1928 */
1929
1930 pmap_remove(kpm, kva, kva + klen);
1931 pmap_update(kpm);
1932 uvm_km_free(kernel_map, kva, klen, UVM_KMF_VAONLY);
1933
1934 /*
1935 * Unwire the user pages.
1936 */
1937
1938 uvm_vsunlock(vs, (void *)uva, len);
1939 return error;
1940 }
1941
1942