genfs_io.c revision 1.13.4.2.2.2 1 /* $NetBSD: genfs_io.c,v 1.13.4.2.2.2 2012/04/21 15:54:03 riz 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.2.2.2 2012/04/21 15:54:03 riz 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 // replacement for the previous KASSERT to get debug output, by rmind
808 if (!(startoff < endoff || endoff == 0)) {
809 proc_t *p = curproc;
810 mutex_exit(slock);
811 printf("genfs_do_putpages: startoff 0x%lx, endoff 0x%lx vm_map %p\n", (uint64_t) startoff, (uint64_t) endoff, &p->p_vmspace->vm_map);
812 void uvm_map_printit(struct vm_map *, bool, void (*)(const char *, ...));
813 uvm_map_printit(&p->p_vmspace->vm_map, true, printf);
814 KASSERT(p == l->l_proc);
815 Debugger(); /* Bang ! */
816 return EIO;
817 }
818
819 UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
820 vp, uobj->uo_npages, startoff, endoff - startoff);
821
822 has_trans = false;
823 need_wapbl = (!pagedaemon && vp->v_mount && vp->v_mount->mnt_wapbl &&
824 (origflags & PGO_JOURNALLOCKED) == 0);
825
826 retry:
827 modified = false;
828 flags = origflags;
829 KASSERT((vp->v_iflag & VI_ONWORKLST) != 0 ||
830 (vp->v_iflag & VI_WRMAPDIRTY) == 0);
831 if (uobj->uo_npages == 0) {
832 if (vp->v_iflag & VI_ONWORKLST) {
833 vp->v_iflag &= ~VI_WRMAPDIRTY;
834 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
835 vn_syncer_remove_from_worklist(vp);
836 }
837 if (has_trans) {
838 if (need_wapbl)
839 WAPBL_END(vp->v_mount);
840 fstrans_done(vp->v_mount);
841 }
842 mutex_exit(slock);
843 return (0);
844 }
845
846 /*
847 * the vnode has pages, set up to process the request.
848 */
849
850 if (!has_trans && (flags & PGO_CLEANIT) != 0) {
851 mutex_exit(slock);
852 if (pagedaemon) {
853 error = fstrans_start_nowait(vp->v_mount, FSTRANS_LAZY);
854 if (error)
855 return error;
856 } else
857 fstrans_start(vp->v_mount, FSTRANS_LAZY);
858 if (need_wapbl) {
859 error = WAPBL_BEGIN(vp->v_mount);
860 if (error) {
861 fstrans_done(vp->v_mount);
862 return error;
863 }
864 }
865 has_trans = true;
866 mutex_enter(slock);
867 goto retry;
868 }
869
870 error = 0;
871 wasclean = (vp->v_numoutput == 0);
872 off = startoff;
873 if (endoff == 0 || flags & PGO_ALLPAGES) {
874 endoff = trunc_page(LLONG_MAX);
875 }
876 by_list = (uobj->uo_npages <=
877 ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
878
879 #if !defined(DEBUG)
880 /*
881 * if this vnode is known not to have dirty pages,
882 * don't bother to clean it out.
883 */
884
885 if ((vp->v_iflag & VI_ONWORKLST) == 0) {
886 if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == 0) {
887 goto skip_scan;
888 }
889 flags &= ~PGO_CLEANIT;
890 }
891 #endif /* !defined(DEBUG) */
892
893 /*
894 * start the loop. when scanning by list, hold the last page
895 * in the list before we start. pages allocated after we start
896 * will be added to the end of the list, so we can stop at the
897 * current last page.
898 */
899
900 cleanall = (flags & PGO_CLEANIT) != 0 && wasclean &&
901 startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
902 (vp->v_iflag & VI_ONWORKLST) != 0;
903 dirtygen = gp->g_dirtygen;
904 freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
905 if (by_list) {
906 curmp.uobject = uobj;
907 curmp.offset = (voff_t)-1;
908 curmp.flags = PG_BUSY;
909 endmp.uobject = uobj;
910 endmp.offset = (voff_t)-1;
911 endmp.flags = PG_BUSY;
912 pg = TAILQ_FIRST(&uobj->memq);
913 TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq.queue);
914 uvm_lwp_hold(l);
915 } else {
916 pg = uvm_pagelookup(uobj, off);
917 }
918 nextpg = NULL;
919 while (by_list || off < endoff) {
920
921 /*
922 * if the current page is not interesting, move on to the next.
923 */
924
925 KASSERT(pg == NULL || pg->uobject == uobj);
926 KASSERT(pg == NULL ||
927 (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
928 (pg->flags & PG_BUSY) != 0);
929 if (by_list) {
930 if (pg == &endmp) {
931 break;
932 }
933 if (pg->offset < startoff || pg->offset >= endoff ||
934 pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
935 if (pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
936 wasclean = false;
937 }
938 pg = TAILQ_NEXT(pg, listq.queue);
939 continue;
940 }
941 off = pg->offset;
942 } else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
943 if (pg != NULL) {
944 wasclean = false;
945 }
946 off += PAGE_SIZE;
947 if (off < endoff) {
948 pg = uvm_pagelookup(uobj, off);
949 }
950 continue;
951 }
952
953 /*
954 * if the current page needs to be cleaned and it's busy,
955 * wait for it to become unbusy.
956 */
957
958 yld = (l->l_cpu->ci_schedstate.spc_flags &
959 SPCF_SHOULDYIELD) && !pagedaemon;
960 if (pg->flags & PG_BUSY || yld) {
961 UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
962 if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
963 UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
964 error = EDEADLK;
965 if (busypg != NULL)
966 *busypg = pg;
967 break;
968 }
969 if (pagedaemon) {
970 /*
971 * someone has taken the page while we
972 * dropped the lock for fstrans_start.
973 */
974 break;
975 }
976 if (by_list) {
977 TAILQ_INSERT_BEFORE(pg, &curmp, listq.queue);
978 UVMHIST_LOG(ubchist, "curmp next %p",
979 TAILQ_NEXT(&curmp, listq.queue), 0,0,0);
980 }
981 if (yld) {
982 mutex_exit(slock);
983 preempt();
984 mutex_enter(slock);
985 } else {
986 pg->flags |= PG_WANTED;
987 UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
988 mutex_enter(slock);
989 }
990 if (by_list) {
991 UVMHIST_LOG(ubchist, "after next %p",
992 TAILQ_NEXT(&curmp, listq.queue), 0,0,0);
993 pg = TAILQ_NEXT(&curmp, listq.queue);
994 TAILQ_REMOVE(&uobj->memq, &curmp, listq.queue);
995 } else {
996 pg = uvm_pagelookup(uobj, off);
997 }
998 continue;
999 }
1000
1001 /*
1002 * if we're freeing, remove all mappings of the page now.
1003 * if we're cleaning, check if the page is needs to be cleaned.
1004 */
1005
1006 if (flags & PGO_FREE) {
1007 pmap_page_protect(pg, VM_PROT_NONE);
1008 } else if (flags & PGO_CLEANIT) {
1009
1010 /*
1011 * if we still have some hope to pull this vnode off
1012 * from the syncer queue, write-protect the page.
1013 */
1014
1015 if (cleanall && wasclean &&
1016 gp->g_dirtygen == dirtygen) {
1017
1018 /*
1019 * uobj pages get wired only by uvm_fault
1020 * where uobj is locked.
1021 */
1022
1023 if (pg->wire_count == 0) {
1024 pmap_page_protect(pg,
1025 VM_PROT_READ|VM_PROT_EXECUTE);
1026 } else {
1027 cleanall = false;
1028 }
1029 }
1030 }
1031
1032 if (flags & PGO_CLEANIT) {
1033 needs_clean = pmap_clear_modify(pg) ||
1034 (pg->flags & PG_CLEAN) == 0;
1035 pg->flags |= PG_CLEAN;
1036 } else {
1037 needs_clean = false;
1038 }
1039
1040 /*
1041 * if we're cleaning, build a cluster.
1042 * the cluster will consist of pages which are currently dirty,
1043 * but they will be returned to us marked clean.
1044 * if not cleaning, just operate on the one page.
1045 */
1046
1047 if (needs_clean) {
1048 KDASSERT((vp->v_iflag & VI_ONWORKLST));
1049 wasclean = false;
1050 memset(pgs, 0, sizeof(pgs));
1051 pg->flags |= PG_BUSY;
1052 UVM_PAGE_OWN(pg, "genfs_putpages");
1053
1054 /*
1055 * first look backward.
1056 */
1057
1058 npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
1059 nback = npages;
1060 uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
1061 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
1062 if (nback) {
1063 memmove(&pgs[0], &pgs[npages - nback],
1064 nback * sizeof(pgs[0]));
1065 if (npages - nback < nback)
1066 memset(&pgs[nback], 0,
1067 (npages - nback) * sizeof(pgs[0]));
1068 else
1069 memset(&pgs[npages - nback], 0,
1070 nback * sizeof(pgs[0]));
1071 }
1072
1073 /*
1074 * then plug in our page of interest.
1075 */
1076
1077 pgs[nback] = pg;
1078
1079 /*
1080 * then look forward to fill in the remaining space in
1081 * the array of pages.
1082 */
1083
1084 npages = maxpages - nback - 1;
1085 uvn_findpages(uobj, off + PAGE_SIZE, &npages,
1086 &pgs[nback + 1],
1087 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
1088 npages += nback + 1;
1089 } else {
1090 pgs[0] = pg;
1091 npages = 1;
1092 nback = 0;
1093 }
1094
1095 /*
1096 * apply FREE or DEACTIVATE options if requested.
1097 */
1098
1099 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1100 mutex_enter(&uvm_pageqlock);
1101 }
1102 for (i = 0; i < npages; i++) {
1103 tpg = pgs[i];
1104 KASSERT(tpg->uobject == uobj);
1105 if (by_list && tpg == TAILQ_NEXT(pg, listq.queue))
1106 pg = tpg;
1107 if (tpg->offset < startoff || tpg->offset >= endoff)
1108 continue;
1109 if (flags & PGO_DEACTIVATE && tpg->wire_count == 0) {
1110 uvm_pagedeactivate(tpg);
1111 } else if (flags & PGO_FREE) {
1112 pmap_page_protect(tpg, VM_PROT_NONE);
1113 if (tpg->flags & PG_BUSY) {
1114 tpg->flags |= freeflag;
1115 if (pagedaemon) {
1116 uvm_pageout_start(1);
1117 uvm_pagedequeue(tpg);
1118 }
1119 } else {
1120
1121 /*
1122 * ``page is not busy''
1123 * implies that npages is 1
1124 * and needs_clean is false.
1125 */
1126
1127 nextpg = TAILQ_NEXT(tpg, listq.queue);
1128 uvm_pagefree(tpg);
1129 if (pagedaemon)
1130 uvmexp.pdfreed++;
1131 }
1132 }
1133 }
1134 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1135 mutex_exit(&uvm_pageqlock);
1136 }
1137 if (needs_clean) {
1138 modified = true;
1139
1140 /*
1141 * start the i/o. if we're traversing by list,
1142 * keep our place in the list with a marker page.
1143 */
1144
1145 if (by_list) {
1146 TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
1147 listq.queue);
1148 }
1149 mutex_exit(slock);
1150 error = GOP_WRITE(vp, pgs, npages, flags);
1151 mutex_enter(slock);
1152 if (by_list) {
1153 pg = TAILQ_NEXT(&curmp, listq.queue);
1154 TAILQ_REMOVE(&uobj->memq, &curmp, listq.queue);
1155 }
1156 if (error) {
1157 break;
1158 }
1159 if (by_list) {
1160 continue;
1161 }
1162 }
1163
1164 /*
1165 * find the next page and continue if there was no error.
1166 */
1167
1168 if (by_list) {
1169 if (nextpg) {
1170 pg = nextpg;
1171 nextpg = NULL;
1172 } else {
1173 pg = TAILQ_NEXT(pg, listq.queue);
1174 }
1175 } else {
1176 off += (npages - nback) << PAGE_SHIFT;
1177 if (off < endoff) {
1178 pg = uvm_pagelookup(uobj, off);
1179 }
1180 }
1181 }
1182 if (by_list) {
1183 TAILQ_REMOVE(&uobj->memq, &endmp, listq.queue);
1184 uvm_lwp_rele(l);
1185 }
1186
1187 if (modified && (vp->v_iflag & VI_WRMAPDIRTY) != 0 &&
1188 (vp->v_type != VBLK ||
1189 (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
1190 GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
1191 }
1192
1193 /*
1194 * if we're cleaning and there was nothing to clean,
1195 * take us off the syncer list. if we started any i/o
1196 * and we're doing sync i/o, wait for all writes to finish.
1197 */
1198
1199 if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
1200 (vp->v_iflag & VI_ONWORKLST) != 0) {
1201 #if defined(DEBUG)
1202 TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
1203 if ((pg->flags & PG_CLEAN) == 0) {
1204 printf("%s: %p: !CLEAN\n", __func__, pg);
1205 }
1206 if (pmap_is_modified(pg)) {
1207 printf("%s: %p: modified\n", __func__, pg);
1208 }
1209 }
1210 #endif /* defined(DEBUG) */
1211 vp->v_iflag &= ~VI_WRMAPDIRTY;
1212 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
1213 vn_syncer_remove_from_worklist(vp);
1214 }
1215
1216 #if !defined(DEBUG)
1217 skip_scan:
1218 #endif /* !defined(DEBUG) */
1219
1220 /* Wait for output to complete. */
1221 if (!wasclean && !async && vp->v_numoutput != 0) {
1222 while (vp->v_numoutput != 0)
1223 cv_wait(&vp->v_cv, slock);
1224 }
1225 onworklst = (vp->v_iflag & VI_ONWORKLST) != 0;
1226 mutex_exit(slock);
1227
1228 if ((flags & PGO_RECLAIM) != 0 && onworklst) {
1229 /*
1230 * in the case of PGO_RECLAIM, ensure to make the vnode clean.
1231 * retrying is not a big deal because, in many cases,
1232 * uobj->uo_npages is already 0 here.
1233 */
1234 mutex_enter(slock);
1235 goto retry;
1236 }
1237
1238 if (has_trans) {
1239 if (need_wapbl)
1240 WAPBL_END(vp->v_mount);
1241 fstrans_done(vp->v_mount);
1242 }
1243
1244 return (error);
1245 }
1246
1247 int
1248 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1249 {
1250 off_t off;
1251 vaddr_t kva;
1252 size_t len;
1253 int error;
1254 UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1255
1256 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1257 vp, pgs, npages, flags);
1258
1259 off = pgs[0]->offset;
1260 kva = uvm_pagermapin(pgs, npages,
1261 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1262 len = npages << PAGE_SHIFT;
1263
1264 error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
1265 uvm_aio_biodone);
1266
1267 return error;
1268 }
1269
1270 int
1271 genfs_gop_write_rwmap(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1272 {
1273 off_t off;
1274 vaddr_t kva;
1275 size_t len;
1276 int error;
1277 UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1278
1279 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1280 vp, pgs, npages, flags);
1281
1282 off = pgs[0]->offset;
1283 kva = uvm_pagermapin(pgs, npages,
1284 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1285 len = npages << PAGE_SHIFT;
1286
1287 error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
1288 uvm_aio_biodone);
1289
1290 return error;
1291 }
1292
1293 /*
1294 * Backend routine for doing I/O to vnode pages. Pages are already locked
1295 * and mapped into kernel memory. Here we just look up the underlying
1296 * device block addresses and call the strategy routine.
1297 */
1298
1299 static int
1300 genfs_do_io(struct vnode *vp, off_t off, vaddr_t kva, size_t len, int flags,
1301 enum uio_rw rw, void (*iodone)(struct buf *))
1302 {
1303 int s, error, run;
1304 int fs_bshift, dev_bshift;
1305 off_t eof, offset, startoffset;
1306 size_t bytes, iobytes, skipbytes;
1307 daddr_t lbn, blkno;
1308 struct buf *mbp, *bp;
1309 struct vnode *devvp;
1310 bool async = (flags & PGO_SYNCIO) == 0;
1311 bool write = rw == UIO_WRITE;
1312 int brw = write ? B_WRITE : B_READ;
1313 UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1314
1315 UVMHIST_LOG(ubchist, "vp %p kva %p len 0x%x flags 0x%x",
1316 vp, kva, len, flags);
1317
1318 KASSERT(vp->v_size <= vp->v_writesize);
1319 GOP_SIZE(vp, vp->v_writesize, &eof, 0);
1320 if (vp->v_type != VBLK) {
1321 fs_bshift = vp->v_mount->mnt_fs_bshift;
1322 dev_bshift = vp->v_mount->mnt_dev_bshift;
1323 } else {
1324 fs_bshift = DEV_BSHIFT;
1325 dev_bshift = DEV_BSHIFT;
1326 }
1327 error = 0;
1328 startoffset = off;
1329 bytes = MIN(len, eof - startoffset);
1330 skipbytes = 0;
1331 KASSERT(bytes != 0);
1332
1333 if (write) {
1334 mutex_enter(&vp->v_interlock);
1335 vp->v_numoutput += 2;
1336 mutex_exit(&vp->v_interlock);
1337 }
1338 mbp = getiobuf(vp, true);
1339 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1340 vp, mbp, vp->v_numoutput, bytes);
1341 mbp->b_bufsize = len;
1342 mbp->b_data = (void *)kva;
1343 mbp->b_resid = mbp->b_bcount = bytes;
1344 mbp->b_cflags = BC_BUSY | BC_AGE;
1345 if (async) {
1346 mbp->b_flags = brw | B_ASYNC;
1347 mbp->b_iodone = iodone;
1348 } else {
1349 mbp->b_flags = brw;
1350 mbp->b_iodone = NULL;
1351 }
1352 if (curlwp == uvm.pagedaemon_lwp)
1353 BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
1354 else if (async)
1355 BIO_SETPRIO(mbp, BPRIO_TIMENONCRITICAL);
1356 else
1357 BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
1358
1359 bp = NULL;
1360 for (offset = startoffset;
1361 bytes > 0;
1362 offset += iobytes, bytes -= iobytes) {
1363 lbn = offset >> fs_bshift;
1364 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1365 if (error) {
1366 UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
1367 skipbytes += bytes;
1368 bytes = 0;
1369 break;
1370 }
1371
1372 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1373 bytes);
1374 if (blkno == (daddr_t)-1) {
1375 if (!write) {
1376 memset((char *)kva + (offset - startoffset), 0,
1377 iobytes);
1378 }
1379 skipbytes += iobytes;
1380 continue;
1381 }
1382
1383 /* if it's really one i/o, don't make a second buf */
1384 if (offset == startoffset && iobytes == bytes) {
1385 bp = mbp;
1386 } else {
1387 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1388 vp, bp, vp->v_numoutput, 0);
1389 bp = getiobuf(vp, true);
1390 nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
1391 }
1392 bp->b_lblkno = 0;
1393
1394 /* adjust physical blkno for partial blocks */
1395 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
1396 dev_bshift);
1397 UVMHIST_LOG(ubchist,
1398 "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
1399 vp, offset, bp->b_bcount, bp->b_blkno);
1400
1401 VOP_STRATEGY(devvp, bp);
1402 }
1403 if (skipbytes) {
1404 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1405 }
1406 nestiobuf_done(mbp, skipbytes, error);
1407 if (async) {
1408 UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
1409 return (0);
1410 }
1411 UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1412 error = biowait(mbp);
1413 s = splbio();
1414 (*iodone)(mbp);
1415 splx(s);
1416 UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1417 return (error);
1418 }
1419
1420 /*
1421 * VOP_PUTPAGES() for vnodes which never have pages.
1422 */
1423
1424 int
1425 genfs_null_putpages(void *v)
1426 {
1427 struct vop_putpages_args /* {
1428 struct vnode *a_vp;
1429 voff_t a_offlo;
1430 voff_t a_offhi;
1431 int a_flags;
1432 } */ *ap = v;
1433 struct vnode *vp = ap->a_vp;
1434
1435 KASSERT(vp->v_uobj.uo_npages == 0);
1436 mutex_exit(&vp->v_interlock);
1437 return (0);
1438 }
1439
1440 int
1441 genfs_compat_getpages(void *v)
1442 {
1443 struct vop_getpages_args /* {
1444 struct vnode *a_vp;
1445 voff_t a_offset;
1446 struct vm_page **a_m;
1447 int *a_count;
1448 int a_centeridx;
1449 vm_prot_t a_access_type;
1450 int a_advice;
1451 int a_flags;
1452 } */ *ap = v;
1453
1454 off_t origoffset;
1455 struct vnode *vp = ap->a_vp;
1456 struct uvm_object *uobj = &vp->v_uobj;
1457 struct vm_page *pg, **pgs;
1458 vaddr_t kva;
1459 int i, error, orignpages, npages;
1460 struct iovec iov;
1461 struct uio uio;
1462 kauth_cred_t cred = curlwp->l_cred;
1463 bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1464
1465 error = 0;
1466 origoffset = ap->a_offset;
1467 orignpages = *ap->a_count;
1468 pgs = ap->a_m;
1469
1470 if (write && (vp->v_iflag & VI_ONWORKLST) == 0) {
1471 vn_syncer_add_to_worklist(vp, filedelay);
1472 }
1473 if (ap->a_flags & PGO_LOCKED) {
1474 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1475 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
1476
1477 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
1478 }
1479 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
1480 mutex_exit(&uobj->vmobjlock);
1481 return (EINVAL);
1482 }
1483 if ((ap->a_flags & PGO_SYNCIO) == 0) {
1484 mutex_exit(&uobj->vmobjlock);
1485 return 0;
1486 }
1487 npages = orignpages;
1488 uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
1489 mutex_exit(&uobj->vmobjlock);
1490 kva = uvm_pagermapin(pgs, npages,
1491 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1492 for (i = 0; i < npages; i++) {
1493 pg = pgs[i];
1494 if ((pg->flags & PG_FAKE) == 0) {
1495 continue;
1496 }
1497 iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
1498 iov.iov_len = PAGE_SIZE;
1499 uio.uio_iov = &iov;
1500 uio.uio_iovcnt = 1;
1501 uio.uio_offset = origoffset + (i << PAGE_SHIFT);
1502 uio.uio_rw = UIO_READ;
1503 uio.uio_resid = PAGE_SIZE;
1504 UIO_SETUP_SYSSPACE(&uio);
1505 /* XXX vn_lock */
1506 error = VOP_READ(vp, &uio, 0, cred);
1507 if (error) {
1508 break;
1509 }
1510 if (uio.uio_resid) {
1511 memset(iov.iov_base, 0, uio.uio_resid);
1512 }
1513 }
1514 uvm_pagermapout(kva, npages);
1515 mutex_enter(&uobj->vmobjlock);
1516 mutex_enter(&uvm_pageqlock);
1517 for (i = 0; i < npages; i++) {
1518 pg = pgs[i];
1519 if (error && (pg->flags & PG_FAKE) != 0) {
1520 pg->flags |= PG_RELEASED;
1521 } else {
1522 pmap_clear_modify(pg);
1523 uvm_pageactivate(pg);
1524 }
1525 }
1526 if (error) {
1527 uvm_page_unbusy(pgs, npages);
1528 }
1529 mutex_exit(&uvm_pageqlock);
1530 mutex_exit(&uobj->vmobjlock);
1531 return (error);
1532 }
1533
1534 int
1535 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1536 int flags)
1537 {
1538 off_t offset;
1539 struct iovec iov;
1540 struct uio uio;
1541 kauth_cred_t cred = curlwp->l_cred;
1542 struct buf *bp;
1543 vaddr_t kva;
1544 int error;
1545
1546 offset = pgs[0]->offset;
1547 kva = uvm_pagermapin(pgs, npages,
1548 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1549
1550 iov.iov_base = (void *)kva;
1551 iov.iov_len = npages << PAGE_SHIFT;
1552 uio.uio_iov = &iov;
1553 uio.uio_iovcnt = 1;
1554 uio.uio_offset = offset;
1555 uio.uio_rw = UIO_WRITE;
1556 uio.uio_resid = npages << PAGE_SHIFT;
1557 UIO_SETUP_SYSSPACE(&uio);
1558 /* XXX vn_lock */
1559 error = VOP_WRITE(vp, &uio, 0, cred);
1560
1561 mutex_enter(&vp->v_interlock);
1562 vp->v_numoutput++;
1563 mutex_exit(&vp->v_interlock);
1564
1565 bp = getiobuf(vp, true);
1566 bp->b_cflags = BC_BUSY | BC_AGE;
1567 bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1568 bp->b_data = (char *)kva;
1569 bp->b_bcount = npages << PAGE_SHIFT;
1570 bp->b_bufsize = npages << PAGE_SHIFT;
1571 bp->b_resid = 0;
1572 bp->b_error = error;
1573 uvm_aio_aiodone(bp);
1574 return (error);
1575 }
1576
1577 /*
1578 * Process a uio using direct I/O. If we reach a part of the request
1579 * which cannot be processed in this fashion for some reason, just return.
1580 * The caller must handle some additional part of the request using
1581 * buffered I/O before trying direct I/O again.
1582 */
1583
1584 void
1585 genfs_directio(struct vnode *vp, struct uio *uio, int ioflag)
1586 {
1587 struct vmspace *vs;
1588 struct iovec *iov;
1589 vaddr_t va;
1590 size_t len;
1591 const int mask = DEV_BSIZE - 1;
1592 int error;
1593 bool need_wapbl = (vp->v_mount && vp->v_mount->mnt_wapbl &&
1594 (ioflag & IO_JOURNALLOCKED) == 0);
1595
1596 /*
1597 * We only support direct I/O to user space for now.
1598 */
1599
1600 if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
1601 return;
1602 }
1603
1604 /*
1605 * If the vnode is mapped, we would need to get the getpages lock
1606 * to stabilize the bmap, but then we would get into trouble whil e
1607 * locking the pages if the pages belong to this same vnode (or a
1608 * multi-vnode cascade to the same effect). Just fall back to
1609 * buffered I/O if the vnode is mapped to avoid this mess.
1610 */
1611
1612 if (vp->v_vflag & VV_MAPPED) {
1613 return;
1614 }
1615
1616 if (need_wapbl) {
1617 error = WAPBL_BEGIN(vp->v_mount);
1618 if (error)
1619 return;
1620 }
1621
1622 /*
1623 * Do as much of the uio as possible with direct I/O.
1624 */
1625
1626 vs = uio->uio_vmspace;
1627 while (uio->uio_resid) {
1628 iov = uio->uio_iov;
1629 if (iov->iov_len == 0) {
1630 uio->uio_iov++;
1631 uio->uio_iovcnt--;
1632 continue;
1633 }
1634 va = (vaddr_t)iov->iov_base;
1635 len = MIN(iov->iov_len, genfs_maxdio);
1636 len &= ~mask;
1637
1638 /*
1639 * If the next chunk is smaller than DEV_BSIZE or extends past
1640 * the current EOF, then fall back to buffered I/O.
1641 */
1642
1643 if (len == 0 || uio->uio_offset + len > vp->v_size) {
1644 break;
1645 }
1646
1647 /*
1648 * Check alignment. The file offset must be at least
1649 * sector-aligned. The exact constraint on memory alignment
1650 * is very hardware-dependent, but requiring sector-aligned
1651 * addresses there too is safe.
1652 */
1653
1654 if (uio->uio_offset & mask || va & mask) {
1655 break;
1656 }
1657 error = genfs_do_directio(vs, va, len, vp, uio->uio_offset,
1658 uio->uio_rw);
1659 if (error) {
1660 break;
1661 }
1662 iov->iov_base = (char *)iov->iov_base + len;
1663 iov->iov_len -= len;
1664 uio->uio_offset += len;
1665 uio->uio_resid -= len;
1666 }
1667
1668 if (need_wapbl)
1669 WAPBL_END(vp->v_mount);
1670 }
1671
1672 /*
1673 * Iodone routine for direct I/O. We don't do much here since the request is
1674 * always synchronous, so the caller will do most of the work after biowait().
1675 */
1676
1677 static void
1678 genfs_dio_iodone(struct buf *bp)
1679 {
1680
1681 KASSERT((bp->b_flags & B_ASYNC) == 0);
1682 if ((bp->b_flags & B_READ) == 0 && (bp->b_cflags & BC_AGE) != 0) {
1683 mutex_enter(bp->b_objlock);
1684 vwakeup(bp);
1685 mutex_exit(bp->b_objlock);
1686 }
1687 putiobuf(bp);
1688 }
1689
1690 /*
1691 * Process one chunk of a direct I/O request.
1692 */
1693
1694 static int
1695 genfs_do_directio(struct vmspace *vs, vaddr_t uva, size_t len, struct vnode *vp,
1696 off_t off, enum uio_rw rw)
1697 {
1698 struct vm_map *map;
1699 struct pmap *upm, *kpm;
1700 size_t klen = round_page(uva + len) - trunc_page(uva);
1701 off_t spoff, epoff;
1702 vaddr_t kva, puva;
1703 paddr_t pa;
1704 vm_prot_t prot;
1705 int error, rv, poff, koff;
1706 const int pgoflags = PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED |
1707 (rw == UIO_WRITE ? PGO_FREE : 0);
1708
1709 /*
1710 * For writes, verify that this range of the file already has fully
1711 * allocated backing store. If there are any holes, just punt and
1712 * make the caller take the buffered write path.
1713 */
1714
1715 if (rw == UIO_WRITE) {
1716 daddr_t lbn, elbn, blkno;
1717 int bsize, bshift, run;
1718
1719 bshift = vp->v_mount->mnt_fs_bshift;
1720 bsize = 1 << bshift;
1721 lbn = off >> bshift;
1722 elbn = (off + len + bsize - 1) >> bshift;
1723 while (lbn < elbn) {
1724 error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
1725 if (error) {
1726 return error;
1727 }
1728 if (blkno == (daddr_t)-1) {
1729 return ENOSPC;
1730 }
1731 lbn += 1 + run;
1732 }
1733 }
1734
1735 /*
1736 * Flush any cached pages for parts of the file that we're about to
1737 * access. If we're writing, invalidate pages as well.
1738 */
1739
1740 spoff = trunc_page(off);
1741 epoff = round_page(off + len);
1742 mutex_enter(&vp->v_interlock);
1743 error = VOP_PUTPAGES(vp, spoff, epoff, pgoflags);
1744 if (error) {
1745 return error;
1746 }
1747
1748 /*
1749 * Wire the user pages and remap them into kernel memory.
1750 */
1751
1752 prot = rw == UIO_READ ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
1753 error = uvm_vslock(vs, (void *)uva, len, prot);
1754 if (error) {
1755 return error;
1756 }
1757
1758 map = &vs->vm_map;
1759 upm = vm_map_pmap(map);
1760 kpm = vm_map_pmap(kernel_map);
1761 kva = uvm_km_alloc(kernel_map, klen, 0,
1762 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
1763 puva = trunc_page(uva);
1764 for (poff = 0; poff < klen; poff += PAGE_SIZE) {
1765 rv = pmap_extract(upm, puva + poff, &pa);
1766 KASSERT(rv);
1767 pmap_enter(kpm, kva + poff, pa, prot, prot | PMAP_WIRED);
1768 }
1769 pmap_update(kpm);
1770
1771 /*
1772 * Do the I/O.
1773 */
1774
1775 koff = uva - trunc_page(uva);
1776 error = genfs_do_io(vp, off, kva + koff, len, PGO_SYNCIO, rw,
1777 genfs_dio_iodone);
1778
1779 /*
1780 * Tear down the kernel mapping.
1781 */
1782
1783 pmap_remove(kpm, kva, kva + klen);
1784 pmap_update(kpm);
1785 uvm_km_free(kernel_map, kva, klen, UVM_KMF_VAONLY);
1786
1787 /*
1788 * Unwire the user pages.
1789 */
1790
1791 uvm_vsunlock(vs, (void *)uva, len);
1792 return error;
1793 }
1794
1795