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