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