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