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