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$"); 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> 48248084Sattilio#include <sys/rwlock.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 849513Sdg if (handle != NULL) 859513Sdg panic("default_pager_alloc: handle specified"); 86194766Skib if (cred != NULL) { 87216128Strasz if (!swap_reserve_by_cred(size, cred)) 88194766Skib return (NULL); 89216128Strasz crhold(cred); 90194766Skib } 91194766Skib object = vm_object_allocate(OBJT_DEFAULT, 92194766Skib OFF_TO_IDX(round_page(offset + size))); 93194766Skib if (cred != NULL) { 94248084Sattilio VM_OBJECT_WLOCK(object); 95216128Strasz object->cred = cred; 96194766Skib object->charge = size; 97248084Sattilio VM_OBJECT_WUNLOCK(object); 98194766Skib } 99194766Skib return (object); 1009513Sdg} 1019513Sdg 10242957Sdillon/* 10342957Sdillon * deallocate resources associated with default objects. The default objects 10442957Sdillon * have no special resources allocated to them, but the vm_page's being used 10542957Sdillon * in this object might. Still, we do not have to do anything - we will free 10642957Sdillon * the swapblk in the underlying vm_page's when we free the vm_page or 10742957Sdillon * garbage collect the vm_page cache list. 10842957Sdillon */ 10912820Sphkstatic void 1109513Sdgdefault_pager_dealloc(object) 1119513Sdg vm_object_t object; 1129513Sdg{ 1139513Sdg /* 1149513Sdg * OBJT_DEFAULT objects have no special resources allocated to them. 1159513Sdg */ 1169513Sdg} 1179513Sdg 1189513Sdg/* 11942957Sdillon * Load pages from backing store. Since OBJT_DEFAULT is converted to 12042957Sdillon * OBJT_SWAP at the time a swap-backed vm_page_t is freed, we will never 12142957Sdillon * see a vm_page with assigned swap here. 1229513Sdg */ 12312820Sphkstatic int 1249513Sdgdefault_pager_getpages(object, m, count, reqpage) 1259513Sdg vm_object_t object; 1269513Sdg vm_page_t *m; 1279513Sdg int count; 1289513Sdg int reqpage; 1299513Sdg{ 1309513Sdg return VM_PAGER_FAIL; 1319513Sdg} 1329513Sdg 13342957Sdillon/* 13442957Sdillon * Store pages to backing store. We should assign swap and initiate 13542957Sdillon * I/O. We do not actually convert the object to OBJT_SWAP here. The 13642957Sdillon * object will be converted when the written-out vm_page_t is moved from the 13742957Sdillon * cache to the free list. 13842957Sdillon */ 13943129Sdillonstatic void 1409513Sdgdefault_pager_putpages(object, m, c, sync, rtvals) 1419513Sdg vm_object_t object; 1429513Sdg vm_page_t *m; 1439513Sdg int c; 1449513Sdg boolean_t sync; 1459513Sdg int *rtvals; 1469513Sdg{ 147118535Sphk swappagerops.pgo_putpages(object, m, c, sync, rtvals); 1489513Sdg} 1499513Sdg 15042957Sdillon/* 15142957Sdillon * Tell us whether the backing store for the requested (object,index) is 15242957Sdillon * synchronized. i.e. tell us whether we can throw the page away and 15342957Sdillon * reload it later. So, for example, if we are in the process of writing 15442957Sdillon * the page to its backing store, or if no backing store has been assigned, 15542957Sdillon * it is not yet synchronized. 15642957Sdillon * 15742957Sdillon * It is possible to have fully-synchronized swap assigned without the 15842957Sdillon * object having been converted. We just call swap_pager_haspage() to 15942957Sdillon * deal with it since it must already deal with it plus deal with swap 16042957Sdillon * meta-data structures. 16142957Sdillon */ 16212820Sphkstatic boolean_t 16312767Sdysondefault_pager_haspage(object, pindex, before, after) 1649513Sdg vm_object_t object; 16512767Sdyson vm_pindex_t pindex; 1669513Sdg int *before; 1679513Sdg int *after; 1689513Sdg{ 1699513Sdg return FALSE; 1709513Sdg} 17115978Sdyson 172