xref: /freebsd/sys/vm/vm_pager.h (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990 University of Utah.
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)vm_pager.h	8.4 (Berkeley) 1/12/94
37  * $FreeBSD$
38  */
39 
40 /*
41  * Pager routine interface definition.
42  */
43 
44 #ifndef	_VM_PAGER_
45 #define	_VM_PAGER_
46 
47 #include <sys/systm.h>
48 
49 TAILQ_HEAD(pagerlst, vm_object);
50 struct vnode;
51 
52 typedef void pgo_init_t(void);
53 typedef vm_object_t pgo_alloc_t(void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t,
54     struct ucred *);
55 typedef void pgo_dealloc_t(vm_object_t);
56 typedef int pgo_getpages_t(vm_object_t, vm_page_t *, int, int *, int *);
57 typedef void pgo_getpages_iodone_t(void *, vm_page_t *, int, int);
58 typedef int pgo_getpages_async_t(vm_object_t, vm_page_t *, int, int *, int *,
59     pgo_getpages_iodone_t, void *);
60 typedef void pgo_putpages_t(vm_object_t, vm_page_t *, int, int, int *);
61 typedef boolean_t pgo_haspage_t(vm_object_t, vm_pindex_t, int *, int *);
62 typedef int pgo_populate_t(vm_object_t, vm_pindex_t, int, vm_prot_t,
63     vm_pindex_t *, vm_pindex_t *);
64 typedef void pgo_pageunswapped_t(vm_page_t);
65 typedef void pgo_writecount_t(vm_object_t, vm_offset_t, vm_offset_t);
66 typedef void pgo_set_writeable_dirty_t(vm_object_t);
67 typedef bool pgo_mightbedirty_t(vm_object_t);
68 typedef void pgo_getvp_t(vm_object_t object, struct vnode **vpp,
69     bool *vp_heldp);
70 typedef void pgo_freespace_t(vm_object_t object, vm_pindex_t start,
71     vm_size_t size);
72 
73 struct pagerops {
74 	int			pgo_kvme_type;
75 	pgo_init_t		*pgo_init;		/* Initialize pager. */
76 	pgo_alloc_t		*pgo_alloc;		/* Allocate pager. */
77 	pgo_dealloc_t		*pgo_dealloc;		/* Disassociate. */
78 	pgo_getpages_t		*pgo_getpages;		/* Get (read) page. */
79 	pgo_getpages_async_t	*pgo_getpages_async;	/* Get page asyncly. */
80 	pgo_putpages_t		*pgo_putpages;		/* Put (write) page. */
81 	pgo_haspage_t		*pgo_haspage;		/* Query page. */
82 	pgo_populate_t		*pgo_populate;		/* Bulk spec pagein. */
83 	pgo_pageunswapped_t	*pgo_pageunswapped;
84 	pgo_writecount_t	*pgo_update_writecount;
85 	pgo_writecount_t	*pgo_release_writecount;
86 	pgo_set_writeable_dirty_t *pgo_set_writeable_dirty;
87 	pgo_mightbedirty_t	*pgo_mightbedirty;
88 	pgo_getvp_t		*pgo_getvp;
89 	pgo_freespace_t		*pgo_freespace;
90 };
91 
92 extern const struct pagerops defaultpagerops;
93 extern const struct pagerops swappagerops;
94 extern const struct pagerops vnodepagerops;
95 extern const struct pagerops devicepagerops;
96 extern const struct pagerops physpagerops;
97 extern const struct pagerops sgpagerops;
98 extern const struct pagerops mgtdevicepagerops;
99 extern const struct pagerops swaptmpfspagerops;
100 
101 /*
102  * get/put return values
103  * OK	 operation was successful
104  * BAD	 specified data was out of the accepted range
105  * FAIL	 specified data was in range, but doesn't exist
106  * PEND	 operations was initiated but not completed
107  * ERROR error while accessing data that is in range and exists
108  * AGAIN temporary resource shortage prevented operation from happening
109  */
110 #define	VM_PAGER_OK	0
111 #define	VM_PAGER_BAD	1
112 #define	VM_PAGER_FAIL	2
113 #define	VM_PAGER_PEND	3
114 #define	VM_PAGER_ERROR	4
115 #define VM_PAGER_AGAIN	5
116 
117 #define	VM_PAGER_PUT_SYNC		0x0001
118 #define	VM_PAGER_PUT_INVAL		0x0002
119 #define	VM_PAGER_PUT_NOREUSE		0x0004
120 #define VM_PAGER_CLUSTER_OK		0x0008
121 
122 #ifdef _KERNEL
123 
124 extern const struct pagerops *pagertab[] __read_mostly;
125 extern struct mtx_padalign pbuf_mtx;
126 
127 /*
128  * Number of pages that pbuf buffer can store in b_pages.
129  * It is +1 to allow for unaligned data buffer of maxphys size.
130  */
131 #define	PBUF_PAGES	(atop(maxphys) + 1)
132 
133 vm_object_t vm_pager_allocate(objtype_t, void *, vm_ooffset_t, vm_prot_t,
134     vm_ooffset_t, struct ucred *);
135 void vm_pager_bufferinit(void);
136 void vm_pager_deallocate(vm_object_t);
137 int vm_pager_get_pages(vm_object_t, vm_page_t *, int, int *, int *);
138 int vm_pager_get_pages_async(vm_object_t, vm_page_t *, int, int *, int *,
139     pgo_getpages_iodone_t, void *);
140 void vm_pager_init(void);
141 vm_object_t vm_pager_object_lookup(struct pagerlst *, void *);
142 
143 static __inline void
144 vm_pager_put_pages(vm_object_t object, vm_page_t *m, int count, int flags,
145     int *rtvals)
146 {
147 	VM_OBJECT_ASSERT_WLOCKED(object);
148 	(*pagertab[object->type]->pgo_putpages)
149 	    (object, m, count, flags, rtvals);
150 }
151 
152 /*
153  *	vm_pager_haspage
154  *
155  *	Check to see if an object's pager has the requested page.  The
156  *	object's pager will also set before and after to give the caller
157  *	some idea of the number of pages before and after the requested
158  *	page can be I/O'd efficiently.
159  *
160  *	The object must be locked.
161  */
162 static __inline boolean_t
163 vm_pager_has_page(vm_object_t object, vm_pindex_t offset, int *before,
164     int *after)
165 {
166 	boolean_t ret;
167 
168 	VM_OBJECT_ASSERT_LOCKED(object);
169 	ret = (*pagertab[object->type]->pgo_haspage)
170 	    (object, offset, before, after);
171 	return (ret);
172 }
173 
174 static __inline int
175 vm_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
176     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
177 {
178 
179 	MPASS((object->flags & OBJ_POPULATE) != 0);
180 	MPASS(pidx < object->size);
181 	MPASS(blockcount_read(&object->paging_in_progress) > 0);
182 	return ((*pagertab[object->type]->pgo_populate)(object, pidx,
183 	    fault_type, max_prot, first, last));
184 }
185 
186 /*
187  *      vm_pager_page_unswapped
188  *
189  *	Destroy swap associated with the page.
190  *
191  *	XXX: A much better name would be "vm_pager_page_dirtied()"
192  *	XXX: It is not obvious if this could be profitably used by any
193  *	XXX: pagers besides the swap_pager or if it should even be a
194  *	XXX: generic pager_op in the first place.
195  */
196 static __inline void
197 vm_pager_page_unswapped(vm_page_t m)
198 {
199 	pgo_pageunswapped_t *method;
200 
201 	method = pagertab[m->object->type]->pgo_pageunswapped;
202 	if (method != NULL)
203 		method(m);
204 }
205 
206 static __inline void
207 vm_pager_update_writecount(vm_object_t object, vm_offset_t start,
208     vm_offset_t end)
209 {
210 	pgo_writecount_t *method;
211 
212 	method = pagertab[object->type]->pgo_update_writecount;
213 	if (method != NULL)
214 		method(object, start, end);
215 }
216 
217 static __inline void
218 vm_pager_release_writecount(vm_object_t object, vm_offset_t start,
219     vm_offset_t end)
220 {
221 	pgo_writecount_t *method;
222 
223 	method = pagertab[object->type]->pgo_release_writecount;
224 	if (method != NULL)
225 		method(object, start, end);
226 }
227 
228 static __inline void
229 vm_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp)
230 {
231 	pgo_getvp_t *method;
232 
233 	*vpp = NULL;
234 	if (vp_heldp != NULL)
235 		*vp_heldp = false;
236 	method = pagertab[object->type]->pgo_getvp;
237 	if (method != NULL)
238 		method(object, vpp, vp_heldp);
239 }
240 
241 static __inline void
242 vm_pager_freespace(vm_object_t object, vm_pindex_t start,
243     vm_size_t size)
244 {
245 	pgo_freespace_t *method;
246 
247 	method = pagertab[object->type]->pgo_freespace;
248 	if (method != NULL)
249 		method(object, start, size);
250 }
251 
252 int vm_pager_alloc_dyn_type(struct pagerops *ops, int base_type);
253 void vm_pager_free_dyn_type(objtype_t type);
254 
255 struct cdev_pager_ops {
256 	int (*cdev_pg_fault)(vm_object_t vm_obj, vm_ooffset_t offset,
257 	    int prot, vm_page_t *mres);
258 	int (*cdev_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx,
259 	    int fault_type, vm_prot_t max_prot, vm_pindex_t *first,
260 	    vm_pindex_t *last);
261 	int (*cdev_pg_ctor)(void *handle, vm_ooffset_t size, vm_prot_t prot,
262 	    vm_ooffset_t foff, struct ucred *cred, u_short *color);
263 	void (*cdev_pg_dtor)(void *handle);
264 };
265 
266 vm_object_t cdev_pager_allocate(void *handle, enum obj_type tp,
267     const struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
268     vm_ooffset_t foff, struct ucred *cred);
269 vm_object_t cdev_pager_lookup(void *handle);
270 void cdev_pager_free_page(vm_object_t object, vm_page_t m);
271 
272 struct phys_pager_ops {
273 	int (*phys_pg_getpages)(vm_object_t vm_obj, vm_page_t *m, int count,
274 	    int *rbehind, int *rahead);
275 	int (*phys_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx,
276 	    int fault_type, vm_prot_t max_prot, vm_pindex_t *first,
277 	    vm_pindex_t *last);
278 	boolean_t (*phys_pg_haspage)(vm_object_t obj,  vm_pindex_t pindex,
279 	    int *before, int *after);
280 	void (*phys_pg_ctor)(vm_object_t vm_obj, vm_prot_t prot,
281 	    vm_ooffset_t foff, struct ucred *cred);
282 	void (*phys_pg_dtor)(vm_object_t vm_obj);
283 };
284 extern const struct phys_pager_ops default_phys_pg_ops;
285 vm_object_t phys_pager_allocate(void *handle, const struct phys_pager_ops *ops,
286     void *data, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff,
287     struct ucred *cred);
288 
289 #endif				/* _KERNEL */
290 #endif				/* _VM_PAGER_ */
291