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