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