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