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