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