default_pager.c revision 194766
1139825Simp/*- 29514Sdg * Copyright (c) 1995, David Greenman 39514Sdg * All rights reserved. 49514Sdg * 59514Sdg * Redistribution and use in source and binary forms, with or without 69514Sdg * modification, are permitted provided that the following conditions 79514Sdg * are met: 89514Sdg * 1. Redistributions of source code must retain the above copyright 99514Sdg * notice, this list of conditions and the following disclaimer. 109514Sdg * 2. Redistributions in binary form must reproduce the above copyright 119514Sdg * notice, this list of conditions and the following disclaimer in the 129514Sdg * documentation and/or other materials provided with the distribution. 139514Sdg * 3. All advertising materials mentioning features or use of this software 1458705Scharnier * must display the following acknowledgement: 159514Sdg * This product includes software developed by David Greenman. 169514Sdg * 4. The name of the author may not be used to endorse or promote products 179514Sdg * derived from this software without specific prior written permission. 189514Sdg * 199514Sdg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 209514Sdg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219514Sdg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229514Sdg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 239514Sdg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249514Sdg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259514Sdg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269514Sdg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279514Sdg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289514Sdg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299514Sdg * SUCH DAMAGE. 309514Sdg * 3142957Sdillon * The default pager is responsible for supplying backing store to unbacked 3242957Sdillon * storage. The backing store is usually swap so we just fall through to 3342957Sdillon * the swap routines. However, since swap metadata has not been assigned, 3442957Sdillon * the swap routines assign and manage the swap backing store through the 3542957Sdillon * vm_page->swapblk field. The object is only converted when the page is 3642957Sdillon * physically freed after having been cleaned and even then vm_page->swapblk 3742957Sdillon * is maintained whenever a resident page also has swap backing store. 389514Sdg */ 399514Sdg 40116226Sobrien#include <sys/cdefs.h> 41116226Sobrien__FBSDID("$FreeBSD: head/sys/vm/default_pager.c 194766 2009-06-23 20:45:22Z kib $"); 42116226Sobrien 439513Sdg#include <sys/param.h> 449513Sdg#include <sys/systm.h> 4576827Salfred#include <sys/lock.h> 4679224Sdillon#include <sys/proc.h> 47194766Skib#include <sys/resourcevar.h> 4876827Salfred#include <sys/mutex.h> 499513Sdg 509513Sdg#include <vm/vm.h> 5112662Sdg#include <vm/vm_object.h> 5212662Sdg#include <vm/vm_page.h> 539513Sdg#include <vm/vm_pager.h> 549513Sdg#include <vm/swap_pager.h> 559513Sdg 5692727Salfredstatic vm_object_t default_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 57194766Skib vm_ooffset_t, struct ucred *); 5892727Salfredstatic void default_pager_dealloc(vm_object_t); 5992727Salfredstatic int default_pager_getpages(vm_object_t, vm_page_t *, int, int); 6092727Salfredstatic void default_pager_putpages(vm_object_t, vm_page_t *, int, 6192727Salfred boolean_t, int *); 6292727Salfredstatic boolean_t default_pager_haspage(vm_object_t, vm_pindex_t, int *, 6392727Salfred int *); 649513Sdg/* 659513Sdg * pagerops for OBJT_DEFAULT - "default pager". 669513Sdg */ 679513Sdgstruct pagerops defaultpagerops = { 68118466Sphk .pgo_alloc = default_pager_alloc, 69118466Sphk .pgo_dealloc = default_pager_dealloc, 70118466Sphk .pgo_getpages = default_pager_getpages, 71118466Sphk .pgo_putpages = default_pager_putpages, 72118466Sphk .pgo_haspage = default_pager_haspage, 739513Sdg}; 749513Sdg 759513Sdg/* 769513Sdg * no_pager_alloc just returns an initialized object. 779513Sdg */ 7812820Sphkstatic vm_object_t 7940286Sdgdefault_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 80194766Skib vm_ooffset_t offset, struct ucred *cred) 819513Sdg{ 82194766Skib vm_object_t object; 83194766Skib struct uidinfo *uip; 84194766Skib 859513Sdg if (handle != NULL) 869513Sdg panic("default_pager_alloc: handle specified"); 87194766Skib if (cred != NULL) { 88194766Skib uip = cred->cr_ruidinfo; 89194766Skib if (!swap_reserve_by_uid(size, uip)) 90194766Skib return (NULL); 91194766Skib uihold(uip); 92194766Skib } 93194766Skib object = vm_object_allocate(OBJT_DEFAULT, 94194766Skib OFF_TO_IDX(round_page(offset + size))); 95194766Skib if (cred != NULL) { 96194766Skib VM_OBJECT_LOCK(object); 97194766Skib object->uip = uip; 98194766Skib object->charge = size; 99194766Skib VM_OBJECT_UNLOCK(object); 100194766Skib } 101194766Skib return (object); 1029513Sdg} 1039513Sdg 10442957Sdillon/* 10542957Sdillon * deallocate resources associated with default objects. The default objects 10642957Sdillon * have no special resources allocated to them, but the vm_page's being used 10742957Sdillon * in this object might. Still, we do not have to do anything - we will free 10842957Sdillon * the swapblk in the underlying vm_page's when we free the vm_page or 10942957Sdillon * garbage collect the vm_page cache list. 11042957Sdillon */ 11112820Sphkstatic void 1129513Sdgdefault_pager_dealloc(object) 1139513Sdg vm_object_t object; 1149513Sdg{ 1159513Sdg /* 1169513Sdg * OBJT_DEFAULT objects have no special resources allocated to them. 1179513Sdg */ 1189513Sdg} 1199513Sdg 1209513Sdg/* 12142957Sdillon * Load pages from backing store. Since OBJT_DEFAULT is converted to 12242957Sdillon * OBJT_SWAP at the time a swap-backed vm_page_t is freed, we will never 12342957Sdillon * see a vm_page with assigned swap here. 1249513Sdg */ 12512820Sphkstatic int 1269513Sdgdefault_pager_getpages(object, m, count, reqpage) 1279513Sdg vm_object_t object; 1289513Sdg vm_page_t *m; 1299513Sdg int count; 1309513Sdg int reqpage; 1319513Sdg{ 1329513Sdg return VM_PAGER_FAIL; 1339513Sdg} 1349513Sdg 13542957Sdillon/* 13642957Sdillon * Store pages to backing store. We should assign swap and initiate 13742957Sdillon * I/O. We do not actually convert the object to OBJT_SWAP here. The 13842957Sdillon * object will be converted when the written-out vm_page_t is moved from the 13942957Sdillon * cache to the free list. 14042957Sdillon */ 14143129Sdillonstatic void 1429513Sdgdefault_pager_putpages(object, m, c, sync, rtvals) 1439513Sdg vm_object_t object; 1449513Sdg vm_page_t *m; 1459513Sdg int c; 1469513Sdg boolean_t sync; 1479513Sdg int *rtvals; 1489513Sdg{ 149118535Sphk swappagerops.pgo_putpages(object, m, c, sync, rtvals); 1509513Sdg} 1519513Sdg 15242957Sdillon/* 15342957Sdillon * Tell us whether the backing store for the requested (object,index) is 15442957Sdillon * synchronized. i.e. tell us whether we can throw the page away and 15542957Sdillon * reload it later. So, for example, if we are in the process of writing 15642957Sdillon * the page to its backing store, or if no backing store has been assigned, 15742957Sdillon * it is not yet synchronized. 15842957Sdillon * 15942957Sdillon * It is possible to have fully-synchronized swap assigned without the 16042957Sdillon * object having been converted. We just call swap_pager_haspage() to 16142957Sdillon * deal with it since it must already deal with it plus deal with swap 16242957Sdillon * meta-data structures. 16342957Sdillon */ 16412820Sphkstatic boolean_t 16512767Sdysondefault_pager_haspage(object, pindex, before, after) 1669513Sdg vm_object_t object; 16712767Sdyson vm_pindex_t pindex; 1689513Sdg int *before; 1699513Sdg int *after; 1709513Sdg{ 1719513Sdg return FALSE; 1729513Sdg} 17315978Sdyson 174