160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
351369649SPedro F. Giffuni *
4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved.
6df8bae1dSRodney W. Grimes *
7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University.
9df8bae1dSRodney W. Grimes *
10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes * are met:
13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes * without specific prior written permission.
21df8bae1dSRodney W. Grimes *
22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes *
34df8bae1dSRodney W. Grimes *
35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes * All rights reserved.
37df8bae1dSRodney W. Grimes *
38df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes *
40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes *
46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes *
50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes *
52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes * School of Computer Science
54df8bae1dSRodney W. Grimes * Carnegie Mellon University
55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes *
57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes */
60df8bae1dSRodney W. Grimes
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes * Paging space routine stubs. Emulates a matchmaker-like interface
63df8bae1dSRodney W. Grimes * for builtin pagers.
64df8bae1dSRodney W. Grimes */
65df8bae1dSRodney W. Grimes
66874651b1SDavid E. O'Brien #include <sys/cdefs.h>
67756a5412SGleb Smirnoff #include "opt_param.h"
68756a5412SGleb Smirnoff
69df8bae1dSRodney W. Grimes #include <sys/param.h>
70df8bae1dSRodney W. Grimes #include <sys/systm.h>
711c5bb3eaSPeter Wemm #include <sys/kernel.h>
72a5296b05SJulian Elischer #include <sys/vnode.h>
739626b608SPoul-Henning Kamp #include <sys/bio.h>
7416f62314SDavid Greenman #include <sys/buf.h>
7503b67a39SBruce Evans #include <sys/ucred.h>
76a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
7789f6b863SAttilio Rao #include <sys/rwlock.h>
7800a3fe96SKonstantin Belousov #include <sys/user.h>
79df8bae1dSRodney W. Grimes
80df8bae1dSRodney W. Grimes #include <vm/vm.h>
81efeaf95aSDavid Greenman #include <vm/vm_param.h>
825f518366SJeff Roberson #include <vm/vm_kern.h>
83efeaf95aSDavid Greenman #include <vm/vm_object.h>
84df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8524a1cce3SDavid Greenman #include <vm/vm_pager.h>
86efeaf95aSDavid Greenman #include <vm/vm_extern.h>
87756a5412SGleb Smirnoff #include <vm/uma.h>
88df8bae1dSRodney W. Grimes
89756a5412SGleb Smirnoff uma_zone_t pbuf_zone;
90756a5412SGleb Smirnoff static int pbuf_init(void *, int, int);
91756a5412SGleb Smirnoff static int pbuf_ctor(void *, int, void *, int);
92756a5412SGleb Smirnoff static void pbuf_dtor(void *, int, void *);
9398082691SJeff Roberson
94b0cd2017SGleb Smirnoff static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
9511caded3SAlfred Perlstein static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
963364c323SKonstantin Belousov vm_ooffset_t, struct ucred *);
9711caded3SAlfred Perlstein static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
9811caded3SAlfred Perlstein static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
9911caded3SAlfred Perlstein static void dead_pager_dealloc(vm_object_t);
100192112b7SKonstantin Belousov static void dead_pager_getvp(vm_object_t, struct vnode **, bool *);
101bef608bdSJohn Dyson
102e4542174SMatthew Dillon static int
dead_pager_getpages(vm_object_t obj,vm_page_t * ma,int count,int * rbehind,int * rahead)103b0cd2017SGleb Smirnoff dead_pager_getpages(vm_object_t obj, vm_page_t *ma, int count, int *rbehind,
104b0cd2017SGleb Smirnoff int *rahead)
105bef608bdSJohn Dyson {
106b0cd2017SGleb Smirnoff
107b0cd2017SGleb Smirnoff return (VM_PAGER_FAIL);
108bef608bdSJohn Dyson }
109bef608bdSJohn Dyson
110e4542174SMatthew Dillon static vm_object_t
dead_pager_alloc(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t off,struct ucred * cred)111af83f5d7SRoman Divacky dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
1123364c323SKonstantin Belousov vm_ooffset_t off, struct ucred *cred)
113bef608bdSJohn Dyson {
11403302b13SKonstantin Belousov
11503302b13SKonstantin Belousov return (NULL);
116bef608bdSJohn Dyson }
117bef608bdSJohn Dyson
118e4542174SMatthew Dillon static void
dead_pager_putpages(vm_object_t object,vm_page_t * m,int count,int flags,int * rtvals)11903302b13SKonstantin Belousov dead_pager_putpages(vm_object_t object, vm_page_t *m, int count,
12003302b13SKonstantin Belousov int flags, int *rtvals)
121bef608bdSJohn Dyson {
122bef608bdSJohn Dyson int i;
123e4542174SMatthew Dillon
12403302b13SKonstantin Belousov for (i = 0; i < count; i++)
125bef608bdSJohn Dyson rtvals[i] = VM_PAGER_AGAIN;
126bef608bdSJohn Dyson }
127bef608bdSJohn Dyson
128f74be55eSDimitry Andric static boolean_t
dead_pager_haspage(vm_object_t object,vm_pindex_t pindex,int * prev,int * next)12903302b13SKonstantin Belousov dead_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *prev, int *next)
130bef608bdSJohn Dyson {
13103302b13SKonstantin Belousov
13203302b13SKonstantin Belousov if (prev != NULL)
133bef608bdSJohn Dyson *prev = 0;
13403302b13SKonstantin Belousov if (next != NULL)
135bef608bdSJohn Dyson *next = 0;
13603302b13SKonstantin Belousov return (FALSE);
137bef608bdSJohn Dyson }
138bef608bdSJohn Dyson
139e4542174SMatthew Dillon static void
dead_pager_dealloc(vm_object_t object)14003302b13SKonstantin Belousov dead_pager_dealloc(vm_object_t object)
141bef608bdSJohn Dyson {
14203302b13SKonstantin Belousov
143bef608bdSJohn Dyson }
144bef608bdSJohn Dyson
145192112b7SKonstantin Belousov static void
dead_pager_getvp(vm_object_t object,struct vnode ** vpp,bool * vp_heldp)146192112b7SKonstantin Belousov dead_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp)
147192112b7SKonstantin Belousov {
148192112b7SKonstantin Belousov /*
149192112b7SKonstantin Belousov * For OBJT_DEAD objects, v_writecount was handled in
150192112b7SKonstantin Belousov * vnode_pager_dealloc().
151192112b7SKonstantin Belousov */
152192112b7SKonstantin Belousov }
153192112b7SKonstantin Belousov
154d474440aSKonstantin Belousov static const struct pagerops deadpagerops = {
15500a3fe96SKonstantin Belousov .pgo_kvme_type = KVME_TYPE_DEAD,
1564e658600SPoul-Henning Kamp .pgo_alloc = dead_pager_alloc,
1574e658600SPoul-Henning Kamp .pgo_dealloc = dead_pager_dealloc,
1584e658600SPoul-Henning Kamp .pgo_getpages = dead_pager_getpages,
1594e658600SPoul-Henning Kamp .pgo_putpages = dead_pager_putpages,
1604e658600SPoul-Henning Kamp .pgo_haspage = dead_pager_haspage,
161192112b7SKonstantin Belousov .pgo_getvp = dead_pager_getvp,
162bef608bdSJohn Dyson };
163bef608bdSJohn Dyson
164b730fd30SKonstantin Belousov const struct pagerops *pagertab[16] __read_mostly = {
1650d2dfc6fSKonstantin Belousov [OBJT_SWAP] = &swappagerops,
1660d2dfc6fSKonstantin Belousov [OBJT_VNODE] = &vnodepagerops,
1670d2dfc6fSKonstantin Belousov [OBJT_DEVICE] = &devicepagerops,
1680d2dfc6fSKonstantin Belousov [OBJT_PHYS] = &physpagerops,
1690d2dfc6fSKonstantin Belousov [OBJT_DEAD] = &deadpagerops,
1700d2dfc6fSKonstantin Belousov [OBJT_SG] = &sgpagerops,
1710d2dfc6fSKonstantin Belousov [OBJT_MGTDEVICE] = &mgtdevicepagerops,
172df8bae1dSRodney W. Grimes };
173b730fd30SKonstantin Belousov static struct mtx pagertab_lock;
1741c7c3c6aSMatthew Dillon
175df8bae1dSRodney W. Grimes void
vm_pager_init(void)17603302b13SKonstantin Belousov vm_pager_init(void)
177df8bae1dSRodney W. Grimes {
178d474440aSKonstantin Belousov const struct pagerops **pgops;
179b730fd30SKonstantin Belousov int i;
180b730fd30SKonstantin Belousov
181b730fd30SKonstantin Belousov mtx_init(&pagertab_lock, "dynpag", NULL, MTX_DEF);
182df8bae1dSRodney W. Grimes
183df8bae1dSRodney W. Grimes /*
184df8bae1dSRodney W. Grimes * Initialize known pagers
185df8bae1dSRodney W. Grimes */
186b730fd30SKonstantin Belousov for (i = 0; i < OBJT_FIRST_DYN; i++) {
187b730fd30SKonstantin Belousov pgops = &pagertab[i];
1881424f65bSMark Johnston if (*pgops != NULL && (*pgops)->pgo_init != NULL)
189df8bae1dSRodney W. Grimes (*(*pgops)->pgo_init)();
190df8bae1dSRodney W. Grimes }
191b730fd30SKonstantin Belousov }
192df8bae1dSRodney W. Grimes
193756a5412SGleb Smirnoff static int nswbuf_max;
194756a5412SGleb Smirnoff
19516f62314SDavid Greenman void
vm_pager_bufferinit(void)19603302b13SKonstantin Belousov vm_pager_bufferinit(void)
19716f62314SDavid Greenman {
1980d94caffSDavid Greenman
199756a5412SGleb Smirnoff /* Main zone for paging bufs. */
200cd853791SKonstantin Belousov pbuf_zone = uma_zcreate("pbuf",
201cd853791SKonstantin Belousov sizeof(struct buf) + PBUF_PAGES * sizeof(vm_page_t),
202756a5412SGleb Smirnoff pbuf_ctor, pbuf_dtor, pbuf_init, NULL, UMA_ALIGN_CACHE,
2036c5f36ffSJeff Roberson UMA_ZONE_NOFREE);
204756a5412SGleb Smirnoff /* Few systems may still use this zone directly, so it needs a limit. */
205756a5412SGleb Smirnoff nswbuf_max += uma_zone_set_max(pbuf_zone, NSWBUF_MIN);
206756a5412SGleb Smirnoff }
207756a5412SGleb Smirnoff
208756a5412SGleb Smirnoff uma_zone_t
pbuf_zsecond_create(const char * name,int max)209eaa17d42SRyan Libby pbuf_zsecond_create(const char *name, int max)
210756a5412SGleb Smirnoff {
211756a5412SGleb Smirnoff uma_zone_t zone;
212756a5412SGleb Smirnoff
213756a5412SGleb Smirnoff zone = uma_zsecond_create(name, pbuf_ctor, pbuf_dtor, NULL, NULL,
214756a5412SGleb Smirnoff pbuf_zone);
21589786088SMark Johnston
21689786088SMark Johnston #ifdef KMSAN
21789786088SMark Johnston /*
21889786088SMark Johnston * Shrink the size of the pbuf pools if KMSAN is enabled, otherwise the
21989786088SMark Johnston * shadows of the large KVA allocations eat up too much memory.
22089786088SMark Johnston */
22189786088SMark Johnston max /= 3;
22289786088SMark Johnston #endif
22389786088SMark Johnston
22416f62314SDavid Greenman /*
225756a5412SGleb Smirnoff * uma_prealloc() rounds up to items per slab. If we would prealloc
226756a5412SGleb Smirnoff * immediately on every pbuf_zsecond_create(), we may accumulate too
227756a5412SGleb Smirnoff * much of difference between hard limit and prealloced items, which
228756a5412SGleb Smirnoff * means wasted memory.
22916f62314SDavid Greenman */
230756a5412SGleb Smirnoff if (nswbuf_max > 0)
231756a5412SGleb Smirnoff nswbuf_max += uma_zone_set_max(zone, max);
232756a5412SGleb Smirnoff else
233756a5412SGleb Smirnoff uma_prealloc(pbuf_zone, uma_zone_set_max(zone, max));
234756a5412SGleb Smirnoff
235756a5412SGleb Smirnoff return (zone);
23616f62314SDavid Greenman }
23716f62314SDavid Greenman
238756a5412SGleb Smirnoff static void
pbuf_prealloc(void * arg __unused)239756a5412SGleb Smirnoff pbuf_prealloc(void *arg __unused)
240756a5412SGleb Smirnoff {
241756a5412SGleb Smirnoff
242756a5412SGleb Smirnoff uma_prealloc(pbuf_zone, nswbuf_max);
243756a5412SGleb Smirnoff nswbuf_max = -1;
24416f62314SDavid Greenman }
24516f62314SDavid Greenman
246756a5412SGleb Smirnoff SYSINIT(pbuf, SI_SUB_KTHREAD_BUF, SI_ORDER_ANY, pbuf_prealloc, NULL);
247756a5412SGleb Smirnoff
248df8bae1dSRodney W. Grimes /*
249df8bae1dSRodney W. Grimes * Allocate an instance of a pager of the given type.
250df8bae1dSRodney W. Grimes * Size, protection and offset parameters are passed in for pagers that
251df8bae1dSRodney W. Grimes * need to perform page-level validation (e.g. the device pager).
252df8bae1dSRodney W. Grimes */
25324a1cce3SDavid Greenman vm_object_t
vm_pager_allocate(objtype_t type,void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t off,struct ucred * cred)25423955314SAlfred Perlstein vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
2553364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
256df8bae1dSRodney W. Grimes {
257cd086696SKonstantin Belousov vm_object_t object;
258cd086696SKonstantin Belousov
259b730fd30SKonstantin Belousov MPASS(type < nitems(pagertab));
260df8bae1dSRodney W. Grimes
261cd086696SKonstantin Belousov object = (*pagertab[type]->pgo_alloc)(handle, size, prot, off, cred);
262cd086696SKonstantin Belousov if (object != NULL)
263cd086696SKonstantin Belousov object->type = type;
264cd086696SKonstantin Belousov return (object);
265df8bae1dSRodney W. Grimes }
266df8bae1dSRodney W. Grimes
267658ad5ffSAlan Cox /*
268658ad5ffSAlan Cox * The object must be locked.
269658ad5ffSAlan Cox */
270df8bae1dSRodney W. Grimes void
vm_pager_deallocate(vm_object_t object)27103302b13SKonstantin Belousov vm_pager_deallocate(vm_object_t object)
272df8bae1dSRodney W. Grimes {
273658ad5ffSAlan Cox
27489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object);
275b730fd30SKonstantin Belousov MPASS(object->type < nitems(pagertab));
27624a1cce3SDavid Greenman (*pagertab[object->type]->pgo_dealloc) (object);
277df8bae1dSRodney W. Grimes }
278df8bae1dSRodney W. Grimes
279093ebe1dSGleb Smirnoff static void
vm_pager_assert_in(vm_object_t object,vm_page_t * m,int count)280093ebe1dSGleb Smirnoff vm_pager_assert_in(vm_object_t object, vm_page_t *m, int count)
281093ebe1dSGleb Smirnoff {
282093ebe1dSGleb Smirnoff #ifdef INVARIANTS
283093ebe1dSGleb Smirnoff
28442a62162SGleb Smirnoff /*
28542a62162SGleb Smirnoff * All pages must be consecutive, busied, not mapped, not fully valid,
28642a62162SGleb Smirnoff * not dirty and belong to the proper object. Some pages may be the
28742a62162SGleb Smirnoff * bogus page, but the first and last pages must be a real ones.
28842a62162SGleb Smirnoff */
28942a62162SGleb Smirnoff
290d6e13f3bSJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(object);
291d6e13f3bSJeff Roberson VM_OBJECT_ASSERT_PAGING(object);
292093ebe1dSGleb Smirnoff KASSERT(count > 0, ("%s: 0 count", __func__));
293093ebe1dSGleb Smirnoff for (int i = 0 ; i < count; i++) {
29442a62162SGleb Smirnoff if (m[i] == bogus_page) {
29542a62162SGleb Smirnoff KASSERT(i != 0 && i != count - 1,
29642a62162SGleb Smirnoff ("%s: page %d is the bogus page", __func__, i));
297255003daSGleb Smirnoff continue;
29842a62162SGleb Smirnoff }
299093ebe1dSGleb Smirnoff vm_page_assert_xbusied(m[i]);
300093ebe1dSGleb Smirnoff KASSERT(!pmap_page_is_mapped(m[i]),
301093ebe1dSGleb Smirnoff ("%s: page %p is mapped", __func__, m[i]));
302093ebe1dSGleb Smirnoff KASSERT(m[i]->valid != VM_PAGE_BITS_ALL,
303093ebe1dSGleb Smirnoff ("%s: request for a valid page %p", __func__, m[i]));
304093ebe1dSGleb Smirnoff KASSERT(m[i]->dirty == 0,
305093ebe1dSGleb Smirnoff ("%s: page %p is dirty", __func__, m[i]));
306093ebe1dSGleb Smirnoff KASSERT(m[i]->object == object,
307093ebe1dSGleb Smirnoff ("%s: wrong object %p/%p", __func__, object, m[i]->object));
30842a62162SGleb Smirnoff KASSERT(m[i]->pindex == m[0]->pindex + i,
30942a62162SGleb Smirnoff ("%s: page %p isn't consecutive", __func__, m[i]));
310093ebe1dSGleb Smirnoff }
311093ebe1dSGleb Smirnoff #endif
312093ebe1dSGleb Smirnoff }
313093ebe1dSGleb Smirnoff
314093ebe1dSGleb Smirnoff /*
315093ebe1dSGleb Smirnoff * Page in the pages for the object using its associated pager.
316093ebe1dSGleb Smirnoff * The requested page must be fully valid on successful return.
317093ebe1dSGleb Smirnoff */
318093ebe1dSGleb Smirnoff int
vm_pager_get_pages(vm_object_t object,vm_page_t * m,int count,int * rbehind,int * rahead)319b0cd2017SGleb Smirnoff vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
320b0cd2017SGleb Smirnoff int *rahead)
321093ebe1dSGleb Smirnoff {
322b0cd2017SGleb Smirnoff #ifdef INVARIANTS
323b0cd2017SGleb Smirnoff vm_pindex_t pindex = m[0]->pindex;
324b0cd2017SGleb Smirnoff #endif
325093ebe1dSGleb Smirnoff int r;
326093ebe1dSGleb Smirnoff
327b730fd30SKonstantin Belousov MPASS(object->type < nitems(pagertab));
328093ebe1dSGleb Smirnoff vm_pager_assert_in(object, m, count);
329093ebe1dSGleb Smirnoff
330b0cd2017SGleb Smirnoff r = (*pagertab[object->type]->pgo_getpages)(object, m, count, rbehind,
331b0cd2017SGleb Smirnoff rahead);
332093ebe1dSGleb Smirnoff if (r != VM_PAGER_OK)
333093ebe1dSGleb Smirnoff return (r);
334093ebe1dSGleb Smirnoff
335b0cd2017SGleb Smirnoff for (int i = 0; i < count; i++) {
336093ebe1dSGleb Smirnoff /*
337b0cd2017SGleb Smirnoff * If pager has replaced a page, assert that it had
338b0cd2017SGleb Smirnoff * updated the array.
339093ebe1dSGleb Smirnoff */
340d6e13f3bSJeff Roberson #ifdef INVARIANTS
341b0acc3f1SMark Johnston KASSERT(m[i] == vm_page_relookup(object, pindex++),
342093ebe1dSGleb Smirnoff ("%s: mismatch page %p pindex %ju", __func__,
343b0cd2017SGleb Smirnoff m[i], (uintmax_t )pindex - 1));
344d6e13f3bSJeff Roberson #endif
345b0acc3f1SMark Johnston
346093ebe1dSGleb Smirnoff /*
347b0cd2017SGleb Smirnoff * Zero out partially filled data.
348093ebe1dSGleb Smirnoff */
349b0cd2017SGleb Smirnoff if (m[i]->valid != VM_PAGE_BITS_ALL)
350b0cd2017SGleb Smirnoff vm_page_zero_invalid(m[i], TRUE);
351b0cd2017SGleb Smirnoff }
352093ebe1dSGleb Smirnoff return (VM_PAGER_OK);
353093ebe1dSGleb Smirnoff }
354093ebe1dSGleb Smirnoff
355093ebe1dSGleb Smirnoff int
vm_pager_get_pages_async(vm_object_t object,vm_page_t * m,int count,int * rbehind,int * rahead,pgo_getpages_iodone_t iodone,void * arg)356093ebe1dSGleb Smirnoff vm_pager_get_pages_async(vm_object_t object, vm_page_t *m, int count,
357b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
358093ebe1dSGleb Smirnoff {
359093ebe1dSGleb Smirnoff
360b730fd30SKonstantin Belousov MPASS(object->type < nitems(pagertab));
361093ebe1dSGleb Smirnoff vm_pager_assert_in(object, m, count);
362093ebe1dSGleb Smirnoff
363093ebe1dSGleb Smirnoff return ((*pagertab[object->type]->pgo_getpages_async)(object, m,
364b0cd2017SGleb Smirnoff count, rbehind, rahead, iodone, arg));
365093ebe1dSGleb Smirnoff }
366093ebe1dSGleb Smirnoff
367093ebe1dSGleb Smirnoff /*
3681c7c3c6aSMatthew Dillon * vm_pager_put_pages() - inline, see vm/vm_pager.h
3691c7c3c6aSMatthew Dillon * vm_pager_has_page() - inline, see vm/vm_pager.h
3701c7c3c6aSMatthew Dillon */
3711c7c3c6aSMatthew Dillon
372b5e8f167SAlan Cox /*
373b5e8f167SAlan Cox * Search the specified pager object list for an object with the
374b5e8f167SAlan Cox * specified handle. If an object with the specified handle is found,
375b5e8f167SAlan Cox * increase its reference count and return it. Otherwise, return NULL.
376b5e8f167SAlan Cox *
377b5e8f167SAlan Cox * The pager object list must be locked.
378b5e8f167SAlan Cox */
37924a1cce3SDavid Greenman vm_object_t
vm_pager_object_lookup(struct pagerlst * pg_list,void * handle)380b5e8f167SAlan Cox vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
381df8bae1dSRodney W. Grimes {
38254d92145SMatthew Dillon vm_object_t object;
383df8bae1dSRodney W. Grimes
384b5e8f167SAlan Cox TAILQ_FOREACH(object, pg_list, pager_object_list) {
385b6062382SAndriy Gapon if (object->handle == handle) {
38689f6b863SAttilio Rao VM_OBJECT_WLOCK(object);
387b6062382SAndriy Gapon if ((object->flags & OBJ_DEAD) == 0) {
388b5e8f167SAlan Cox vm_object_reference_locked(object);
38989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object);
390b5e8f167SAlan Cox break;
391b5e8f167SAlan Cox }
39289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object);
393b5e8f167SAlan Cox }
394b6062382SAndriy Gapon }
39524a1cce3SDavid Greenman return (object);
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes
398b730fd30SKonstantin Belousov int
vm_pager_alloc_dyn_type(struct pagerops * ops,int base_type)399b730fd30SKonstantin Belousov vm_pager_alloc_dyn_type(struct pagerops *ops, int base_type)
400b730fd30SKonstantin Belousov {
401b730fd30SKonstantin Belousov int res;
402b730fd30SKonstantin Belousov
403b730fd30SKonstantin Belousov mtx_lock(&pagertab_lock);
404b730fd30SKonstantin Belousov MPASS(base_type == -1 ||
4051424f65bSMark Johnston (base_type >= OBJT_SWAP && base_type < nitems(pagertab)));
406b730fd30SKonstantin Belousov for (res = OBJT_FIRST_DYN; res < nitems(pagertab); res++) {
407b730fd30SKonstantin Belousov if (pagertab[res] == NULL)
408b730fd30SKonstantin Belousov break;
409b730fd30SKonstantin Belousov }
410b730fd30SKonstantin Belousov if (res == nitems(pagertab)) {
411b730fd30SKonstantin Belousov mtx_unlock(&pagertab_lock);
412b730fd30SKonstantin Belousov return (-1);
413b730fd30SKonstantin Belousov }
414b730fd30SKonstantin Belousov if (base_type != -1) {
415b730fd30SKonstantin Belousov MPASS(pagertab[base_type] != NULL);
416b730fd30SKonstantin Belousov #define FIX(n) \
417b730fd30SKonstantin Belousov if (ops->pgo_##n == NULL) \
418b730fd30SKonstantin Belousov ops->pgo_##n = pagertab[base_type]->pgo_##n
419b730fd30SKonstantin Belousov FIX(init);
420b730fd30SKonstantin Belousov FIX(alloc);
421b730fd30SKonstantin Belousov FIX(dealloc);
422b730fd30SKonstantin Belousov FIX(getpages);
423b730fd30SKonstantin Belousov FIX(getpages_async);
424b730fd30SKonstantin Belousov FIX(putpages);
425b730fd30SKonstantin Belousov FIX(haspage);
426b730fd30SKonstantin Belousov FIX(populate);
427b730fd30SKonstantin Belousov FIX(pageunswapped);
428b730fd30SKonstantin Belousov FIX(update_writecount);
429b730fd30SKonstantin Belousov FIX(release_writecount);
430b730fd30SKonstantin Belousov FIX(set_writeable_dirty);
431b730fd30SKonstantin Belousov FIX(mightbedirty);
432b730fd30SKonstantin Belousov FIX(getvp);
433b730fd30SKonstantin Belousov FIX(freespace);
434d537d1f1SKonstantin Belousov FIX(page_inserted);
435d537d1f1SKonstantin Belousov FIX(page_removed);
436ec201dddSKonstantin Belousov FIX(can_alloc_page);
437b730fd30SKonstantin Belousov #undef FIX
438b730fd30SKonstantin Belousov }
439b730fd30SKonstantin Belousov pagertab[res] = ops; /* XXXKIB should be rel, but acq is too much */
440b730fd30SKonstantin Belousov mtx_unlock(&pagertab_lock);
441b730fd30SKonstantin Belousov return (res);
442b730fd30SKonstantin Belousov }
443b730fd30SKonstantin Belousov
444b730fd30SKonstantin Belousov void
vm_pager_free_dyn_type(objtype_t type)445b730fd30SKonstantin Belousov vm_pager_free_dyn_type(objtype_t type)
446b730fd30SKonstantin Belousov {
447b730fd30SKonstantin Belousov MPASS(type >= OBJT_FIRST_DYN && type < nitems(pagertab));
448b730fd30SKonstantin Belousov
449b730fd30SKonstantin Belousov mtx_lock(&pagertab_lock);
450b730fd30SKonstantin Belousov MPASS(pagertab[type] != NULL);
451b730fd30SKonstantin Belousov pagertab[type] = NULL;
452b730fd30SKonstantin Belousov mtx_unlock(&pagertab_lock);
453b730fd30SKonstantin Belousov }
454b730fd30SKonstantin Belousov
455756a5412SGleb Smirnoff static int
pbuf_ctor(void * mem,int size,void * arg,int flags)456756a5412SGleb Smirnoff pbuf_ctor(void *mem, int size, void *arg, int flags)
457e96c1fdcSPeter Wemm {
458756a5412SGleb Smirnoff struct buf *bp = mem;
45903302b13SKonstantin Belousov
460756a5412SGleb Smirnoff bp->b_vp = NULL;
461756a5412SGleb Smirnoff bp->b_bufobj = NULL;
462756a5412SGleb Smirnoff
463756a5412SGleb Smirnoff /* copied from initpbuf() */
46409e0c6ccSJohn Dyson bp->b_rcred = NOCRED;
46509e0c6ccSJohn Dyson bp->b_wcred = NOCRED;
466f52bd684SEivind Eklund bp->b_qindex = 0; /* On no queue (QUEUE_NONE) */
467fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase;
468fade8dd7SJeff Roberson bp->b_xflags = 0;
469cd853791SKonstantin Belousov bp->b_flags = B_MAXPHYS;
470c244d2deSPoul-Henning Kamp bp->b_ioflags = 0;
47121144e3bSPoul-Henning Kamp bp->b_iodone = NULL;
47267812eacSKirk McKusick bp->b_error = 0;
473*9c975a0dSRyan Libby BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
474756a5412SGleb Smirnoff
475756a5412SGleb Smirnoff return (0);
47609e0c6ccSJohn Dyson }
47709e0c6ccSJohn Dyson
478756a5412SGleb Smirnoff static void
pbuf_dtor(void * mem,int size,void * arg)479756a5412SGleb Smirnoff pbuf_dtor(void *mem, int size, void *arg)
4800d94caffSDavid Greenman {
481756a5412SGleb Smirnoff struct buf *bp = mem;
48216f62314SDavid Greenman
48316f62314SDavid Greenman if (bp->b_rcred != NOCRED) {
48416f62314SDavid Greenman crfree(bp->b_rcred);
48516f62314SDavid Greenman bp->b_rcred = NOCRED;
48616f62314SDavid Greenman }
48716f62314SDavid Greenman if (bp->b_wcred != NOCRED) {
48816f62314SDavid Greenman crfree(bp->b_wcred);
48916f62314SDavid Greenman bp->b_wcred = NOCRED;
49016f62314SDavid Greenman }
4911c7c3c6aSMatthew Dillon
49267812eacSKirk McKusick BUF_UNLOCK(bp);
49316f62314SDavid Greenman }
494756a5412SGleb Smirnoff
495531f8cfeSKonstantin Belousov static const char pbuf_wmesg[] = "pbufwait";
496531f8cfeSKonstantin Belousov
497756a5412SGleb Smirnoff static int
pbuf_init(void * mem,int size,int flags)498756a5412SGleb Smirnoff pbuf_init(void *mem, int size, int flags)
499756a5412SGleb Smirnoff {
500756a5412SGleb Smirnoff struct buf *bp = mem;
501756a5412SGleb Smirnoff
502b9fd884aSColin Percival TSENTER();
503b9fd884aSColin Percival
504cd853791SKonstantin Belousov bp->b_kvabase = (void *)kva_alloc(ptoa(PBUF_PAGES));
505756a5412SGleb Smirnoff if (bp->b_kvabase == NULL)
506756a5412SGleb Smirnoff return (ENOMEM);
507cd853791SKonstantin Belousov bp->b_kvasize = ptoa(PBUF_PAGES);
508531f8cfeSKonstantin Belousov BUF_LOCKINIT(bp, pbuf_wmesg);
509756a5412SGleb Smirnoff LIST_INIT(&bp->b_dep);
510756a5412SGleb Smirnoff bp->b_rcred = bp->b_wcred = NOCRED;
511756a5412SGleb Smirnoff bp->b_xflags = 0;
512756a5412SGleb Smirnoff
513b9fd884aSColin Percival TSEXIT();
514b9fd884aSColin Percival
515756a5412SGleb Smirnoff return (0);
51616f62314SDavid Greenman }
517a752aa8fSPoul-Henning Kamp
518a752aa8fSPoul-Henning Kamp /*
519a752aa8fSPoul-Henning Kamp * Associate a p-buffer with a vnode.
520a752aa8fSPoul-Henning Kamp *
521a752aa8fSPoul-Henning Kamp * Also sets B_PAGING flag to indicate that vnode is not fully associated
522a752aa8fSPoul-Henning Kamp * with the buffer. i.e. the bp has not been linked into the vnode or
523a752aa8fSPoul-Henning Kamp * ref-counted.
524a752aa8fSPoul-Henning Kamp */
525a752aa8fSPoul-Henning Kamp void
pbgetvp(struct vnode * vp,struct buf * bp)526a752aa8fSPoul-Henning Kamp pbgetvp(struct vnode *vp, struct buf *bp)
527a752aa8fSPoul-Henning Kamp {
528a752aa8fSPoul-Henning Kamp
529a752aa8fSPoul-Henning Kamp KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
530287013d2SPoul-Henning Kamp KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
531a752aa8fSPoul-Henning Kamp
532a752aa8fSPoul-Henning Kamp bp->b_vp = vp;
533a752aa8fSPoul-Henning Kamp bp->b_flags |= B_PAGING;
534a752aa8fSPoul-Henning Kamp bp->b_bufobj = &vp->v_bufobj;
535a752aa8fSPoul-Henning Kamp }
536a752aa8fSPoul-Henning Kamp
537a752aa8fSPoul-Henning Kamp /*
5385c6e573fSPoul-Henning Kamp * Associate a p-buffer with a vnode.
5395c6e573fSPoul-Henning Kamp *
5405c6e573fSPoul-Henning Kamp * Also sets B_PAGING flag to indicate that vnode is not fully associated
5415c6e573fSPoul-Henning Kamp * with the buffer. i.e. the bp has not been linked into the vnode or
5425c6e573fSPoul-Henning Kamp * ref-counted.
5435c6e573fSPoul-Henning Kamp */
5445c6e573fSPoul-Henning Kamp void
pbgetbo(struct bufobj * bo,struct buf * bp)5455c6e573fSPoul-Henning Kamp pbgetbo(struct bufobj *bo, struct buf *bp)
5465c6e573fSPoul-Henning Kamp {
5475c6e573fSPoul-Henning Kamp
5485c6e573fSPoul-Henning Kamp KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
5495c6e573fSPoul-Henning Kamp KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
5505c6e573fSPoul-Henning Kamp
5515c6e573fSPoul-Henning Kamp bp->b_flags |= B_PAGING;
5525c6e573fSPoul-Henning Kamp bp->b_bufobj = bo;
5535c6e573fSPoul-Henning Kamp }
5545c6e573fSPoul-Henning Kamp
5555c6e573fSPoul-Henning Kamp /*
556a752aa8fSPoul-Henning Kamp * Disassociate a p-buffer from a vnode.
557a752aa8fSPoul-Henning Kamp */
558a752aa8fSPoul-Henning Kamp void
pbrelvp(struct buf * bp)559a752aa8fSPoul-Henning Kamp pbrelvp(struct buf *bp)
560a752aa8fSPoul-Henning Kamp {
561a752aa8fSPoul-Henning Kamp
562a752aa8fSPoul-Henning Kamp KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
563a752aa8fSPoul-Henning Kamp KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
56426089666SJeff Roberson KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
56526089666SJeff Roberson ("pbrelvp: pager buf on vnode list."));
566a752aa8fSPoul-Henning Kamp
567a752aa8fSPoul-Henning Kamp bp->b_vp = NULL;
568a752aa8fSPoul-Henning Kamp bp->b_bufobj = NULL;
569a752aa8fSPoul-Henning Kamp bp->b_flags &= ~B_PAGING;
570a752aa8fSPoul-Henning Kamp }
5715c6e573fSPoul-Henning Kamp
5725c6e573fSPoul-Henning Kamp /*
5735c6e573fSPoul-Henning Kamp * Disassociate a p-buffer from a bufobj.
5745c6e573fSPoul-Henning Kamp */
5755c6e573fSPoul-Henning Kamp void
pbrelbo(struct buf * bp)5765c6e573fSPoul-Henning Kamp pbrelbo(struct buf *bp)
5775c6e573fSPoul-Henning Kamp {
5785c6e573fSPoul-Henning Kamp
5795c6e573fSPoul-Henning Kamp KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
5805c6e573fSPoul-Henning Kamp KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
58126089666SJeff Roberson KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
58226089666SJeff Roberson ("pbrelbo: pager buf on vnode list."));
5835c6e573fSPoul-Henning Kamp
5845c6e573fSPoul-Henning Kamp bp->b_bufobj = NULL;
5855c6e573fSPoul-Henning Kamp bp->b_flags &= ~B_PAGING;
5865c6e573fSPoul-Henning Kamp }
587180bcaa4SKonstantin Belousov
588180bcaa4SKonstantin Belousov void
vm_object_set_writeable_dirty(vm_object_t object)589180bcaa4SKonstantin Belousov vm_object_set_writeable_dirty(vm_object_t object)
590180bcaa4SKonstantin Belousov {
591180bcaa4SKonstantin Belousov pgo_set_writeable_dirty_t *method;
592180bcaa4SKonstantin Belousov
593b730fd30SKonstantin Belousov MPASS(object->type < nitems(pagertab));
594b730fd30SKonstantin Belousov
595180bcaa4SKonstantin Belousov method = pagertab[object->type]->pgo_set_writeable_dirty;
596180bcaa4SKonstantin Belousov if (method != NULL)
597180bcaa4SKonstantin Belousov method(object);
598180bcaa4SKonstantin Belousov }
599180bcaa4SKonstantin Belousov
600c23c555bSKonstantin Belousov bool
vm_object_mightbedirty(vm_object_t object)601c23c555bSKonstantin Belousov vm_object_mightbedirty(vm_object_t object)
602c23c555bSKonstantin Belousov {
603c23c555bSKonstantin Belousov pgo_mightbedirty_t *method;
604c23c555bSKonstantin Belousov
605b730fd30SKonstantin Belousov MPASS(object->type < nitems(pagertab));
606b730fd30SKonstantin Belousov
607c23c555bSKonstantin Belousov method = pagertab[object->type]->pgo_mightbedirty;
608c23c555bSKonstantin Belousov if (method == NULL)
609c23c555bSKonstantin Belousov return (false);
610c23c555bSKonstantin Belousov return (method(object));
611c23c555bSKonstantin Belousov }
61200a3fe96SKonstantin Belousov
61300a3fe96SKonstantin Belousov /*
61400a3fe96SKonstantin Belousov * Return the kvme type of the given object.
61500a3fe96SKonstantin Belousov * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL.
61600a3fe96SKonstantin Belousov */
61700a3fe96SKonstantin Belousov int
vm_object_kvme_type(vm_object_t object,struct vnode ** vpp)61800a3fe96SKonstantin Belousov vm_object_kvme_type(vm_object_t object, struct vnode **vpp)
61900a3fe96SKonstantin Belousov {
62000a3fe96SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object);
621b730fd30SKonstantin Belousov MPASS(object->type < nitems(pagertab));
62200a3fe96SKonstantin Belousov
62300a3fe96SKonstantin Belousov if (vpp != NULL)
62400a3fe96SKonstantin Belousov *vpp = vm_object_vnode(object);
62500a3fe96SKonstantin Belousov return (pagertab[object->type]->pgo_kvme_type);
62600a3fe96SKonstantin Belousov }
627