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