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