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