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