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