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