FreeBSD/Linux Kernel Cross Reference
sys/vm/vnode_pager.c
1 /*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1990 University of Utah.
5 * Copyright (c) 1991 The Regents of the University of California.
6 * All rights reserved.
7 * Copyright (c) 1993, 1994 John S. Dyson
8 * Copyright (c) 1995, David Greenman
9 *
10 * This code is derived from software contributed to Berkeley by
11 * the Systems Programming Group of the University of Utah Computer
12 * Science Department.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91
43 */
44
45 /*
46 * Page to/from files (vnodes).
47 */
48
49 /*
50 * TODO:
51 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
52 * greatly re-simplify the vnode_pager.
53 */
54
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD: releng/12.0/sys/vm/vnode_pager.c 331723 2018-03-29 02:54:50Z jeff $");
57
58 #include "opt_vm.h"
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/sysctl.h>
63 #include <sys/proc.h>
64 #include <sys/vnode.h>
65 #include <sys/mount.h>
66 #include <sys/bio.h>
67 #include <sys/buf.h>
68 #include <sys/vmmeter.h>
69 #include <sys/limits.h>
70 #include <sys/conf.h>
71 #include <sys/rwlock.h>
72 #include <sys/sf_buf.h>
73 #include <sys/domainset.h>
74
75 #include <machine/atomic.h>
76
77 #include <vm/vm.h>
78 #include <vm/vm_param.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_pager.h>
82 #include <vm/vm_map.h>
83 #include <vm/vnode_pager.h>
84 #include <vm/vm_extern.h>
85
86 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
87 daddr_t *rtaddress, int *run);
88 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
89 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
90 static void vnode_pager_dealloc(vm_object_t);
91 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
92 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
93 int *, vop_getpages_iodone_t, void *);
94 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
95 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
96 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
97 vm_ooffset_t, struct ucred *cred);
98 static int vnode_pager_generic_getpages_done(struct buf *);
99 static void vnode_pager_generic_getpages_done_async(struct buf *);
100
101 struct pagerops vnodepagerops = {
102 .pgo_alloc = vnode_pager_alloc,
103 .pgo_dealloc = vnode_pager_dealloc,
104 .pgo_getpages = vnode_pager_getpages,
105 .pgo_getpages_async = vnode_pager_getpages_async,
106 .pgo_putpages = vnode_pager_putpages,
107 .pgo_haspage = vnode_pager_haspage,
108 };
109
110 int vnode_pbuf_freecnt;
111 int vnode_async_pbuf_freecnt;
112
113 static struct domainset *vnode_domainset = NULL;
114
115 SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset, CTLTYPE_STRING | CTLFLAG_RW,
116 &vnode_domainset, 0, sysctl_handle_domainset, "A",
117 "Default vnode NUMA policy");
118
119 /* Create the VM system backing object for this vnode */
120 int
121 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
122 {
123 vm_object_t object;
124 vm_ooffset_t size = isize;
125 struct vattr va;
126
127 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
128 return (0);
129
130 while ((object = vp->v_object) != NULL) {
131 VM_OBJECT_WLOCK(object);
132 if (!(object->flags & OBJ_DEAD)) {
133 VM_OBJECT_WUNLOCK(object);
134 return (0);
135 }
136 VOP_UNLOCK(vp, 0);
137 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
138 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
139 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
140 }
141
142 if (size == 0) {
143 if (vn_isdisk(vp, NULL)) {
144 size = IDX_TO_OFF(INT_MAX);
145 } else {
146 if (VOP_GETATTR(vp, &va, td->td_ucred))
147 return (0);
148 size = va.va_size;
149 }
150 }
151
152 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
153 /*
154 * Dereference the reference we just created. This assumes
155 * that the object is associated with the vp.
156 */
157 VM_OBJECT_WLOCK(object);
158 object->ref_count--;
159 VM_OBJECT_WUNLOCK(object);
160 vrele(vp);
161
162 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
163
164 return (0);
165 }
166
167 void
168 vnode_destroy_vobject(struct vnode *vp)
169 {
170 struct vm_object *obj;
171
172 obj = vp->v_object;
173 if (obj == NULL)
174 return;
175 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
176 VM_OBJECT_WLOCK(obj);
177 umtx_shm_object_terminated(obj);
178 if (obj->ref_count == 0) {
179 /*
180 * don't double-terminate the object
181 */
182 if ((obj->flags & OBJ_DEAD) == 0) {
183 vm_object_terminate(obj);
184 } else {
185 /*
186 * Waiters were already handled during object
187 * termination. The exclusive vnode lock hopefully
188 * prevented new waiters from referencing the dying
189 * object.
190 */
191 KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0,
192 ("OBJ_DISCONNECTWNT set obj %p flags %x",
193 obj, obj->flags));
194 vp->v_object = NULL;
195 VM_OBJECT_WUNLOCK(obj);
196 }
197 } else {
198 /*
199 * Woe to the process that tries to page now :-).
200 */
201 vm_pager_deallocate(obj);
202 VM_OBJECT_WUNLOCK(obj);
203 }
204 KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
205 }
206
207
208 /*
209 * Allocate (or lookup) pager for a vnode.
210 * Handle is a vnode pointer.
211 *
212 * MPSAFE
213 */
214 vm_object_t
215 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
216 vm_ooffset_t offset, struct ucred *cred)
217 {
218 vm_object_t object;
219 struct vnode *vp;
220
221 /*
222 * Pageout to vnode, no can do yet.
223 */
224 if (handle == NULL)
225 return (NULL);
226
227 vp = (struct vnode *) handle;
228
229 /*
230 * If the object is being terminated, wait for it to
231 * go away.
232 */
233 retry:
234 while ((object = vp->v_object) != NULL) {
235 VM_OBJECT_WLOCK(object);
236 if ((object->flags & OBJ_DEAD) == 0)
237 break;
238 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
239 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
240 }
241
242 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
243
244 if (object == NULL) {
245 /*
246 * Add an object of the appropriate size
247 */
248 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
249
250 object->un_pager.vnp.vnp_size = size;
251 object->un_pager.vnp.writemappings = 0;
252 object->domain.dr_policy = vnode_domainset;
253
254 object->handle = handle;
255 VI_LOCK(vp);
256 if (vp->v_object != NULL) {
257 /*
258 * Object has been created while we were sleeping
259 */
260 VI_UNLOCK(vp);
261 VM_OBJECT_WLOCK(object);
262 KASSERT(object->ref_count == 1,
263 ("leaked ref %p %d", object, object->ref_count));
264 object->type = OBJT_DEAD;
265 object->ref_count = 0;
266 VM_OBJECT_WUNLOCK(object);
267 vm_object_destroy(object);
268 goto retry;
269 }
270 vp->v_object = object;
271 VI_UNLOCK(vp);
272 } else {
273 object->ref_count++;
274 #if VM_NRESERVLEVEL > 0
275 vm_object_color(object, 0);
276 #endif
277 VM_OBJECT_WUNLOCK(object);
278 }
279 vrefact(vp);
280 return (object);
281 }
282
283 /*
284 * The object must be locked.
285 */
286 static void
287 vnode_pager_dealloc(vm_object_t object)
288 {
289 struct vnode *vp;
290 int refs;
291
292 vp = object->handle;
293 if (vp == NULL)
294 panic("vnode_pager_dealloc: pager already dealloced");
295
296 VM_OBJECT_ASSERT_WLOCKED(object);
297 vm_object_pip_wait(object, "vnpdea");
298 refs = object->ref_count;
299
300 object->handle = NULL;
301 object->type = OBJT_DEAD;
302 if (object->flags & OBJ_DISCONNECTWNT) {
303 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
304 wakeup(object);
305 }
306 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
307 if (object->un_pager.vnp.writemappings > 0) {
308 object->un_pager.vnp.writemappings = 0;
309 VOP_ADD_WRITECOUNT(vp, -1);
310 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
311 __func__, vp, vp->v_writecount);
312 }
313 vp->v_object = NULL;
314 VOP_UNSET_TEXT(vp);
315 VM_OBJECT_WUNLOCK(object);
316 while (refs-- > 0)
317 vunref(vp);
318 VM_OBJECT_WLOCK(object);
319 }
320
321 static boolean_t
322 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
323 int *after)
324 {
325 struct vnode *vp = object->handle;
326 daddr_t bn;
327 int err;
328 daddr_t reqblock;
329 int poff;
330 int bsize;
331 int pagesperblock, blocksperpage;
332
333 VM_OBJECT_ASSERT_WLOCKED(object);
334 /*
335 * If no vp or vp is doomed or marked transparent to VM, we do not
336 * have the page.
337 */
338 if (vp == NULL || vp->v_iflag & VI_DOOMED)
339 return FALSE;
340 /*
341 * If the offset is beyond end of file we do
342 * not have the page.
343 */
344 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
345 return FALSE;
346
347 bsize = vp->v_mount->mnt_stat.f_iosize;
348 pagesperblock = bsize / PAGE_SIZE;
349 blocksperpage = 0;
350 if (pagesperblock > 0) {
351 reqblock = pindex / pagesperblock;
352 } else {
353 blocksperpage = (PAGE_SIZE / bsize);
354 reqblock = pindex * blocksperpage;
355 }
356 VM_OBJECT_WUNLOCK(object);
357 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
358 VM_OBJECT_WLOCK(object);
359 if (err)
360 return TRUE;
361 if (bn == -1)
362 return FALSE;
363 if (pagesperblock > 0) {
364 poff = pindex - (reqblock * pagesperblock);
365 if (before) {
366 *before *= pagesperblock;
367 *before += poff;
368 }
369 if (after) {
370 /*
371 * The BMAP vop can report a partial block in the
372 * 'after', but must not report blocks after EOF.
373 * Assert the latter, and truncate 'after' in case
374 * of the former.
375 */
376 KASSERT((reqblock + *after) * pagesperblock <
377 roundup2(object->size, pagesperblock),
378 ("%s: reqblock %jd after %d size %ju", __func__,
379 (intmax_t )reqblock, *after,
380 (uintmax_t )object->size));
381 *after *= pagesperblock;
382 *after += pagesperblock - (poff + 1);
383 if (pindex + *after >= object->size)
384 *after = object->size - 1 - pindex;
385 }
386 } else {
387 if (before) {
388 *before /= blocksperpage;
389 }
390
391 if (after) {
392 *after /= blocksperpage;
393 }
394 }
395 return TRUE;
396 }
397
398 /*
399 * Lets the VM system know about a change in size for a file.
400 * We adjust our own internal size and flush any cached pages in
401 * the associated object that are affected by the size change.
402 *
403 * Note: this routine may be invoked as a result of a pager put
404 * operation (possibly at object termination time), so we must be careful.
405 */
406 void
407 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
408 {
409 vm_object_t object;
410 vm_page_t m;
411 vm_pindex_t nobjsize;
412
413 if ((object = vp->v_object) == NULL)
414 return;
415 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
416 VM_OBJECT_WLOCK(object);
417 if (object->type == OBJT_DEAD) {
418 VM_OBJECT_WUNLOCK(object);
419 return;
420 }
421 KASSERT(object->type == OBJT_VNODE,
422 ("not vnode-backed object %p", object));
423 if (nsize == object->un_pager.vnp.vnp_size) {
424 /*
425 * Hasn't changed size
426 */
427 VM_OBJECT_WUNLOCK(object);
428 return;
429 }
430 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
431 if (nsize < object->un_pager.vnp.vnp_size) {
432 /*
433 * File has shrunk. Toss any cached pages beyond the new EOF.
434 */
435 if (nobjsize < object->size)
436 vm_object_page_remove(object, nobjsize, object->size,
437 0);
438 /*
439 * this gets rid of garbage at the end of a page that is now
440 * only partially backed by the vnode.
441 *
442 * XXX for some reason (I don't know yet), if we take a
443 * completely invalid page and mark it partially valid
444 * it can screw up NFS reads, so we don't allow the case.
445 */
446 if ((nsize & PAGE_MASK) &&
447 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
448 m->valid != 0) {
449 int base = (int)nsize & PAGE_MASK;
450 int size = PAGE_SIZE - base;
451
452 /*
453 * Clear out partial-page garbage in case
454 * the page has been mapped.
455 */
456 pmap_zero_page_area(m, base, size);
457
458 /*
459 * Update the valid bits to reflect the blocks that
460 * have been zeroed. Some of these valid bits may
461 * have already been set.
462 */
463 vm_page_set_valid_range(m, base, size);
464
465 /*
466 * Round "base" to the next block boundary so that the
467 * dirty bit for a partially zeroed block is not
468 * cleared.
469 */
470 base = roundup2(base, DEV_BSIZE);
471
472 /*
473 * Clear out partial-page dirty bits.
474 *
475 * note that we do not clear out the valid
476 * bits. This would prevent bogus_page
477 * replacement from working properly.
478 */
479 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
480 }
481 }
482 object->un_pager.vnp.vnp_size = nsize;
483 object->size = nobjsize;
484 VM_OBJECT_WUNLOCK(object);
485 }
486
487 /*
488 * calculate the linear (byte) disk address of specified virtual
489 * file address
490 */
491 static int
492 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
493 int *run)
494 {
495 int bsize;
496 int err;
497 daddr_t vblock;
498 daddr_t voffset;
499
500 if (address < 0)
501 return -1;
502
503 if (vp->v_iflag & VI_DOOMED)
504 return -1;
505
506 bsize = vp->v_mount->mnt_stat.f_iosize;
507 vblock = address / bsize;
508 voffset = address % bsize;
509
510 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
511 if (err == 0) {
512 if (*rtaddress != -1)
513 *rtaddress += voffset / DEV_BSIZE;
514 if (run) {
515 *run += 1;
516 *run *= bsize/PAGE_SIZE;
517 *run -= voffset/PAGE_SIZE;
518 }
519 }
520
521 return (err);
522 }
523
524 /*
525 * small block filesystem vnode pager input
526 */
527 static int
528 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
529 {
530 struct vnode *vp;
531 struct bufobj *bo;
532 struct buf *bp;
533 struct sf_buf *sf;
534 daddr_t fileaddr;
535 vm_offset_t bsize;
536 vm_page_bits_t bits;
537 int error, i;
538
539 error = 0;
540 vp = object->handle;
541 if (vp->v_iflag & VI_DOOMED)
542 return VM_PAGER_BAD;
543
544 bsize = vp->v_mount->mnt_stat.f_iosize;
545
546 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
547
548 sf = sf_buf_alloc(m, 0);
549
550 for (i = 0; i < PAGE_SIZE / bsize; i++) {
551 vm_ooffset_t address;
552
553 bits = vm_page_bits(i * bsize, bsize);
554 if (m->valid & bits)
555 continue;
556
557 address = IDX_TO_OFF(m->pindex) + i * bsize;
558 if (address >= object->un_pager.vnp.vnp_size) {
559 fileaddr = -1;
560 } else {
561 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
562 if (error)
563 break;
564 }
565 if (fileaddr != -1) {
566 bp = getpbuf(&vnode_pbuf_freecnt);
567
568 /* build a minimal buffer header */
569 bp->b_iocmd = BIO_READ;
570 bp->b_iodone = bdone;
571 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
572 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
573 bp->b_rcred = crhold(curthread->td_ucred);
574 bp->b_wcred = crhold(curthread->td_ucred);
575 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
576 bp->b_blkno = fileaddr;
577 pbgetbo(bo, bp);
578 bp->b_vp = vp;
579 bp->b_bcount = bsize;
580 bp->b_bufsize = bsize;
581 bp->b_runningbufspace = bp->b_bufsize;
582 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
583
584 /* do the input */
585 bp->b_iooffset = dbtob(bp->b_blkno);
586 bstrategy(bp);
587
588 bwait(bp, PVM, "vnsrd");
589
590 if ((bp->b_ioflags & BIO_ERROR) != 0)
591 error = EIO;
592
593 /*
594 * free the buffer header back to the swap buffer pool
595 */
596 bp->b_vp = NULL;
597 pbrelbo(bp);
598 relpbuf(bp, &vnode_pbuf_freecnt);
599 if (error)
600 break;
601 } else
602 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
603 KASSERT((m->dirty & bits) == 0,
604 ("vnode_pager_input_smlfs: page %p is dirty", m));
605 VM_OBJECT_WLOCK(object);
606 m->valid |= bits;
607 VM_OBJECT_WUNLOCK(object);
608 }
609 sf_buf_free(sf);
610 if (error) {
611 return VM_PAGER_ERROR;
612 }
613 return VM_PAGER_OK;
614 }
615
616 /*
617 * old style vnode pager input routine
618 */
619 static int
620 vnode_pager_input_old(vm_object_t object, vm_page_t m)
621 {
622 struct uio auio;
623 struct iovec aiov;
624 int error;
625 int size;
626 struct sf_buf *sf;
627 struct vnode *vp;
628
629 VM_OBJECT_ASSERT_WLOCKED(object);
630 error = 0;
631
632 /*
633 * Return failure if beyond current EOF
634 */
635 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
636 return VM_PAGER_BAD;
637 } else {
638 size = PAGE_SIZE;
639 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
640 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
641 vp = object->handle;
642 VM_OBJECT_WUNLOCK(object);
643
644 /*
645 * Allocate a kernel virtual address and initialize so that
646 * we can use VOP_READ/WRITE routines.
647 */
648 sf = sf_buf_alloc(m, 0);
649
650 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
651 aiov.iov_len = size;
652 auio.uio_iov = &aiov;
653 auio.uio_iovcnt = 1;
654 auio.uio_offset = IDX_TO_OFF(m->pindex);
655 auio.uio_segflg = UIO_SYSSPACE;
656 auio.uio_rw = UIO_READ;
657 auio.uio_resid = size;
658 auio.uio_td = curthread;
659
660 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
661 if (!error) {
662 int count = size - auio.uio_resid;
663
664 if (count == 0)
665 error = EINVAL;
666 else if (count != PAGE_SIZE)
667 bzero((caddr_t)sf_buf_kva(sf) + count,
668 PAGE_SIZE - count);
669 }
670 sf_buf_free(sf);
671
672 VM_OBJECT_WLOCK(object);
673 }
674 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
675 if (!error)
676 m->valid = VM_PAGE_BITS_ALL;
677 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
678 }
679
680 /*
681 * generic vnode pager input routine
682 */
683
684 /*
685 * Local media VFS's that do not implement their own VOP_GETPAGES
686 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
687 * to implement the previous behaviour.
688 *
689 * All other FS's should use the bypass to get to the local media
690 * backing vp's VOP_GETPAGES.
691 */
692 static int
693 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
694 int *rahead)
695 {
696 struct vnode *vp;
697 int rtval;
698
699 vp = object->handle;
700 VM_OBJECT_WUNLOCK(object);
701 rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
702 KASSERT(rtval != EOPNOTSUPP,
703 ("vnode_pager: FS getpages not implemented\n"));
704 VM_OBJECT_WLOCK(object);
705 return rtval;
706 }
707
708 static int
709 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
710 int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
711 {
712 struct vnode *vp;
713 int rtval;
714
715 vp = object->handle;
716 VM_OBJECT_WUNLOCK(object);
717 rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
718 KASSERT(rtval != EOPNOTSUPP,
719 ("vnode_pager: FS getpages_async not implemented\n"));
720 VM_OBJECT_WLOCK(object);
721 return (rtval);
722 }
723
724 /*
725 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
726 * local filesystems, where partially valid pages can only occur at
727 * the end of file.
728 */
729 int
730 vnode_pager_local_getpages(struct vop_getpages_args *ap)
731 {
732
733 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
734 ap->a_rbehind, ap->a_rahead, NULL, NULL));
735 }
736
737 int
738 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
739 {
740
741 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
742 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
743 }
744
745 /*
746 * This is now called from local media FS's to operate against their
747 * own vnodes if they fail to implement VOP_GETPAGES.
748 */
749 int
750 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
751 int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
752 {
753 vm_object_t object;
754 struct bufobj *bo;
755 struct buf *bp;
756 off_t foff;
757 #ifdef INVARIANTS
758 off_t blkno0;
759 #endif
760 int bsize, pagesperblock, *freecnt;
761 int error, before, after, rbehind, rahead, poff, i;
762 int bytecount, secmask;
763
764 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
765 ("%s does not support devices", __func__));
766
767 if (vp->v_iflag & VI_DOOMED)
768 return (VM_PAGER_BAD);
769
770 object = vp->v_object;
771 foff = IDX_TO_OFF(m[0]->pindex);
772 bsize = vp->v_mount->mnt_stat.f_iosize;
773 pagesperblock = bsize / PAGE_SIZE;
774
775 KASSERT(foff < object->un_pager.vnp.vnp_size,
776 ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
777 KASSERT(count <= sizeof(bp->b_pages),
778 ("%s: requested %d pages", __func__, count));
779
780 /*
781 * The last page has valid blocks. Invalid part can only
782 * exist at the end of file, and the page is made fully valid
783 * by zeroing in vm_pager_get_pages().
784 */
785 if (m[count - 1]->valid != 0 && --count == 0) {
786 if (iodone != NULL)
787 iodone(arg, m, 1, 0);
788 return (VM_PAGER_OK);
789 }
790
791 /*
792 * Synchronous and asynchronous paging operations use different
793 * free pbuf counters. This is done to avoid asynchronous requests
794 * to consume all pbufs.
795 * Allocate the pbuf at the very beginning of the function, so that
796 * if we are low on certain kind of pbufs don't even proceed to BMAP,
797 * but sleep.
798 */
799 freecnt = iodone != NULL ?
800 &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
801 bp = getpbuf(freecnt);
802
803 /*
804 * Get the underlying device blocks for the file with VOP_BMAP().
805 * If the file system doesn't support VOP_BMAP, use old way of
806 * getting pages via VOP_READ.
807 */
808 error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
809 if (error == EOPNOTSUPP) {
810 relpbuf(bp, freecnt);
811 VM_OBJECT_WLOCK(object);
812 for (i = 0; i < count; i++) {
813 VM_CNT_INC(v_vnodein);
814 VM_CNT_INC(v_vnodepgsin);
815 error = vnode_pager_input_old(object, m[i]);
816 if (error)
817 break;
818 }
819 VM_OBJECT_WUNLOCK(object);
820 return (error);
821 } else if (error != 0) {
822 relpbuf(bp, freecnt);
823 return (VM_PAGER_ERROR);
824 }
825
826 /*
827 * If the file system supports BMAP, but blocksize is smaller
828 * than a page size, then use special small filesystem code.
829 */
830 if (pagesperblock == 0) {
831 relpbuf(bp, freecnt);
832 for (i = 0; i < count; i++) {
833 VM_CNT_INC(v_vnodein);
834 VM_CNT_INC(v_vnodepgsin);
835 error = vnode_pager_input_smlfs(object, m[i]);
836 if (error)
837 break;
838 }
839 return (error);
840 }
841
842 /*
843 * A sparse file can be encountered only for a single page request,
844 * which may not be preceded by call to vm_pager_haspage().
845 */
846 if (bp->b_blkno == -1) {
847 KASSERT(count == 1,
848 ("%s: array[%d] request to a sparse file %p", __func__,
849 count, vp));
850 relpbuf(bp, freecnt);
851 pmap_zero_page(m[0]);
852 KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
853 __func__, m[0]));
854 VM_OBJECT_WLOCK(object);
855 m[0]->valid = VM_PAGE_BITS_ALL;
856 VM_OBJECT_WUNLOCK(object);
857 return (VM_PAGER_OK);
858 }
859
860 #ifdef INVARIANTS
861 blkno0 = bp->b_blkno;
862 #endif
863 bp->b_blkno += (foff % bsize) / DEV_BSIZE;
864
865 /* Recalculate blocks available after/before to pages. */
866 poff = (foff % bsize) / PAGE_SIZE;
867 before *= pagesperblock;
868 before += poff;
869 after *= pagesperblock;
870 after += pagesperblock - (poff + 1);
871 if (m[0]->pindex + after >= object->size)
872 after = object->size - 1 - m[0]->pindex;
873 KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
874 __func__, count, after + 1));
875 after -= count - 1;
876
877 /* Trim requested rbehind/rahead to possible values. */
878 rbehind = a_rbehind ? *a_rbehind : 0;
879 rahead = a_rahead ? *a_rahead : 0;
880 rbehind = min(rbehind, before);
881 rbehind = min(rbehind, m[0]->pindex);
882 rahead = min(rahead, after);
883 rahead = min(rahead, object->size - m[count - 1]->pindex);
884 /*
885 * Check that total amount of pages fit into buf. Trim rbehind and
886 * rahead evenly if not.
887 */
888 if (rbehind + rahead + count > nitems(bp->b_pages)) {
889 int trim, sum;
890
891 trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
892 sum = rbehind + rahead;
893 if (rbehind == before) {
894 /* Roundup rbehind trim to block size. */
895 rbehind -= roundup(trim * rbehind / sum, pagesperblock);
896 if (rbehind < 0)
897 rbehind = 0;
898 } else
899 rbehind -= trim * rbehind / sum;
900 rahead -= trim * rahead / sum;
901 }
902 KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
903 ("%s: behind %d ahead %d count %d", __func__,
904 rbehind, rahead, count));
905
906 /*
907 * Fill in the bp->b_pages[] array with requested and optional
908 * read behind or read ahead pages. Read behind pages are looked
909 * up in a backward direction, down to a first cached page. Same
910 * for read ahead pages, but there is no need to shift the array
911 * in case of encountering a cached page.
912 */
913 i = bp->b_npages = 0;
914 if (rbehind) {
915 vm_pindex_t startpindex, tpindex;
916 vm_page_t p;
917
918 VM_OBJECT_WLOCK(object);
919 startpindex = m[0]->pindex - rbehind;
920 if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
921 p->pindex >= startpindex)
922 startpindex = p->pindex + 1;
923
924 /* tpindex is unsigned; beware of numeric underflow. */
925 for (tpindex = m[0]->pindex - 1;
926 tpindex >= startpindex && tpindex < m[0]->pindex;
927 tpindex--, i++) {
928 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
929 if (p == NULL) {
930 /* Shift the array. */
931 for (int j = 0; j < i; j++)
932 bp->b_pages[j] = bp->b_pages[j +
933 tpindex + 1 - startpindex];
934 break;
935 }
936 bp->b_pages[tpindex - startpindex] = p;
937 }
938
939 bp->b_pgbefore = i;
940 bp->b_npages += i;
941 bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
942 } else
943 bp->b_pgbefore = 0;
944
945 /* Requested pages. */
946 for (int j = 0; j < count; j++, i++)
947 bp->b_pages[i] = m[j];
948 bp->b_npages += count;
949
950 if (rahead) {
951 vm_pindex_t endpindex, tpindex;
952 vm_page_t p;
953
954 if (!VM_OBJECT_WOWNED(object))
955 VM_OBJECT_WLOCK(object);
956 endpindex = m[count - 1]->pindex + rahead + 1;
957 if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
958 p->pindex < endpindex)
959 endpindex = p->pindex;
960 if (endpindex > object->size)
961 endpindex = object->size;
962
963 for (tpindex = m[count - 1]->pindex + 1;
964 tpindex < endpindex; i++, tpindex++) {
965 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
966 if (p == NULL)
967 break;
968 bp->b_pages[i] = p;
969 }
970
971 bp->b_pgafter = i - bp->b_npages;
972 bp->b_npages = i;
973 } else
974 bp->b_pgafter = 0;
975
976 if (VM_OBJECT_WOWNED(object))
977 VM_OBJECT_WUNLOCK(object);
978
979 /* Report back actual behind/ahead read. */
980 if (a_rbehind)
981 *a_rbehind = bp->b_pgbefore;
982 if (a_rahead)
983 *a_rahead = bp->b_pgafter;
984
985 #ifdef INVARIANTS
986 KASSERT(bp->b_npages <= nitems(bp->b_pages),
987 ("%s: buf %p overflowed", __func__, bp));
988 for (int j = 1, prev = 0; j < bp->b_npages; j++) {
989 if (bp->b_pages[j] == bogus_page)
990 continue;
991 KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
992 j - prev, ("%s: pages array not consecutive, bp %p",
993 __func__, bp));
994 prev = j;
995 }
996 #endif
997
998 /*
999 * Recalculate first offset and bytecount with regards to read behind.
1000 * Truncate bytecount to vnode real size and round up physical size
1001 * for real devices.
1002 */
1003 foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1004 bytecount = bp->b_npages << PAGE_SHIFT;
1005 if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
1006 bytecount = object->un_pager.vnp.vnp_size - foff;
1007 secmask = bo->bo_bsize - 1;
1008 KASSERT(secmask < PAGE_SIZE && secmask > 0,
1009 ("%s: sector size %d too large", __func__, secmask + 1));
1010 bytecount = (bytecount + secmask) & ~secmask;
1011
1012 /*
1013 * And map the pages to be read into the kva, if the filesystem
1014 * requires mapped buffers.
1015 */
1016 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
1017 unmapped_buf_allowed) {
1018 bp->b_data = unmapped_buf;
1019 bp->b_offset = 0;
1020 } else {
1021 bp->b_data = bp->b_kvabase;
1022 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1023 }
1024
1025 /* Build a minimal buffer header. */
1026 bp->b_iocmd = BIO_READ;
1027 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1028 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1029 bp->b_rcred = crhold(curthread->td_ucred);
1030 bp->b_wcred = crhold(curthread->td_ucred);
1031 pbgetbo(bo, bp);
1032 bp->b_vp = vp;
1033 bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
1034 bp->b_iooffset = dbtob(bp->b_blkno);
1035 KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1036 (blkno0 - bp->b_blkno) * DEV_BSIZE +
1037 IDX_TO_OFF(m[0]->pindex) % bsize,
1038 ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1039 "blkno0 %ju b_blkno %ju", bsize,
1040 (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1041 (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
1042
1043 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
1044 VM_CNT_INC(v_vnodein);
1045 VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1046
1047 if (iodone != NULL) { /* async */
1048 bp->b_pgiodone = iodone;
1049 bp->b_caller1 = arg;
1050 bp->b_iodone = vnode_pager_generic_getpages_done_async;
1051 bp->b_flags |= B_ASYNC;
1052 BUF_KERNPROC(bp);
1053 bstrategy(bp);
1054 return (VM_PAGER_OK);
1055 } else {
1056 bp->b_iodone = bdone;
1057 bstrategy(bp);
1058 bwait(bp, PVM, "vnread");
1059 error = vnode_pager_generic_getpages_done(bp);
1060 for (i = 0; i < bp->b_npages; i++)
1061 bp->b_pages[i] = NULL;
1062 bp->b_vp = NULL;
1063 pbrelbo(bp);
1064 relpbuf(bp, &vnode_pbuf_freecnt);
1065 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1066 }
1067 }
1068
1069 static void
1070 vnode_pager_generic_getpages_done_async(struct buf *bp)
1071 {
1072 int error;
1073
1074 error = vnode_pager_generic_getpages_done(bp);
1075 /* Run the iodone upon the requested range. */
1076 bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1077 bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
1078 for (int i = 0; i < bp->b_npages; i++)
1079 bp->b_pages[i] = NULL;
1080 bp->b_vp = NULL;
1081 pbrelbo(bp);
1082 relpbuf(bp, &vnode_async_pbuf_freecnt);
1083 }
1084
1085 static int
1086 vnode_pager_generic_getpages_done(struct buf *bp)
1087 {
1088 vm_object_t object;
1089 off_t tfoff, nextoff;
1090 int i, error;
1091
1092 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1093 object = bp->b_vp->v_object;
1094
1095 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1096 if (!buf_mapped(bp)) {
1097 bp->b_data = bp->b_kvabase;
1098 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
1099 bp->b_npages);
1100 }
1101 bzero(bp->b_data + bp->b_bcount,
1102 PAGE_SIZE * bp->b_npages - bp->b_bcount);
1103 }
1104 if (buf_mapped(bp)) {
1105 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1106 bp->b_data = unmapped_buf;
1107 }
1108
1109 VM_OBJECT_WLOCK(object);
1110 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1111 i < bp->b_npages; i++, tfoff = nextoff) {
1112 vm_page_t mt;
1113
1114 nextoff = tfoff + PAGE_SIZE;
1115 mt = bp->b_pages[i];
1116
1117 if (nextoff <= object->un_pager.vnp.vnp_size) {
1118 /*
1119 * Read filled up entire page.
1120 */
1121 mt->valid = VM_PAGE_BITS_ALL;
1122 KASSERT(mt->dirty == 0,
1123 ("%s: page %p is dirty", __func__, mt));
1124 KASSERT(!pmap_page_is_mapped(mt),
1125 ("%s: page %p is mapped", __func__, mt));
1126 } else {
1127 /*
1128 * Read did not fill up entire page.
1129 *
1130 * Currently we do not set the entire page valid,
1131 * we just try to clear the piece that we couldn't
1132 * read.
1133 */
1134 vm_page_set_valid_range(mt, 0,
1135 object->un_pager.vnp.vnp_size - tfoff);
1136 KASSERT((mt->dirty & vm_page_bits(0,
1137 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1138 ("%s: page %p is dirty", __func__, mt));
1139 }
1140
1141 if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1142 vm_page_readahead_finish(mt);
1143 }
1144 VM_OBJECT_WUNLOCK(object);
1145 if (error != 0)
1146 printf("%s: I/O read error %d\n", __func__, error);
1147
1148 return (error);
1149 }
1150
1151 /*
1152 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1153 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1154 * vnode_pager_generic_putpages() to implement the previous behaviour.
1155 *
1156 * All other FS's should use the bypass to get to the local media
1157 * backing vp's VOP_PUTPAGES.
1158 */
1159 static void
1160 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1161 int flags, int *rtvals)
1162 {
1163 int rtval;
1164 struct vnode *vp;
1165 int bytes = count * PAGE_SIZE;
1166
1167 /*
1168 * Force synchronous operation if we are extremely low on memory
1169 * to prevent a low-memory deadlock. VOP operations often need to
1170 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1171 * operation ). The swapper handles the case by limiting the amount
1172 * of asynchronous I/O, but that sort of solution doesn't scale well
1173 * for the vnode pager without a lot of work.
1174 *
1175 * Also, the backing vnode's iodone routine may not wake the pageout
1176 * daemon up. This should be probably be addressed XXX.
1177 */
1178
1179 if (vm_page_count_min())
1180 flags |= VM_PAGER_PUT_SYNC;
1181
1182 /*
1183 * Call device-specific putpages function
1184 */
1185 vp = object->handle;
1186 VM_OBJECT_WUNLOCK(object);
1187 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1188 KASSERT(rtval != EOPNOTSUPP,
1189 ("vnode_pager: stale FS putpages\n"));
1190 VM_OBJECT_WLOCK(object);
1191 }
1192
1193 static int
1194 vn_off2bidx(vm_ooffset_t offset)
1195 {
1196
1197 return ((offset & PAGE_MASK) / DEV_BSIZE);
1198 }
1199
1200 static bool
1201 vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
1202 {
1203
1204 KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
1205 offset < IDX_TO_OFF(m->pindex + 1),
1206 ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
1207 (uintmax_t)offset));
1208 return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
1209 }
1210
1211 /*
1212 * This is now called from local media FS's to operate against their
1213 * own vnodes if they fail to implement VOP_PUTPAGES.
1214 *
1215 * This is typically called indirectly via the pageout daemon and
1216 * clustering has already typically occurred, so in general we ask the
1217 * underlying filesystem to write the data out asynchronously rather
1218 * then delayed.
1219 */
1220 int
1221 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1222 int flags, int *rtvals)
1223 {
1224 vm_object_t object;
1225 vm_page_t m;
1226 vm_ooffset_t maxblksz, next_offset, poffset, prev_offset;
1227 struct uio auio;
1228 struct iovec aiov;
1229 off_t prev_resid, wrsz;
1230 int count, error, i, maxsize, ncount, pgoff, ppscheck;
1231 bool in_hole;
1232 static struct timeval lastfail;
1233 static int curfail;
1234
1235 object = vp->v_object;
1236 count = bytecount / PAGE_SIZE;
1237
1238 for (i = 0; i < count; i++)
1239 rtvals[i] = VM_PAGER_ERROR;
1240
1241 if ((int64_t)ma[0]->pindex < 0) {
1242 printf("vnode_pager_generic_putpages: "
1243 "attempt to write meta-data 0x%jx(%lx)\n",
1244 (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1245 rtvals[0] = VM_PAGER_BAD;
1246 return (VM_PAGER_BAD);
1247 }
1248
1249 maxsize = count * PAGE_SIZE;
1250 ncount = count;
1251
1252 poffset = IDX_TO_OFF(ma[0]->pindex);
1253
1254 /*
1255 * If the page-aligned write is larger then the actual file we
1256 * have to invalidate pages occurring beyond the file EOF. However,
1257 * there is an edge case where a file may not be page-aligned where
1258 * the last page is partially invalid. In this case the filesystem
1259 * may not properly clear the dirty bits for the entire page (which
1260 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1261 * With the page locked we are free to fix-up the dirty bits here.
1262 *
1263 * We do not under any circumstances truncate the valid bits, as
1264 * this will screw up bogus page replacement.
1265 */
1266 VM_OBJECT_RLOCK(object);
1267 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1268 if (!VM_OBJECT_TRYUPGRADE(object)) {
1269 VM_OBJECT_RUNLOCK(object);
1270 VM_OBJECT_WLOCK(object);
1271 if (maxsize + poffset <= object->un_pager.vnp.vnp_size)
1272 goto downgrade;
1273 }
1274 if (object->un_pager.vnp.vnp_size > poffset) {
1275 maxsize = object->un_pager.vnp.vnp_size - poffset;
1276 ncount = btoc(maxsize);
1277 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1278 pgoff = roundup2(pgoff, DEV_BSIZE);
1279
1280 /*
1281 * If the object is locked and the following
1282 * conditions hold, then the page's dirty
1283 * field cannot be concurrently changed by a
1284 * pmap operation.
1285 */
1286 m = ma[ncount - 1];
1287 vm_page_assert_sbusied(m);
1288 KASSERT(!pmap_page_is_write_mapped(m),
1289 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1290 MPASS(m->dirty != 0);
1291 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1292 pgoff);
1293 }
1294 } else {
1295 maxsize = 0;
1296 ncount = 0;
1297 }
1298 for (i = ncount; i < count; i++)
1299 rtvals[i] = VM_PAGER_BAD;
1300 downgrade:
1301 VM_OBJECT_LOCK_DOWNGRADE(object);
1302 }
1303
1304 auio.uio_iov = &aiov;
1305 auio.uio_segflg = UIO_NOCOPY;
1306 auio.uio_rw = UIO_WRITE;
1307 auio.uio_td = NULL;
1308 maxblksz = roundup2(poffset + maxsize, DEV_BSIZE);
1309
1310 for (prev_offset = poffset; prev_offset < maxblksz;) {
1311 /* Skip clean blocks. */
1312 for (in_hole = true; in_hole && prev_offset < maxblksz;) {
1313 m = ma[OFF_TO_IDX(prev_offset - poffset)];
1314 for (i = vn_off2bidx(prev_offset);
1315 i < sizeof(vm_page_bits_t) * NBBY &&
1316 prev_offset < maxblksz; i++) {
1317 if (vn_dirty_blk(m, prev_offset)) {
1318 in_hole = false;
1319 break;
1320 }
1321 prev_offset += DEV_BSIZE;
1322 }
1323 }
1324 if (in_hole)
1325 goto write_done;
1326
1327 /* Find longest run of dirty blocks. */
1328 for (next_offset = prev_offset; next_offset < maxblksz;) {
1329 m = ma[OFF_TO_IDX(next_offset - poffset)];
1330 for (i = vn_off2bidx(next_offset);
1331 i < sizeof(vm_page_bits_t) * NBBY &&
1332 next_offset < maxblksz; i++) {
1333 if (!vn_dirty_blk(m, next_offset))
1334 goto start_write;
1335 next_offset += DEV_BSIZE;
1336 }
1337 }
1338 start_write:
1339 if (next_offset > poffset + maxsize)
1340 next_offset = poffset + maxsize;
1341
1342 /*
1343 * Getting here requires finding a dirty block in the
1344 * 'skip clean blocks' loop.
1345 */
1346 MPASS(prev_offset < next_offset);
1347
1348 VM_OBJECT_RUNLOCK(object);
1349 aiov.iov_base = NULL;
1350 auio.uio_iovcnt = 1;
1351 auio.uio_offset = prev_offset;
1352 prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
1353 prev_offset;
1354 error = VOP_WRITE(vp, &auio,
1355 vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
1356
1357 wrsz = prev_resid - auio.uio_resid;
1358 if (wrsz == 0) {
1359 if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
1360 vn_printf(vp, "vnode_pager_putpages: "
1361 "zero-length write at %ju resid %zd\n",
1362 auio.uio_offset, auio.uio_resid);
1363 }
1364 VM_OBJECT_RLOCK(object);
1365 break;
1366 }
1367
1368 /* Adjust the starting offset for next iteration. */
1369 prev_offset += wrsz;
1370 MPASS(auio.uio_offset == prev_offset);
1371
1372 ppscheck = 0;
1373 if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
1374 &curfail, 1)) != 0)
1375 vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
1376 error);
1377 if (auio.uio_resid != 0 && (ppscheck != 0 ||
1378 ppsratecheck(&lastfail, &curfail, 1) != 0))
1379 vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
1380 "at %ju\n", auio.uio_resid,
1381 (uintmax_t)ma[0]->pindex);
1382 VM_OBJECT_RLOCK(object);
1383 if (error != 0 || auio.uio_resid != 0)
1384 break;
1385 }
1386 write_done:
1387 /* Mark completely processed pages. */
1388 for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
1389 rtvals[i] = VM_PAGER_OK;
1390 /* Mark partial EOF page. */
1391 if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
1392 rtvals[i++] = VM_PAGER_OK;
1393 /* Unwritten pages in range, free bonus if the page is clean. */
1394 for (; i < ncount; i++)
1395 rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
1396 VM_OBJECT_RUNLOCK(object);
1397 VM_CNT_ADD(v_vnodepgsout, i);
1398 VM_CNT_INC(v_vnodeout);
1399 return (rtvals[0]);
1400 }
1401
1402 int
1403 vnode_pager_putpages_ioflags(int pager_flags)
1404 {
1405 int ioflags;
1406
1407 /*
1408 * Pageouts are already clustered, use IO_ASYNC to force a
1409 * bawrite() rather then a bdwrite() to prevent paging I/O
1410 * from saturating the buffer cache. Dummy-up the sequential
1411 * heuristic to cause large ranges to cluster. If neither
1412 * IO_SYNC or IO_ASYNC is set, the system decides how to
1413 * cluster.
1414 */
1415 ioflags = IO_VMIO;
1416 if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0)
1417 ioflags |= IO_SYNC;
1418 else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0)
1419 ioflags |= IO_ASYNC;
1420 ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0;
1421 ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0;
1422 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1423 return (ioflags);
1424 }
1425
1426 /*
1427 * vnode_pager_undirty_pages().
1428 *
1429 * A helper to mark pages as clean after pageout that was possibly
1430 * done with a short write. The lpos argument specifies the page run
1431 * length in bytes, and the written argument specifies how many bytes
1432 * were actually written. eof is the offset past the last valid byte
1433 * in the vnode using the absolute file position of the first byte in
1434 * the run as the base from which it is computed.
1435 */
1436 void
1437 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof,
1438 int lpos)
1439 {
1440 vm_object_t obj;
1441 int i, pos, pos_devb;
1442
1443 if (written == 0 && eof >= lpos)
1444 return;
1445 obj = ma[0]->object;
1446 VM_OBJECT_WLOCK(obj);
1447 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1448 if (pos < trunc_page(written)) {
1449 rtvals[i] = VM_PAGER_OK;
1450 vm_page_undirty(ma[i]);
1451 } else {
1452 /* Partially written page. */
1453 rtvals[i] = VM_PAGER_AGAIN;
1454 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1455 }
1456 }
1457 if (eof >= lpos) /* avoid truncation */
1458 goto done;
1459 for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1460 if (pos != trunc_page(pos)) {
1461 /*
1462 * The page contains the last valid byte in
1463 * the vnode, mark the rest of the page as
1464 * clean, potentially making the whole page
1465 * clean.
1466 */
1467 pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1468 vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1469 pos_devb);
1470
1471 /*
1472 * If the page was cleaned, report the pageout
1473 * on it as successful. msync() no longer
1474 * needs to write out the page, endlessly
1475 * creating write requests and dirty buffers.
1476 */
1477 if (ma[i]->dirty == 0)
1478 rtvals[i] = VM_PAGER_OK;
1479
1480 pos = round_page(pos);
1481 } else {
1482 /* vm_pageout_flush() clears dirty */
1483 rtvals[i] = VM_PAGER_BAD;
1484 pos += PAGE_SIZE;
1485 }
1486 }
1487 done:
1488 VM_OBJECT_WUNLOCK(obj);
1489 }
1490
1491 void
1492 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1493 vm_offset_t end)
1494 {
1495 struct vnode *vp;
1496 vm_ooffset_t old_wm;
1497
1498 VM_OBJECT_WLOCK(object);
1499 if (object->type != OBJT_VNODE) {
1500 VM_OBJECT_WUNLOCK(object);
1501 return;
1502 }
1503 old_wm = object->un_pager.vnp.writemappings;
1504 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1505 vp = object->handle;
1506 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1507 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1508 VOP_ADD_WRITECOUNT(vp, 1);
1509 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1510 __func__, vp, vp->v_writecount);
1511 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1512 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1513 VOP_ADD_WRITECOUNT(vp, -1);
1514 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1515 __func__, vp, vp->v_writecount);
1516 }
1517 VM_OBJECT_WUNLOCK(object);
1518 }
1519
1520 void
1521 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1522 vm_offset_t end)
1523 {
1524 struct vnode *vp;
1525 struct mount *mp;
1526 vm_offset_t inc;
1527
1528 VM_OBJECT_WLOCK(object);
1529
1530 /*
1531 * First, recheck the object type to account for the race when
1532 * the vnode is reclaimed.
1533 */
1534 if (object->type != OBJT_VNODE) {
1535 VM_OBJECT_WUNLOCK(object);
1536 return;
1537 }
1538
1539 /*
1540 * Optimize for the case when writemappings is not going to
1541 * zero.
1542 */
1543 inc = end - start;
1544 if (object->un_pager.vnp.writemappings != inc) {
1545 object->un_pager.vnp.writemappings -= inc;
1546 VM_OBJECT_WUNLOCK(object);
1547 return;
1548 }
1549
1550 vp = object->handle;
1551 vhold(vp);
1552 VM_OBJECT_WUNLOCK(object);
1553 mp = NULL;
1554 vn_start_write(vp, &mp, V_WAIT);
1555 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1556
1557 /*
1558 * Decrement the object's writemappings, by swapping the start
1559 * and end arguments for vnode_pager_update_writecount(). If
1560 * there was not a race with vnode reclaimation, then the
1561 * vnode's v_writecount is decremented.
1562 */
1563 vnode_pager_update_writecount(object, end, start);
1564 VOP_UNLOCK(vp, 0);
1565 vdrop(vp);
1566 if (mp != NULL)
1567 vn_finished_write(mp);
1568 }
Cache object: 229a0788f1a21d010a3d5552f19ac4de
|