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