xref: /freebsd/sys/vm/vm_pager.c (revision 180bcaa46c5d297d137749258b23593d578d76a5)
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  *
343c4dd356SDavid Greenman  *	from: @(#)vm_pager.c	8.6 (Berkeley) 1/12/94
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38df8bae1dSRodney W. Grimes  * All rights reserved.
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
43df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
44df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
45df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
46df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55df8bae1dSRodney W. Grimes  *  School of Computer Science
56df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
57df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
60df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  *	Paging space routine stubs.  Emulates a matchmaker-like interface
65df8bae1dSRodney W. Grimes  *	for builtin pagers.
66df8bae1dSRodney W. Grimes  */
67df8bae1dSRodney W. Grimes 
68874651b1SDavid E. O'Brien #include <sys/cdefs.h>
69874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
70874651b1SDavid E. O'Brien 
71756a5412SGleb Smirnoff #include "opt_param.h"
72756a5412SGleb Smirnoff 
73df8bae1dSRodney W. Grimes #include <sys/param.h>
74df8bae1dSRodney W. Grimes #include <sys/systm.h>
751c5bb3eaSPeter Wemm #include <sys/kernel.h>
76a5296b05SJulian Elischer #include <sys/vnode.h>
779626b608SPoul-Henning Kamp #include <sys/bio.h>
7816f62314SDavid Greenman #include <sys/buf.h>
7903b67a39SBruce Evans #include <sys/ucred.h>
80a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
8189f6b863SAttilio Rao #include <sys/rwlock.h>
82df8bae1dSRodney W. Grimes 
83df8bae1dSRodney W. Grimes #include <vm/vm.h>
84efeaf95aSDavid Greenman #include <vm/vm_param.h>
855f518366SJeff Roberson #include <vm/vm_kern.h>
86efeaf95aSDavid Greenman #include <vm/vm_object.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8824a1cce3SDavid Greenman #include <vm/vm_pager.h>
89efeaf95aSDavid Greenman #include <vm/vm_extern.h>
90756a5412SGleb Smirnoff #include <vm/uma.h>
91df8bae1dSRodney W. Grimes 
92756a5412SGleb Smirnoff uma_zone_t pbuf_zone;
93756a5412SGleb Smirnoff static int	pbuf_init(void *, int, int);
94756a5412SGleb Smirnoff static int	pbuf_ctor(void *, int, void *, int);
95756a5412SGleb Smirnoff static void	pbuf_dtor(void *, int, void *);
9698082691SJeff Roberson 
97b0cd2017SGleb Smirnoff static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
9811caded3SAlfred Perlstein static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
993364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *);
10011caded3SAlfred Perlstein static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
10111caded3SAlfred Perlstein static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
10211caded3SAlfred Perlstein static void dead_pager_dealloc(vm_object_t);
103bef608bdSJohn Dyson 
104e4542174SMatthew Dillon static int
105b0cd2017SGleb Smirnoff dead_pager_getpages(vm_object_t obj, vm_page_t *ma, int count, int *rbehind,
106b0cd2017SGleb Smirnoff     int *rahead)
107bef608bdSJohn Dyson {
108b0cd2017SGleb Smirnoff 
109b0cd2017SGleb Smirnoff 	return (VM_PAGER_FAIL);
110bef608bdSJohn Dyson }
111bef608bdSJohn Dyson 
112e4542174SMatthew Dillon static vm_object_t
113af83f5d7SRoman Divacky dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
1143364c323SKonstantin Belousov     vm_ooffset_t off, struct ucred *cred)
115bef608bdSJohn Dyson {
11603302b13SKonstantin Belousov 
11703302b13SKonstantin Belousov 	return (NULL);
118bef608bdSJohn Dyson }
119bef608bdSJohn Dyson 
120e4542174SMatthew Dillon static void
12103302b13SKonstantin Belousov dead_pager_putpages(vm_object_t object, vm_page_t *m, int count,
12203302b13SKonstantin Belousov     int flags, int *rtvals)
123bef608bdSJohn Dyson {
124bef608bdSJohn Dyson 	int i;
125e4542174SMatthew Dillon 
12603302b13SKonstantin Belousov 	for (i = 0; i < count; i++)
127bef608bdSJohn Dyson 		rtvals[i] = VM_PAGER_AGAIN;
128bef608bdSJohn Dyson }
129bef608bdSJohn Dyson 
130e4542174SMatthew Dillon static int
13103302b13SKonstantin Belousov dead_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *prev, int *next)
132bef608bdSJohn Dyson {
13303302b13SKonstantin Belousov 
13403302b13SKonstantin Belousov 	if (prev != NULL)
135bef608bdSJohn Dyson 		*prev = 0;
13603302b13SKonstantin Belousov 	if (next != NULL)
137bef608bdSJohn Dyson 		*next = 0;
13803302b13SKonstantin Belousov 	return (FALSE);
139bef608bdSJohn Dyson }
140bef608bdSJohn Dyson 
141e4542174SMatthew Dillon static void
14203302b13SKonstantin Belousov dead_pager_dealloc(vm_object_t object)
143bef608bdSJohn Dyson {
14403302b13SKonstantin Belousov 
145bef608bdSJohn Dyson }
146bef608bdSJohn Dyson 
1470776e10cSEivind Eklund static struct pagerops deadpagerops = {
1484e658600SPoul-Henning Kamp 	.pgo_alloc = 	dead_pager_alloc,
1494e658600SPoul-Henning Kamp 	.pgo_dealloc =	dead_pager_dealloc,
1504e658600SPoul-Henning Kamp 	.pgo_getpages =	dead_pager_getpages,
1514e658600SPoul-Henning Kamp 	.pgo_putpages =	dead_pager_putpages,
1524e658600SPoul-Henning Kamp 	.pgo_haspage =	dead_pager_haspage,
153bef608bdSJohn Dyson };
154bef608bdSJohn Dyson 
1551c7c3c6aSMatthew Dillon struct pagerops *pagertab[] = {
15624a1cce3SDavid Greenman 	&defaultpagerops,	/* OBJT_DEFAULT */
15724a1cce3SDavid Greenman 	&swappagerops,		/* OBJT_SWAP */
15824a1cce3SDavid Greenman 	&vnodepagerops,		/* OBJT_VNODE */
15924a1cce3SDavid Greenman 	&devicepagerops,	/* OBJT_DEVICE */
16024964514SPeter Wemm 	&physpagerops,		/* OBJT_PHYS */
16101381811SJohn Baldwin 	&deadpagerops,		/* OBJT_DEAD */
162b7ac5a85SKonstantin Belousov 	&sgpagerops,		/* OBJT_SG */
163b7ac5a85SKonstantin Belousov 	&mgtdevicepagerops,	/* OBJT_MGTDEVICE */
164df8bae1dSRodney W. Grimes };
1651c7c3c6aSMatthew Dillon 
166df8bae1dSRodney W. Grimes void
16703302b13SKonstantin Belousov vm_pager_init(void)
168df8bae1dSRodney W. Grimes {
169df8bae1dSRodney W. Grimes 	struct pagerops **pgops;
170df8bae1dSRodney W. Grimes 
171df8bae1dSRodney W. Grimes 	/*
172df8bae1dSRodney W. Grimes 	 * Initialize known pagers
173df8bae1dSRodney W. Grimes 	 */
1748dfea464SPedro F. Giffuni 	for (pgops = pagertab; pgops < &pagertab[nitems(pagertab)]; pgops++)
175bf277cf4SKonstantin Belousov 		if ((*pgops)->pgo_init != NULL)
176df8bae1dSRodney W. Grimes 			(*(*pgops)->pgo_init)();
177df8bae1dSRodney W. Grimes }
178df8bae1dSRodney W. Grimes 
179756a5412SGleb Smirnoff static int nswbuf_max;
180756a5412SGleb Smirnoff 
18116f62314SDavid Greenman void
18203302b13SKonstantin Belousov vm_pager_bufferinit(void)
18316f62314SDavid Greenman {
1840d94caffSDavid Greenman 
185756a5412SGleb Smirnoff 	/* Main zone for paging bufs. */
186cd853791SKonstantin Belousov 	pbuf_zone = uma_zcreate("pbuf",
187cd853791SKonstantin Belousov 	    sizeof(struct buf) + PBUF_PAGES * sizeof(vm_page_t),
188756a5412SGleb Smirnoff 	    pbuf_ctor, pbuf_dtor, pbuf_init, NULL, UMA_ALIGN_CACHE,
1896c5f36ffSJeff Roberson 	    UMA_ZONE_NOFREE);
190756a5412SGleb Smirnoff 	/* Few systems may still use this zone directly, so it needs a limit. */
191756a5412SGleb Smirnoff 	nswbuf_max += uma_zone_set_max(pbuf_zone, NSWBUF_MIN);
192756a5412SGleb Smirnoff }
193756a5412SGleb Smirnoff 
194756a5412SGleb Smirnoff uma_zone_t
195eaa17d42SRyan Libby pbuf_zsecond_create(const char *name, int max)
196756a5412SGleb Smirnoff {
197756a5412SGleb Smirnoff 	uma_zone_t zone;
198756a5412SGleb Smirnoff 
199756a5412SGleb Smirnoff 	zone = uma_zsecond_create(name, pbuf_ctor, pbuf_dtor, NULL, NULL,
200756a5412SGleb Smirnoff 	    pbuf_zone);
20116f62314SDavid Greenman 	/*
202756a5412SGleb Smirnoff 	 * uma_prealloc() rounds up to items per slab. If we would prealloc
203756a5412SGleb Smirnoff 	 * immediately on every pbuf_zsecond_create(), we may accumulate too
204756a5412SGleb Smirnoff 	 * much of difference between hard limit and prealloced items, which
205756a5412SGleb Smirnoff 	 * means wasted memory.
20616f62314SDavid Greenman 	 */
207756a5412SGleb Smirnoff 	if (nswbuf_max > 0)
208756a5412SGleb Smirnoff 		nswbuf_max += uma_zone_set_max(zone, max);
209756a5412SGleb Smirnoff 	else
210756a5412SGleb Smirnoff 		uma_prealloc(pbuf_zone, uma_zone_set_max(zone, max));
211756a5412SGleb Smirnoff 
212756a5412SGleb Smirnoff 	return (zone);
21316f62314SDavid Greenman }
21416f62314SDavid Greenman 
215756a5412SGleb Smirnoff static void
216756a5412SGleb Smirnoff pbuf_prealloc(void *arg __unused)
217756a5412SGleb Smirnoff {
218756a5412SGleb Smirnoff 
219756a5412SGleb Smirnoff 	uma_prealloc(pbuf_zone, nswbuf_max);
220756a5412SGleb Smirnoff 	nswbuf_max = -1;
22116f62314SDavid Greenman }
22216f62314SDavid Greenman 
223756a5412SGleb Smirnoff SYSINIT(pbuf, SI_SUB_KTHREAD_BUF, SI_ORDER_ANY, pbuf_prealloc, NULL);
224756a5412SGleb Smirnoff 
225df8bae1dSRodney W. Grimes /*
226df8bae1dSRodney W. Grimes  * Allocate an instance of a pager of the given type.
227df8bae1dSRodney W. Grimes  * Size, protection and offset parameters are passed in for pagers that
228df8bae1dSRodney W. Grimes  * need to perform page-level validation (e.g. the device pager).
229df8bae1dSRodney W. Grimes  */
23024a1cce3SDavid Greenman vm_object_t
23123955314SAlfred Perlstein vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
2323364c323SKonstantin Belousov     vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
233df8bae1dSRodney W. Grimes {
23423955314SAlfred Perlstein 	vm_object_t ret;
235df8bae1dSRodney W. Grimes 	struct pagerops *ops;
236df8bae1dSRodney W. Grimes 
23724a1cce3SDavid Greenman 	ops = pagertab[type];
238df8bae1dSRodney W. Grimes 	if (ops)
2393364c323SKonstantin Belousov 		ret = (*ops->pgo_alloc)(handle, size, prot, off, cred);
24023955314SAlfred Perlstein 	else
24123955314SAlfred Perlstein 		ret = NULL;
24223955314SAlfred Perlstein 	return (ret);
243df8bae1dSRodney W. Grimes }
244df8bae1dSRodney W. Grimes 
245658ad5ffSAlan Cox /*
246658ad5ffSAlan Cox  *	The object must be locked.
247658ad5ffSAlan Cox  */
248df8bae1dSRodney W. Grimes void
24903302b13SKonstantin Belousov vm_pager_deallocate(vm_object_t object)
250df8bae1dSRodney W. Grimes {
251658ad5ffSAlan Cox 
25289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
25324a1cce3SDavid Greenman 	(*pagertab[object->type]->pgo_dealloc) (object);
254df8bae1dSRodney W. Grimes }
255df8bae1dSRodney W. Grimes 
256093ebe1dSGleb Smirnoff static void
257093ebe1dSGleb Smirnoff vm_pager_assert_in(vm_object_t object, vm_page_t *m, int count)
258093ebe1dSGleb Smirnoff {
259093ebe1dSGleb Smirnoff #ifdef INVARIANTS
260093ebe1dSGleb Smirnoff 
26142a62162SGleb Smirnoff 	/*
26242a62162SGleb Smirnoff 	 * All pages must be consecutive, busied, not mapped, not fully valid,
26342a62162SGleb Smirnoff 	 * not dirty and belong to the proper object.  Some pages may be the
26442a62162SGleb Smirnoff 	 * bogus page, but the first and last pages must be a real ones.
26542a62162SGleb Smirnoff 	 */
26642a62162SGleb Smirnoff 
267d6e13f3bSJeff Roberson 	VM_OBJECT_ASSERT_UNLOCKED(object);
268d6e13f3bSJeff Roberson 	VM_OBJECT_ASSERT_PAGING(object);
269093ebe1dSGleb Smirnoff 	KASSERT(count > 0, ("%s: 0 count", __func__));
270093ebe1dSGleb Smirnoff 	for (int i = 0 ; i < count; i++) {
27142a62162SGleb Smirnoff 		if (m[i] == bogus_page) {
27242a62162SGleb Smirnoff 			KASSERT(i != 0 && i != count - 1,
27342a62162SGleb Smirnoff 			    ("%s: page %d is the bogus page", __func__, i));
274255003daSGleb Smirnoff 			continue;
27542a62162SGleb Smirnoff 		}
276093ebe1dSGleb Smirnoff 		vm_page_assert_xbusied(m[i]);
277093ebe1dSGleb Smirnoff 		KASSERT(!pmap_page_is_mapped(m[i]),
278093ebe1dSGleb Smirnoff 		    ("%s: page %p is mapped", __func__, m[i]));
279093ebe1dSGleb Smirnoff 		KASSERT(m[i]->valid != VM_PAGE_BITS_ALL,
280093ebe1dSGleb Smirnoff 		    ("%s: request for a valid page %p", __func__, m[i]));
281093ebe1dSGleb Smirnoff 		KASSERT(m[i]->dirty == 0,
282093ebe1dSGleb Smirnoff 		    ("%s: page %p is dirty", __func__, m[i]));
283093ebe1dSGleb Smirnoff 		KASSERT(m[i]->object == object,
284093ebe1dSGleb Smirnoff 		    ("%s: wrong object %p/%p", __func__, object, m[i]->object));
28542a62162SGleb Smirnoff 		KASSERT(m[i]->pindex == m[0]->pindex + i,
28642a62162SGleb Smirnoff 		    ("%s: page %p isn't consecutive", __func__, m[i]));
287093ebe1dSGleb Smirnoff 	}
288093ebe1dSGleb Smirnoff #endif
289093ebe1dSGleb Smirnoff }
290093ebe1dSGleb Smirnoff 
291093ebe1dSGleb Smirnoff /*
292093ebe1dSGleb Smirnoff  * Page in the pages for the object using its associated pager.
293093ebe1dSGleb Smirnoff  * The requested page must be fully valid on successful return.
294093ebe1dSGleb Smirnoff  */
295093ebe1dSGleb Smirnoff int
296b0cd2017SGleb Smirnoff vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
297b0cd2017SGleb Smirnoff     int *rahead)
298093ebe1dSGleb Smirnoff {
299b0cd2017SGleb Smirnoff #ifdef INVARIANTS
300b0cd2017SGleb Smirnoff 	vm_pindex_t pindex = m[0]->pindex;
301b0cd2017SGleb Smirnoff #endif
302093ebe1dSGleb Smirnoff 	int r;
303093ebe1dSGleb Smirnoff 
304093ebe1dSGleb Smirnoff 	vm_pager_assert_in(object, m, count);
305093ebe1dSGleb Smirnoff 
306b0cd2017SGleb Smirnoff 	r = (*pagertab[object->type]->pgo_getpages)(object, m, count, rbehind,
307b0cd2017SGleb Smirnoff 	    rahead);
308093ebe1dSGleb Smirnoff 	if (r != VM_PAGER_OK)
309093ebe1dSGleb Smirnoff 		return (r);
310093ebe1dSGleb Smirnoff 
311b0cd2017SGleb Smirnoff 	for (int i = 0; i < count; i++) {
312093ebe1dSGleb Smirnoff 		/*
313b0cd2017SGleb Smirnoff 		 * If pager has replaced a page, assert that it had
314b0cd2017SGleb Smirnoff 		 * updated the array.
315093ebe1dSGleb Smirnoff 		 */
316d6e13f3bSJeff Roberson #ifdef INVARIANTS
317d6e13f3bSJeff Roberson 		VM_OBJECT_RLOCK(object);
318b0cd2017SGleb Smirnoff 		KASSERT(m[i] == vm_page_lookup(object, pindex++),
319093ebe1dSGleb Smirnoff 		    ("%s: mismatch page %p pindex %ju", __func__,
320b0cd2017SGleb Smirnoff 		    m[i], (uintmax_t )pindex - 1));
321d6e13f3bSJeff Roberson 		VM_OBJECT_RUNLOCK(object);
322d6e13f3bSJeff Roberson #endif
323093ebe1dSGleb Smirnoff 		/*
324b0cd2017SGleb Smirnoff 		 * Zero out partially filled data.
325093ebe1dSGleb Smirnoff 		 */
326b0cd2017SGleb Smirnoff 		if (m[i]->valid != VM_PAGE_BITS_ALL)
327b0cd2017SGleb Smirnoff 			vm_page_zero_invalid(m[i], TRUE);
328b0cd2017SGleb Smirnoff 	}
329093ebe1dSGleb Smirnoff 	return (VM_PAGER_OK);
330093ebe1dSGleb Smirnoff }
331093ebe1dSGleb Smirnoff 
332093ebe1dSGleb Smirnoff int
333093ebe1dSGleb Smirnoff vm_pager_get_pages_async(vm_object_t object, vm_page_t *m, int count,
334b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
335093ebe1dSGleb Smirnoff {
336093ebe1dSGleb Smirnoff 
337093ebe1dSGleb Smirnoff 	vm_pager_assert_in(object, m, count);
338093ebe1dSGleb Smirnoff 
339093ebe1dSGleb Smirnoff 	return ((*pagertab[object->type]->pgo_getpages_async)(object, m,
340b0cd2017SGleb Smirnoff 	    count, rbehind, rahead, iodone, arg));
341093ebe1dSGleb Smirnoff }
342093ebe1dSGleb Smirnoff 
343093ebe1dSGleb Smirnoff /*
3441c7c3c6aSMatthew Dillon  * vm_pager_put_pages() - inline, see vm/vm_pager.h
3451c7c3c6aSMatthew Dillon  * vm_pager_has_page() - inline, see vm/vm_pager.h
3461c7c3c6aSMatthew Dillon  */
3471c7c3c6aSMatthew Dillon 
348b5e8f167SAlan Cox /*
349b5e8f167SAlan Cox  * Search the specified pager object list for an object with the
350b5e8f167SAlan Cox  * specified handle.  If an object with the specified handle is found,
351b5e8f167SAlan Cox  * increase its reference count and return it.  Otherwise, return NULL.
352b5e8f167SAlan Cox  *
353b5e8f167SAlan Cox  * The pager object list must be locked.
354b5e8f167SAlan Cox  */
35524a1cce3SDavid Greenman vm_object_t
356b5e8f167SAlan Cox vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
357df8bae1dSRodney W. Grimes {
35854d92145SMatthew Dillon 	vm_object_t object;
359df8bae1dSRodney W. Grimes 
360b5e8f167SAlan Cox 	TAILQ_FOREACH(object, pg_list, pager_object_list) {
361b6062382SAndriy Gapon 		if (object->handle == handle) {
36289f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
363b6062382SAndriy Gapon 			if ((object->flags & OBJ_DEAD) == 0) {
364b5e8f167SAlan Cox 				vm_object_reference_locked(object);
36589f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
366b5e8f167SAlan Cox 				break;
367b5e8f167SAlan Cox 			}
36889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
369b5e8f167SAlan Cox 		}
370b6062382SAndriy Gapon 	}
37124a1cce3SDavid Greenman 	return (object);
372df8bae1dSRodney W. Grimes }
373df8bae1dSRodney W. Grimes 
374756a5412SGleb Smirnoff static int
375756a5412SGleb Smirnoff pbuf_ctor(void *mem, int size, void *arg, int flags)
376e96c1fdcSPeter Wemm {
377756a5412SGleb Smirnoff 	struct buf *bp = mem;
37803302b13SKonstantin Belousov 
379756a5412SGleb Smirnoff 	bp->b_vp = NULL;
380756a5412SGleb Smirnoff 	bp->b_bufobj = NULL;
381756a5412SGleb Smirnoff 
382756a5412SGleb Smirnoff 	/* copied from initpbuf() */
38309e0c6ccSJohn Dyson 	bp->b_rcred = NOCRED;
38409e0c6ccSJohn Dyson 	bp->b_wcred = NOCRED;
385f52bd684SEivind Eklund 	bp->b_qindex = 0;       /* On no queue (QUEUE_NONE) */
386fade8dd7SJeff Roberson 	bp->b_data = bp->b_kvabase;
387fade8dd7SJeff Roberson 	bp->b_xflags = 0;
388cd853791SKonstantin Belousov 	bp->b_flags = B_MAXPHYS;
389c244d2deSPoul-Henning Kamp 	bp->b_ioflags = 0;
39021144e3bSPoul-Henning Kamp 	bp->b_iodone = NULL;
39167812eacSKirk McKusick 	bp->b_error = 0;
39217661e5aSJeff Roberson 	BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
393756a5412SGleb Smirnoff 
394756a5412SGleb Smirnoff 	return (0);
39509e0c6ccSJohn Dyson }
39609e0c6ccSJohn Dyson 
397756a5412SGleb Smirnoff static void
398756a5412SGleb Smirnoff pbuf_dtor(void *mem, int size, void *arg)
3990d94caffSDavid Greenman {
400756a5412SGleb Smirnoff 	struct buf *bp = mem;
40116f62314SDavid Greenman 
40216f62314SDavid Greenman 	if (bp->b_rcred != NOCRED) {
40316f62314SDavid Greenman 		crfree(bp->b_rcred);
40416f62314SDavid Greenman 		bp->b_rcred = NOCRED;
40516f62314SDavid Greenman 	}
40616f62314SDavid Greenman 	if (bp->b_wcred != NOCRED) {
40716f62314SDavid Greenman 		crfree(bp->b_wcred);
40816f62314SDavid Greenman 		bp->b_wcred = NOCRED;
40916f62314SDavid Greenman 	}
4101c7c3c6aSMatthew Dillon 
41167812eacSKirk McKusick 	BUF_UNLOCK(bp);
41216f62314SDavid Greenman }
413756a5412SGleb Smirnoff 
414756a5412SGleb Smirnoff static int
415756a5412SGleb Smirnoff pbuf_init(void *mem, int size, int flags)
416756a5412SGleb Smirnoff {
417756a5412SGleb Smirnoff 	struct buf *bp = mem;
418756a5412SGleb Smirnoff 
419cd853791SKonstantin Belousov 	bp->b_kvabase = (void *)kva_alloc(ptoa(PBUF_PAGES));
420756a5412SGleb Smirnoff 	if (bp->b_kvabase == NULL)
421756a5412SGleb Smirnoff 		return (ENOMEM);
422cd853791SKonstantin Belousov 	bp->b_kvasize = ptoa(PBUF_PAGES);
423756a5412SGleb Smirnoff 	BUF_LOCKINIT(bp);
424756a5412SGleb Smirnoff 	LIST_INIT(&bp->b_dep);
425756a5412SGleb Smirnoff 	bp->b_rcred = bp->b_wcred = NOCRED;
426756a5412SGleb Smirnoff 	bp->b_xflags = 0;
427756a5412SGleb Smirnoff 
428756a5412SGleb Smirnoff 	return (0);
42916f62314SDavid Greenman }
430a752aa8fSPoul-Henning Kamp 
431a752aa8fSPoul-Henning Kamp /*
432a752aa8fSPoul-Henning Kamp  * Associate a p-buffer with a vnode.
433a752aa8fSPoul-Henning Kamp  *
434a752aa8fSPoul-Henning Kamp  * Also sets B_PAGING flag to indicate that vnode is not fully associated
435a752aa8fSPoul-Henning Kamp  * with the buffer.  i.e. the bp has not been linked into the vnode or
436a752aa8fSPoul-Henning Kamp  * ref-counted.
437a752aa8fSPoul-Henning Kamp  */
438a752aa8fSPoul-Henning Kamp void
439a752aa8fSPoul-Henning Kamp pbgetvp(struct vnode *vp, struct buf *bp)
440a752aa8fSPoul-Henning Kamp {
441a752aa8fSPoul-Henning Kamp 
442a752aa8fSPoul-Henning Kamp 	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
443287013d2SPoul-Henning Kamp 	KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
444a752aa8fSPoul-Henning Kamp 
445a752aa8fSPoul-Henning Kamp 	bp->b_vp = vp;
446a752aa8fSPoul-Henning Kamp 	bp->b_flags |= B_PAGING;
447a752aa8fSPoul-Henning Kamp 	bp->b_bufobj = &vp->v_bufobj;
448a752aa8fSPoul-Henning Kamp }
449a752aa8fSPoul-Henning Kamp 
450a752aa8fSPoul-Henning Kamp /*
4515c6e573fSPoul-Henning Kamp  * Associate a p-buffer with a vnode.
4525c6e573fSPoul-Henning Kamp  *
4535c6e573fSPoul-Henning Kamp  * Also sets B_PAGING flag to indicate that vnode is not fully associated
4545c6e573fSPoul-Henning Kamp  * with the buffer.  i.e. the bp has not been linked into the vnode or
4555c6e573fSPoul-Henning Kamp  * ref-counted.
4565c6e573fSPoul-Henning Kamp  */
4575c6e573fSPoul-Henning Kamp void
4585c6e573fSPoul-Henning Kamp pbgetbo(struct bufobj *bo, struct buf *bp)
4595c6e573fSPoul-Henning Kamp {
4605c6e573fSPoul-Henning Kamp 
4615c6e573fSPoul-Henning Kamp 	KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
4625c6e573fSPoul-Henning Kamp 	KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
4635c6e573fSPoul-Henning Kamp 
4645c6e573fSPoul-Henning Kamp 	bp->b_flags |= B_PAGING;
4655c6e573fSPoul-Henning Kamp 	bp->b_bufobj = bo;
4665c6e573fSPoul-Henning Kamp }
4675c6e573fSPoul-Henning Kamp 
4685c6e573fSPoul-Henning Kamp /*
469a752aa8fSPoul-Henning Kamp  * Disassociate a p-buffer from a vnode.
470a752aa8fSPoul-Henning Kamp  */
471a752aa8fSPoul-Henning Kamp void
472a752aa8fSPoul-Henning Kamp pbrelvp(struct buf *bp)
473a752aa8fSPoul-Henning Kamp {
474a752aa8fSPoul-Henning Kamp 
475a752aa8fSPoul-Henning Kamp 	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
476a752aa8fSPoul-Henning Kamp 	KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
47726089666SJeff Roberson 	KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
47826089666SJeff Roberson 	    ("pbrelvp: pager buf on vnode list."));
479a752aa8fSPoul-Henning Kamp 
480a752aa8fSPoul-Henning Kamp 	bp->b_vp = NULL;
481a752aa8fSPoul-Henning Kamp 	bp->b_bufobj = NULL;
482a752aa8fSPoul-Henning Kamp 	bp->b_flags &= ~B_PAGING;
483a752aa8fSPoul-Henning Kamp }
4845c6e573fSPoul-Henning Kamp 
4855c6e573fSPoul-Henning Kamp /*
4865c6e573fSPoul-Henning Kamp  * Disassociate a p-buffer from a bufobj.
4875c6e573fSPoul-Henning Kamp  */
4885c6e573fSPoul-Henning Kamp void
4895c6e573fSPoul-Henning Kamp pbrelbo(struct buf *bp)
4905c6e573fSPoul-Henning Kamp {
4915c6e573fSPoul-Henning Kamp 
4925c6e573fSPoul-Henning Kamp 	KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
4935c6e573fSPoul-Henning Kamp 	KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
49426089666SJeff Roberson 	KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
49526089666SJeff Roberson 	    ("pbrelbo: pager buf on vnode list."));
4965c6e573fSPoul-Henning Kamp 
4975c6e573fSPoul-Henning Kamp 	bp->b_bufobj = NULL;
4985c6e573fSPoul-Henning Kamp 	bp->b_flags &= ~B_PAGING;
4995c6e573fSPoul-Henning Kamp }
500*180bcaa4SKonstantin Belousov 
501*180bcaa4SKonstantin Belousov void
502*180bcaa4SKonstantin Belousov vm_object_set_writeable_dirty(vm_object_t object)
503*180bcaa4SKonstantin Belousov {
504*180bcaa4SKonstantin Belousov 	pgo_set_writeable_dirty_t *method;
505*180bcaa4SKonstantin Belousov 
506*180bcaa4SKonstantin Belousov 	method = pagertab[object->type]->pgo_set_writeable_dirty;
507*180bcaa4SKonstantin Belousov 	if (method != NULL)
508*180bcaa4SKonstantin Belousov 		method(object);
509*180bcaa4SKonstantin Belousov }
510*180bcaa4SKonstantin Belousov 
511