vm_pager.c revision 248084
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 248084 2013-03-09 02:32:23Z attilio $");
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#include <sys/rwlock.h>
78
79#include <vm/vm.h>
80#include <vm/vm_param.h>
81#include <vm/vm_object.h>
82#include <vm/vm_page.h>
83#include <vm/vm_pager.h>
84#include <vm/vm_extern.h>
85
86int cluster_pbuf_freecnt = -1;	/* unlimited to begin with */
87
88static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int);
89static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
90    vm_ooffset_t, struct ucred *);
91static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
92static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
93static void dead_pager_dealloc(vm_object_t);
94
95static int
96dead_pager_getpages(obj, ma, count, req)
97	vm_object_t obj;
98	vm_page_t *ma;
99	int count;
100	int req;
101{
102	return VM_PAGER_FAIL;
103}
104
105static vm_object_t
106dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
107    vm_ooffset_t off, struct ucred *cred)
108{
109	return NULL;
110}
111
112static void
113dead_pager_putpages(object, m, count, flags, rtvals)
114	vm_object_t object;
115	vm_page_t *m;
116	int count;
117	int flags;
118	int *rtvals;
119{
120	int i;
121
122	for (i = 0; i < count; i++) {
123		rtvals[i] = VM_PAGER_AGAIN;
124	}
125}
126
127static int
128dead_pager_haspage(object, pindex, prev, next)
129	vm_object_t object;
130	vm_pindex_t pindex;
131	int *prev;
132	int *next;
133{
134	if (prev)
135		*prev = 0;
136	if (next)
137		*next = 0;
138	return FALSE;
139}
140
141static void
142dead_pager_dealloc(object)
143	vm_object_t object;
144{
145	return;
146}
147
148static struct pagerops deadpagerops = {
149	.pgo_alloc = 	dead_pager_alloc,
150	.pgo_dealloc =	dead_pager_dealloc,
151	.pgo_getpages =	dead_pager_getpages,
152	.pgo_putpages =	dead_pager_putpages,
153	.pgo_haspage =	dead_pager_haspage,
154};
155
156struct pagerops *pagertab[] = {
157	&defaultpagerops,	/* OBJT_DEFAULT */
158	&swappagerops,		/* OBJT_SWAP */
159	&vnodepagerops,		/* OBJT_VNODE */
160	&devicepagerops,	/* OBJT_DEVICE */
161	&physpagerops,		/* OBJT_PHYS */
162	&deadpagerops,		/* OBJT_DEAD */
163	&sgpagerops,		/* OBJT_SG */
164	&mgtdevicepagerops,	/* OBJT_MGTDEVICE */
165};
166
167static const int npagers = sizeof(pagertab) / sizeof(pagertab[0]);
168
169/*
170 * Kernel address space for mapping pages.
171 * Used by pagers where KVAs are needed for IO.
172 *
173 * XXX needs to be large enough to support the number of pending async
174 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
175 * (MAXPHYS == 64k) if you want to get the most efficiency.
176 */
177vm_map_t pager_map;
178static int bswneeded;
179static vm_offset_t swapbkva;		/* swap buffers kva */
180struct mtx pbuf_mtx;
181static TAILQ_HEAD(swqueue, buf) bswlist;
182
183void
184vm_pager_init()
185{
186	struct pagerops **pgops;
187
188	TAILQ_INIT(&bswlist);
189	/*
190	 * Initialize known pagers
191	 */
192	for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
193		if ((*pgops)->pgo_init != NULL)
194			(*(*pgops)->pgo_init) ();
195}
196
197void
198vm_pager_bufferinit()
199{
200	struct buf *bp;
201	int i;
202
203	mtx_init(&pbuf_mtx, "pbuf mutex", NULL, MTX_DEF);
204	bp = swbuf;
205	/*
206	 * Now set up swap and physical I/O buffer headers.
207	 */
208	for (i = 0; i < nswbuf; i++, bp++) {
209		TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
210		BUF_LOCKINIT(bp);
211		LIST_INIT(&bp->b_dep);
212		bp->b_rcred = bp->b_wcred = NOCRED;
213		bp->b_xflags = 0;
214	}
215
216	cluster_pbuf_freecnt = nswbuf / 2;
217	vnode_pbuf_freecnt = nswbuf / 2 + 1;
218
219	swapbkva = kmem_alloc_nofault(pager_map, nswbuf * MAXPHYS);
220	if (!swapbkva)
221		panic("Not enough pager_map VM space for physical buffers");
222}
223
224/*
225 * Allocate an instance of a pager of the given type.
226 * Size, protection and offset parameters are passed in for pagers that
227 * need to perform page-level validation (e.g. the device pager).
228 */
229vm_object_t
230vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
231    vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
232{
233	vm_object_t ret;
234	struct pagerops *ops;
235
236	ops = pagertab[type];
237	if (ops)
238		ret = (*ops->pgo_alloc) (handle, size, prot, off, cred);
239	else
240		ret = NULL;
241	return (ret);
242}
243
244/*
245 *	The object must be locked.
246 */
247void
248vm_pager_deallocate(object)
249	vm_object_t object;
250{
251
252	VM_OBJECT_ASSERT_WLOCKED(object);
253	(*pagertab[object->type]->pgo_dealloc) (object);
254}
255
256/*
257 * vm_pager_get_pages() - inline, see vm/vm_pager.h
258 * vm_pager_put_pages() - inline, see vm/vm_pager.h
259 * vm_pager_has_page() - inline, see vm/vm_pager.h
260 */
261
262/*
263 * Search the specified pager object list for an object with the
264 * specified handle.  If an object with the specified handle is found,
265 * increase its reference count and return it.  Otherwise, return NULL.
266 *
267 * The pager object list must be locked.
268 */
269vm_object_t
270vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
271{
272	vm_object_t object;
273
274	TAILQ_FOREACH(object, pg_list, pager_object_list) {
275		if (object->handle == handle) {
276			VM_OBJECT_WLOCK(object);
277			if ((object->flags & OBJ_DEAD) == 0) {
278				vm_object_reference_locked(object);
279				VM_OBJECT_WUNLOCK(object);
280				break;
281			}
282			VM_OBJECT_WUNLOCK(object);
283		}
284	}
285	return (object);
286}
287
288/*
289 * initialize a physical buffer
290 */
291
292/*
293 * XXX This probably belongs in vfs_bio.c
294 */
295static void
296initpbuf(struct buf *bp)
297{
298	KASSERT(bp->b_bufobj == NULL, ("initpbuf with bufobj"));
299	KASSERT(bp->b_vp == NULL, ("initpbuf with vp"));
300	bp->b_rcred = NOCRED;
301	bp->b_wcred = NOCRED;
302	bp->b_qindex = 0;	/* On no queue (QUEUE_NONE) */
303	bp->b_saveaddr = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
304	bp->b_data = bp->b_saveaddr;
305	bp->b_kvabase = bp->b_saveaddr;
306	bp->b_kvasize = MAXPHYS;
307	bp->b_xflags = 0;
308	bp->b_flags = 0;
309	bp->b_ioflags = 0;
310	bp->b_iodone = NULL;
311	bp->b_error = 0;
312	BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
313}
314
315/*
316 * allocate a physical buffer
317 *
318 *	There are a limited number (nswbuf) of physical buffers.  We need
319 *	to make sure that no single subsystem is able to hog all of them,
320 *	so each subsystem implements a counter which is typically initialized
321 *	to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
322 *	increments it on release, and blocks if the counter hits zero.  A
323 *	subsystem may initialize the counter to -1 to disable the feature,
324 *	but it must still be sure to match up all uses of getpbuf() with
325 *	relpbuf() using the same variable.
326 *
327 *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
328 *	relatively soon when the rest of the subsystems get smart about it. XXX
329 */
330struct buf *
331getpbuf(int *pfreecnt)
332{
333	struct buf *bp;
334
335	mtx_lock(&pbuf_mtx);
336
337	for (;;) {
338		if (pfreecnt) {
339			while (*pfreecnt == 0) {
340				msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0);
341			}
342		}
343
344		/* get a bp from the swap buffer header pool */
345		if ((bp = TAILQ_FIRST(&bswlist)) != NULL)
346			break;
347
348		bswneeded = 1;
349		msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0);
350		/* loop in case someone else grabbed one */
351	}
352	TAILQ_REMOVE(&bswlist, bp, b_freelist);
353	if (pfreecnt)
354		--*pfreecnt;
355	mtx_unlock(&pbuf_mtx);
356
357	initpbuf(bp);
358	return bp;
359}
360
361/*
362 * allocate a physical buffer, if one is available.
363 *
364 *	Note that there is no NULL hack here - all subsystems using this
365 *	call understand how to use pfreecnt.
366 */
367struct buf *
368trypbuf(int *pfreecnt)
369{
370	struct buf *bp;
371
372	mtx_lock(&pbuf_mtx);
373	if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) {
374		mtx_unlock(&pbuf_mtx);
375		return NULL;
376	}
377	TAILQ_REMOVE(&bswlist, bp, b_freelist);
378
379	--*pfreecnt;
380
381	mtx_unlock(&pbuf_mtx);
382
383	initpbuf(bp);
384
385	return bp;
386}
387
388/*
389 * release a physical buffer
390 *
391 *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
392 *	relatively soon when the rest of the subsystems get smart about it. XXX
393 */
394void
395relpbuf(struct buf *bp, int *pfreecnt)
396{
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	KASSERT(bp->b_vp == NULL, ("relpbuf with vp"));
408	KASSERT(bp->b_bufobj == NULL, ("relpbuf with bufobj"));
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}
425
426/*
427 * Associate a p-buffer with a vnode.
428 *
429 * Also sets B_PAGING flag to indicate that vnode is not fully associated
430 * with the buffer.  i.e. the bp has not been linked into the vnode or
431 * ref-counted.
432 */
433void
434pbgetvp(struct vnode *vp, struct buf *bp)
435{
436
437	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
438	KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
439
440	bp->b_vp = vp;
441	bp->b_flags |= B_PAGING;
442	bp->b_bufobj = &vp->v_bufobj;
443}
444
445/*
446 * Associate a p-buffer with a vnode.
447 *
448 * Also sets B_PAGING flag to indicate that vnode is not fully associated
449 * with the buffer.  i.e. the bp has not been linked into the vnode or
450 * ref-counted.
451 */
452void
453pbgetbo(struct bufobj *bo, struct buf *bp)
454{
455
456	KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
457	KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
458
459	bp->b_flags |= B_PAGING;
460	bp->b_bufobj = bo;
461}
462
463/*
464 * Disassociate a p-buffer from a vnode.
465 */
466void
467pbrelvp(struct buf *bp)
468{
469
470	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
471	KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
472
473	/* XXX REMOVE ME */
474	BO_LOCK(bp->b_bufobj);
475	if (TAILQ_NEXT(bp, b_bobufs) != NULL) {
476		panic(
477		    "relpbuf(): b_vp was probably reassignbuf()d %p %x",
478		    bp,
479		    (int)bp->b_flags
480		);
481	}
482	BO_UNLOCK(bp->b_bufobj);
483	bp->b_vp = NULL;
484	bp->b_bufobj = NULL;
485	bp->b_flags &= ~B_PAGING;
486}
487
488/*
489 * Disassociate a p-buffer from a bufobj.
490 */
491void
492pbrelbo(struct buf *bp)
493{
494
495	KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
496	KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
497
498	/* XXX REMOVE ME */
499	BO_LOCK(bp->b_bufobj);
500	if (TAILQ_NEXT(bp, b_bobufs) != NULL) {
501		panic(
502		    "relpbuf(): b_vp was probably reassignbuf()d %p %x",
503		    bp,
504		    (int)bp->b_flags
505		);
506	}
507	BO_UNLOCK(bp->b_bufobj);
508	bp->b_bufobj = NULL;
509	bp->b_flags &= ~B_PAGING;
510}
511