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.90 1998/03/09 08:58:53 msmith Exp $
| 41 * $Id: vnode_pager.c,v 1.91 1998/03/16 01:56:03 dyson 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 int 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 91 92/* 93 * Allocate (or lookup) pager for a vnode. 94 * Handle is a vnode pointer. 95 */ 96vm_object_t 97vnode_pager_alloc(void *handle, vm_size_t size, vm_prot_t prot, 98 vm_ooffset_t offset) 99{ 100 vm_object_t object; 101 struct vnode *vp; 102 103 /* 104 * Pageout to vnode, no can do yet. 105 */ 106 if (handle == NULL) 107 return (NULL); 108 109 vp = (struct vnode *) handle; 110 111 /* 112 * Prevent race condition when allocating the object. This 113 * can happen with NFS vnodes since the nfsnode isn't locked. 114 */ 115 while (vp->v_flag & VOLOCK) { 116 vp->v_flag |= VOWANT; 117 tsleep(vp, PVM, "vnpobj", 0); 118 } 119 vp->v_flag |= VOLOCK; 120 121 /* 122 * If the object is being terminated, wait for it to 123 * go away. 124 */ 125 while (((object = vp->v_object) != NULL) && 126 (object->flags & OBJ_DEAD)) { 127 tsleep(object, PVM, "vadead", 0); 128 } 129 130 if (vp->v_usecount == 0) 131 panic("vnode_pager_alloc: no vnode reference"); 132 133 if (object == NULL) { 134 /* 135 * And an object of the appropriate size 136 */ 137 object = vm_object_allocate(OBJT_VNODE, size); 138 object->flags = 0; 139 140 object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE; 141 142 object->handle = handle; 143 vp->v_object = object; 144 vp->v_usecount++; 145 } else { 146 object->ref_count++; 147 vp->v_usecount++; 148 } 149 150 vp->v_flag &= ~VOLOCK; 151 if (vp->v_flag & VOWANT) { 152 vp->v_flag &= ~VOWANT; 153 wakeup(vp); 154 } 155 return (object); 156} 157 158static void 159vnode_pager_dealloc(object) 160 vm_object_t object; 161{ 162 register struct vnode *vp = object->handle; 163 164 if (vp == NULL) 165 panic("vnode_pager_dealloc: pager already dealloced"); 166 167 vm_object_pip_wait(object, "vnpdea"); 168 169 object->handle = NULL; 170 object->type = OBJT_DEAD; 171 vp->v_object = NULL; 172 vp->v_flag &= ~(VTEXT | VOBJBUF); 173} 174 175static boolean_t 176vnode_pager_haspage(object, pindex, before, after) 177 vm_object_t object; 178 vm_pindex_t pindex; 179 int *before; 180 int *after; 181{ 182 struct vnode *vp = object->handle; 183 daddr_t bn; 184 int err; 185 daddr_t reqblock; 186 int poff; 187 int bsize; 188 int pagesperblock, blocksperpage; 189 190 if ((vp == NULL) || (vp->v_flag & VDOOMED)) 191 return FALSE; 192 193 /* 194 * If filesystem no longer mounted or offset beyond end of file we do 195 * not have the page. 196 */ 197 if ((vp->v_mount == NULL) || 198 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 199 return FALSE; 200 201 bsize = vp->v_mount->mnt_stat.f_iosize; 202 pagesperblock = bsize / PAGE_SIZE; 203 blocksperpage = 0; 204 if (pagesperblock > 0) { 205 reqblock = pindex / pagesperblock; 206 } else { 207 blocksperpage = (PAGE_SIZE / bsize); 208 reqblock = pindex * blocksperpage; 209 } 210 err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 211 after, before); 212 if (err) 213 return TRUE; 214 if ( bn == -1) 215 return FALSE; 216 if (pagesperblock > 0) { 217 poff = pindex - (reqblock * pagesperblock); 218 if (before) { 219 *before *= pagesperblock; 220 *before += poff; 221 } 222 if (after) { 223 int numafter; 224 *after *= pagesperblock; 225 numafter = pagesperblock - (poff + 1); 226 if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 227 numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 228 } 229 *after += numafter; 230 } 231 } else { 232 if (before) { 233 *before /= blocksperpage; 234 } 235 236 if (after) { 237 *after /= blocksperpage; 238 } 239 } 240 return TRUE; 241} 242 243/* 244 * Lets the VM system know about a change in size for a file. 245 * We adjust our own internal size and flush any cached pages in 246 * the associated object that are affected by the size change. 247 * 248 * Note: this routine may be invoked as a result of a pager put 249 * operation (possibly at object termination time), so we must be careful. 250 */ 251void 252vnode_pager_setsize(vp, nsize) 253 struct vnode *vp; 254 vm_ooffset_t nsize; 255{ 256 vm_object_t object = vp->v_object; 257 258 if (object == NULL) 259 return; 260 261 /* 262 * Hasn't changed size 263 */ 264 if (nsize == object->un_pager.vnp.vnp_size) 265 return; 266 267 /* 268 * File has shrunk. Toss any cached pages beyond the new EOF. 269 */ 270 if (nsize < object->un_pager.vnp.vnp_size) { 271 vm_ooffset_t nsizerounded; 272 nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_MASK)); 273 if (nsizerounded < object->un_pager.vnp.vnp_size) { 274 vm_pindex_t st, end; 275 st = OFF_TO_IDX(nsize + PAGE_MASK); 276 end = OFF_TO_IDX(object->un_pager.vnp.vnp_size); 277 278 vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size); 279 vm_object_page_remove(object, st, end, FALSE); 280 } 281 /* 282 * this gets rid of garbage at the end of a page that is now 283 * only partially backed by the vnode... 284 */ 285 if (nsize & PAGE_MASK) { 286 vm_offset_t kva; 287 vm_page_t m; 288 289 m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 290 if (m) { 291 kva = vm_pager_map_page(m); 292 bzero((caddr_t) kva + (nsize & PAGE_MASK), 293 (int) (round_page(nsize) - nsize)); 294 vm_pager_unmap_page(kva); 295 } 296 } 297 } 298 object->un_pager.vnp.vnp_size = nsize; 299 object->size = OFF_TO_IDX(nsize + PAGE_MASK); 300} 301 302void 303vnode_pager_freepage(m) 304 vm_page_t m; 305{ 306 vm_page_free(m); 307} 308 309/* 310 * calculate the linear (byte) disk address of specified virtual 311 * file address 312 */ 313static vm_offset_t 314vnode_pager_addr(vp, address, run) 315 struct vnode *vp; 316 vm_ooffset_t address; 317 int *run; 318{ 319 int rtaddress; 320 int bsize; 321 daddr_t block; 322 struct vnode *rtvp; 323 int err; 324 daddr_t vblock; 325 int voffset; 326 327 if ((int) address < 0) 328 return -1; 329 330 if (vp->v_mount == NULL) 331 return -1; 332 333 bsize = vp->v_mount->mnt_stat.f_iosize; 334 vblock = address / bsize; 335 voffset = address % bsize; 336 337 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 338 339 if (err || (block == -1)) 340 rtaddress = -1; 341 else { 342 rtaddress = block + voffset / DEV_BSIZE; 343 if( run) { 344 *run += 1; 345 *run *= bsize/PAGE_SIZE; 346 *run -= voffset/PAGE_SIZE; 347 } 348 } 349 350 return rtaddress; 351} 352 353/* 354 * interrupt routine for I/O completion 355 */ 356static void 357vnode_pager_iodone(bp) 358 struct buf *bp; 359{ 360 bp->b_flags |= B_DONE; 361 wakeup(bp); 362} 363 364/* 365 * small block file system vnode pager input 366 */ 367static int 368vnode_pager_input_smlfs(object, m) 369 vm_object_t object; 370 vm_page_t m; 371{ 372 int i; 373 int s; 374 struct vnode *dp, *vp; 375 struct buf *bp; 376 vm_offset_t kva; 377 int fileaddr; 378 vm_offset_t bsize; 379 int error = 0; 380 381 vp = object->handle; 382 if (vp->v_mount == NULL) 383 return VM_PAGER_BAD; 384 385 bsize = vp->v_mount->mnt_stat.f_iosize; 386 387 388 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 389 390 kva = vm_pager_map_page(m); 391 392 for (i = 0; i < PAGE_SIZE / bsize; i++) { 393 394 if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid)) 395 continue; 396 397 fileaddr = vnode_pager_addr(vp, 398 IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 399 if (fileaddr != -1) { 400 bp = getpbuf(); 401 402 /* build a minimal buffer header */ 403 bp->b_flags = B_BUSY | B_READ | B_CALL; 404 bp->b_iodone = vnode_pager_iodone; 405 bp->b_proc = curproc; 406 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 407 if (bp->b_rcred != NOCRED) 408 crhold(bp->b_rcred); 409 if (bp->b_wcred != NOCRED) 410 crhold(bp->b_wcred); 411 bp->b_data = (caddr_t) kva + i * bsize; 412 bp->b_blkno = fileaddr; 413 pbgetvp(dp, bp); 414 bp->b_bcount = bsize; 415 bp->b_bufsize = bsize; 416 417 /* do the input */
| 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 int 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 91 92/* 93 * Allocate (or lookup) pager for a vnode. 94 * Handle is a vnode pointer. 95 */ 96vm_object_t 97vnode_pager_alloc(void *handle, vm_size_t size, vm_prot_t prot, 98 vm_ooffset_t offset) 99{ 100 vm_object_t object; 101 struct vnode *vp; 102 103 /* 104 * Pageout to vnode, no can do yet. 105 */ 106 if (handle == NULL) 107 return (NULL); 108 109 vp = (struct vnode *) handle; 110 111 /* 112 * Prevent race condition when allocating the object. This 113 * can happen with NFS vnodes since the nfsnode isn't locked. 114 */ 115 while (vp->v_flag & VOLOCK) { 116 vp->v_flag |= VOWANT; 117 tsleep(vp, PVM, "vnpobj", 0); 118 } 119 vp->v_flag |= VOLOCK; 120 121 /* 122 * If the object is being terminated, wait for it to 123 * go away. 124 */ 125 while (((object = vp->v_object) != NULL) && 126 (object->flags & OBJ_DEAD)) { 127 tsleep(object, PVM, "vadead", 0); 128 } 129 130 if (vp->v_usecount == 0) 131 panic("vnode_pager_alloc: no vnode reference"); 132 133 if (object == NULL) { 134 /* 135 * And an object of the appropriate size 136 */ 137 object = vm_object_allocate(OBJT_VNODE, size); 138 object->flags = 0; 139 140 object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE; 141 142 object->handle = handle; 143 vp->v_object = object; 144 vp->v_usecount++; 145 } else { 146 object->ref_count++; 147 vp->v_usecount++; 148 } 149 150 vp->v_flag &= ~VOLOCK; 151 if (vp->v_flag & VOWANT) { 152 vp->v_flag &= ~VOWANT; 153 wakeup(vp); 154 } 155 return (object); 156} 157 158static void 159vnode_pager_dealloc(object) 160 vm_object_t object; 161{ 162 register struct vnode *vp = object->handle; 163 164 if (vp == NULL) 165 panic("vnode_pager_dealloc: pager already dealloced"); 166 167 vm_object_pip_wait(object, "vnpdea"); 168 169 object->handle = NULL; 170 object->type = OBJT_DEAD; 171 vp->v_object = NULL; 172 vp->v_flag &= ~(VTEXT | VOBJBUF); 173} 174 175static boolean_t 176vnode_pager_haspage(object, pindex, before, after) 177 vm_object_t object; 178 vm_pindex_t pindex; 179 int *before; 180 int *after; 181{ 182 struct vnode *vp = object->handle; 183 daddr_t bn; 184 int err; 185 daddr_t reqblock; 186 int poff; 187 int bsize; 188 int pagesperblock, blocksperpage; 189 190 if ((vp == NULL) || (vp->v_flag & VDOOMED)) 191 return FALSE; 192 193 /* 194 * If filesystem no longer mounted or offset beyond end of file we do 195 * not have the page. 196 */ 197 if ((vp->v_mount == NULL) || 198 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)) 199 return FALSE; 200 201 bsize = vp->v_mount->mnt_stat.f_iosize; 202 pagesperblock = bsize / PAGE_SIZE; 203 blocksperpage = 0; 204 if (pagesperblock > 0) { 205 reqblock = pindex / pagesperblock; 206 } else { 207 blocksperpage = (PAGE_SIZE / bsize); 208 reqblock = pindex * blocksperpage; 209 } 210 err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn, 211 after, before); 212 if (err) 213 return TRUE; 214 if ( bn == -1) 215 return FALSE; 216 if (pagesperblock > 0) { 217 poff = pindex - (reqblock * pagesperblock); 218 if (before) { 219 *before *= pagesperblock; 220 *before += poff; 221 } 222 if (after) { 223 int numafter; 224 *after *= pagesperblock; 225 numafter = pagesperblock - (poff + 1); 226 if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) { 227 numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex))); 228 } 229 *after += numafter; 230 } 231 } else { 232 if (before) { 233 *before /= blocksperpage; 234 } 235 236 if (after) { 237 *after /= blocksperpage; 238 } 239 } 240 return TRUE; 241} 242 243/* 244 * Lets the VM system know about a change in size for a file. 245 * We adjust our own internal size and flush any cached pages in 246 * the associated object that are affected by the size change. 247 * 248 * Note: this routine may be invoked as a result of a pager put 249 * operation (possibly at object termination time), so we must be careful. 250 */ 251void 252vnode_pager_setsize(vp, nsize) 253 struct vnode *vp; 254 vm_ooffset_t nsize; 255{ 256 vm_object_t object = vp->v_object; 257 258 if (object == NULL) 259 return; 260 261 /* 262 * Hasn't changed size 263 */ 264 if (nsize == object->un_pager.vnp.vnp_size) 265 return; 266 267 /* 268 * File has shrunk. Toss any cached pages beyond the new EOF. 269 */ 270 if (nsize < object->un_pager.vnp.vnp_size) { 271 vm_ooffset_t nsizerounded; 272 nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_MASK)); 273 if (nsizerounded < object->un_pager.vnp.vnp_size) { 274 vm_pindex_t st, end; 275 st = OFF_TO_IDX(nsize + PAGE_MASK); 276 end = OFF_TO_IDX(object->un_pager.vnp.vnp_size); 277 278 vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size); 279 vm_object_page_remove(object, st, end, FALSE); 280 } 281 /* 282 * this gets rid of garbage at the end of a page that is now 283 * only partially backed by the vnode... 284 */ 285 if (nsize & PAGE_MASK) { 286 vm_offset_t kva; 287 vm_page_t m; 288 289 m = vm_page_lookup(object, OFF_TO_IDX(nsize)); 290 if (m) { 291 kva = vm_pager_map_page(m); 292 bzero((caddr_t) kva + (nsize & PAGE_MASK), 293 (int) (round_page(nsize) - nsize)); 294 vm_pager_unmap_page(kva); 295 } 296 } 297 } 298 object->un_pager.vnp.vnp_size = nsize; 299 object->size = OFF_TO_IDX(nsize + PAGE_MASK); 300} 301 302void 303vnode_pager_freepage(m) 304 vm_page_t m; 305{ 306 vm_page_free(m); 307} 308 309/* 310 * calculate the linear (byte) disk address of specified virtual 311 * file address 312 */ 313static vm_offset_t 314vnode_pager_addr(vp, address, run) 315 struct vnode *vp; 316 vm_ooffset_t address; 317 int *run; 318{ 319 int rtaddress; 320 int bsize; 321 daddr_t block; 322 struct vnode *rtvp; 323 int err; 324 daddr_t vblock; 325 int voffset; 326 327 if ((int) address < 0) 328 return -1; 329 330 if (vp->v_mount == NULL) 331 return -1; 332 333 bsize = vp->v_mount->mnt_stat.f_iosize; 334 vblock = address / bsize; 335 voffset = address % bsize; 336 337 err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL); 338 339 if (err || (block == -1)) 340 rtaddress = -1; 341 else { 342 rtaddress = block + voffset / DEV_BSIZE; 343 if( run) { 344 *run += 1; 345 *run *= bsize/PAGE_SIZE; 346 *run -= voffset/PAGE_SIZE; 347 } 348 } 349 350 return rtaddress; 351} 352 353/* 354 * interrupt routine for I/O completion 355 */ 356static void 357vnode_pager_iodone(bp) 358 struct buf *bp; 359{ 360 bp->b_flags |= B_DONE; 361 wakeup(bp); 362} 363 364/* 365 * small block file system vnode pager input 366 */ 367static int 368vnode_pager_input_smlfs(object, m) 369 vm_object_t object; 370 vm_page_t m; 371{ 372 int i; 373 int s; 374 struct vnode *dp, *vp; 375 struct buf *bp; 376 vm_offset_t kva; 377 int fileaddr; 378 vm_offset_t bsize; 379 int error = 0; 380 381 vp = object->handle; 382 if (vp->v_mount == NULL) 383 return VM_PAGER_BAD; 384 385 bsize = vp->v_mount->mnt_stat.f_iosize; 386 387 388 VOP_BMAP(vp, 0, &dp, 0, NULL, NULL); 389 390 kva = vm_pager_map_page(m); 391 392 for (i = 0; i < PAGE_SIZE / bsize; i++) { 393 394 if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid)) 395 continue; 396 397 fileaddr = vnode_pager_addr(vp, 398 IDX_TO_OFF(m->pindex) + i * bsize, (int *)0); 399 if (fileaddr != -1) { 400 bp = getpbuf(); 401 402 /* build a minimal buffer header */ 403 bp->b_flags = B_BUSY | B_READ | B_CALL; 404 bp->b_iodone = vnode_pager_iodone; 405 bp->b_proc = curproc; 406 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 407 if (bp->b_rcred != NOCRED) 408 crhold(bp->b_rcred); 409 if (bp->b_wcred != NOCRED) 410 crhold(bp->b_wcred); 411 bp->b_data = (caddr_t) kva + i * bsize; 412 bp->b_blkno = fileaddr; 413 pbgetvp(dp, bp); 414 bp->b_bcount = bsize; 415 bp->b_bufsize = bsize; 416 417 /* do the input */
|
418 VOP_STRATEGY(bp);
| 418 VOP_STRATEGY(bp->b_vp, bp);
|
419 420 /* we definitely need to be at splvm here */ 421 422 s = splvm(); 423 while ((bp->b_flags & B_DONE) == 0) { 424 tsleep(bp, PVM, "vnsrd", 0); 425 } 426 splx(s); 427 if ((bp->b_flags & B_ERROR) != 0) 428 error = EIO; 429 430 /* 431 * free the buffer header back to the swap buffer pool 432 */ 433 relpbuf(bp); 434 if (error) 435 break; 436 437 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 438 } else { 439 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 440 bzero((caddr_t) kva + i * bsize, bsize); 441 } 442 } 443 vm_pager_unmap_page(kva); 444 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 445 m->flags &= ~PG_ZERO; 446 if (error) { 447 return VM_PAGER_ERROR; 448 } 449 return VM_PAGER_OK; 450 451} 452 453 454/* 455 * old style vnode pager output routine 456 */ 457static int 458vnode_pager_input_old(object, m) 459 vm_object_t object; 460 vm_page_t m; 461{ 462 struct uio auio; 463 struct iovec aiov; 464 int error; 465 int size; 466 vm_offset_t kva; 467 468 error = 0; 469 470 /* 471 * Return failure if beyond current EOF 472 */ 473 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 474 return VM_PAGER_BAD; 475 } else { 476 size = PAGE_SIZE; 477 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 478 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 479 480 /* 481 * Allocate a kernel virtual address and initialize so that 482 * we can use VOP_READ/WRITE routines. 483 */ 484 kva = vm_pager_map_page(m); 485 486 aiov.iov_base = (caddr_t) kva; 487 aiov.iov_len = size; 488 auio.uio_iov = &aiov; 489 auio.uio_iovcnt = 1; 490 auio.uio_offset = IDX_TO_OFF(m->pindex); 491 auio.uio_segflg = UIO_SYSSPACE; 492 auio.uio_rw = UIO_READ; 493 auio.uio_resid = size; 494 auio.uio_procp = (struct proc *) 0; 495 496 error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 497 if (!error) { 498 register int count = size - auio.uio_resid; 499 500 if (count == 0) 501 error = EINVAL; 502 else if (count != PAGE_SIZE) 503 bzero((caddr_t) kva + count, PAGE_SIZE - count); 504 } 505 vm_pager_unmap_page(kva); 506 } 507 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 508 m->dirty = 0; 509 m->flags &= ~PG_ZERO; 510 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 511} 512 513/* 514 * generic vnode pager input routine 515 */ 516 517/* 518 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 519 * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to 520 * vnode_pager_generic_getpages() to implement the previous behaviour. 521 * 522 * All other FS's should use the bypass to get to the local media 523 * backing vp's VOP_GETPAGES. 524 */ 525static int 526vnode_pager_getpages(object, m, count, reqpage) 527 vm_object_t object; 528 vm_page_t *m; 529 int count; 530 int reqpage; 531{ 532 int rtval; 533 struct vnode *vp; 534 int bytes = count * PAGE_SIZE; 535 536 vp = object->handle; 537 /* 538 * XXX temporary diagnostic message to help track stale FS code, 539 * Returning EOPNOTSUPP from here may make things unhappy. 540 */ 541 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 542 if (rtval == EOPNOTSUPP) { 543 printf("vnode_pager: *** WARNING *** stale FS getpages\n"); 544 rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage); 545 } 546 return rtval; 547} 548 549 550/* 551 * This is now called from local media FS's to operate against their 552 * own vnodes if they fail to implement VOP_GETPAGES. 553 */ 554int 555vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 556 struct vnode *vp; 557 vm_page_t *m; 558 int bytecount; 559 int reqpage; 560{ 561 vm_object_t object; 562 vm_offset_t kva; 563 off_t foff, tfoff, nextoff; 564 int i, size, bsize, first, firstaddr; 565 struct vnode *dp; 566 int runpg; 567 int runend; 568 struct buf *bp; 569 int s; 570 int count; 571 int error = 0; 572 573 object = vp->v_object; 574 count = bytecount / PAGE_SIZE; 575 576 if (vp->v_mount == NULL) 577 return VM_PAGER_BAD; 578 579 bsize = vp->v_mount->mnt_stat.f_iosize; 580 581 /* get the UNDERLYING device for the file with VOP_BMAP() */ 582 583 /* 584 * originally, we did not check for an error return value -- assuming 585 * an fs always has a bmap entry point -- that assumption is wrong!!! 586 */ 587 foff = IDX_TO_OFF(m[reqpage]->pindex); 588 589 /* 590 * if we can't bmap, use old VOP code 591 */ 592 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 593 for (i = 0; i < count; i++) { 594 if (i != reqpage) { 595 vnode_pager_freepage(m[i]); 596 } 597 } 598 cnt.v_vnodein++; 599 cnt.v_vnodepgsin++; 600 return vnode_pager_input_old(object, m[reqpage]); 601 602 /* 603 * if the blocksize is smaller than a page size, then use 604 * special small filesystem code. NFS sometimes has a small 605 * blocksize, but it can handle large reads itself. 606 */ 607 } else if ((PAGE_SIZE / bsize) > 1 && 608 (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) { 609 610 for (i = 0; i < count; i++) { 611 if (i != reqpage) { 612 vnode_pager_freepage(m[i]); 613 } 614 } 615 cnt.v_vnodein++; 616 cnt.v_vnodepgsin++; 617 return vnode_pager_input_smlfs(object, m[reqpage]); 618 } 619 /* 620 * if ANY DEV_BSIZE blocks are valid on a large filesystem block 621 * then, the entire page is valid -- 622 * XXX no it isn't 623 */ 624 625 if (m[reqpage]->valid != VM_PAGE_BITS_ALL) 626 m[reqpage]->valid = 0; 627 628 if (m[reqpage]->valid) { 629 m[reqpage]->valid = VM_PAGE_BITS_ALL; 630 for (i = 0; i < count; i++) { 631 if (i != reqpage) 632 vnode_pager_freepage(m[i]); 633 } 634 return VM_PAGER_OK; 635 } 636 637 /* 638 * here on direct device I/O 639 */ 640 641 firstaddr = -1; 642 /* 643 * calculate the run that includes the required page 644 */ 645 for(first = 0, i = 0; i < count; i = runend) { 646 firstaddr = vnode_pager_addr(vp, 647 IDX_TO_OFF(m[i]->pindex), &runpg); 648 if (firstaddr == -1) { 649 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 650 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d", 651 firstaddr, foff, object->un_pager.vnp.vnp_size); 652 } 653 vnode_pager_freepage(m[i]); 654 runend = i + 1; 655 first = runend; 656 continue; 657 } 658 runend = i + runpg; 659 if (runend <= reqpage) { 660 int j; 661 for (j = i; j < runend; j++) { 662 vnode_pager_freepage(m[j]); 663 } 664 } else { 665 if (runpg < (count - first)) { 666 for (i = first + runpg; i < count; i++) 667 vnode_pager_freepage(m[i]); 668 count = first + runpg; 669 } 670 break; 671 } 672 first = runend; 673 } 674 675 /* 676 * the first and last page have been calculated now, move input pages 677 * to be zero based... 678 */ 679 if (first != 0) { 680 for (i = first; i < count; i++) { 681 m[i - first] = m[i]; 682 } 683 count -= first; 684 reqpage -= first; 685 } 686 687 /* 688 * calculate the file virtual address for the transfer 689 */ 690 foff = IDX_TO_OFF(m[0]->pindex); 691 692 /* 693 * calculate the size of the transfer 694 */ 695 size = count * PAGE_SIZE; 696 if ((foff + size) > object->un_pager.vnp.vnp_size) 697 size = object->un_pager.vnp.vnp_size - foff; 698 699 /* 700 * round up physical size for real devices 701 */ 702 if (dp->v_type == VBLK || dp->v_type == VCHR) 703 size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 704 705 bp = getpbuf(); 706 kva = (vm_offset_t) bp->b_data; 707 708 /* 709 * and map the pages to be read into the kva 710 */ 711 pmap_qenter(kva, m, count); 712 713 /* build a minimal buffer header */ 714 bp->b_flags = B_BUSY | B_READ | B_CALL; 715 bp->b_iodone = vnode_pager_iodone; 716 /* B_PHYS is not set, but it is nice to fill this in */ 717 bp->b_proc = curproc; 718 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 719 if (bp->b_rcred != NOCRED) 720 crhold(bp->b_rcred); 721 if (bp->b_wcred != NOCRED) 722 crhold(bp->b_wcred); 723 bp->b_blkno = firstaddr; 724 pbgetvp(dp, bp); 725 bp->b_bcount = size; 726 bp->b_bufsize = size; 727 728 cnt.v_vnodein++; 729 cnt.v_vnodepgsin += count; 730 731 /* do the input */
| 419 420 /* we definitely need to be at splvm here */ 421 422 s = splvm(); 423 while ((bp->b_flags & B_DONE) == 0) { 424 tsleep(bp, PVM, "vnsrd", 0); 425 } 426 splx(s); 427 if ((bp->b_flags & B_ERROR) != 0) 428 error = EIO; 429 430 /* 431 * free the buffer header back to the swap buffer pool 432 */ 433 relpbuf(bp); 434 if (error) 435 break; 436 437 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 438 } else { 439 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize); 440 bzero((caddr_t) kva + i * bsize, bsize); 441 } 442 } 443 vm_pager_unmap_page(kva); 444 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 445 m->flags &= ~PG_ZERO; 446 if (error) { 447 return VM_PAGER_ERROR; 448 } 449 return VM_PAGER_OK; 450 451} 452 453 454/* 455 * old style vnode pager output routine 456 */ 457static int 458vnode_pager_input_old(object, m) 459 vm_object_t object; 460 vm_page_t m; 461{ 462 struct uio auio; 463 struct iovec aiov; 464 int error; 465 int size; 466 vm_offset_t kva; 467 468 error = 0; 469 470 /* 471 * Return failure if beyond current EOF 472 */ 473 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) { 474 return VM_PAGER_BAD; 475 } else { 476 size = PAGE_SIZE; 477 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size) 478 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex); 479 480 /* 481 * Allocate a kernel virtual address and initialize so that 482 * we can use VOP_READ/WRITE routines. 483 */ 484 kva = vm_pager_map_page(m); 485 486 aiov.iov_base = (caddr_t) kva; 487 aiov.iov_len = size; 488 auio.uio_iov = &aiov; 489 auio.uio_iovcnt = 1; 490 auio.uio_offset = IDX_TO_OFF(m->pindex); 491 auio.uio_segflg = UIO_SYSSPACE; 492 auio.uio_rw = UIO_READ; 493 auio.uio_resid = size; 494 auio.uio_procp = (struct proc *) 0; 495 496 error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred); 497 if (!error) { 498 register int count = size - auio.uio_resid; 499 500 if (count == 0) 501 error = EINVAL; 502 else if (count != PAGE_SIZE) 503 bzero((caddr_t) kva + count, PAGE_SIZE - count); 504 } 505 vm_pager_unmap_page(kva); 506 } 507 pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 508 m->dirty = 0; 509 m->flags &= ~PG_ZERO; 510 return error ? VM_PAGER_ERROR : VM_PAGER_OK; 511} 512 513/* 514 * generic vnode pager input routine 515 */ 516 517/* 518 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 519 * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to 520 * vnode_pager_generic_getpages() to implement the previous behaviour. 521 * 522 * All other FS's should use the bypass to get to the local media 523 * backing vp's VOP_GETPAGES. 524 */ 525static int 526vnode_pager_getpages(object, m, count, reqpage) 527 vm_object_t object; 528 vm_page_t *m; 529 int count; 530 int reqpage; 531{ 532 int rtval; 533 struct vnode *vp; 534 int bytes = count * PAGE_SIZE; 535 536 vp = object->handle; 537 /* 538 * XXX temporary diagnostic message to help track stale FS code, 539 * Returning EOPNOTSUPP from here may make things unhappy. 540 */ 541 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0); 542 if (rtval == EOPNOTSUPP) { 543 printf("vnode_pager: *** WARNING *** stale FS getpages\n"); 544 rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage); 545 } 546 return rtval; 547} 548 549 550/* 551 * This is now called from local media FS's to operate against their 552 * own vnodes if they fail to implement VOP_GETPAGES. 553 */ 554int 555vnode_pager_generic_getpages(vp, m, bytecount, reqpage) 556 struct vnode *vp; 557 vm_page_t *m; 558 int bytecount; 559 int reqpage; 560{ 561 vm_object_t object; 562 vm_offset_t kva; 563 off_t foff, tfoff, nextoff; 564 int i, size, bsize, first, firstaddr; 565 struct vnode *dp; 566 int runpg; 567 int runend; 568 struct buf *bp; 569 int s; 570 int count; 571 int error = 0; 572 573 object = vp->v_object; 574 count = bytecount / PAGE_SIZE; 575 576 if (vp->v_mount == NULL) 577 return VM_PAGER_BAD; 578 579 bsize = vp->v_mount->mnt_stat.f_iosize; 580 581 /* get the UNDERLYING device for the file with VOP_BMAP() */ 582 583 /* 584 * originally, we did not check for an error return value -- assuming 585 * an fs always has a bmap entry point -- that assumption is wrong!!! 586 */ 587 foff = IDX_TO_OFF(m[reqpage]->pindex); 588 589 /* 590 * if we can't bmap, use old VOP code 591 */ 592 if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) { 593 for (i = 0; i < count; i++) { 594 if (i != reqpage) { 595 vnode_pager_freepage(m[i]); 596 } 597 } 598 cnt.v_vnodein++; 599 cnt.v_vnodepgsin++; 600 return vnode_pager_input_old(object, m[reqpage]); 601 602 /* 603 * if the blocksize is smaller than a page size, then use 604 * special small filesystem code. NFS sometimes has a small 605 * blocksize, but it can handle large reads itself. 606 */ 607 } else if ((PAGE_SIZE / bsize) > 1 && 608 (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) { 609 610 for (i = 0; i < count; i++) { 611 if (i != reqpage) { 612 vnode_pager_freepage(m[i]); 613 } 614 } 615 cnt.v_vnodein++; 616 cnt.v_vnodepgsin++; 617 return vnode_pager_input_smlfs(object, m[reqpage]); 618 } 619 /* 620 * if ANY DEV_BSIZE blocks are valid on a large filesystem block 621 * then, the entire page is valid -- 622 * XXX no it isn't 623 */ 624 625 if (m[reqpage]->valid != VM_PAGE_BITS_ALL) 626 m[reqpage]->valid = 0; 627 628 if (m[reqpage]->valid) { 629 m[reqpage]->valid = VM_PAGE_BITS_ALL; 630 for (i = 0; i < count; i++) { 631 if (i != reqpage) 632 vnode_pager_freepage(m[i]); 633 } 634 return VM_PAGER_OK; 635 } 636 637 /* 638 * here on direct device I/O 639 */ 640 641 firstaddr = -1; 642 /* 643 * calculate the run that includes the required page 644 */ 645 for(first = 0, i = 0; i < count; i = runend) { 646 firstaddr = vnode_pager_addr(vp, 647 IDX_TO_OFF(m[i]->pindex), &runpg); 648 if (firstaddr == -1) { 649 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) { 650 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d", 651 firstaddr, foff, object->un_pager.vnp.vnp_size); 652 } 653 vnode_pager_freepage(m[i]); 654 runend = i + 1; 655 first = runend; 656 continue; 657 } 658 runend = i + runpg; 659 if (runend <= reqpage) { 660 int j; 661 for (j = i; j < runend; j++) { 662 vnode_pager_freepage(m[j]); 663 } 664 } else { 665 if (runpg < (count - first)) { 666 for (i = first + runpg; i < count; i++) 667 vnode_pager_freepage(m[i]); 668 count = first + runpg; 669 } 670 break; 671 } 672 first = runend; 673 } 674 675 /* 676 * the first and last page have been calculated now, move input pages 677 * to be zero based... 678 */ 679 if (first != 0) { 680 for (i = first; i < count; i++) { 681 m[i - first] = m[i]; 682 } 683 count -= first; 684 reqpage -= first; 685 } 686 687 /* 688 * calculate the file virtual address for the transfer 689 */ 690 foff = IDX_TO_OFF(m[0]->pindex); 691 692 /* 693 * calculate the size of the transfer 694 */ 695 size = count * PAGE_SIZE; 696 if ((foff + size) > object->un_pager.vnp.vnp_size) 697 size = object->un_pager.vnp.vnp_size - foff; 698 699 /* 700 * round up physical size for real devices 701 */ 702 if (dp->v_type == VBLK || dp->v_type == VCHR) 703 size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 704 705 bp = getpbuf(); 706 kva = (vm_offset_t) bp->b_data; 707 708 /* 709 * and map the pages to be read into the kva 710 */ 711 pmap_qenter(kva, m, count); 712 713 /* build a minimal buffer header */ 714 bp->b_flags = B_BUSY | B_READ | B_CALL; 715 bp->b_iodone = vnode_pager_iodone; 716 /* B_PHYS is not set, but it is nice to fill this in */ 717 bp->b_proc = curproc; 718 bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred; 719 if (bp->b_rcred != NOCRED) 720 crhold(bp->b_rcred); 721 if (bp->b_wcred != NOCRED) 722 crhold(bp->b_wcred); 723 bp->b_blkno = firstaddr; 724 pbgetvp(dp, bp); 725 bp->b_bcount = size; 726 bp->b_bufsize = size; 727 728 cnt.v_vnodein++; 729 cnt.v_vnodepgsin += count; 730 731 /* do the input */
|
732 VOP_STRATEGY(bp);
| 732 VOP_STRATEGY(bp->b_vp, bp);
|
733 734 s = splvm(); 735 /* we definitely need to be at splvm here */ 736 737 while ((bp->b_flags & B_DONE) == 0) { 738 tsleep(bp, PVM, "vnread", 0); 739 } 740 splx(s); 741 if ((bp->b_flags & B_ERROR) != 0) 742 error = EIO; 743 744 if (!error) { 745 if (size != count * PAGE_SIZE) 746 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 747 } 748 pmap_qremove(kva, count); 749 750 /* 751 * free the buffer header back to the swap buffer pool 752 */ 753 relpbuf(bp); 754 755 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 756 vm_page_t mt; 757 758 nextoff = tfoff + PAGE_SIZE; 759 mt = m[i]; 760 761 if (nextoff <= size) { 762 mt->valid = VM_PAGE_BITS_ALL; 763 mt->dirty = 0; 764 pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 765 } else { 766 int nvalid = ((size + DEV_BSIZE - 1) - tfoff) & ~(DEV_BSIZE - 1); 767 vm_page_set_validclean(mt, 0, nvalid); 768 } 769 770 mt->flags &= ~PG_ZERO; 771 if (i != reqpage) { 772 773 /* 774 * whether or not to leave the page activated is up in 775 * the air, but we should put the page on a page queue 776 * somewhere. (it already is in the object). Result: 777 * It appears that emperical results show that 778 * deactivating pages is best. 779 */ 780 781 /* 782 * just in case someone was asking for this page we 783 * now tell them that it is ok to use 784 */ 785 if (!error) { 786 if (mt->flags & PG_WANTED) 787 vm_page_activate(mt); 788 else 789 vm_page_deactivate(mt); 790 PAGE_WAKEUP(mt); 791 } else { 792 vnode_pager_freepage(mt); 793 } 794 } 795 } 796 if (error) { 797 printf("vnode_pager_getpages: I/O read error\n"); 798 } 799 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 800} 801 802/* 803 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 804 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 805 * vnode_pager_generic_putpages() to implement the previous behaviour. 806 * 807 * All other FS's should use the bypass to get to the local media 808 * backing vp's VOP_PUTPAGES. 809 */ 810static int 811vnode_pager_putpages(object, m, count, sync, rtvals) 812 vm_object_t object; 813 vm_page_t *m; 814 int count; 815 boolean_t sync; 816 int *rtvals; 817{ 818 int rtval; 819 struct vnode *vp; 820 int bytes = count * PAGE_SIZE; 821 822 vp = object->handle; 823 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 824 if (rtval == EOPNOTSUPP) { 825 printf("vnode_pager: *** WARNING *** stale FS putpages\n"); 826 rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals); 827 } 828 return rtval; 829} 830 831 832/* 833 * This is now called from local media FS's to operate against their 834 * own vnodes if they fail to implement VOP_GETPAGES. 835 */ 836int 837vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 838 struct vnode *vp; 839 vm_page_t *m; 840 int bytecount; 841 int flags; 842 int *rtvals; 843{ 844 int i; 845 vm_object_t object; 846 int count; 847 848 int maxsize, ncount; 849 vm_ooffset_t poffset; 850 struct uio auio; 851 struct iovec aiov; 852 int error; 853 int ioflags; 854 855 object = vp->v_object; 856 count = bytecount / PAGE_SIZE; 857 858 for (i = 0; i < count; i++) 859 rtvals[i] = VM_PAGER_AGAIN; 860 861 if ((int) m[0]->pindex < 0) { 862 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", 863 m[0]->pindex, m[0]->dirty); 864 rtvals[0] = VM_PAGER_BAD; 865 return VM_PAGER_BAD; 866 } 867 868 maxsize = count * PAGE_SIZE; 869 ncount = count; 870 871 poffset = IDX_TO_OFF(m[0]->pindex); 872 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 873 if (object->un_pager.vnp.vnp_size > poffset) 874 maxsize = object->un_pager.vnp.vnp_size - poffset; 875 else 876 maxsize = 0; 877 ncount = btoc(maxsize); 878 if (ncount < count) { 879 for (i = ncount; i < count; i++) { 880 rtvals[i] = VM_PAGER_BAD; 881 } 882 } 883 } 884 885 ioflags = IO_VMIO; 886 ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: 0; 887 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 888 889 aiov.iov_base = (caddr_t) 0; 890 aiov.iov_len = maxsize; 891 auio.uio_iov = &aiov; 892 auio.uio_iovcnt = 1; 893 auio.uio_offset = poffset; 894 auio.uio_segflg = UIO_NOCOPY; 895 auio.uio_rw = UIO_WRITE; 896 auio.uio_resid = maxsize; 897 auio.uio_procp = (struct proc *) 0; 898 error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred); 899 cnt.v_vnodeout++; 900 cnt.v_vnodepgsout += ncount; 901 902 if (error) { 903 printf("vnode_pager_putpages: I/O error %d\n", error); 904 } 905 if (auio.uio_resid) { 906 printf("vnode_pager_putpages: residual I/O %d at %ld\n", 907 auio.uio_resid, m[0]->pindex); 908 } 909 for (i = 0; i < ncount; i++) { 910 rtvals[i] = VM_PAGER_OK; 911 } 912 return rtvals[0]; 913} 914 915struct vnode * 916vnode_pager_lock(object) 917 vm_object_t object; 918{ 919 struct proc *p = curproc; /* XXX */ 920 921 for (; object != NULL; object = object->backing_object) { 922 if (object->type != OBJT_VNODE) 923 continue; 924 if (object->flags & OBJ_DEAD) 925 return NULL; 926 927 while (vget(object->handle, 928 LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) { 929 if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE)) 930 return NULL; 931 printf("vnode_pager_lock: retrying\n"); 932 } 933 return object->handle; 934 } 935 return NULL; 936}
| 733 734 s = splvm(); 735 /* we definitely need to be at splvm here */ 736 737 while ((bp->b_flags & B_DONE) == 0) { 738 tsleep(bp, PVM, "vnread", 0); 739 } 740 splx(s); 741 if ((bp->b_flags & B_ERROR) != 0) 742 error = EIO; 743 744 if (!error) { 745 if (size != count * PAGE_SIZE) 746 bzero((caddr_t) kva + size, PAGE_SIZE * count - size); 747 } 748 pmap_qremove(kva, count); 749 750 /* 751 * free the buffer header back to the swap buffer pool 752 */ 753 relpbuf(bp); 754 755 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) { 756 vm_page_t mt; 757 758 nextoff = tfoff + PAGE_SIZE; 759 mt = m[i]; 760 761 if (nextoff <= size) { 762 mt->valid = VM_PAGE_BITS_ALL; 763 mt->dirty = 0; 764 pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 765 } else { 766 int nvalid = ((size + DEV_BSIZE - 1) - tfoff) & ~(DEV_BSIZE - 1); 767 vm_page_set_validclean(mt, 0, nvalid); 768 } 769 770 mt->flags &= ~PG_ZERO; 771 if (i != reqpage) { 772 773 /* 774 * whether or not to leave the page activated is up in 775 * the air, but we should put the page on a page queue 776 * somewhere. (it already is in the object). Result: 777 * It appears that emperical results show that 778 * deactivating pages is best. 779 */ 780 781 /* 782 * just in case someone was asking for this page we 783 * now tell them that it is ok to use 784 */ 785 if (!error) { 786 if (mt->flags & PG_WANTED) 787 vm_page_activate(mt); 788 else 789 vm_page_deactivate(mt); 790 PAGE_WAKEUP(mt); 791 } else { 792 vnode_pager_freepage(mt); 793 } 794 } 795 } 796 if (error) { 797 printf("vnode_pager_getpages: I/O read error\n"); 798 } 799 return (error ? VM_PAGER_ERROR : VM_PAGER_OK); 800} 801 802/* 803 * EOPNOTSUPP is no longer legal. For local media VFS's that do not 804 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to 805 * vnode_pager_generic_putpages() to implement the previous behaviour. 806 * 807 * All other FS's should use the bypass to get to the local media 808 * backing vp's VOP_PUTPAGES. 809 */ 810static int 811vnode_pager_putpages(object, m, count, sync, rtvals) 812 vm_object_t object; 813 vm_page_t *m; 814 int count; 815 boolean_t sync; 816 int *rtvals; 817{ 818 int rtval; 819 struct vnode *vp; 820 int bytes = count * PAGE_SIZE; 821 822 vp = object->handle; 823 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0); 824 if (rtval == EOPNOTSUPP) { 825 printf("vnode_pager: *** WARNING *** stale FS putpages\n"); 826 rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals); 827 } 828 return rtval; 829} 830 831 832/* 833 * This is now called from local media FS's to operate against their 834 * own vnodes if they fail to implement VOP_GETPAGES. 835 */ 836int 837vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals) 838 struct vnode *vp; 839 vm_page_t *m; 840 int bytecount; 841 int flags; 842 int *rtvals; 843{ 844 int i; 845 vm_object_t object; 846 int count; 847 848 int maxsize, ncount; 849 vm_ooffset_t poffset; 850 struct uio auio; 851 struct iovec aiov; 852 int error; 853 int ioflags; 854 855 object = vp->v_object; 856 count = bytecount / PAGE_SIZE; 857 858 for (i = 0; i < count; i++) 859 rtvals[i] = VM_PAGER_AGAIN; 860 861 if ((int) m[0]->pindex < 0) { 862 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", 863 m[0]->pindex, m[0]->dirty); 864 rtvals[0] = VM_PAGER_BAD; 865 return VM_PAGER_BAD; 866 } 867 868 maxsize = count * PAGE_SIZE; 869 ncount = count; 870 871 poffset = IDX_TO_OFF(m[0]->pindex); 872 if (maxsize + poffset > object->un_pager.vnp.vnp_size) { 873 if (object->un_pager.vnp.vnp_size > poffset) 874 maxsize = object->un_pager.vnp.vnp_size - poffset; 875 else 876 maxsize = 0; 877 ncount = btoc(maxsize); 878 if (ncount < count) { 879 for (i = ncount; i < count; i++) { 880 rtvals[i] = VM_PAGER_BAD; 881 } 882 } 883 } 884 885 ioflags = IO_VMIO; 886 ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: 0; 887 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0; 888 889 aiov.iov_base = (caddr_t) 0; 890 aiov.iov_len = maxsize; 891 auio.uio_iov = &aiov; 892 auio.uio_iovcnt = 1; 893 auio.uio_offset = poffset; 894 auio.uio_segflg = UIO_NOCOPY; 895 auio.uio_rw = UIO_WRITE; 896 auio.uio_resid = maxsize; 897 auio.uio_procp = (struct proc *) 0; 898 error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred); 899 cnt.v_vnodeout++; 900 cnt.v_vnodepgsout += ncount; 901 902 if (error) { 903 printf("vnode_pager_putpages: I/O error %d\n", error); 904 } 905 if (auio.uio_resid) { 906 printf("vnode_pager_putpages: residual I/O %d at %ld\n", 907 auio.uio_resid, m[0]->pindex); 908 } 909 for (i = 0; i < ncount; i++) { 910 rtvals[i] = VM_PAGER_OK; 911 } 912 return rtvals[0]; 913} 914 915struct vnode * 916vnode_pager_lock(object) 917 vm_object_t object; 918{ 919 struct proc *p = curproc; /* XXX */ 920 921 for (; object != NULL; object = object->backing_object) { 922 if (object->type != OBJT_VNODE) 923 continue; 924 if (object->flags & OBJ_DEAD) 925 return NULL; 926 927 while (vget(object->handle, 928 LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) { 929 if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE)) 930 return NULL; 931 printf("vnode_pager_lock: retrying\n"); 932 } 933 return object->handle; 934 } 935 return NULL; 936}
|