kernel - Add many sysctl definitions, sysv, vfs, nfs, etc.
[dragonfly.git] / sys / vm / vm_pager.c
CommitLineData
984263bc 1/*
99ad9bc4
MD
2 * (MPSAFE)
3 *
984263bc
MD
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: @(#)vm_pager.c 8.6 (Berkeley) 1/12/94
39 *
40 *
41 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42 * All rights reserved.
43 *
44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
45 *
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
65 *
66 * $FreeBSD: src/sys/vm/vm_pager.c,v 1.54.2.2 2001/11/18 07:11:00 dillon Exp $
408357d8 67 * $DragonFly: src/sys/vm/vm_pager.c,v 1.24 2007/11/06 03:50:01 dillon Exp $
984263bc
MD
68 */
69
70/*
71 * Paging space routine stubs. Emulates a matchmaker-like interface
72 * for builtin pagers.
73 */
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/kernel.h>
78#include <sys/vnode.h>
79#include <sys/buf.h>
80#include <sys/ucred.h>
81#include <sys/malloc.h>
8c72e3d5 82#include <sys/dsched.h>
984263bc 83#include <sys/proc.h>
9a82e536 84#include <sys/sysctl.h>
cdd46d2e 85#include <sys/thread2.h>
984263bc
MD
86
87#include <vm/vm.h>
88#include <vm/vm_param.h>
9a82e536 89#include <vm/vm_kern.h>
984263bc
MD
90#include <vm/vm_object.h>
91#include <vm/vm_page.h>
92#include <vm/vm_pager.h>
93#include <vm/vm_extern.h>
94
3020e3be
MD
95#include <sys/buf2.h>
96
984263bc
MD
97MALLOC_DEFINE(M_VMPGDATA, "VM pgdata", "XXX: VM pager private data");
98
99extern struct pagerops defaultpagerops;
100extern struct pagerops swappagerops;
101extern struct pagerops vnodepagerops;
102extern struct pagerops devicepagerops;
103extern struct pagerops physpagerops;
104
105int cluster_pbuf_freecnt = -1; /* unlimited to begin with */
106
1b9d3514 107static int dead_pager_getpage (vm_object_t, vm_page_t *, int);
1388df65 108static void dead_pager_putpages (vm_object_t, vm_page_t *, int, int, int *);
1b9d3514 109static boolean_t dead_pager_haspage (vm_object_t, vm_pindex_t);
1388df65 110static void dead_pager_dealloc (vm_object_t);
984263bc 111
99ad9bc4
MD
112/*
113 * No requirements.
114 */
984263bc 115static int
1b9d3514 116dead_pager_getpage(vm_object_t obj, vm_page_t *mpp, int seqaccess)
984263bc
MD
117{
118 return VM_PAGER_FAIL;
119}
120
99ad9bc4
MD
121/*
122 * No requirements.
123 */
984263bc 124static void
57e43348 125dead_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
5a648714 126 int *rtvals)
984263bc
MD
127{
128 int i;
129
130 for (i = 0; i < count; i++) {
131 rtvals[i] = VM_PAGER_AGAIN;
132 }
133}
134
99ad9bc4
MD
135/*
136 * No requirements.
137 */
984263bc 138static int
1b9d3514 139dead_pager_haspage(vm_object_t object, vm_pindex_t pindex)
984263bc 140{
984263bc
MD
141 return FALSE;
142}
143
99ad9bc4
MD
144/*
145 * No requirements.
146 */
984263bc 147static void
57e43348 148dead_pager_dealloc(vm_object_t object)
984263bc 149{
8d292090 150 KKASSERT(object->swblock_count == 0);
984263bc
MD
151 return;
152}
153
154static struct pagerops deadpagerops = {
984263bc 155 dead_pager_dealloc,
1b9d3514 156 dead_pager_getpage,
984263bc 157 dead_pager_putpages,
107e9bcc 158 dead_pager_haspage
984263bc
MD
159};
160
161struct pagerops *pagertab[] = {
162 &defaultpagerops, /* OBJT_DEFAULT */
163 &swappagerops, /* OBJT_SWAP */
164 &vnodepagerops, /* OBJT_VNODE */
165 &devicepagerops, /* OBJT_DEVICE */
166 &physpagerops, /* OBJT_PHYS */
167 &deadpagerops /* OBJT_DEAD */
168};
169
170int npagers = sizeof(pagertab) / sizeof(pagertab[0]);
171
172/*
173 * Kernel address space for mapping pages.
174 * Used by pagers where KVAs are needed for IO.
175 *
176 * XXX needs to be large enough to support the number of pending async
177 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
178 * (MAXPHYS == 64k) if you want to get the most efficiency.
179 */
180#define PAGER_MAP_SIZE (8 * 1024 * 1024)
181
9a82e536
MD
182TAILQ_HEAD(swqueue, buf);
183
984263bc 184int pager_map_size = PAGER_MAP_SIZE;
e4846942
MD
185struct vm_map pager_map;
186
9a82e536
MD
187static int bswneeded_raw;
188static int bswneeded_kva;
189static int nswbuf_raw;
190static struct buf *swbuf_raw;
984263bc 191static vm_offset_t swapbkva; /* swap buffers kva */
9a82e536
MD
192static struct swqueue bswlist_raw; /* without kva */
193static struct swqueue bswlist_kva; /* with kva */
a50fe973 194static struct spinlock bswspin = SPINLOCK_INITIALIZER(&bswspin);
9a82e536
MD
195static int pbuf_raw_count;
196static int pbuf_kva_count;
197
093e85dc
SG
198SYSCTL_INT(_vfs, OID_AUTO, pbuf_raw_count, CTLFLAG_RD, &pbuf_raw_count, 0,
199 "Kernel virtual address space reservations");
200SYSCTL_INT(_vfs, OID_AUTO, pbuf_kva_count, CTLFLAG_RD, &pbuf_kva_count, 0,
201 "Kernel raw address space reservations");
984263bc 202
99ad9bc4
MD
203/*
204 * Initialize the swap buffer list.
205 *
206 * Called from the low level boot code only.
207 */
107e9bcc
MD
208static void
209vm_pager_init(void *arg __unused)
984263bc 210{
9a82e536
MD
211 TAILQ_INIT(&bswlist_raw);
212 TAILQ_INIT(&bswlist_kva);
984263bc 213}
107e9bcc 214SYSINIT(vm_mem, SI_BOOT1_VM, SI_ORDER_SECOND, vm_pager_init, NULL)
984263bc 215
99ad9bc4
MD
216/*
217 * Called from the low level boot code only.
218 */
984263bc 219void
57e43348 220vm_pager_bufferinit(void)
984263bc
MD
221{
222 struct buf *bp;
223 int i;
224
984263bc 225 /*
312dcd01
MD
226 * Reserve KVM space for pbuf data.
227 */
e4846942 228 swapbkva = kmem_alloc_pageable(&pager_map, nswbuf * MAXPHYS);
312dcd01
MD
229 if (!swapbkva)
230 panic("Not enough pager_map VM space for physical buffers");
231
232 /*
9a82e536 233 * Initial pbuf setup. These pbufs have KVA reservations.
984263bc 234 */
312dcd01
MD
235 bp = swbuf;
236 for (i = 0; i < nswbuf; ++i, ++bp) {
973c11b9 237 bp->b_kvabase = (caddr_t)((intptr_t)i * MAXPHYS) + swapbkva;
312dcd01 238 bp->b_kvasize = MAXPHYS;
984263bc 239 BUF_LOCKINIT(bp);
408357d8 240 buf_dep_init(bp);
9a82e536
MD
241 TAILQ_INSERT_HEAD(&bswlist_kva, bp, b_freelist);
242 ++pbuf_kva_count;
243 }
244
245 /*
246 * Initial pbuf setup. These pbufs do not have KVA reservations,
247 * so we can have a lot more of them. These are typically used
248 * to massage low level buf/bio requests.
249 */
250 nswbuf_raw = nbuf * 2;
251 swbuf_raw = (void *)kmem_alloc(&kernel_map,
252 round_page(nswbuf_raw * sizeof(struct buf)));
253 bp = swbuf_raw;
254 for (i = 0; i < nswbuf_raw; ++i, ++bp) {
255 BUF_LOCKINIT(bp);
256 buf_dep_init(bp);
257 TAILQ_INSERT_HEAD(&bswlist_raw, bp, b_freelist);
258 ++pbuf_raw_count;
984263bc
MD
259 }
260
312dcd01
MD
261 /*
262 * Allow the clustering code to use half of our pbufs.
263 */
984263bc 264 cluster_pbuf_freecnt = nswbuf / 2;
984263bc
MD
265}
266
99ad9bc4
MD
267/*
268 * No requirements.
269 */
984263bc 270void
57e43348 271vm_pager_deallocate(vm_object_t object)
984263bc
MD
272{
273 (*pagertab[object->type]->pgo_dealloc) (object);
274}
275
276/*
984263bc
MD
277 * vm_pager_get_pages() - inline, see vm/vm_pager.h
278 * vm_pager_put_pages() - inline, see vm/vm_pager.h
279 * vm_pager_has_page() - inline, see vm/vm_pager.h
280 * vm_pager_page_inserted() - inline, see vm/vm_pager.h
281 * vm_pager_page_removed() - inline, see vm/vm_pager.h
282 */
283
284#if 0
285/*
286 * vm_pager_sync:
287 *
288 * Called by pageout daemon before going back to sleep.
289 * Gives pagers a chance to clean up any completed async pageing
290 * operations.
291 */
292void
57e43348 293vm_pager_sync(void)
984263bc
MD
294{
295 struct pagerops **pgops;
296
297 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
298 if (pgops && ((*pgops)->pgo_sync != NULL))
299 (*(*pgops)->pgo_sync) ();
300}
301
302#endif
303
984263bc 304/*
312dcd01 305 * Initialize a physical buffer.
99ad9bc4
MD
306 *
307 * No requirements.
984263bc 308 */
984263bc
MD
309static void
310initpbuf(struct buf *bp)
311{
9a82e536
MD
312 bp->b_qindex = 0; /* BQUEUE_NONE */
313 bp->b_data = bp->b_kvabase; /* NULL if pbuf sans kva */
a8f169e2 314 bp->b_flags = B_PAGING;
10f3fee5 315 bp->b_cmd = BUF_CMD_DONE;
984263bc 316 bp->b_error = 0;
9a71d53f
MD
317 bp->b_bcount = 0;
318 bp->b_bufsize = MAXPHYS;
81b5c339 319 initbufbio(bp);
54f51aeb 320 xio_init(&bp->b_xio);
984263bc
MD
321 BUF_LOCK(bp, LK_EXCLUSIVE);
322}
323
324/*
a50fe973 325 * Allocate a physical buffer
984263bc
MD
326 *
327 * There are a limited number (nswbuf) of physical buffers. We need
328 * to make sure that no single subsystem is able to hog all of them,
329 * so each subsystem implements a counter which is typically initialized
330 * to 1/2 nswbuf. getpbuf() decrements this counter in allocation and
331 * increments it on release, and blocks if the counter hits zero. A
332 * subsystem may initialize the counter to -1 to disable the feature,
333 * but it must still be sure to match up all uses of getpbuf() with
334 * relpbuf() using the same variable.
335 *
336 * NOTE: pfreecnt can be NULL, but this 'feature' will be removed
337 * relatively soon when the rest of the subsystems get smart about it. XXX
a50fe973 338 *
9a82e536
MD
339 * Physical buffers can be with or without KVA space reserved. There
340 * are severe limitations on the ones with KVA reserved, and fewer
341 * limitations on the ones without. getpbuf() gets one without,
342 * getpbuf_kva() gets one with.
343 *
99ad9bc4 344 * No requirements.
984263bc
MD
345 */
346struct buf *
57e43348 347getpbuf(int *pfreecnt)
984263bc 348{
984263bc
MD
349 struct buf *bp;
350
287a8577 351 spin_lock(&bswspin);
984263bc
MD
352
353 for (;;) {
354 if (pfreecnt) {
a50fe973 355 while (*pfreecnt == 0)
e590ee86 356 ssleep(pfreecnt, &bswspin, 0, "wswbuf0", 0);
984263bc
MD
357 }
358
359 /* get a bp from the swap buffer header pool */
9a82e536 360 if ((bp = TAILQ_FIRST(&bswlist_raw)) != NULL)
984263bc 361 break;
9a82e536
MD
362 bswneeded_raw = 1;
363 ssleep(&bswneeded_raw, &bswspin, 0, "wswbuf1", 0);
984263bc
MD
364 /* loop in case someone else grabbed one */
365 }
9a82e536
MD
366 TAILQ_REMOVE(&bswlist_raw, bp, b_freelist);
367 --pbuf_raw_count;
984263bc
MD
368 if (pfreecnt)
369 --*pfreecnt;
a50fe973 370
287a8577 371 spin_unlock(&bswspin);
984263bc
MD
372
373 initpbuf(bp);
8c72e3d5 374 KKASSERT(dsched_is_clear_buf_priv(bp));
9a82e536
MD
375
376 return (bp);
377}
378
379struct buf *
380getpbuf_kva(int *pfreecnt)
381{
382 struct buf *bp;
383
287a8577 384 spin_lock(&bswspin);
9a82e536
MD
385
386 for (;;) {
387 if (pfreecnt) {
388 while (*pfreecnt == 0)
389 ssleep(pfreecnt, &bswspin, 0, "wswbuf0", 0);
390 }
391
392 /* get a bp from the swap buffer header pool */
393 if ((bp = TAILQ_FIRST(&bswlist_kva)) != NULL)
394 break;
395 bswneeded_kva = 1;
396 ssleep(&bswneeded_kva, &bswspin, 0, "wswbuf1", 0);
397 /* loop in case someone else grabbed one */
398 }
399 TAILQ_REMOVE(&bswlist_kva, bp, b_freelist);
400 --pbuf_kva_count;
401 if (pfreecnt)
402 --*pfreecnt;
403
287a8577 404 spin_unlock(&bswspin);
9a82e536
MD
405
406 initpbuf(bp);
407 KKASSERT(dsched_is_clear_buf_priv(bp));
408
409 return (bp);
984263bc
MD
410}
411
412/*
a50fe973 413 * Allocate a physical buffer, if one is available.
984263bc
MD
414 *
415 * Note that there is no NULL hack here - all subsystems using this
416 * call understand how to use pfreecnt.
a50fe973 417 *
99ad9bc4 418 * No requirements.
984263bc
MD
419 */
420struct buf *
57e43348 421trypbuf(int *pfreecnt)
984263bc 422{
984263bc
MD
423 struct buf *bp;
424
287a8577 425 spin_lock(&bswspin);
a50fe973 426
9a82e536 427 if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist_raw)) == NULL) {
287a8577 428 spin_unlock(&bswspin);
9a82e536
MD
429 return NULL;
430 }
431 TAILQ_REMOVE(&bswlist_raw, bp, b_freelist);
432 --pbuf_raw_count;
433 --*pfreecnt;
434
287a8577 435 spin_unlock(&bswspin);
9a82e536
MD
436
437 initpbuf(bp);
438
439 return bp;
440}
441
442struct buf *
443trypbuf_kva(int *pfreecnt)
444{
445 struct buf *bp;
446
287a8577 447 spin_lock(&bswspin);
9a82e536
MD
448
449 if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist_kva)) == NULL) {
287a8577 450 spin_unlock(&bswspin);
984263bc
MD
451 return NULL;
452 }
9a82e536
MD
453 TAILQ_REMOVE(&bswlist_kva, bp, b_freelist);
454 --pbuf_kva_count;
984263bc
MD
455 --*pfreecnt;
456
287a8577 457 spin_unlock(&bswspin);
984263bc
MD
458
459 initpbuf(bp);
460
461 return bp;
462}
463
464/*
a50fe973 465 * Release a physical buffer
984263bc
MD
466 *
467 * NOTE: pfreecnt can be NULL, but this 'feature' will be removed
468 * relatively soon when the rest of the subsystems get smart about it. XXX
a50fe973 469 *
99ad9bc4 470 * No requirements.
984263bc
MD
471 */
472void
57e43348 473relpbuf(struct buf *bp, int *pfreecnt)
984263bc 474{
9a82e536
MD
475 int wake_bsw_kva = 0;
476 int wake_bsw_raw = 0;
a50fe973 477 int wake_freecnt = 0;
984263bc 478
a8f169e2 479 KKASSERT(bp->b_flags & B_PAGING);
aa166ad1 480 dsched_exit_buf(bp);
984263bc 481
a50fe973 482 BUF_UNLOCK(bp);
7757d1c2 483
287a8577 484 spin_lock(&bswspin);
9a82e536
MD
485 if (bp->b_kvabase) {
486 TAILQ_INSERT_HEAD(&bswlist_kva, bp, b_freelist);
487 ++pbuf_kva_count;
488 } else {
489 TAILQ_INSERT_HEAD(&bswlist_raw, bp, b_freelist);
490 ++pbuf_raw_count;
491 }
492 if (bswneeded_kva) {
493 bswneeded_kva = 0;
494 wake_bsw_kva = 1;
495 }
496 if (bswneeded_raw) {
497 bswneeded_raw = 0;
498 wake_bsw_raw = 1;
984263bc
MD
499 }
500 if (pfreecnt) {
501 if (++*pfreecnt == 1)
a50fe973 502 wake_freecnt = 1;
984263bc 503 }
287a8577 504 spin_unlock(&bswspin);
a50fe973 505
9a82e536
MD
506 if (wake_bsw_kva)
507 wakeup(&bswneeded_kva);
508 if (wake_bsw_raw)
509 wakeup(&bswneeded_raw);
a50fe973
MD
510 if (wake_freecnt)
511 wakeup(pfreecnt);
984263bc 512}