xref: /freebsd/sys/vm/vm_pager.h (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
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 typedef void pgo_page_inserted_t(vm_object_t object, vm_page_t m);
73 typedef void pgo_page_removed_t(vm_object_t object, vm_page_t m);
74 typedef boolean_t pgo_can_alloc_page_t(vm_object_t object, vm_pindex_t pindex);
75 
76 struct pagerops {
77 	int			pgo_kvme_type;
78 	pgo_init_t		*pgo_init;		/* Initialize pager. */
79 	pgo_alloc_t		*pgo_alloc;		/* Allocate pager. */
80 	pgo_dealloc_t		*pgo_dealloc;		/* Disassociate. */
81 	pgo_getpages_t		*pgo_getpages;		/* Get (read) page. */
82 	pgo_getpages_async_t	*pgo_getpages_async;	/* Get page asyncly. */
83 	pgo_putpages_t		*pgo_putpages;		/* Put (write) page. */
84 	pgo_haspage_t		*pgo_haspage;		/* Query page. */
85 	pgo_populate_t		*pgo_populate;		/* Bulk spec pagein. */
86 	pgo_pageunswapped_t	*pgo_pageunswapped;
87 	pgo_writecount_t	*pgo_update_writecount;
88 	pgo_writecount_t	*pgo_release_writecount;
89 	pgo_set_writeable_dirty_t *pgo_set_writeable_dirty;
90 	pgo_mightbedirty_t	*pgo_mightbedirty;
91 	pgo_getvp_t		*pgo_getvp;
92 	pgo_freespace_t		*pgo_freespace;
93 	pgo_page_inserted_t	*pgo_page_inserted;
94 	pgo_page_removed_t	*pgo_page_removed;
95 	pgo_can_alloc_page_t	*pgo_can_alloc_page;
96 };
97 
98 extern const struct pagerops defaultpagerops;
99 extern const struct pagerops swappagerops;
100 extern const struct pagerops vnodepagerops;
101 extern const struct pagerops devicepagerops;
102 extern const struct pagerops physpagerops;
103 extern const struct pagerops sgpagerops;
104 extern const struct pagerops mgtdevicepagerops;
105 extern const struct pagerops swaptmpfspagerops;
106 
107 /*
108  * get/put return values
109  * OK	 operation was successful
110  * BAD	 specified data was out of the accepted range
111  * FAIL	 specified data was in range, but doesn't exist
112  * PEND	 operations was initiated but not completed
113  * ERROR error while accessing data that is in range and exists
114  * AGAIN temporary resource shortage prevented operation from happening
115  */
116 #define	VM_PAGER_OK	0
117 #define	VM_PAGER_BAD	1
118 #define	VM_PAGER_FAIL	2
119 #define	VM_PAGER_PEND	3
120 #define	VM_PAGER_ERROR	4
121 #define VM_PAGER_AGAIN	5
122 
123 #define	VM_PAGER_PUT_SYNC		0x0001
124 #define	VM_PAGER_PUT_INVAL		0x0002
125 #define	VM_PAGER_PUT_NOREUSE		0x0004
126 #define VM_PAGER_CLUSTER_OK		0x0008
127 
128 #ifdef _KERNEL
129 
130 extern const struct pagerops *pagertab[] __read_mostly;
131 extern struct mtx_padalign pbuf_mtx;
132 
133 /*
134  * Number of pages that pbuf buffer can store in b_pages.
135  * It is +1 to allow for unaligned data buffer of maxphys size.
136  */
137 #define	PBUF_PAGES	(atop(maxphys) + 1)
138 
139 vm_object_t vm_pager_allocate(objtype_t, void *, vm_ooffset_t, vm_prot_t,
140     vm_ooffset_t, struct ucred *);
141 void vm_pager_bufferinit(void);
142 void vm_pager_deallocate(vm_object_t);
143 int vm_pager_get_pages(vm_object_t, vm_page_t *, int, int *, int *);
144 int vm_pager_get_pages_async(vm_object_t, vm_page_t *, int, int *, int *,
145     pgo_getpages_iodone_t, void *);
146 void vm_pager_init(void);
147 vm_object_t vm_pager_object_lookup(struct pagerlst *, void *);
148 
149 static __inline void
150 vm_pager_put_pages(vm_object_t object, vm_page_t *m, int count, int flags,
151     int *rtvals)
152 {
153 	VM_OBJECT_ASSERT_WLOCKED(object);
154 	(*pagertab[object->type]->pgo_putpages)
155 	    (object, m, count, flags, rtvals);
156 }
157 
158 /*
159  *	vm_pager_haspage
160  *
161  *	Check to see if an object's pager has the requested page.  The
162  *	object's pager will also set before and after to give the caller
163  *	some idea of the number of pages before and after the requested
164  *	page can be I/O'd efficiently.
165  *
166  *	The object must be locked.
167  */
168 static __inline boolean_t
169 vm_pager_has_page(vm_object_t object, vm_pindex_t offset, int *before,
170     int *after)
171 {
172 	boolean_t ret;
173 
174 	VM_OBJECT_ASSERT_LOCKED(object);
175 	ret = (*pagertab[object->type]->pgo_haspage)
176 	    (object, offset, before, after);
177 	return (ret);
178 }
179 
180 static __inline int
181 vm_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
182     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
183 {
184 
185 	MPASS((object->flags & OBJ_POPULATE) != 0);
186 	MPASS(pidx < object->size);
187 	MPASS(blockcount_read(&object->paging_in_progress) > 0);
188 	return ((*pagertab[object->type]->pgo_populate)(object, pidx,
189 	    fault_type, max_prot, first, last));
190 }
191 
192 /*
193  *      vm_pager_page_unswapped
194  *
195  *	Destroy swap associated with the page.
196  *
197  *	XXX: A much better name would be "vm_pager_page_dirtied()"
198  *	XXX: It is not obvious if this could be profitably used by any
199  *	XXX: pagers besides the swap_pager or if it should even be a
200  *	XXX: generic pager_op in the first place.
201  */
202 static __inline void
203 vm_pager_page_unswapped(vm_page_t m)
204 {
205 	pgo_pageunswapped_t *method;
206 
207 	method = pagertab[m->object->type]->pgo_pageunswapped;
208 	if (method != NULL)
209 		method(m);
210 }
211 
212 static __inline void
213 vm_pager_update_writecount(vm_object_t object, vm_offset_t start,
214     vm_offset_t end)
215 {
216 	pgo_writecount_t *method;
217 
218 	method = pagertab[object->type]->pgo_update_writecount;
219 	if (method != NULL)
220 		method(object, start, end);
221 }
222 
223 static __inline void
224 vm_pager_release_writecount(vm_object_t object, vm_offset_t start,
225     vm_offset_t end)
226 {
227 	pgo_writecount_t *method;
228 
229 	method = pagertab[object->type]->pgo_release_writecount;
230 	if (method != NULL)
231 		method(object, start, end);
232 }
233 
234 static __inline void
235 vm_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp)
236 {
237 	pgo_getvp_t *method;
238 
239 	*vpp = NULL;
240 	if (vp_heldp != NULL)
241 		*vp_heldp = false;
242 	method = pagertab[object->type]->pgo_getvp;
243 	if (method != NULL)
244 		method(object, vpp, vp_heldp);
245 }
246 
247 static __inline void
248 vm_pager_freespace(vm_object_t object, vm_pindex_t start,
249     vm_size_t size)
250 {
251 	pgo_freespace_t *method;
252 
253 	method = pagertab[object->type]->pgo_freespace;
254 	if (method != NULL)
255 		method(object, start, size);
256 }
257 
258 static __inline void
259 vm_pager_page_inserted(vm_object_t object, vm_page_t m)
260 {
261 	pgo_page_inserted_t *method;
262 
263 	method = pagertab[object->type]->pgo_page_inserted;
264 	if (method != NULL)
265 		method(object, m);
266 }
267 
268 static __inline void
269 vm_pager_page_removed(vm_object_t object, vm_page_t m)
270 {
271 	pgo_page_removed_t *method;
272 
273 	method = pagertab[object->type]->pgo_page_removed;
274 	if (method != NULL)
275 		method(object, m);
276 }
277 
278 static __inline bool
279 vm_pager_can_alloc_page(vm_object_t object, vm_pindex_t pindex)
280 {
281 	pgo_can_alloc_page_t *method;
282 
283 	method = pagertab[object->type]->pgo_can_alloc_page;
284 	return (method != NULL ? method(object, pindex) : true);
285 }
286 
287 int vm_pager_alloc_dyn_type(struct pagerops *ops, int base_type);
288 void vm_pager_free_dyn_type(objtype_t type);
289 
290 struct cdev_pager_ops {
291 	int (*cdev_pg_fault)(vm_object_t vm_obj, vm_ooffset_t offset,
292 	    int prot, vm_page_t *mres);
293 	int (*cdev_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx,
294 	    int fault_type, vm_prot_t max_prot, vm_pindex_t *first,
295 	    vm_pindex_t *last);
296 	int (*cdev_pg_ctor)(void *handle, vm_ooffset_t size, vm_prot_t prot,
297 	    vm_ooffset_t foff, struct ucred *cred, u_short *color);
298 	void (*cdev_pg_dtor)(void *handle);
299 };
300 
301 vm_object_t cdev_pager_allocate(void *handle, enum obj_type tp,
302     const struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
303     vm_ooffset_t foff, struct ucred *cred);
304 vm_object_t cdev_pager_lookup(void *handle);
305 void cdev_pager_free_page(vm_object_t object, vm_page_t m);
306 
307 struct phys_pager_ops {
308 	int (*phys_pg_getpages)(vm_object_t vm_obj, vm_page_t *m, int count,
309 	    int *rbehind, int *rahead);
310 	int (*phys_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx,
311 	    int fault_type, vm_prot_t max_prot, vm_pindex_t *first,
312 	    vm_pindex_t *last);
313 	boolean_t (*phys_pg_haspage)(vm_object_t obj,  vm_pindex_t pindex,
314 	    int *before, int *after);
315 	void (*phys_pg_ctor)(vm_object_t vm_obj, vm_prot_t prot,
316 	    vm_ooffset_t foff, struct ucred *cred);
317 	void (*phys_pg_dtor)(vm_object_t vm_obj);
318 };
319 extern const struct phys_pager_ops default_phys_pg_ops;
320 vm_object_t phys_pager_allocate(void *handle, const struct phys_pager_ops *ops,
321     void *data, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff,
322     struct ucred *cred);
323 
324 #endif				/* _KERNEL */
325 #endif				/* _VM_PAGER_ */
326