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