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