vm_pager.c revision 137197
1/*
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	from: @(#)vm_pager.c	8.6 (Berkeley) 1/12/94
33 *
34 *
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
37 *
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39 *
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
45 *
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 *
50 * Carnegie Mellon requests users of this software to return to
51 *
52 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53 *  School of Computer Science
54 *  Carnegie Mellon University
55 *  Pittsburgh PA 15213-3890
56 *
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
59 */
60
61/*
62 *	Paging space routine stubs.  Emulates a matchmaker-like interface
63 *	for builtin pagers.
64 */
65
66#include <sys/cdefs.h>
67__FBSDID("$FreeBSD: head/sys/vm/vm_pager.c 137197 2004-11-04 09:48:18Z phk $");
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/kernel.h>
72#include <sys/vnode.h>
73#include <sys/bio.h>
74#include <sys/buf.h>
75#include <sys/ucred.h>
76#include <sys/malloc.h>
77
78#include <vm/vm.h>
79#include <vm/vm_param.h>
80#include <vm/vm_object.h>
81#include <vm/vm_page.h>
82#include <vm/vm_pager.h>
83#include <vm/vm_extern.h>
84
85MALLOC_DEFINE(M_VMPGDATA, "VM pgdata", "XXX: VM pager private data");
86
87int cluster_pbuf_freecnt = -1;	/* unlimited to begin with */
88
89static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int);
90static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
91	vm_ooffset_t);
92static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
93static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
94static void dead_pager_dealloc(vm_object_t);
95
96static int
97dead_pager_getpages(obj, ma, count, req)
98	vm_object_t obj;
99	vm_page_t *ma;
100	int count;
101	int req;
102{
103	return VM_PAGER_FAIL;
104}
105
106static vm_object_t
107dead_pager_alloc(handle, size, prot, off)
108	void *handle;
109	vm_ooffset_t size;
110	vm_prot_t prot;
111	vm_ooffset_t off;
112{
113	return NULL;
114}
115
116static void
117dead_pager_putpages(object, m, count, flags, rtvals)
118	vm_object_t object;
119	vm_page_t *m;
120	int count;
121	int flags;
122	int *rtvals;
123{
124	int i;
125
126	for (i = 0; i < count; i++) {
127		rtvals[i] = VM_PAGER_AGAIN;
128	}
129}
130
131static int
132dead_pager_haspage(object, pindex, prev, next)
133	vm_object_t object;
134	vm_pindex_t pindex;
135	int *prev;
136	int *next;
137{
138	if (prev)
139		*prev = 0;
140	if (next)
141		*next = 0;
142	return FALSE;
143}
144
145static void
146dead_pager_dealloc(object)
147	vm_object_t object;
148{
149	return;
150}
151
152static struct pagerops deadpagerops = {
153	.pgo_alloc = 	dead_pager_alloc,
154	.pgo_dealloc =	dead_pager_dealloc,
155	.pgo_getpages =	dead_pager_getpages,
156	.pgo_putpages =	dead_pager_putpages,
157	.pgo_haspage =	dead_pager_haspage,
158};
159
160struct pagerops *pagertab[] = {
161	&defaultpagerops,	/* OBJT_DEFAULT */
162	&swappagerops,		/* OBJT_SWAP */
163	&vnodepagerops,		/* OBJT_VNODE */
164	&devicepagerops,	/* OBJT_DEVICE */
165	&physpagerops,		/* OBJT_PHYS */
166	&deadpagerops		/* OBJT_DEAD */
167};
168
169int npagers = sizeof(pagertab) / sizeof(pagertab[0]);
170
171/*
172 * Kernel address space for mapping pages.
173 * Used by pagers where KVAs are needed for IO.
174 *
175 * XXX needs to be large enough to support the number of pending async
176 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
177 * (MAXPHYS == 64k) if you want to get the most efficiency.
178 */
179vm_map_t pager_map;
180static int bswneeded;
181static vm_offset_t swapbkva;		/* swap buffers kva */
182struct mtx pbuf_mtx;
183static TAILQ_HEAD(swqueue, buf) bswlist;
184
185void
186vm_pager_init()
187{
188	struct pagerops **pgops;
189
190	TAILQ_INIT(&bswlist);
191	/*
192	 * Initialize known pagers
193	 */
194	for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
195		if (pgops && ((*pgops)->pgo_init != NULL))
196			(*(*pgops)->pgo_init) ();
197}
198
199void
200vm_pager_bufferinit()
201{
202	struct buf *bp;
203	int i;
204
205	mtx_init(&pbuf_mtx, "pbuf mutex", NULL, MTX_DEF);
206	bp = swbuf;
207	/*
208	 * Now set up swap and physical I/O buffer headers.
209	 */
210	for (i = 0; i < nswbuf; i++, bp++) {
211		TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
212		BUF_LOCKINIT(bp);
213		LIST_INIT(&bp->b_dep);
214		bp->b_rcred = bp->b_wcred = NOCRED;
215		bp->b_xflags = 0;
216	}
217
218	cluster_pbuf_freecnt = nswbuf / 2;
219
220	swapbkva = kmem_alloc_nofault(pager_map, nswbuf * MAXPHYS);
221	if (!swapbkva)
222		panic("Not enough pager_map VM space for physical buffers");
223}
224
225/*
226 * Allocate an instance of a pager of the given type.
227 * Size, protection and offset parameters are passed in for pagers that
228 * need to perform page-level validation (e.g. the device pager).
229 */
230vm_object_t
231vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
232		  vm_prot_t prot, vm_ooffset_t off)
233{
234	vm_object_t ret;
235	struct pagerops *ops;
236
237	ops = pagertab[type];
238	if (ops)
239		ret = (*ops->pgo_alloc) (handle, size, prot, off);
240	else
241		ret = NULL;
242	return (ret);
243}
244
245/*
246 *	The object must be locked.
247 */
248void
249vm_pager_deallocate(object)
250	vm_object_t object;
251{
252
253	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
254	(*pagertab[object->type]->pgo_dealloc) (object);
255}
256
257/*
258 * vm_pager_get_pages() - inline, see vm/vm_pager.h
259 * vm_pager_put_pages() - inline, see vm/vm_pager.h
260 * vm_pager_has_page() - inline, see vm/vm_pager.h
261 */
262
263vm_object_t
264vm_pager_object_lookup(pg_list, handle)
265	struct pagerlst *pg_list;
266	void *handle;
267{
268	vm_object_t object;
269
270	TAILQ_FOREACH(object, pg_list, pager_object_list)
271		if (object->handle == handle)
272			return (object);
273	return (NULL);
274}
275
276/*
277 * initialize a physical buffer
278 */
279
280/*
281 * XXX This probably belongs in vfs_bio.c
282 */
283static void
284initpbuf(struct buf *bp)
285{
286	bp->b_rcred = NOCRED;
287	bp->b_wcred = NOCRED;
288	bp->b_qindex = 0;	/* On no queue (QUEUE_NONE) */
289	bp->b_saveaddr = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
290	bp->b_data = bp->b_saveaddr;
291	bp->b_kvabase = bp->b_saveaddr;
292	bp->b_kvasize = MAXPHYS;
293	bp->b_xflags = 0;
294	bp->b_flags = 0;
295	bp->b_ioflags = 0;
296	bp->b_iodone = NULL;
297	bp->b_error = 0;
298	BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
299}
300
301/*
302 * allocate a physical buffer
303 *
304 *	There are a limited number (nswbuf) of physical buffers.  We need
305 *	to make sure that no single subsystem is able to hog all of them,
306 *	so each subsystem implements a counter which is typically initialized
307 *	to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
308 *	increments it on release, and blocks if the counter hits zero.  A
309 *	subsystem may initialize the counter to -1 to disable the feature,
310 *	but it must still be sure to match up all uses of getpbuf() with
311 *	relpbuf() using the same variable.
312 *
313 *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
314 *	relatively soon when the rest of the subsystems get smart about it. XXX
315 */
316struct buf *
317getpbuf(pfreecnt)
318	int *pfreecnt;
319{
320	int s;
321	struct buf *bp;
322
323	s = splvm();
324	mtx_lock(&pbuf_mtx);
325
326	for (;;) {
327		if (pfreecnt) {
328			while (*pfreecnt == 0) {
329				msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0);
330			}
331		}
332
333		/* get a bp from the swap buffer header pool */
334		if ((bp = TAILQ_FIRST(&bswlist)) != NULL)
335			break;
336
337		bswneeded = 1;
338		msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0);
339		/* loop in case someone else grabbed one */
340	}
341	TAILQ_REMOVE(&bswlist, bp, b_freelist);
342	if (pfreecnt)
343		--*pfreecnt;
344	mtx_unlock(&pbuf_mtx);
345	splx(s);
346
347	initpbuf(bp);
348	return bp;
349}
350
351/*
352 * allocate a physical buffer, if one is available.
353 *
354 *	Note that there is no NULL hack here - all subsystems using this
355 *	call understand how to use pfreecnt.
356 */
357struct buf *
358trypbuf(pfreecnt)
359	int *pfreecnt;
360{
361	int s;
362	struct buf *bp;
363
364	s = splvm();
365	mtx_lock(&pbuf_mtx);
366	if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) {
367		mtx_unlock(&pbuf_mtx);
368		splx(s);
369		return NULL;
370	}
371	TAILQ_REMOVE(&bswlist, bp, b_freelist);
372
373	--*pfreecnt;
374
375	mtx_unlock(&pbuf_mtx);
376	splx(s);
377
378	initpbuf(bp);
379
380	return bp;
381}
382
383/*
384 * release a physical buffer
385 *
386 *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
387 *	relatively soon when the rest of the subsystems get smart about it. XXX
388 */
389void
390relpbuf(bp, pfreecnt)
391	struct buf *bp;
392	int *pfreecnt;
393{
394	int s;
395
396	s = splvm();
397
398	if (bp->b_rcred != NOCRED) {
399		crfree(bp->b_rcred);
400		bp->b_rcred = NOCRED;
401	}
402	if (bp->b_wcred != NOCRED) {
403		crfree(bp->b_wcred);
404		bp->b_wcred = NOCRED;
405	}
406
407	if (bp->b_vp)
408		pbrelvp(bp);
409
410	BUF_UNLOCK(bp);
411
412	mtx_lock(&pbuf_mtx);
413	TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
414
415	if (bswneeded) {
416		bswneeded = 0;
417		wakeup(&bswneeded);
418	}
419	if (pfreecnt) {
420		if (++*pfreecnt == 1)
421			wakeup(pfreecnt);
422	}
423	mtx_unlock(&pbuf_mtx);
424	splx(s);
425}
426