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