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