1/* 2 * Copyright (c) 1990 University of Utah. 3 * Copyright (c) 1991 The Regents of the University of California. 4 * All rights reserved. 5 * Copyright (c) 1993, 1994 John S. Dyson 6 * Copyright (c) 1995, David Greenman 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91
| 1/* 2 * Copyright (c) 1990 University of Utah. 3 * Copyright (c) 1991 The Regents of the University of California. 4 * All rights reserved. 5 * Copyright (c) 1993, 1994 John S. Dyson 6 * Copyright (c) 1995, David Greenman 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91
|
41 * $Id: vnode_pager.c,v 1.107 1999/04/10 20:52:11 dt Exp $
| 41 * $Id: vnode_pager.c,v 1.108 1999/05/02 23:57:16 alc Exp $
|
42 */ 43 44/* 45 * Page to/from files (vnodes). 46 */ 47 48/* 49 * TODO: 50 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 51 * greatly re-simplify the vnode_pager. 52 */ 53 54#include <sys/param.h> 55#include <sys/systm.h> 56#include <sys/proc.h> 57#include <sys/vnode.h> 58#include <sys/mount.h> 59#include <sys/buf.h> 60#include <sys/vmmeter.h> 61 62#include <vm/vm.h> 63#include <vm/vm_prot.h> 64#include <vm/vm_object.h> 65#include <vm/vm_page.h> 66#include <vm/vm_pager.h> 67#include <vm/vm_map.h> 68#include <vm/vnode_pager.h> 69#include <vm/vm_extern.h> 70 71static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address, 72 int *run)); 73static void vnode_pager_iodone __P((struct buf *bp)); 74static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m)); 75static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m)); 76static void vnode_pager_dealloc __P((vm_object_t)); 77static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 78static void vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *)); 79static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 80 81struct pagerops vnodepagerops = { 82 NULL, 83 vnode_pager_alloc, 84 vnode_pager_dealloc, 85 vnode_pager_getpages, 86 vnode_pager_putpages, 87 vnode_pager_haspage, 88 NULL 89}; 90 91int vnode_pbuf_freecnt = -1; /* start out unlimited */ 92 93 94/* 95 * Allocate (or lookup) pager for a vnode. 96 * Handle is a vnode pointer. 97 */ 98vm_object_t 99vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 100 vm_ooffset_t offset) 101{ 102 vm_object_t object; 103 struct vnode *vp; 104 105 /* 106 * Pageout to vnode, no can do yet. 107 */ 108 if (handle == NULL) 109 return (NULL); 110 111 /* 112 * XXX hack - This initialization should be put somewhere else. 113 */ 114 if (vnode_pbuf_freecnt < 0) { 115 vnode_pbuf_freecnt = nswbuf / 2 + 1; 116 } 117 118 vp = (struct vnode *) handle; 119 120 /* 121 * Prevent race condition when allocating the object. This 122 * can happen with NFS vnodes since the nfsnode isn't locked. 123 */ 124 while (vp->v_flag & VOLOCK) { 125 vp->v_flag |= VOWANT; 126 tsleep(vp, PVM, "vnpobj", 0); 127 } 128 vp->v_flag |= VOLOCK; 129 130 /* 131 * If the object is being terminated, wait for it to 132 * go away. 133 */ 134 while (((object = vp->v_object) != NULL) && 135 (object->flags & OBJ_DEAD)) { 136 tsleep(object, PVM, "vadead", 0); 137 } 138 139 if (vp->v_usecount == 0) 140 panic("vnode_pager_alloc: no vnode reference"); 141 142 if (object == NULL) { 143 /* 144 * And an object of the appropriate size 145 */ 146 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 147 object->flags = 0; 148 149 object->un_pager.vnp.vnp_size = size; 150 151 object->handle = handle; 152 vp->v_object = object; 153 vp->v_usecount++; 154 } else { 155 object->ref_count++; 156 vp->v_usecount++; 157 } 158 159 vp->v_flag &= ~VOLOCK; 160 if (vp->v_flag & VOWANT) { 161 vp->v_flag &= ~VOWANT; 162 wakeup(vp); 163 } 164 return (object); 165} 166 167static void 168vnode_pager_dealloc(object) 169 vm_object_t object; 170{ 171 register struct vnode *vp = object->handle; 172 173 if (vp == NULL) 174 panic("vnode_pager_dealloc: pager already dealloced"); 175 176 vm_object_pip_wait(object, "vnpdea"); 177 178 object->handle = NULL; 179 object->type = OBJT_DEAD; 180 vp->v_object = NULL; 181 vp->v_flag &= ~(VTEXT | VOBJBUF); 182} 183 184static boolean_t 185vnode_pager_haspage(object, pindex, before, after) 186 vm_object_t object; 187 vm_pindex_t pindex; 188 int *before; 189 int *after; 190{ 191 struct vnode *vp = object->handle; 192 daddr_t bn; 193 int err; 194 daddr_t reqblock; 195 int poff; 196 int bsize; 197 int pagesperblock, blocksperpage; 198 199 if ((vp == NULL) || (vp->v_flag & VDOOMED)) 200 return FALSE; 201 202 /* 203 * If filesystem no longer mounted or offset beyond end of file we do 204 * not have the page. 205 */ 206 if ((vp->v_mount == NULL) || 207 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 208 return FALSE; 209 210 bsize = vp->v_mount->mnt_stat.f_iosize; 211 pagesperblock = bsize / PAGE_SIZE; 212 blocksperpage = 0; 213 if (pagesperblock > 0) { 214 reqblock = pindex / pagesperblock; 215 } else { 216 blocksperpage = (PAGE_SIZE / bsize); 217 reqblock = pindex * blocksperpage; 218 } 219 err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 220 after, before); 221 if (err) 222 return TRUE; 223 if ( bn == -1) 224 return FALSE; 225 if (pagesperblock > 0) { 226 poff = pindex - (reqblock * pagesperblock); 227 if (before) { 228 *before *= pagesperblock; 229 *before += poff; 230 } 231 if (after) { 232 int numafter; 233 *after *= pagesperblock; 234 numafter = pagesperblock - (poff + 1); 235 if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 236 numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 237 } 238 *after += numafter; 239 } 240 } else { 241 if (before) { 242 *before /= blocksperpage; 243 } 244 245 if (after) { 246 *after /= blocksperpage; 247 } 248 } 249 return TRUE; 250} 251 252/* 253 * Lets the VM system know about a change in size for a file. 254 * We adjust our own internal size and flush any cached pages in 255 * the associated object that are affected by the size change. 256 * 257 * Note: this routine may be invoked as a result of a pager put 258 * operation (possibly at object termination time), so we must be careful. 259 */ 260void 261vnode_pager_setsize(vp, nsize) 262 struct vnode *vp; 263 vm_ooffset_t nsize; 264{ 265 vm_pindex_t nobjsize; 266 vm_object_t object = vp->v_object; 267 268 if (object == NULL) 269 return; 270 271 /* 272 * Hasn't changed size 273 */ 274 if (nsize == object->un_pager.vnp.vnp_size) 275 return; 276 277 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 278 279 /* 280 * File has shrunk. Toss any cached pages beyond the new EOF. 281 */ 282 if (nsize < object->un_pager.vnp.vnp_size) { 283 vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size); 284 if (nobjsize < object->size) { 285 vm_object_page_remove(object, nobjsize, object->size, 286 FALSE); 287 } 288 /* 289 * this gets rid of garbage at the end of a page that is now 290 * only partially backed by the vnode... 291 */ 292 if (nsize & PAGE_MASK) { 293 vm_offset_t kva; 294 vm_page_t m; 295 296 m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 297 if (m) { 298 kva = vm_pager_map_page(m); 299 bzero((caddr_t) kva + (nsize & PAGE_MASK), 300 (int) (round_page(nsize) - nsize)); 301 vm_pager_unmap_page(kva); 302 } 303 } 304 } 305 object->un_pager.vnp.vnp_size = nsize; 306 object->size = nobjsize; 307} 308 309void 310vnode_pager_freepage(m) 311 vm_page_t m; 312{ 313 vm_page_free(m); 314} 315 316/* 317 * calculate the linear (byte) disk address of specified virtual 318 * file address 319 */ 320static vm_offset_t 321vnode_pager_addr(vp, address, run) 322 struct vnode *vp; 323 vm_ooffset_t address; 324 int *run; 325{ 326 int rtaddress; 327 int bsize; 328 daddr_t block; 329 struct vnode *rtvp; 330 int err; 331 daddr_t vblock; 332 int voffset; 333 334 if ((int) address < 0) 335 return -1; 336 337 if (vp->v_mount == NULL) 338 return -1; 339 340 bsize = vp->v_mount->mnt_stat.f_iosize; 341 vblock = address / bsize; 342 voffset = address % bsize; 343 344 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 345 346 if (err || (block == -1)) 347 rtaddress = -1; 348 else { 349 rtaddress = block + voffset / DEV_BSIZE; 350 if( run) { 351 *run += 1; 352 *run *= bsize/PAGE_SIZE; 353 *run -= voffset/PAGE_SIZE; 354 } 355 } 356 357 return rtaddress; 358} 359 360/* 361 * interrupt routine for I/O completion 362 */ 363static void 364vnode_pager_iodone(bp) 365 struct buf *bp; 366{ 367 bp->b_flags |= B_DONE; 368 wakeup(bp); 369} 370 371/* 372 * small block file system vnode pager input 373 */ 374static int 375vnode_pager_input_smlfs(object, m) 376 vm_object_t object; 377 vm_page_t m; 378{ 379 int i; 380 int s; 381 struct vnode *dp, *vp; 382 struct buf *bp; 383 vm_offset_t kva; 384 int fileaddr; 385 vm_offset_t bsize; 386 int error = 0; 387 388 vp = object->handle; 389 if (vp->v_mount == NULL) 390 return VM_PAGER_BAD; 391 392 bsize = vp->v_mount->mnt_stat.f_iosize; 393 394 395 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 396 397 kva = vm_pager_map_page(m); 398 399 for (i = 0; i < PAGE_SIZE / bsize; i++) { 400 401 if (vm_page_bits(i * bsize, bsize) & m->valid) 402 continue; 403 404 fileaddr = vnode_pager_addr(vp, 405 IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 406 if (fileaddr != -1) { 407 bp = getpbuf(&vnode_pbuf_freecnt); 408 409 /* build a minimal buffer header */ 410 bp->b_flags = B_BUSY | B_READ | B_CALL; 411 bp->b_iodone = vnode_pager_iodone;
| 42 */ 43 44/* 45 * Page to/from files (vnodes). 46 */ 47 48/* 49 * TODO: 50 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will 51 * greatly re-simplify the vnode_pager. 52 */ 53 54#include <sys/param.h> 55#include <sys/systm.h> 56#include <sys/proc.h> 57#include <sys/vnode.h> 58#include <sys/mount.h> 59#include <sys/buf.h> 60#include <sys/vmmeter.h> 61 62#include <vm/vm.h> 63#include <vm/vm_prot.h> 64#include <vm/vm_object.h> 65#include <vm/vm_page.h> 66#include <vm/vm_pager.h> 67#include <vm/vm_map.h> 68#include <vm/vnode_pager.h> 69#include <vm/vm_extern.h> 70 71static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address, 72 int *run)); 73static void vnode_pager_iodone __P((struct buf *bp)); 74static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m)); 75static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m)); 76static void vnode_pager_dealloc __P((vm_object_t)); 77static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 78static void vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *)); 79static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 80 81struct pagerops vnodepagerops = { 82 NULL, 83 vnode_pager_alloc, 84 vnode_pager_dealloc, 85 vnode_pager_getpages, 86 vnode_pager_putpages, 87 vnode_pager_haspage, 88 NULL 89}; 90 91int vnode_pbuf_freecnt = -1; /* start out unlimited */ 92 93 94/* 95 * Allocate (or lookup) pager for a vnode. 96 * Handle is a vnode pointer. 97 */ 98vm_object_t 99vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 100 vm_ooffset_t offset) 101{ 102 vm_object_t object; 103 struct vnode *vp; 104 105 /* 106 * Pageout to vnode, no can do yet. 107 */ 108 if (handle == NULL) 109 return (NULL); 110 111 /* 112 * XXX hack - This initialization should be put somewhere else. 113 */ 114 if (vnode_pbuf_freecnt < 0) { 115 vnode_pbuf_freecnt = nswbuf / 2 + 1; 116 } 117 118 vp = (struct vnode *) handle; 119 120 /* 121 * Prevent race condition when allocating the object. This 122 * can happen with NFS vnodes since the nfsnode isn't locked. 123 */ 124 while (vp->v_flag & VOLOCK) { 125 vp->v_flag |= VOWANT; 126 tsleep(vp, PVM, "vnpobj", 0); 127 } 128 vp->v_flag |= VOLOCK; 129 130 /* 131 * If the object is being terminated, wait for it to 132 * go away. 133 */ 134 while (((object = vp->v_object) != NULL) && 135 (object->flags & OBJ_DEAD)) { 136 tsleep(object, PVM, "vadead", 0); 137 } 138 139 if (vp->v_usecount == 0) 140 panic("vnode_pager_alloc: no vnode reference"); 141 142 if (object == NULL) { 143 /* 144 * And an object of the appropriate size 145 */ 146 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size))); 147 object->flags = 0; 148 149 object->un_pager.vnp.vnp_size = size; 150 151 object->handle = handle; 152 vp->v_object = object; 153 vp->v_usecount++; 154 } else { 155 object->ref_count++; 156 vp->v_usecount++; 157 } 158 159 vp->v_flag &= ~VOLOCK; 160 if (vp->v_flag & VOWANT) { 161 vp->v_flag &= ~VOWANT; 162 wakeup(vp); 163 } 164 return (object); 165} 166 167static void 168vnode_pager_dealloc(object) 169 vm_object_t object; 170{ 171 register struct vnode *vp = object->handle; 172 173 if (vp == NULL) 174 panic("vnode_pager_dealloc: pager already dealloced"); 175 176 vm_object_pip_wait(object, "vnpdea"); 177 178 object->handle = NULL; 179 object->type = OBJT_DEAD; 180 vp->v_object = NULL; 181 vp->v_flag &= ~(VTEXT | VOBJBUF); 182} 183 184static boolean_t 185vnode_pager_haspage(object, pindex, before, after) 186 vm_object_t object; 187 vm_pindex_t pindex; 188 int *before; 189 int *after; 190{ 191 struct vnode *vp = object->handle; 192 daddr_t bn; 193 int err; 194 daddr_t reqblock; 195 int poff; 196 int bsize; 197 int pagesperblock, blocksperpage; 198 199 if ((vp == NULL) || (vp->v_flag & VDOOMED)) 200 return FALSE; 201 202 /* 203 * If filesystem no longer mounted or offset beyond end of file we do 204 * not have the page. 205 */ 206 if ((vp->v_mount == NULL) || 207 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 208 return FALSE; 209 210 bsize = vp->v_mount->mnt_stat.f_iosize; 211 pagesperblock = bsize / PAGE_SIZE; 212 blocksperpage = 0; 213 if (pagesperblock > 0) { 214 reqblock = pindex / pagesperblock; 215 } else { 216 blocksperpage = (PAGE_SIZE / bsize); 217 reqblock = pindex * blocksperpage; 218 } 219 err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 220 after, before); 221 if (err) 222 return TRUE; 223 if ( bn == -1) 224 return FALSE; 225 if (pagesperblock > 0) { 226 poff = pindex - (reqblock * pagesperblock); 227 if (before) { 228 *before *= pagesperblock; 229 *before += poff; 230 } 231 if (after) { 232 int numafter; 233 *after *= pagesperblock; 234 numafter = pagesperblock - (poff + 1); 235 if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 236 numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 237 } 238 *after += numafter; 239 } 240 } else { 241 if (before) { 242 *before /= blocksperpage; 243 } 244 245 if (after) { 246 *after /= blocksperpage; 247 } 248 } 249 return TRUE; 250} 251 252/* 253 * Lets the VM system know about a change in size for a file. 254 * We adjust our own internal size and flush any cached pages in 255 * the associated object that are affected by the size change. 256 * 257 * Note: this routine may be invoked as a result of a pager put 258 * operation (possibly at object termination time), so we must be careful. 259 */ 260void 261vnode_pager_setsize(vp, nsize) 262 struct vnode *vp; 263 vm_ooffset_t nsize; 264{ 265 vm_pindex_t nobjsize; 266 vm_object_t object = vp->v_object; 267 268 if (object == NULL) 269 return; 270 271 /* 272 * Hasn't changed size 273 */ 274 if (nsize == object->un_pager.vnp.vnp_size) 275 return; 276 277 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK); 278 279 /* 280 * File has shrunk. Toss any cached pages beyond the new EOF. 281 */ 282 if (nsize < object->un_pager.vnp.vnp_size) { 283 vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size); 284 if (nobjsize < object->size) { 285 vm_object_page_remove(object, nobjsize, object->size, 286 FALSE); 287 } 288 /* 289 * this gets rid of garbage at the end of a page that is now 290 * only partially backed by the vnode... 291 */ 292 if (nsize & PAGE_MASK) { 293 vm_offset_t kva; 294 vm_page_t m; 295 296 m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 297 if (m) { 298 kva = vm_pager_map_page(m); 299 bzero((caddr_t) kva + (nsize & PAGE_MASK), 300 (int) (round_page(nsize) - nsize)); 301 vm_pager_unmap_page(kva); 302 } 303 } 304 } 305 object->un_pager.vnp.vnp_size = nsize; 306 object->size = nobjsize; 307} 308 309void 310vnode_pager_freepage(m) 311 vm_page_t m; 312{ 313 vm_page_free(m); 314} 315 316/* 317 * calculate the linear (byte) disk address of specified virtual 318 * file address 319 */ 320static vm_offset_t 321vnode_pager_addr(vp, address, run) 322 struct vnode *vp; 323 vm_ooffset_t address; 324 int *run; 325{ 326 int rtaddress; 327 int bsize; 328 daddr_t block; 329 struct vnode *rtvp; 330 int err; 331 daddr_t vblock; 332 int voffset; 333 334 if ((int) address < 0) 335 return -1; 336 337 if (vp->v_mount == NULL) 338 return -1; 339 340 bsize = vp->v_mount->mnt_stat.f_iosize; 341 vblock = address / bsize; 342 voffset = address % bsize; 343 344 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 345 346 if (err || (block == -1)) 347 rtaddress = -1; 348 else { 349 rtaddress = block + voffset / DEV_BSIZE; 350 if( run) { 351 *run += 1; 352 *run *= bsize/PAGE_SIZE; 353 *run -= voffset/PAGE_SIZE; 354 } 355 } 356 357 return rtaddress; 358} 359 360/* 361 * interrupt routine for I/O completion 362 */ 363static void 364vnode_pager_iodone(bp) 365 struct buf *bp; 366{ 367 bp->b_flags |= B_DONE; 368 wakeup(bp); 369} 370 371/* 372 * small block file system vnode pager input 373 */ 374static int 375vnode_pager_input_smlfs(object, m) 376 vm_object_t object; 377 vm_page_t m; 378{ 379 int i; 380 int s; 381 struct vnode *dp, *vp; 382 struct buf *bp; 383 vm_offset_t kva; 384 int fileaddr; 385 vm_offset_t bsize; 386 int error = 0; 387 388 vp = object->handle; 389 if (vp->v_mount == NULL) 390 return VM_PAGER_BAD; 391 392 bsize = vp->v_mount->mnt_stat.f_iosize; 393 394 395 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 396 397 kva = vm_pager_map_page(m); 398 399 for (i = 0; i < PAGE_SIZE / bsize; i++) { 400 401 if (vm_page_bits(i * bsize, bsize) & m->valid) 402 continue; 403 404 fileaddr = vnode_pager_addr(vp, 405 IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 406 if (fileaddr != -1) { 407 bp = getpbuf(&vnode_pbuf_freecnt); 408 409 /* build a minimal buffer header */ 410 bp->b_flags = B_BUSY | B_READ | B_CALL; 411 bp->b_iodone = vnode_pager_iodone;
|
412 bp->b_proc = curproc; 413 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
| 412 bp->b_rcred = bp->b_wcred = curproc->p_ucred;
|
414 if (bp->b_rcred != NOCRED) 415 crhold(bp->b_rcred); 416 if (bp->b_wcred != NOCRED) 417 crhold(bp->b_wcred); 418 bp->b_data = (caddr_t) kva + i * bsize; 419 bp->b_blkno = fileaddr; 420 pbgetvp(dp, bp); 421 bp->b_bcount = bsize; 422 bp->b_bufsize = bsize; 423 424 /* do the input */ 425 VOP_STRATEGY(bp->b_vp, bp); 426 427 /* we definitely need to be at splvm here */ 428 429 s = splvm(); 430 while ((bp->b_flags & B_DONE) == 0) { 431 tsleep(bp, PVM, "vnsrd", 0); 432 } 433 splx(s); 434 if ((bp->b_flags & B_ERROR) != 0) 435 error = EIO; 436 437 /* 438 * free the buffer header back to the swap buffer pool 439 */ 440 relpbuf(bp, &vnode_pbuf_freecnt); 441 if (error) 442 break; 443 444 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 445 } else { 446 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 447 bzero((caddr_t) kva + i * bsize, bsize); 448 } 449 } 450 vm_pager_unmap_page(kva); 451 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 452 vm_page_flag_clear(m, PG_ZERO); 453 if (error) { 454 return VM_PAGER_ERROR; 455 } 456 return VM_PAGER_OK; 457 458} 459 460 461/* 462 * old style vnode pager output routine 463 */ 464static int 465vnode_pager_input_old(object, m) 466 vm_object_t object; 467 vm_page_t m; 468{ 469 struct uio auio; 470 struct iovec aiov; 471 int error; 472 int size; 473 vm_offset_t kva; 474 475 error = 0; 476 477 /* 478 * Return failure if beyond current EOF 479 */ 480 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 481 return VM_PAGER_BAD; 482 } else { 483 size = PAGE_SIZE; 484 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 485 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 486 487 /* 488 * Allocate a kernel virtual address and initialize so that 489 * we can use VOP_READ/WRITE routines. 490 */ 491 kva = vm_pager_map_page(m); 492 493 aiov.iov_base = (caddr_t) kva; 494 aiov.iov_len = size; 495 auio.uio_iov = &aiov; 496 auio.uio_iovcnt = 1; 497 auio.uio_offset = IDX_TO_OFF(m->pindex); 498 auio.uio_segflg = UIO_SYSSPACE; 499 auio.uio_rw = UIO_READ; 500 auio.uio_resid = size; 501 auio.uio_procp = curproc; 502 503 error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 504 if (!error) { 505 register int count = size - auio.uio_resid; 506 507 if (count == 0) 508 error = EINVAL; 509 else if (count != PAGE_SIZE) 510 bzero((caddr_t) kva + count, PAGE_SIZE - count); 511 } 512 vm_pager_unmap_page(kva); 513 } 514 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 515 m->dirty = 0; 516 vm_page_flag_clear(m, PG_ZERO); 517 if (!error) 518 m->valid = VM_PAGE_BITS_ALL; 519 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 520} 521 522/* 523 * generic vnode pager input routine 524 */ 525 526/* 527 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 528 * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to 529 * vnode_pager_generic_getpages() to implement the previous behaviour. 530 * 531 * All other FS's should use the bypass to get to the local media 532 * backing vp's VOP_GETPAGES. 533 */ 534static int 535vnode_pager_getpages(object, m, count, reqpage) 536 vm_object_t object; 537 vm_page_t *m; 538 int count; 539 int reqpage; 540{ 541 int rtval; 542 struct vnode *vp; 543 int bytes = count * PAGE_SIZE; 544 545 vp = object->handle; 546 /* 547 * XXX temporary diagnostic message to help track stale FS code, 548 * Returning EOPNOTSUPP from here may make things unhappy. 549 */ 550 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 551 if (rtval == EOPNOTSUPP) { 552 printf("vnode_pager: *** WARNING *** stale FS getpages\n"); 553 rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage); 554 } 555 return rtval; 556} 557 558 559/* 560 * This is now called from local media FS's to operate against their 561 * own vnodes if they fail to implement VOP_GETPAGES. 562 */ 563int 564vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 565 struct vnode *vp; 566 vm_page_t *m; 567 int bytecount; 568 int reqpage; 569{ 570 vm_object_t object; 571 vm_offset_t kva; 572 off_t foff, tfoff, nextoff; 573 int i, size, bsize, first, firstaddr; 574 struct vnode *dp; 575 int runpg; 576 int runend; 577 struct buf *bp; 578 int s; 579 int count; 580 int error = 0; 581 582 object = vp->v_object; 583 count = bytecount / PAGE_SIZE; 584 585 if (vp->v_mount == NULL) 586 return VM_PAGER_BAD; 587 588 bsize = vp->v_mount->mnt_stat.f_iosize; 589 590 /* get the UNDERLYING device for the file with VOP_BMAP() */ 591 592 /* 593 * originally, we did not check for an error return value -- assuming 594 * an fs always has a bmap entry point -- that assumption is wrong!!! 595 */ 596 foff = IDX_TO_OFF(m[reqpage]->pindex); 597 598 /* 599 * if we can't bmap, use old VOP code 600 */ 601 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 602 for (i = 0; i < count; i++) { 603 if (i != reqpage) { 604 vnode_pager_freepage(m[i]); 605 } 606 } 607 cnt.v_vnodein++; 608 cnt.v_vnodepgsin++; 609 return vnode_pager_input_old(object, m[reqpage]); 610 611 /* 612 * if the blocksize is smaller than a page size, then use 613 * special small filesystem code. NFS sometimes has a small 614 * blocksize, but it can handle large reads itself. 615 */ 616 } else if ((PAGE_SIZE / bsize) > 1 && 617 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 618 for (i = 0; i < count; i++) { 619 if (i != reqpage) { 620 vnode_pager_freepage(m[i]); 621 } 622 } 623 cnt.v_vnodein++; 624 cnt.v_vnodepgsin++; 625 return vnode_pager_input_smlfs(object, m[reqpage]); 626 } 627 628 /* 629 * If we have a completely valid page available to us, we can 630 * clean up and return. Otherwise we have to re-read the 631 * media. 632 */ 633 634 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 635 for (i = 0; i < count; i++) { 636 if (i != reqpage) 637 vnode_pager_freepage(m[i]); 638 } 639 return VM_PAGER_OK; 640 } 641 m[reqpage]->valid = 0; 642 643 /* 644 * here on direct device I/O 645 */ 646 647 firstaddr = -1; 648 /* 649 * calculate the run that includes the required page 650 */ 651 for(first = 0, i = 0; i < count; i = runend) { 652 firstaddr = vnode_pager_addr(vp, 653 IDX_TO_OFF(m[i]->pindex), &runpg); 654 if (firstaddr == -1) { 655 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 656 /* XXX no %qd in kernel. */ 657 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx", 658 firstaddr, (u_long)(foff >> 32), 659 (u_long)(u_int32_t)foff, 660 (u_long)(u_int32_t) 661 (object->un_pager.vnp.vnp_size >> 32), 662 (u_long)(u_int32_t) 663 object->un_pager.vnp.vnp_size); 664 } 665 vnode_pager_freepage(m[i]); 666 runend = i + 1; 667 first = runend; 668 continue; 669 } 670 runend = i + runpg; 671 if (runend <= reqpage) { 672 int j; 673 for (j = i; j < runend; j++) { 674 vnode_pager_freepage(m[j]); 675 } 676 } else { 677 if (runpg < (count - first)) { 678 for (i = first + runpg; i < count; i++) 679 vnode_pager_freepage(m[i]); 680 count = first + runpg; 681 } 682 break; 683 } 684 first = runend; 685 } 686 687 /* 688 * the first and last page have been calculated now, move input pages 689 * to be zero based... 690 */ 691 if (first != 0) { 692 for (i = first; i < count; i++) { 693 m[i - first] = m[i]; 694 } 695 count -= first; 696 reqpage -= first; 697 } 698 699 /* 700 * calculate the file virtual address for the transfer 701 */ 702 foff = IDX_TO_OFF(m[0]->pindex); 703 704 /* 705 * calculate the size of the transfer 706 */ 707 size = count * PAGE_SIZE; 708 if ((foff + size) > object->un_pager.vnp.vnp_size) 709 size = object->un_pager.vnp.vnp_size - foff; 710 711 /* 712 * round up physical size for real devices 713 */ 714 if (dp->v_type == VBLK || dp->v_type == VCHR) 715 size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 716 717 bp = getpbuf(&vnode_pbuf_freecnt); 718 kva = (vm_offset_t) bp->b_data; 719 720 /* 721 * and map the pages to be read into the kva 722 */ 723 pmap_qenter(kva, m, count); 724 725 /* build a minimal buffer header */ 726 bp->b_flags = B_BUSY | B_READ | B_CALL; 727 bp->b_iodone = vnode_pager_iodone; 728 /* B_PHYS is not set, but it is nice to fill this in */
| 413 if (bp->b_rcred != NOCRED) 414 crhold(bp->b_rcred); 415 if (bp->b_wcred != NOCRED) 416 crhold(bp->b_wcred); 417 bp->b_data = (caddr_t) kva + i * bsize; 418 bp->b_blkno = fileaddr; 419 pbgetvp(dp, bp); 420 bp->b_bcount = bsize; 421 bp->b_bufsize = bsize; 422 423 /* do the input */ 424 VOP_STRATEGY(bp->b_vp, bp); 425 426 /* we definitely need to be at splvm here */ 427 428 s = splvm(); 429 while ((bp->b_flags & B_DONE) == 0) { 430 tsleep(bp, PVM, "vnsrd", 0); 431 } 432 splx(s); 433 if ((bp->b_flags & B_ERROR) != 0) 434 error = EIO; 435 436 /* 437 * free the buffer header back to the swap buffer pool 438 */ 439 relpbuf(bp, &vnode_pbuf_freecnt); 440 if (error) 441 break; 442 443 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 444 } else { 445 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 446 bzero((caddr_t) kva + i * bsize, bsize); 447 } 448 } 449 vm_pager_unmap_page(kva); 450 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 451 vm_page_flag_clear(m, PG_ZERO); 452 if (error) { 453 return VM_PAGER_ERROR; 454 } 455 return VM_PAGER_OK; 456 457} 458 459 460/* 461 * old style vnode pager output routine 462 */ 463static int 464vnode_pager_input_old(object, m) 465 vm_object_t object; 466 vm_page_t m; 467{ 468 struct uio auio; 469 struct iovec aiov; 470 int error; 471 int size; 472 vm_offset_t kva; 473 474 error = 0; 475 476 /* 477 * Return failure if beyond current EOF 478 */ 479 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 480 return VM_PAGER_BAD; 481 } else { 482 size = PAGE_SIZE; 483 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 484 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 485 486 /* 487 * Allocate a kernel virtual address and initialize so that 488 * we can use VOP_READ/WRITE routines. 489 */ 490 kva = vm_pager_map_page(m); 491 492 aiov.iov_base = (caddr_t) kva; 493 aiov.iov_len = size; 494 auio.uio_iov = &aiov; 495 auio.uio_iovcnt = 1; 496 auio.uio_offset = IDX_TO_OFF(m->pindex); 497 auio.uio_segflg = UIO_SYSSPACE; 498 auio.uio_rw = UIO_READ; 499 auio.uio_resid = size; 500 auio.uio_procp = curproc; 501 502 error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 503 if (!error) { 504 register int count = size - auio.uio_resid; 505 506 if (count == 0) 507 error = EINVAL; 508 else if (count != PAGE_SIZE) 509 bzero((caddr_t) kva + count, PAGE_SIZE - count); 510 } 511 vm_pager_unmap_page(kva); 512 } 513 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 514 m->dirty = 0; 515 vm_page_flag_clear(m, PG_ZERO); 516 if (!error) 517 m->valid = VM_PAGE_BITS_ALL; 518 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 519} 520 521/* 522 * generic vnode pager input routine 523 */ 524 525/* 526 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 527 * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to 528 * vnode_pager_generic_getpages() to implement the previous behaviour. 529 * 530 * All other FS's should use the bypass to get to the local media 531 * backing vp's VOP_GETPAGES. 532 */ 533static int 534vnode_pager_getpages(object, m, count, reqpage) 535 vm_object_t object; 536 vm_page_t *m; 537 int count; 538 int reqpage; 539{ 540 int rtval; 541 struct vnode *vp; 542 int bytes = count * PAGE_SIZE; 543 544 vp = object->handle; 545 /* 546 * XXX temporary diagnostic message to help track stale FS code, 547 * Returning EOPNOTSUPP from here may make things unhappy. 548 */ 549 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 550 if (rtval == EOPNOTSUPP) { 551 printf("vnode_pager: *** WARNING *** stale FS getpages\n"); 552 rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage); 553 } 554 return rtval; 555} 556 557 558/* 559 * This is now called from local media FS's to operate against their 560 * own vnodes if they fail to implement VOP_GETPAGES. 561 */ 562int 563vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 564 struct vnode *vp; 565 vm_page_t *m; 566 int bytecount; 567 int reqpage; 568{ 569 vm_object_t object; 570 vm_offset_t kva; 571 off_t foff, tfoff, nextoff; 572 int i, size, bsize, first, firstaddr; 573 struct vnode *dp; 574 int runpg; 575 int runend; 576 struct buf *bp; 577 int s; 578 int count; 579 int error = 0; 580 581 object = vp->v_object; 582 count = bytecount / PAGE_SIZE; 583 584 if (vp->v_mount == NULL) 585 return VM_PAGER_BAD; 586 587 bsize = vp->v_mount->mnt_stat.f_iosize; 588 589 /* get the UNDERLYING device for the file with VOP_BMAP() */ 590 591 /* 592 * originally, we did not check for an error return value -- assuming 593 * an fs always has a bmap entry point -- that assumption is wrong!!! 594 */ 595 foff = IDX_TO_OFF(m[reqpage]->pindex); 596 597 /* 598 * if we can't bmap, use old VOP code 599 */ 600 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 601 for (i = 0; i < count; i++) { 602 if (i != reqpage) { 603 vnode_pager_freepage(m[i]); 604 } 605 } 606 cnt.v_vnodein++; 607 cnt.v_vnodepgsin++; 608 return vnode_pager_input_old(object, m[reqpage]); 609 610 /* 611 * if the blocksize is smaller than a page size, then use 612 * special small filesystem code. NFS sometimes has a small 613 * blocksize, but it can handle large reads itself. 614 */ 615 } else if ((PAGE_SIZE / bsize) > 1 && 616 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) { 617 for (i = 0; i < count; i++) { 618 if (i != reqpage) { 619 vnode_pager_freepage(m[i]); 620 } 621 } 622 cnt.v_vnodein++; 623 cnt.v_vnodepgsin++; 624 return vnode_pager_input_smlfs(object, m[reqpage]); 625 } 626 627 /* 628 * If we have a completely valid page available to us, we can 629 * clean up and return. Otherwise we have to re-read the 630 * media. 631 */ 632 633 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) { 634 for (i = 0; i < count; i++) { 635 if (i != reqpage) 636 vnode_pager_freepage(m[i]); 637 } 638 return VM_PAGER_OK; 639 } 640 m[reqpage]->valid = 0; 641 642 /* 643 * here on direct device I/O 644 */ 645 646 firstaddr = -1; 647 /* 648 * calculate the run that includes the required page 649 */ 650 for(first = 0, i = 0; i < count; i = runend) { 651 firstaddr = vnode_pager_addr(vp, 652 IDX_TO_OFF(m[i]->pindex), &runpg); 653 if (firstaddr == -1) { 654 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 655 /* XXX no %qd in kernel. */ 656 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx", 657 firstaddr, (u_long)(foff >> 32), 658 (u_long)(u_int32_t)foff, 659 (u_long)(u_int32_t) 660 (object->un_pager.vnp.vnp_size >> 32), 661 (u_long)(u_int32_t) 662 object->un_pager.vnp.vnp_size); 663 } 664 vnode_pager_freepage(m[i]); 665 runend = i + 1; 666 first = runend; 667 continue; 668 } 669 runend = i + runpg; 670 if (runend <= reqpage) { 671 int j; 672 for (j = i; j < runend; j++) { 673 vnode_pager_freepage(m[j]); 674 } 675 } else { 676 if (runpg < (count - first)) { 677 for (i = first + runpg; i < count; i++) 678 vnode_pager_freepage(m[i]); 679 count = first + runpg; 680 } 681 break; 682 } 683 first = runend; 684 } 685 686 /* 687 * the first and last page have been calculated now, move input pages 688 * to be zero based... 689 */ 690 if (first != 0) { 691 for (i = first; i < count; i++) { 692 m[i - first] = m[i]; 693 } 694 count -= first; 695 reqpage -= first; 696 } 697 698 /* 699 * calculate the file virtual address for the transfer 700 */ 701 foff = IDX_TO_OFF(m[0]->pindex); 702 703 /* 704 * calculate the size of the transfer 705 */ 706 size = count * PAGE_SIZE; 707 if ((foff + size) > object->un_pager.vnp.vnp_size) 708 size = object->un_pager.vnp.vnp_size - foff; 709 710 /* 711 * round up physical size for real devices 712 */ 713 if (dp->v_type == VBLK || dp->v_type == VCHR) 714 size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 715 716 bp = getpbuf(&vnode_pbuf_freecnt); 717 kva = (vm_offset_t) bp->b_data; 718 719 /* 720 * and map the pages to be read into the kva 721 */ 722 pmap_qenter(kva, m, count); 723 724 /* build a minimal buffer header */ 725 bp->b_flags = B_BUSY | B_READ | B_CALL; 726 bp->b_iodone = vnode_pager_iodone; 727 /* B_PHYS is not set, but it is nice to fill this in */
|
729 bp->b_proc = curproc; 730 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
| 728 bp->b_rcred = bp->b_wcred = curproc->p_ucred;
|
731 if (bp->b_rcred != NOCRED) 732 crhold(bp->b_rcred); 733 if (bp->b_wcred != NOCRED) 734 crhold(bp->b_wcred); 735 bp->b_blkno = firstaddr; 736 pbgetvp(dp, bp); 737 bp->b_bcount = size; 738 bp->b_bufsize = size; 739 740 cnt.v_vnodein++; 741 cnt.v_vnodepgsin += count; 742 743 /* do the input */ 744 VOP_STRATEGY(bp->b_vp, bp); 745 746 s = splvm(); 747 /* we definitely need to be at splvm here */ 748 749 while ((bp->b_flags & B_DONE) == 0) { 750 tsleep(bp, PVM, "vnread", 0); 751 } 752 splx(s); 753 if ((bp->b_flags & B_ERROR) != 0) 754 error = EIO; 755 756 if (!error) { 757 if (size != count * PAGE_SIZE) 758 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 759 } 760 pmap_qremove(kva, count); 761 762 /* 763 * free the buffer header back to the swap buffer pool 764 */ 765 relpbuf(bp, &vnode_pbuf_freecnt); 766 767 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 768 vm_page_t mt; 769 770 nextoff = tfoff + PAGE_SIZE; 771 mt = m[i]; 772 773 if (nextoff <= size) { 774 /* 775 * Read filled up entire page. 776 */ 777 mt->valid = VM_PAGE_BITS_ALL; 778 mt->dirty = 0; 779 pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 780 } else { 781 /* 782 * Read did not fill up entire page. Since this 783 * is getpages, the page may be mapped, so we have 784 * to zero the invalid portions of the page even 785 * though we aren't setting them valid. 786 * 787 * Currently we do not set the entire page valid, 788 * we just try to clear the piece that we couldn't 789 * read. 790 */ 791 vm_page_set_validclean(mt, 0, size - tfoff); 792 /* handled by vm_fault now */ 793 /* vm_page_zero_invalid(mt, FALSE); */ 794 } 795 796 vm_page_flag_clear(mt, PG_ZERO); 797 if (i != reqpage) { 798 799 /* 800 * whether or not to leave the page activated is up in 801 * the air, but we should put the page on a page queue 802 * somewhere. (it already is in the object). Result: 803 * It appears that emperical results show that 804 * deactivating pages is best. 805 */ 806 807 /* 808 * just in case someone was asking for this page we 809 * now tell them that it is ok to use 810 */ 811 if (!error) { 812 if (mt->flags & PG_WANTED) 813 vm_page_activate(mt); 814 else 815 vm_page_deactivate(mt); 816 vm_page_wakeup(mt); 817 } else { 818 vnode_pager_freepage(mt); 819 } 820 } 821 } 822 if (error) { 823 printf("vnode_pager_getpages: I/O read error\n"); 824 } 825 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 826} 827 828/* 829 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 830 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 831 * vnode_pager_generic_putpages() to implement the previous behaviour. 832 * 833 * All other FS's should use the bypass to get to the local media 834 * backing vp's VOP_PUTPAGES. 835 */ 836static void 837vnode_pager_putpages(object, m, count, sync, rtvals) 838 vm_object_t object; 839 vm_page_t *m; 840 int count; 841 boolean_t sync; 842 int *rtvals; 843{ 844 int rtval; 845 struct vnode *vp; 846 int bytes = count * PAGE_SIZE; 847 848 /* 849 * Force synchronous operation if we are extremely low on memory 850 * to prevent a low-memory deadlock. VOP operations often need to 851 * allocate more memory to initiate the I/O ( i.e. do a BMAP 852 * operation ). The swapper handles the case by limiting the amount 853 * of asynchronous I/O, but that sort of solution doesn't scale well 854 * for the vnode pager without a lot of work. 855 * 856 * Also, the backing vnode's iodone routine may not wake the pageout 857 * daemon up. This should be probably be addressed XXX. 858 */ 859 860 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 861 sync |= OBJPC_SYNC; 862 863 /* 864 * Call device-specific putpages function 865 */ 866 867 vp = object->handle; 868 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 869 if (rtval == EOPNOTSUPP) { 870 printf("vnode_pager: *** WARNING *** stale FS putpages\n"); 871 rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals); 872 } 873} 874 875 876/* 877 * This is now called from local media FS's to operate against their 878 * own vnodes if they fail to implement VOP_PUTPAGES. 879 */ 880int 881vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 882 struct vnode *vp; 883 vm_page_t *m; 884 int bytecount; 885 int flags; 886 int *rtvals; 887{ 888 int i; 889 vm_object_t object; 890 int count; 891 892 int maxsize, ncount; 893 vm_ooffset_t poffset; 894 struct uio auio; 895 struct iovec aiov; 896 int error; 897 int ioflags; 898 899 object = vp->v_object; 900 count = bytecount / PAGE_SIZE; 901 902 for (i = 0; i < count; i++) 903 rtvals[i] = VM_PAGER_AGAIN; 904 905 if ((int) m[0]->pindex < 0) { 906 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", 907 m[0]->pindex, m[0]->dirty); 908 rtvals[0] = VM_PAGER_BAD; 909 return VM_PAGER_BAD; 910 } 911 912 maxsize = count * PAGE_SIZE; 913 ncount = count; 914 915 poffset = IDX_TO_OFF(m[0]->pindex); 916 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 917 if (object->un_pager.vnp.vnp_size > poffset) 918 maxsize = object->un_pager.vnp.vnp_size - poffset; 919 else 920 maxsize = 0; 921 ncount = btoc(maxsize); 922 if (ncount < count) { 923 for (i = ncount; i < count; i++) { 924 rtvals[i] = VM_PAGER_BAD; 925 } 926 } 927 } 928 929 ioflags = IO_VMIO; 930 ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: 0; 931 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 932 933 aiov.iov_base = (caddr_t) 0; 934 aiov.iov_len = maxsize; 935 auio.uio_iov = &aiov; 936 auio.uio_iovcnt = 1; 937 auio.uio_offset = poffset; 938 auio.uio_segflg = UIO_NOCOPY; 939 auio.uio_rw = UIO_WRITE; 940 auio.uio_resid = maxsize; 941 auio.uio_procp = (struct proc *) 0; 942 error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred); 943 cnt.v_vnodeout++; 944 cnt.v_vnodepgsout += ncount; 945 946 if (error) { 947 printf("vnode_pager_putpages: I/O error %d\n", error); 948 } 949 if (auio.uio_resid) { 950 printf("vnode_pager_putpages: residual I/O %d at %lu\n", 951 auio.uio_resid, (u_long)m[0]->pindex); 952 } 953 for (i = 0; i < ncount; i++) { 954 rtvals[i] = VM_PAGER_OK; 955 } 956 return rtvals[0]; 957} 958 959struct vnode * 960vnode_pager_lock(object) 961 vm_object_t object; 962{ 963 struct proc *p = curproc; /* XXX */ 964 965 for (; object != NULL; object = object->backing_object) { 966 if (object->type != OBJT_VNODE) 967 continue; 968 if (object->flags & OBJ_DEAD) 969 return NULL; 970 971 while (vget(object->handle, 972 LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) { 973 if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE)) 974 return NULL; 975 printf("vnode_pager_lock: retrying\n"); 976 } 977 return object->handle; 978 } 979 return NULL; 980}
| 729 if (bp->b_rcred != NOCRED) 730 crhold(bp->b_rcred); 731 if (bp->b_wcred != NOCRED) 732 crhold(bp->b_wcred); 733 bp->b_blkno = firstaddr; 734 pbgetvp(dp, bp); 735 bp->b_bcount = size; 736 bp->b_bufsize = size; 737 738 cnt.v_vnodein++; 739 cnt.v_vnodepgsin += count; 740 741 /* do the input */ 742 VOP_STRATEGY(bp->b_vp, bp); 743 744 s = splvm(); 745 /* we definitely need to be at splvm here */ 746 747 while ((bp->b_flags & B_DONE) == 0) { 748 tsleep(bp, PVM, "vnread", 0); 749 } 750 splx(s); 751 if ((bp->b_flags & B_ERROR) != 0) 752 error = EIO; 753 754 if (!error) { 755 if (size != count * PAGE_SIZE) 756 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 757 } 758 pmap_qremove(kva, count); 759 760 /* 761 * free the buffer header back to the swap buffer pool 762 */ 763 relpbuf(bp, &vnode_pbuf_freecnt); 764 765 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 766 vm_page_t mt; 767 768 nextoff = tfoff + PAGE_SIZE; 769 mt = m[i]; 770 771 if (nextoff <= size) { 772 /* 773 * Read filled up entire page. 774 */ 775 mt->valid = VM_PAGE_BITS_ALL; 776 mt->dirty = 0; 777 pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 778 } else { 779 /* 780 * Read did not fill up entire page. Since this 781 * is getpages, the page may be mapped, so we have 782 * to zero the invalid portions of the page even 783 * though we aren't setting them valid. 784 * 785 * Currently we do not set the entire page valid, 786 * we just try to clear the piece that we couldn't 787 * read. 788 */ 789 vm_page_set_validclean(mt, 0, size - tfoff); 790 /* handled by vm_fault now */ 791 /* vm_page_zero_invalid(mt, FALSE); */ 792 } 793 794 vm_page_flag_clear(mt, PG_ZERO); 795 if (i != reqpage) { 796 797 /* 798 * whether or not to leave the page activated is up in 799 * the air, but we should put the page on a page queue 800 * somewhere. (it already is in the object). Result: 801 * It appears that emperical results show that 802 * deactivating pages is best. 803 */ 804 805 /* 806 * just in case someone was asking for this page we 807 * now tell them that it is ok to use 808 */ 809 if (!error) { 810 if (mt->flags & PG_WANTED) 811 vm_page_activate(mt); 812 else 813 vm_page_deactivate(mt); 814 vm_page_wakeup(mt); 815 } else { 816 vnode_pager_freepage(mt); 817 } 818 } 819 } 820 if (error) { 821 printf("vnode_pager_getpages: I/O read error\n"); 822 } 823 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 824} 825 826/* 827 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 828 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 829 * vnode_pager_generic_putpages() to implement the previous behaviour. 830 * 831 * All other FS's should use the bypass to get to the local media 832 * backing vp's VOP_PUTPAGES. 833 */ 834static void 835vnode_pager_putpages(object, m, count, sync, rtvals) 836 vm_object_t object; 837 vm_page_t *m; 838 int count; 839 boolean_t sync; 840 int *rtvals; 841{ 842 int rtval; 843 struct vnode *vp; 844 int bytes = count * PAGE_SIZE; 845 846 /* 847 * Force synchronous operation if we are extremely low on memory 848 * to prevent a low-memory deadlock. VOP operations often need to 849 * allocate more memory to initiate the I/O ( i.e. do a BMAP 850 * operation ). The swapper handles the case by limiting the amount 851 * of asynchronous I/O, but that sort of solution doesn't scale well 852 * for the vnode pager without a lot of work. 853 * 854 * Also, the backing vnode's iodone routine may not wake the pageout 855 * daemon up. This should be probably be addressed XXX. 856 */ 857 858 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 859 sync |= OBJPC_SYNC; 860 861 /* 862 * Call device-specific putpages function 863 */ 864 865 vp = object->handle; 866 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 867 if (rtval == EOPNOTSUPP) { 868 printf("vnode_pager: *** WARNING *** stale FS putpages\n"); 869 rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals); 870 } 871} 872 873 874/* 875 * This is now called from local media FS's to operate against their 876 * own vnodes if they fail to implement VOP_PUTPAGES. 877 */ 878int 879vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 880 struct vnode *vp; 881 vm_page_t *m; 882 int bytecount; 883 int flags; 884 int *rtvals; 885{ 886 int i; 887 vm_object_t object; 888 int count; 889 890 int maxsize, ncount; 891 vm_ooffset_t poffset; 892 struct uio auio; 893 struct iovec aiov; 894 int error; 895 int ioflags; 896 897 object = vp->v_object; 898 count = bytecount / PAGE_SIZE; 899 900 for (i = 0; i < count; i++) 901 rtvals[i] = VM_PAGER_AGAIN; 902 903 if ((int) m[0]->pindex < 0) { 904 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", 905 m[0]->pindex, m[0]->dirty); 906 rtvals[0] = VM_PAGER_BAD; 907 return VM_PAGER_BAD; 908 } 909 910 maxsize = count * PAGE_SIZE; 911 ncount = count; 912 913 poffset = IDX_TO_OFF(m[0]->pindex); 914 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 915 if (object->un_pager.vnp.vnp_size > poffset) 916 maxsize = object->un_pager.vnp.vnp_size - poffset; 917 else 918 maxsize = 0; 919 ncount = btoc(maxsize); 920 if (ncount < count) { 921 for (i = ncount; i < count; i++) { 922 rtvals[i] = VM_PAGER_BAD; 923 } 924 } 925 } 926 927 ioflags = IO_VMIO; 928 ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: 0; 929 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 930 931 aiov.iov_base = (caddr_t) 0; 932 aiov.iov_len = maxsize; 933 auio.uio_iov = &aiov; 934 auio.uio_iovcnt = 1; 935 auio.uio_offset = poffset; 936 auio.uio_segflg = UIO_NOCOPY; 937 auio.uio_rw = UIO_WRITE; 938 auio.uio_resid = maxsize; 939 auio.uio_procp = (struct proc *) 0; 940 error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred); 941 cnt.v_vnodeout++; 942 cnt.v_vnodepgsout += ncount; 943 944 if (error) { 945 printf("vnode_pager_putpages: I/O error %d\n", error); 946 } 947 if (auio.uio_resid) { 948 printf("vnode_pager_putpages: residual I/O %d at %lu\n", 949 auio.uio_resid, (u_long)m[0]->pindex); 950 } 951 for (i = 0; i < ncount; i++) { 952 rtvals[i] = VM_PAGER_OK; 953 } 954 return rtvals[0]; 955} 956 957struct vnode * 958vnode_pager_lock(object) 959 vm_object_t object; 960{ 961 struct proc *p = curproc; /* XXX */ 962 963 for (; object != NULL; object = object->backing_object) { 964 if (object->type != OBJT_VNODE) 965 continue; 966 if (object->flags & OBJ_DEAD) 967 return NULL; 968 969 while (vget(object->handle, 970 LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) { 971 if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE)) 972 return NULL; 973 printf("vnode_pager_lock: retrying\n"); 974 } 975 return object->handle; 976 } 977 return NULL; 978}
|