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