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