160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 343c4dd356SDavid Greenman * from: @(#)vm_pager.c 8.6 (Berkeley) 1/12/94 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38df8bae1dSRodney W. Grimes * All rights reserved. 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 43df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 44df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 45df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 46df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55df8bae1dSRodney W. Grimes * School of Computer Science 56df8bae1dSRodney W. Grimes * Carnegie Mellon University 57df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 58df8bae1dSRodney W. Grimes * 59df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 60df8bae1dSRodney W. Grimes * rights to redistribute these changes. 61df8bae1dSRodney W. Grimes */ 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * Paging space routine stubs. Emulates a matchmaker-like interface 65df8bae1dSRodney W. Grimes * for builtin pagers. 66df8bae1dSRodney W. Grimes */ 67df8bae1dSRodney W. Grimes 68874651b1SDavid E. O'Brien #include <sys/cdefs.h> 69874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 70874651b1SDavid E. O'Brien 71*756a5412SGleb Smirnoff #include "opt_param.h" 72*756a5412SGleb Smirnoff 73df8bae1dSRodney W. Grimes #include <sys/param.h> 74df8bae1dSRodney W. Grimes #include <sys/systm.h> 751c5bb3eaSPeter Wemm #include <sys/kernel.h> 76a5296b05SJulian Elischer #include <sys/vnode.h> 779626b608SPoul-Henning Kamp #include <sys/bio.h> 7816f62314SDavid Greenman #include <sys/buf.h> 7903b67a39SBruce Evans #include <sys/ucred.h> 80a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 8189f6b863SAttilio Rao #include <sys/rwlock.h> 82df8bae1dSRodney W. Grimes 83df8bae1dSRodney W. Grimes #include <vm/vm.h> 84efeaf95aSDavid Greenman #include <vm/vm_param.h> 855f518366SJeff Roberson #include <vm/vm_kern.h> 86efeaf95aSDavid Greenman #include <vm/vm_object.h> 87df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8824a1cce3SDavid Greenman #include <vm/vm_pager.h> 89efeaf95aSDavid Greenman #include <vm/vm_extern.h> 90*756a5412SGleb Smirnoff #include <vm/uma.h> 91df8bae1dSRodney W. Grimes 92*756a5412SGleb Smirnoff uma_zone_t pbuf_zone; 93*756a5412SGleb Smirnoff static int pbuf_init(void *, int, int); 94*756a5412SGleb Smirnoff static int pbuf_ctor(void *, int, void *, int); 95*756a5412SGleb Smirnoff static void pbuf_dtor(void *, int, void *); 9698082691SJeff Roberson 97b0cd2017SGleb Smirnoff static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *); 9811caded3SAlfred Perlstein static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 993364c323SKonstantin Belousov vm_ooffset_t, struct ucred *); 10011caded3SAlfred Perlstein static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 10111caded3SAlfred Perlstein static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 10211caded3SAlfred Perlstein static void dead_pager_dealloc(vm_object_t); 103bef608bdSJohn Dyson 104e4542174SMatthew Dillon static int 105b0cd2017SGleb Smirnoff dead_pager_getpages(vm_object_t obj, vm_page_t *ma, int count, int *rbehind, 106b0cd2017SGleb Smirnoff int *rahead) 107bef608bdSJohn Dyson { 108b0cd2017SGleb Smirnoff 109b0cd2017SGleb Smirnoff return (VM_PAGER_FAIL); 110bef608bdSJohn Dyson } 111bef608bdSJohn Dyson 112e4542174SMatthew Dillon static vm_object_t 113af83f5d7SRoman Divacky dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 1143364c323SKonstantin Belousov vm_ooffset_t off, struct ucred *cred) 115bef608bdSJohn Dyson { 11603302b13SKonstantin Belousov 11703302b13SKonstantin Belousov return (NULL); 118bef608bdSJohn Dyson } 119bef608bdSJohn Dyson 120e4542174SMatthew Dillon static void 12103302b13SKonstantin Belousov dead_pager_putpages(vm_object_t object, vm_page_t *m, int count, 12203302b13SKonstantin Belousov int flags, int *rtvals) 123bef608bdSJohn Dyson { 124bef608bdSJohn Dyson int i; 125e4542174SMatthew Dillon 12603302b13SKonstantin Belousov for (i = 0; i < count; i++) 127bef608bdSJohn Dyson rtvals[i] = VM_PAGER_AGAIN; 128bef608bdSJohn Dyson } 129bef608bdSJohn Dyson 130e4542174SMatthew Dillon static int 13103302b13SKonstantin Belousov dead_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *prev, int *next) 132bef608bdSJohn Dyson { 13303302b13SKonstantin Belousov 13403302b13SKonstantin Belousov if (prev != NULL) 135bef608bdSJohn Dyson *prev = 0; 13603302b13SKonstantin Belousov if (next != NULL) 137bef608bdSJohn Dyson *next = 0; 13803302b13SKonstantin Belousov return (FALSE); 139bef608bdSJohn Dyson } 140bef608bdSJohn Dyson 141e4542174SMatthew Dillon static void 14203302b13SKonstantin Belousov dead_pager_dealloc(vm_object_t object) 143bef608bdSJohn Dyson { 14403302b13SKonstantin Belousov 145bef608bdSJohn Dyson } 146bef608bdSJohn Dyson 1470776e10cSEivind Eklund static struct pagerops deadpagerops = { 1484e658600SPoul-Henning Kamp .pgo_alloc = dead_pager_alloc, 1494e658600SPoul-Henning Kamp .pgo_dealloc = dead_pager_dealloc, 1504e658600SPoul-Henning Kamp .pgo_getpages = dead_pager_getpages, 1514e658600SPoul-Henning Kamp .pgo_putpages = dead_pager_putpages, 1524e658600SPoul-Henning Kamp .pgo_haspage = dead_pager_haspage, 153bef608bdSJohn Dyson }; 154bef608bdSJohn Dyson 1551c7c3c6aSMatthew Dillon struct pagerops *pagertab[] = { 15624a1cce3SDavid Greenman &defaultpagerops, /* OBJT_DEFAULT */ 15724a1cce3SDavid Greenman &swappagerops, /* OBJT_SWAP */ 15824a1cce3SDavid Greenman &vnodepagerops, /* OBJT_VNODE */ 15924a1cce3SDavid Greenman &devicepagerops, /* OBJT_DEVICE */ 16024964514SPeter Wemm &physpagerops, /* OBJT_PHYS */ 16101381811SJohn Baldwin &deadpagerops, /* OBJT_DEAD */ 162b7ac5a85SKonstantin Belousov &sgpagerops, /* OBJT_SG */ 163b7ac5a85SKonstantin Belousov &mgtdevicepagerops, /* OBJT_MGTDEVICE */ 164df8bae1dSRodney W. Grimes }; 1651c7c3c6aSMatthew Dillon 166df8bae1dSRodney W. Grimes /* 167df8bae1dSRodney W. Grimes * Kernel address space for mapping pages. 168df8bae1dSRodney W. Grimes * Used by pagers where KVAs are needed for IO. 169df8bae1dSRodney W. Grimes * 170df8bae1dSRodney W. Grimes * XXX needs to be large enough to support the number of pending async 171df8bae1dSRodney W. Grimes * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size 172df8bae1dSRodney W. Grimes * (MAXPHYS == 64k) if you want to get the most efficiency. 173df8bae1dSRodney W. Grimes */ 1745f518366SJeff Roberson vm_offset_t swapbkva; /* swap buffers kva */ 175df8bae1dSRodney W. Grimes 176df8bae1dSRodney W. Grimes void 17703302b13SKonstantin Belousov vm_pager_init(void) 178df8bae1dSRodney W. Grimes { 179df8bae1dSRodney W. Grimes struct pagerops **pgops; 180df8bae1dSRodney W. Grimes 181df8bae1dSRodney W. Grimes /* 182df8bae1dSRodney W. Grimes * Initialize known pagers 183df8bae1dSRodney W. Grimes */ 1848dfea464SPedro F. Giffuni for (pgops = pagertab; pgops < &pagertab[nitems(pagertab)]; pgops++) 185bf277cf4SKonstantin Belousov if ((*pgops)->pgo_init != NULL) 186df8bae1dSRodney W. Grimes (*(*pgops)->pgo_init)(); 187df8bae1dSRodney W. Grimes } 188df8bae1dSRodney W. Grimes 189*756a5412SGleb Smirnoff static int nswbuf_max; 190*756a5412SGleb Smirnoff 19116f62314SDavid Greenman void 19203302b13SKonstantin Belousov vm_pager_bufferinit(void) 19316f62314SDavid Greenman { 1940d94caffSDavid Greenman 195*756a5412SGleb Smirnoff /* Main zone for paging bufs. */ 196*756a5412SGleb Smirnoff pbuf_zone = uma_zcreate("pbuf", sizeof(struct buf), 197*756a5412SGleb Smirnoff pbuf_ctor, pbuf_dtor, pbuf_init, NULL, UMA_ALIGN_CACHE, 198*756a5412SGleb Smirnoff UMA_ZONE_VM | UMA_ZONE_NOFREE); 199*756a5412SGleb Smirnoff /* Few systems may still use this zone directly, so it needs a limit. */ 200*756a5412SGleb Smirnoff nswbuf_max += uma_zone_set_max(pbuf_zone, NSWBUF_MIN); 201*756a5412SGleb Smirnoff } 202*756a5412SGleb Smirnoff 203*756a5412SGleb Smirnoff uma_zone_t 204*756a5412SGleb Smirnoff pbuf_zsecond_create(char *name, int max) 205*756a5412SGleb Smirnoff { 206*756a5412SGleb Smirnoff uma_zone_t zone; 207*756a5412SGleb Smirnoff 208*756a5412SGleb Smirnoff zone = uma_zsecond_create(name, pbuf_ctor, pbuf_dtor, NULL, NULL, 209*756a5412SGleb Smirnoff pbuf_zone); 21016f62314SDavid Greenman /* 211*756a5412SGleb Smirnoff * uma_prealloc() rounds up to items per slab. If we would prealloc 212*756a5412SGleb Smirnoff * immediately on every pbuf_zsecond_create(), we may accumulate too 213*756a5412SGleb Smirnoff * much of difference between hard limit and prealloced items, which 214*756a5412SGleb Smirnoff * means wasted memory. 21516f62314SDavid Greenman */ 216*756a5412SGleb Smirnoff if (nswbuf_max > 0) 217*756a5412SGleb Smirnoff nswbuf_max += uma_zone_set_max(zone, max); 218*756a5412SGleb Smirnoff else 219*756a5412SGleb Smirnoff uma_prealloc(pbuf_zone, uma_zone_set_max(zone, max)); 220*756a5412SGleb Smirnoff 221*756a5412SGleb Smirnoff return (zone); 22216f62314SDavid Greenman } 22316f62314SDavid Greenman 224*756a5412SGleb Smirnoff static void 225*756a5412SGleb Smirnoff pbuf_prealloc(void *arg __unused) 226*756a5412SGleb Smirnoff { 227*756a5412SGleb Smirnoff 228*756a5412SGleb Smirnoff uma_prealloc(pbuf_zone, nswbuf_max); 229*756a5412SGleb Smirnoff nswbuf_max = -1; 23016f62314SDavid Greenman } 23116f62314SDavid Greenman 232*756a5412SGleb Smirnoff SYSINIT(pbuf, SI_SUB_KTHREAD_BUF, SI_ORDER_ANY, pbuf_prealloc, NULL); 233*756a5412SGleb Smirnoff 234df8bae1dSRodney W. Grimes /* 235df8bae1dSRodney W. Grimes * Allocate an instance of a pager of the given type. 236df8bae1dSRodney W. Grimes * Size, protection and offset parameters are passed in for pagers that 237df8bae1dSRodney W. Grimes * need to perform page-level validation (e.g. the device pager). 238df8bae1dSRodney W. Grimes */ 23924a1cce3SDavid Greenman vm_object_t 24023955314SAlfred Perlstein vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size, 2413364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t off, struct ucred *cred) 242df8bae1dSRodney W. Grimes { 24323955314SAlfred Perlstein vm_object_t ret; 244df8bae1dSRodney W. Grimes struct pagerops *ops; 245df8bae1dSRodney W. Grimes 24624a1cce3SDavid Greenman ops = pagertab[type]; 247df8bae1dSRodney W. Grimes if (ops) 2483364c323SKonstantin Belousov ret = (*ops->pgo_alloc)(handle, size, prot, off, cred); 24923955314SAlfred Perlstein else 25023955314SAlfred Perlstein ret = NULL; 25123955314SAlfred Perlstein return (ret); 252df8bae1dSRodney W. Grimes } 253df8bae1dSRodney W. Grimes 254658ad5ffSAlan Cox /* 255658ad5ffSAlan Cox * The object must be locked. 256658ad5ffSAlan Cox */ 257df8bae1dSRodney W. Grimes void 25803302b13SKonstantin Belousov vm_pager_deallocate(vm_object_t object) 259df8bae1dSRodney W. Grimes { 260658ad5ffSAlan Cox 26189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 26224a1cce3SDavid Greenman (*pagertab[object->type]->pgo_dealloc) (object); 263df8bae1dSRodney W. Grimes } 264df8bae1dSRodney W. Grimes 265093ebe1dSGleb Smirnoff static void 266093ebe1dSGleb Smirnoff vm_pager_assert_in(vm_object_t object, vm_page_t *m, int count) 267093ebe1dSGleb Smirnoff { 268093ebe1dSGleb Smirnoff #ifdef INVARIANTS 269093ebe1dSGleb Smirnoff 270093ebe1dSGleb Smirnoff VM_OBJECT_ASSERT_WLOCKED(object); 271093ebe1dSGleb Smirnoff KASSERT(count > 0, ("%s: 0 count", __func__)); 272df8bae1dSRodney W. Grimes /* 273093ebe1dSGleb Smirnoff * All pages must be busied, not mapped, not fully valid, 274093ebe1dSGleb Smirnoff * not dirty and belong to the proper object. 275093ebe1dSGleb Smirnoff */ 276093ebe1dSGleb Smirnoff for (int i = 0 ; i < count; i++) { 277255003daSGleb Smirnoff if (m[i] == bogus_page) 278255003daSGleb Smirnoff continue; 279093ebe1dSGleb Smirnoff vm_page_assert_xbusied(m[i]); 280093ebe1dSGleb Smirnoff KASSERT(!pmap_page_is_mapped(m[i]), 281093ebe1dSGleb Smirnoff ("%s: page %p is mapped", __func__, m[i])); 282093ebe1dSGleb Smirnoff KASSERT(m[i]->valid != VM_PAGE_BITS_ALL, 283093ebe1dSGleb Smirnoff ("%s: request for a valid page %p", __func__, m[i])); 284093ebe1dSGleb Smirnoff KASSERT(m[i]->dirty == 0, 285093ebe1dSGleb Smirnoff ("%s: page %p is dirty", __func__, m[i])); 286093ebe1dSGleb Smirnoff KASSERT(m[i]->object == object, 287093ebe1dSGleb Smirnoff ("%s: wrong object %p/%p", __func__, object, m[i]->object)); 288093ebe1dSGleb Smirnoff } 289093ebe1dSGleb Smirnoff #endif 290093ebe1dSGleb Smirnoff } 291093ebe1dSGleb Smirnoff 292093ebe1dSGleb Smirnoff /* 293093ebe1dSGleb Smirnoff * Page in the pages for the object using its associated pager. 294093ebe1dSGleb Smirnoff * The requested page must be fully valid on successful return. 295093ebe1dSGleb Smirnoff */ 296093ebe1dSGleb Smirnoff int 297b0cd2017SGleb Smirnoff vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind, 298b0cd2017SGleb Smirnoff int *rahead) 299093ebe1dSGleb Smirnoff { 300b0cd2017SGleb Smirnoff #ifdef INVARIANTS 301b0cd2017SGleb Smirnoff vm_pindex_t pindex = m[0]->pindex; 302b0cd2017SGleb Smirnoff #endif 303093ebe1dSGleb Smirnoff int r; 304093ebe1dSGleb Smirnoff 305093ebe1dSGleb Smirnoff vm_pager_assert_in(object, m, count); 306093ebe1dSGleb Smirnoff 307b0cd2017SGleb Smirnoff r = (*pagertab[object->type]->pgo_getpages)(object, m, count, rbehind, 308b0cd2017SGleb Smirnoff rahead); 309093ebe1dSGleb Smirnoff if (r != VM_PAGER_OK) 310093ebe1dSGleb Smirnoff return (r); 311093ebe1dSGleb Smirnoff 312b0cd2017SGleb Smirnoff for (int i = 0; i < count; i++) { 313093ebe1dSGleb Smirnoff /* 314b0cd2017SGleb Smirnoff * If pager has replaced a page, assert that it had 315b0cd2017SGleb Smirnoff * updated the array. 316093ebe1dSGleb Smirnoff */ 317b0cd2017SGleb Smirnoff KASSERT(m[i] == vm_page_lookup(object, pindex++), 318093ebe1dSGleb Smirnoff ("%s: mismatch page %p pindex %ju", __func__, 319b0cd2017SGleb Smirnoff m[i], (uintmax_t )pindex - 1)); 320093ebe1dSGleb Smirnoff /* 321b0cd2017SGleb Smirnoff * Zero out partially filled data. 322093ebe1dSGleb Smirnoff */ 323b0cd2017SGleb Smirnoff if (m[i]->valid != VM_PAGE_BITS_ALL) 324b0cd2017SGleb Smirnoff vm_page_zero_invalid(m[i], TRUE); 325b0cd2017SGleb Smirnoff } 326093ebe1dSGleb Smirnoff return (VM_PAGER_OK); 327093ebe1dSGleb Smirnoff } 328093ebe1dSGleb Smirnoff 329093ebe1dSGleb Smirnoff int 330093ebe1dSGleb Smirnoff vm_pager_get_pages_async(vm_object_t object, vm_page_t *m, int count, 331b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 332093ebe1dSGleb Smirnoff { 333093ebe1dSGleb Smirnoff 334093ebe1dSGleb Smirnoff vm_pager_assert_in(object, m, count); 335093ebe1dSGleb Smirnoff 336093ebe1dSGleb Smirnoff return ((*pagertab[object->type]->pgo_getpages_async)(object, m, 337b0cd2017SGleb Smirnoff count, rbehind, rahead, iodone, arg)); 338093ebe1dSGleb Smirnoff } 339093ebe1dSGleb Smirnoff 340093ebe1dSGleb Smirnoff /* 3411c7c3c6aSMatthew Dillon * vm_pager_put_pages() - inline, see vm/vm_pager.h 3421c7c3c6aSMatthew Dillon * vm_pager_has_page() - inline, see vm/vm_pager.h 3431c7c3c6aSMatthew Dillon */ 3441c7c3c6aSMatthew Dillon 345b5e8f167SAlan Cox /* 346b5e8f167SAlan Cox * Search the specified pager object list for an object with the 347b5e8f167SAlan Cox * specified handle. If an object with the specified handle is found, 348b5e8f167SAlan Cox * increase its reference count and return it. Otherwise, return NULL. 349b5e8f167SAlan Cox * 350b5e8f167SAlan Cox * The pager object list must be locked. 351b5e8f167SAlan Cox */ 35224a1cce3SDavid Greenman vm_object_t 353b5e8f167SAlan Cox vm_pager_object_lookup(struct pagerlst *pg_list, void *handle) 354df8bae1dSRodney W. Grimes { 35554d92145SMatthew Dillon vm_object_t object; 356df8bae1dSRodney W. Grimes 357b5e8f167SAlan Cox TAILQ_FOREACH(object, pg_list, pager_object_list) { 358b6062382SAndriy Gapon if (object->handle == handle) { 35989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 360b6062382SAndriy Gapon if ((object->flags & OBJ_DEAD) == 0) { 361b5e8f167SAlan Cox vm_object_reference_locked(object); 36289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 363b5e8f167SAlan Cox break; 364b5e8f167SAlan Cox } 36589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 366b5e8f167SAlan Cox } 367b6062382SAndriy Gapon } 36824a1cce3SDavid Greenman return (object); 369df8bae1dSRodney W. Grimes } 370df8bae1dSRodney W. Grimes 371*756a5412SGleb Smirnoff static int 372*756a5412SGleb Smirnoff pbuf_ctor(void *mem, int size, void *arg, int flags) 373e96c1fdcSPeter Wemm { 374*756a5412SGleb Smirnoff struct buf *bp = mem; 37503302b13SKonstantin Belousov 376*756a5412SGleb Smirnoff bp->b_vp = NULL; 377*756a5412SGleb Smirnoff bp->b_bufobj = NULL; 378*756a5412SGleb Smirnoff 379*756a5412SGleb Smirnoff /* copied from initpbuf() */ 38009e0c6ccSJohn Dyson bp->b_rcred = NOCRED; 38109e0c6ccSJohn Dyson bp->b_wcred = NOCRED; 382f52bd684SEivind Eklund bp->b_qindex = 0; /* On no queue (QUEUE_NONE) */ 383fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 384fade8dd7SJeff Roberson bp->b_xflags = 0; 385*756a5412SGleb Smirnoff bp->b_flags = 0; 386c244d2deSPoul-Henning Kamp bp->b_ioflags = 0; 38721144e3bSPoul-Henning Kamp bp->b_iodone = NULL; 38867812eacSKirk McKusick bp->b_error = 0; 38917661e5aSJeff Roberson BUF_LOCK(bp, LK_EXCLUSIVE, NULL); 390*756a5412SGleb Smirnoff 391*756a5412SGleb Smirnoff return (0); 39209e0c6ccSJohn Dyson } 39309e0c6ccSJohn Dyson 394*756a5412SGleb Smirnoff static void 395*756a5412SGleb Smirnoff pbuf_dtor(void *mem, int size, void *arg) 3960d94caffSDavid Greenman { 397*756a5412SGleb Smirnoff struct buf *bp = mem; 39816f62314SDavid Greenman 39916f62314SDavid Greenman if (bp->b_rcred != NOCRED) { 40016f62314SDavid Greenman crfree(bp->b_rcred); 40116f62314SDavid Greenman bp->b_rcred = NOCRED; 40216f62314SDavid Greenman } 40316f62314SDavid Greenman if (bp->b_wcred != NOCRED) { 40416f62314SDavid Greenman crfree(bp->b_wcred); 40516f62314SDavid Greenman bp->b_wcred = NOCRED; 40616f62314SDavid Greenman } 4071c7c3c6aSMatthew Dillon 40867812eacSKirk McKusick BUF_UNLOCK(bp); 40916f62314SDavid Greenman } 410*756a5412SGleb Smirnoff 411*756a5412SGleb Smirnoff static int 412*756a5412SGleb Smirnoff pbuf_init(void *mem, int size, int flags) 413*756a5412SGleb Smirnoff { 414*756a5412SGleb Smirnoff struct buf *bp = mem; 415*756a5412SGleb Smirnoff 416*756a5412SGleb Smirnoff bp->b_kvabase = (void *)kva_alloc(MAXPHYS); 417*756a5412SGleb Smirnoff if (bp->b_kvabase == NULL) 418*756a5412SGleb Smirnoff return (ENOMEM); 419*756a5412SGleb Smirnoff bp->b_kvasize = MAXPHYS; 420*756a5412SGleb Smirnoff BUF_LOCKINIT(bp); 421*756a5412SGleb Smirnoff LIST_INIT(&bp->b_dep); 422*756a5412SGleb Smirnoff bp->b_rcred = bp->b_wcred = NOCRED; 423*756a5412SGleb Smirnoff bp->b_xflags = 0; 424*756a5412SGleb Smirnoff 425*756a5412SGleb Smirnoff return (0); 42616f62314SDavid Greenman } 427a752aa8fSPoul-Henning Kamp 428a752aa8fSPoul-Henning Kamp /* 429a752aa8fSPoul-Henning Kamp * Associate a p-buffer with a vnode. 430a752aa8fSPoul-Henning Kamp * 431a752aa8fSPoul-Henning Kamp * Also sets B_PAGING flag to indicate that vnode is not fully associated 432a752aa8fSPoul-Henning Kamp * with the buffer. i.e. the bp has not been linked into the vnode or 433a752aa8fSPoul-Henning Kamp * ref-counted. 434a752aa8fSPoul-Henning Kamp */ 435a752aa8fSPoul-Henning Kamp void 436a752aa8fSPoul-Henning Kamp pbgetvp(struct vnode *vp, struct buf *bp) 437a752aa8fSPoul-Henning Kamp { 438a752aa8fSPoul-Henning Kamp 439a752aa8fSPoul-Henning Kamp KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 440287013d2SPoul-Henning Kamp KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)")); 441a752aa8fSPoul-Henning Kamp 442a752aa8fSPoul-Henning Kamp bp->b_vp = vp; 443a752aa8fSPoul-Henning Kamp bp->b_flags |= B_PAGING; 444a752aa8fSPoul-Henning Kamp bp->b_bufobj = &vp->v_bufobj; 445a752aa8fSPoul-Henning Kamp } 446a752aa8fSPoul-Henning Kamp 447a752aa8fSPoul-Henning Kamp /* 4485c6e573fSPoul-Henning Kamp * Associate a p-buffer with a vnode. 4495c6e573fSPoul-Henning Kamp * 4505c6e573fSPoul-Henning Kamp * Also sets B_PAGING flag to indicate that vnode is not fully associated 4515c6e573fSPoul-Henning Kamp * with the buffer. i.e. the bp has not been linked into the vnode or 4525c6e573fSPoul-Henning Kamp * ref-counted. 4535c6e573fSPoul-Henning Kamp */ 4545c6e573fSPoul-Henning Kamp void 4555c6e573fSPoul-Henning Kamp pbgetbo(struct bufobj *bo, struct buf *bp) 4565c6e573fSPoul-Henning Kamp { 4575c6e573fSPoul-Henning Kamp 4585c6e573fSPoul-Henning Kamp KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)")); 4595c6e573fSPoul-Henning Kamp KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)")); 4605c6e573fSPoul-Henning Kamp 4615c6e573fSPoul-Henning Kamp bp->b_flags |= B_PAGING; 4625c6e573fSPoul-Henning Kamp bp->b_bufobj = bo; 4635c6e573fSPoul-Henning Kamp } 4645c6e573fSPoul-Henning Kamp 4655c6e573fSPoul-Henning Kamp /* 466a752aa8fSPoul-Henning Kamp * Disassociate a p-buffer from a vnode. 467a752aa8fSPoul-Henning Kamp */ 468a752aa8fSPoul-Henning Kamp void 469a752aa8fSPoul-Henning Kamp pbrelvp(struct buf *bp) 470a752aa8fSPoul-Henning Kamp { 471a752aa8fSPoul-Henning Kamp 472a752aa8fSPoul-Henning Kamp KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 473a752aa8fSPoul-Henning Kamp KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj")); 47426089666SJeff Roberson KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0, 47526089666SJeff Roberson ("pbrelvp: pager buf on vnode list.")); 476a752aa8fSPoul-Henning Kamp 477a752aa8fSPoul-Henning Kamp bp->b_vp = NULL; 478a752aa8fSPoul-Henning Kamp bp->b_bufobj = NULL; 479a752aa8fSPoul-Henning Kamp bp->b_flags &= ~B_PAGING; 480a752aa8fSPoul-Henning Kamp } 4815c6e573fSPoul-Henning Kamp 4825c6e573fSPoul-Henning Kamp /* 4835c6e573fSPoul-Henning Kamp * Disassociate a p-buffer from a bufobj. 4845c6e573fSPoul-Henning Kamp */ 4855c6e573fSPoul-Henning Kamp void 4865c6e573fSPoul-Henning Kamp pbrelbo(struct buf *bp) 4875c6e573fSPoul-Henning Kamp { 4885c6e573fSPoul-Henning Kamp 4895c6e573fSPoul-Henning Kamp KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode")); 4905c6e573fSPoul-Henning Kamp KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj")); 49126089666SJeff Roberson KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0, 49226089666SJeff Roberson ("pbrelbo: pager buf on vnode list.")); 4935c6e573fSPoul-Henning Kamp 4945c6e573fSPoul-Henning Kamp bp->b_bufobj = NULL; 4955c6e573fSPoul-Henning Kamp bp->b_flags &= ~B_PAGING; 4965c6e573fSPoul-Henning Kamp } 497