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