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