xref: /freebsd/sys/vm/vm_pager.h (revision a0409676120c1e558d0ade943019934e0f15118d)
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/queue.h>
48 
49 TAILQ_HEAD(pagerlst, vm_object);
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 
66 struct pagerops {
67 	pgo_init_t		*pgo_init;		/* Initialize pager. */
68 	pgo_alloc_t		*pgo_alloc;		/* Allocate pager. */
69 	pgo_dealloc_t		*pgo_dealloc;		/* Disassociate. */
70 	pgo_getpages_t		*pgo_getpages;		/* Get (read) page. */
71 	pgo_getpages_async_t	*pgo_getpages_async;	/* Get page asyncly. */
72 	pgo_putpages_t		*pgo_putpages;		/* Put (write) page. */
73 	pgo_haspage_t		*pgo_haspage;		/* Query page. */
74 	pgo_populate_t		*pgo_populate;		/* Bulk spec pagein. */
75 	pgo_pageunswapped_t	*pgo_pageunswapped;
76 	/* Operations for specialized writecount handling */
77 	pgo_writecount_t	*pgo_update_writecount;
78 	pgo_writecount_t	*pgo_release_writecount;
79 };
80 
81 extern struct pagerops defaultpagerops;
82 extern struct pagerops swappagerops;
83 extern struct pagerops vnodepagerops;
84 extern struct pagerops devicepagerops;
85 extern struct pagerops physpagerops;
86 extern struct pagerops sgpagerops;
87 extern struct pagerops mgtdevicepagerops;
88 
89 /*
90  * get/put return values
91  * OK	 operation was successful
92  * BAD	 specified data was out of the accepted range
93  * FAIL	 specified data was in range, but doesn't exist
94  * PEND	 operations was initiated but not completed
95  * ERROR error while accessing data that is in range and exists
96  * AGAIN temporary resource shortage prevented operation from happening
97  */
98 #define	VM_PAGER_OK	0
99 #define	VM_PAGER_BAD	1
100 #define	VM_PAGER_FAIL	2
101 #define	VM_PAGER_PEND	3
102 #define	VM_PAGER_ERROR	4
103 #define VM_PAGER_AGAIN	5
104 
105 #define	VM_PAGER_PUT_SYNC		0x0001
106 #define	VM_PAGER_PUT_INVAL		0x0002
107 #define	VM_PAGER_PUT_NOREUSE		0x0004
108 #define VM_PAGER_CLUSTER_OK		0x0008
109 
110 #ifdef _KERNEL
111 
112 extern struct pagerops *pagertab[];
113 extern struct mtx_padalign pbuf_mtx;
114 
115 /*
116  * Number of pages that pbuf buffer can store in b_pages.
117  * It is +1 to allow for unaligned data buffer of maxphys size.
118  */
119 #define	PBUF_PAGES	(atop(maxphys) + 1)
120 
121 vm_object_t vm_pager_allocate(objtype_t, void *, vm_ooffset_t, vm_prot_t,
122     vm_ooffset_t, struct ucred *);
123 void vm_pager_bufferinit(void);
124 void vm_pager_deallocate(vm_object_t);
125 int vm_pager_get_pages(vm_object_t, vm_page_t *, int, int *, int *);
126 int vm_pager_get_pages_async(vm_object_t, vm_page_t *, int, int *, int *,
127     pgo_getpages_iodone_t, void *);
128 void vm_pager_init(void);
129 vm_object_t vm_pager_object_lookup(struct pagerlst *, void *);
130 
131 static __inline void
132 vm_pager_put_pages(
133 	vm_object_t object,
134 	vm_page_t *m,
135 	int count,
136 	int flags,
137 	int *rtvals
138 ) {
139 	VM_OBJECT_ASSERT_WLOCKED(object);
140 	(*pagertab[object->type]->pgo_putpages)
141 	    (object, m, count, flags, rtvals);
142 }
143 
144 /*
145  *	vm_pager_haspage
146  *
147  *	Check to see if an object's pager has the requested page.  The
148  *	object's pager will also set before and after to give the caller
149  *	some idea of the number of pages before and after the requested
150  *	page can be I/O'd efficiently.
151  *
152  *	The object must be locked.
153  */
154 static __inline boolean_t
155 vm_pager_has_page(
156 	vm_object_t object,
157 	vm_pindex_t offset,
158 	int *before,
159 	int *after
160 ) {
161 	boolean_t ret;
162 
163 	VM_OBJECT_ASSERT_LOCKED(object);
164 	ret = (*pagertab[object->type]->pgo_haspage)
165 	    (object, offset, before, after);
166 	return (ret);
167 }
168 
169 static __inline int
170 vm_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
171     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
172 {
173 
174 	MPASS((object->flags & OBJ_POPULATE) != 0);
175 	MPASS(pidx < object->size);
176 	MPASS(blockcount_read(&object->paging_in_progress) > 0);
177 	return ((*pagertab[object->type]->pgo_populate)(object, pidx,
178 	    fault_type, max_prot, first, last));
179 }
180 
181 /*
182  *      vm_pager_page_unswapped
183  *
184  *	Destroy swap associated with the page.
185  *
186  *	XXX: A much better name would be "vm_pager_page_dirtied()"
187  *	XXX: It is not obvious if this could be profitably used by any
188  *	XXX: pagers besides the swap_pager or if it should even be a
189  *	XXX: generic pager_op in the first place.
190  */
191 static __inline void
192 vm_pager_page_unswapped(vm_page_t m)
193 {
194 
195 	if (pagertab[m->object->type]->pgo_pageunswapped)
196 		(*pagertab[m->object->type]->pgo_pageunswapped)(m);
197 }
198 
199 static __inline void
200 vm_pager_update_writecount(vm_object_t object, vm_offset_t start,
201     vm_offset_t end)
202 {
203 
204 	if (pagertab[object->type]->pgo_update_writecount)
205 		pagertab[object->type]->pgo_update_writecount(object, start,
206 		    end);
207 }
208 
209 static __inline void
210 vm_pager_release_writecount(vm_object_t object, vm_offset_t start,
211     vm_offset_t end)
212 {
213 
214 	if (pagertab[object->type]->pgo_release_writecount)
215 		pagertab[object->type]->pgo_release_writecount(object, start,
216 		    end);
217 }
218 
219 struct cdev_pager_ops {
220 	int (*cdev_pg_fault)(vm_object_t vm_obj, vm_ooffset_t offset,
221 	    int prot, vm_page_t *mres);
222 	int (*cdev_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx,
223 	    int fault_type, vm_prot_t max_prot, vm_pindex_t *first,
224 	    vm_pindex_t *last);
225 	int (*cdev_pg_ctor)(void *handle, vm_ooffset_t size, vm_prot_t prot,
226 	    vm_ooffset_t foff, struct ucred *cred, u_short *color);
227 	void (*cdev_pg_dtor)(void *handle);
228 };
229 
230 vm_object_t cdev_pager_allocate(void *handle, enum obj_type tp,
231     struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
232     vm_ooffset_t foff, struct ucred *cred);
233 vm_object_t cdev_pager_lookup(void *handle);
234 void cdev_pager_free_page(vm_object_t object, vm_page_t m);
235 
236 struct phys_pager_ops {
237 	int (*phys_pg_getpages)(vm_object_t vm_obj, vm_page_t *m, int count,
238 	    int *rbehind, int *rahead);
239 	int (*phys_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx,
240 	    int fault_type, vm_prot_t max_prot, vm_pindex_t *first,
241 	    vm_pindex_t *last);
242 	boolean_t (*phys_pg_haspage)(vm_object_t obj,  vm_pindex_t pindex,
243 	    int *before, int *after);
244 	void (*phys_pg_ctor)(vm_object_t vm_obj, vm_prot_t prot,
245 	    vm_ooffset_t foff, struct ucred *cred);
246 	void (*phys_pg_dtor)(vm_object_t vm_obj);
247 };
248 extern struct phys_pager_ops default_phys_pg_ops;
249 vm_object_t phys_pager_allocate(void *handle, struct phys_pager_ops *ops,
250     void *data, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff,
251     struct ucred *cred);
252 
253 #endif				/* _KERNEL */
254 #endif				/* _VM_PAGER_ */
255