xref: /freebsd/sys/vm/vm_pager.h (revision c6989859ae9388eeb46a24fe88f9b8d07101c710)
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 vm_object_t vm_pager_allocate(objtype_t, void *, vm_ooffset_t, vm_prot_t,
116     vm_ooffset_t, struct ucred *);
117 void vm_pager_bufferinit(void);
118 void vm_pager_deallocate(vm_object_t);
119 int vm_pager_get_pages(vm_object_t, vm_page_t *, int, int *, int *);
120 int vm_pager_get_pages_async(vm_object_t, vm_page_t *, int, int *, int *,
121     pgo_getpages_iodone_t, void *);
122 void vm_pager_init(void);
123 vm_object_t vm_pager_object_lookup(struct pagerlst *, void *);
124 
125 static __inline void
126 vm_pager_put_pages(
127 	vm_object_t object,
128 	vm_page_t *m,
129 	int count,
130 	int flags,
131 	int *rtvals
132 ) {
133 	VM_OBJECT_ASSERT_WLOCKED(object);
134 	(*pagertab[object->type]->pgo_putpages)
135 	    (object, m, count, flags, rtvals);
136 }
137 
138 /*
139  *	vm_pager_haspage
140  *
141  *	Check to see if an object's pager has the requested page.  The
142  *	object's pager will also set before and after to give the caller
143  *	some idea of the number of pages before and after the requested
144  *	page can be I/O'd efficiently.
145  *
146  *	The object must be locked.
147  */
148 static __inline boolean_t
149 vm_pager_has_page(
150 	vm_object_t object,
151 	vm_pindex_t offset,
152 	int *before,
153 	int *after
154 ) {
155 	boolean_t ret;
156 
157 	VM_OBJECT_ASSERT_LOCKED(object);
158 	ret = (*pagertab[object->type]->pgo_haspage)
159 	    (object, offset, before, after);
160 	return (ret);
161 }
162 
163 static __inline int
164 vm_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
165     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
166 {
167 
168 	MPASS((object->flags & OBJ_POPULATE) != 0);
169 	MPASS(pidx < object->size);
170 	MPASS(blockcount_read(&object->paging_in_progress) > 0);
171 	return ((*pagertab[object->type]->pgo_populate)(object, pidx,
172 	    fault_type, max_prot, first, last));
173 }
174 
175 /*
176  *      vm_pager_page_unswapped
177  *
178  *	Destroy swap associated with the page.
179  *
180  *	XXX: A much better name would be "vm_pager_page_dirtied()"
181  *	XXX: It is not obvious if this could be profitably used by any
182  *	XXX: pagers besides the swap_pager or if it should even be a
183  *	XXX: generic pager_op in the first place.
184  */
185 static __inline void
186 vm_pager_page_unswapped(vm_page_t m)
187 {
188 
189 	if (pagertab[m->object->type]->pgo_pageunswapped)
190 		(*pagertab[m->object->type]->pgo_pageunswapped)(m);
191 }
192 
193 static __inline void
194 vm_pager_update_writecount(vm_object_t object, vm_offset_t start,
195     vm_offset_t end)
196 {
197 
198 	if (pagertab[object->type]->pgo_update_writecount)
199 		pagertab[object->type]->pgo_update_writecount(object, start,
200 		    end);
201 }
202 
203 static __inline void
204 vm_pager_release_writecount(vm_object_t object, vm_offset_t start,
205     vm_offset_t end)
206 {
207 
208 	if (pagertab[object->type]->pgo_release_writecount)
209 		pagertab[object->type]->pgo_release_writecount(object, start,
210 		    end);
211 }
212 
213 struct cdev_pager_ops {
214 	int (*cdev_pg_fault)(vm_object_t vm_obj, vm_ooffset_t offset,
215 	    int prot, vm_page_t *mres);
216 	int (*cdev_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx,
217 	    int fault_type, vm_prot_t max_prot, vm_pindex_t *first,
218 	    vm_pindex_t *last);
219 	int (*cdev_pg_ctor)(void *handle, vm_ooffset_t size, vm_prot_t prot,
220 	    vm_ooffset_t foff, struct ucred *cred, u_short *color);
221 	void (*cdev_pg_dtor)(void *handle);
222 };
223 
224 vm_object_t cdev_pager_allocate(void *handle, enum obj_type tp,
225     struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
226     vm_ooffset_t foff, struct ucred *cred);
227 vm_object_t cdev_pager_lookup(void *handle);
228 void cdev_pager_free_page(vm_object_t object, vm_page_t m);
229 
230 struct phys_pager_ops {
231 	int (*phys_pg_getpages)(vm_object_t vm_obj, vm_page_t *m, int count,
232 	    int *rbehind, int *rahead);
233 	int (*phys_pg_populate)(vm_object_t vm_obj, vm_pindex_t pidx,
234 	    int fault_type, vm_prot_t max_prot, vm_pindex_t *first,
235 	    vm_pindex_t *last);
236 	boolean_t (*phys_pg_haspage)(vm_object_t obj,  vm_pindex_t pindex,
237 	    int *before, int *after);
238 	void (*phys_pg_ctor)(vm_object_t vm_obj, vm_prot_t prot,
239 	    vm_ooffset_t foff, struct ucred *cred);
240 	void (*phys_pg_dtor)(vm_object_t vm_obj);
241 };
242 extern struct phys_pager_ops default_phys_pg_ops;
243 vm_object_t phys_pager_allocate(void *handle, struct phys_pager_ops *ops,
244     void *data, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff,
245     struct ucred *cred);
246 
247 #endif				/* _KERNEL */
248 #endif				/* _VM_PAGER_ */
249