1 /* $NetBSD: uvm_vnode.c,v 1.17 1998/11/04 06:21:40 chs Exp $ */ 2 3 /* 4 * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE! 5 * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<< 6 */ 7 /* 8 * Copyright (c) 1997 Charles D. Cranor and Washington University. 9 * Copyright (c) 1991, 1993 10 * The Regents of the University of California. 11 * Copyright (c) 1990 University of Utah. 12 * 13 * All rights reserved. 14 * 15 * This code is derived from software contributed to Berkeley by 16 * the Systems Programming Group of the University of Utah Computer 17 * Science Department. 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 3. All advertising materials mentioning features or use of this software 28 * must display the following acknowledgement: 29 * This product includes software developed by Charles D. Cranor, 30 * Washington University, the University of California, Berkeley and 31 * its contributors. 32 * 4. Neither the name of the University nor the names of its contributors 33 * may be used to endorse or promote products derived from this software 34 * without specific prior written permission. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * @(#)vnode_pager.c 8.8 (Berkeley) 2/13/94 49 * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp 50 */ 51 52 #include "fs_nfs.h" 53 #include "opt_uvmhist.h" 54 55 /* 56 * uvm_vnode.c: the vnode pager. 57 */ 58 59 #include <sys/param.h> 60 #include <sys/systm.h> 61 #include <sys/proc.h> 62 #include <sys/malloc.h> 63 #include <sys/vnode.h> 64 #include <sys/disklabel.h> 65 #include <sys/ioctl.h> 66 #include <sys/fcntl.h> 67 #include <sys/conf.h> 68 69 #include <miscfs/specfs/specdev.h> 70 71 #include <vm/vm.h> 72 #include <vm/vm_page.h> 73 #include <vm/vm_kern.h> 74 75 #include <uvm/uvm.h> 76 #include <uvm/uvm_vnode.h> 77 78 /* 79 * private global data structure 80 * 81 * we keep a list of writeable active vnode-backed VM objects for sync op. 82 * we keep a simpleq of vnodes that are currently being sync'd. 83 */ 84 85 LIST_HEAD(uvn_list_struct, uvm_vnode); 86 static struct uvn_list_struct uvn_wlist; /* writeable uvns */ 87 static simple_lock_data_t uvn_wl_lock; /* locks uvn_wlist */ 88 89 SIMPLEQ_HEAD(uvn_sq_struct, uvm_vnode); 90 static struct uvn_sq_struct uvn_sync_q; /* sync'ing uvns */ 91 lock_data_t uvn_sync_lock; /* locks sync operation */ 92 93 /* 94 * functions 95 */ 96 97 static int uvn_asyncget __P((struct uvm_object *, vaddr_t, 98 int)); 99 struct uvm_object *uvn_attach __P((void *, vm_prot_t)); 100 static void uvn_cluster __P((struct uvm_object *, vaddr_t, 101 vaddr_t *, vaddr_t *)); 102 static void uvn_detach __P((struct uvm_object *)); 103 static boolean_t uvn_flush __P((struct uvm_object *, vaddr_t, 104 vaddr_t, int)); 105 static int uvn_get __P((struct uvm_object *, vaddr_t, 106 vm_page_t *, int *, int, 107 vm_prot_t, int, int)); 108 static void uvn_init __P((void)); 109 static int uvn_io __P((struct uvm_vnode *, vm_page_t *, 110 int, int, int)); 111 static int uvn_put __P((struct uvm_object *, vm_page_t *, 112 int, boolean_t)); 113 static void uvn_reference __P((struct uvm_object *)); 114 static boolean_t uvn_releasepg __P((struct vm_page *, 115 struct vm_page **)); 116 117 /* 118 * master pager structure 119 */ 120 121 struct uvm_pagerops uvm_vnodeops = { 122 uvn_init, 123 uvn_attach, 124 uvn_reference, 125 uvn_detach, 126 NULL, /* no specialized fault routine required */ 127 uvn_flush, 128 uvn_get, 129 uvn_asyncget, 130 uvn_put, 131 uvn_cluster, 132 uvm_mk_pcluster, /* use generic version of this: see uvm_pager.c */ 133 uvm_shareprot, /* !NULL: allow us in share maps */ 134 NULL, /* AIO-DONE function (not until we have asyncio) */ 135 uvn_releasepg, 136 }; 137 138 /* 139 * the ops! 140 */ 141 142 /* 143 * uvn_init 144 * 145 * init pager private data structures. 146 */ 147 148 static void 149 uvn_init() 150 { 151 152 LIST_INIT(&uvn_wlist); 153 simple_lock_init(&uvn_wl_lock); 154 /* note: uvn_sync_q init'd in uvm_vnp_sync() */ 155 lockinit(&uvn_sync_lock, PVM, "uvnsync", 0, 0); 156 } 157 158 /* 159 * uvn_attach 160 * 161 * attach a vnode structure to a VM object. if the vnode is already 162 * attached, then just bump the reference count by one and return the 163 * VM object. if not already attached, attach and return the new VM obj. 164 * the "accessprot" tells the max access the attaching thread wants to 165 * our pages. 166 * 167 * => caller must _not_ already be holding the lock on the uvm_object. 168 * => in fact, nothing should be locked so that we can sleep here. 169 * => note that uvm_object is first thing in vnode structure, so their 170 * pointers are equiv. 171 */ 172 173 struct uvm_object * 174 uvn_attach(arg, accessprot) 175 void *arg; 176 vm_prot_t accessprot; 177 { 178 struct vnode *vp = arg; 179 struct uvm_vnode *uvn = &vp->v_uvm; 180 struct vattr vattr; 181 int oldflags, result; 182 struct partinfo pi; 183 u_quad_t used_vnode_size; 184 UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist); 185 186 UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0); 187 188 used_vnode_size = (u_quad_t)0; /* XXX gcc -Wuninitialized */ 189 190 /* 191 * first get a lock on the uvn. 192 */ 193 simple_lock(&uvn->u_obj.vmobjlock); 194 while (uvn->u_flags & UVM_VNODE_BLOCKED) { 195 uvn->u_flags |= UVM_VNODE_WANTED; 196 UVMHIST_LOG(maphist, " SLEEPING on blocked vn",0,0,0,0); 197 UVM_UNLOCK_AND_WAIT(uvn, &uvn->u_obj.vmobjlock, FALSE, 198 "uvn_attach", 0); 199 simple_lock(&uvn->u_obj.vmobjlock); 200 UVMHIST_LOG(maphist," WOKE UP",0,0,0,0); 201 } 202 203 /* 204 * if we're maping a BLK device, make sure it is a disk. 205 */ 206 if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) { 207 simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */ 208 UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)", 0,0,0,0); 209 return(NULL); 210 } 211 212 /* 213 * now we have lock and uvn must not be in a blocked state. 214 * first check to see if it is already active, in which case 215 * we can bump the reference count, check to see if we need to 216 * add it to the writeable list, and then return. 217 */ 218 if (uvn->u_flags & UVM_VNODE_VALID) { /* already active? */ 219 220 /* regain VREF if we were persisting */ 221 if (uvn->u_obj.uo_refs == 0) { 222 VREF(vp); 223 UVMHIST_LOG(maphist," VREF (reclaim persisting vnode)", 224 0,0,0,0); 225 } 226 uvn->u_obj.uo_refs++; /* bump uvn ref! */ 227 228 /* check for new writeable uvn */ 229 if ((accessprot & VM_PROT_WRITE) != 0 && 230 (uvn->u_flags & UVM_VNODE_WRITEABLE) == 0) { 231 simple_lock(&uvn_wl_lock); 232 LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist); 233 simple_unlock(&uvn_wl_lock); 234 /* we are now on wlist! */ 235 uvn->u_flags |= UVM_VNODE_WRITEABLE; 236 } 237 238 /* unlock and return */ 239 simple_unlock(&uvn->u_obj.vmobjlock); 240 UVMHIST_LOG(maphist,"<- done, refcnt=%d", uvn->u_obj.uo_refs, 241 0, 0, 0); 242 return (&uvn->u_obj); 243 } 244 245 /* 246 * need to call VOP_GETATTR() to get the attributes, but that could 247 * block (due to I/O), so we want to unlock the object before calling. 248 * however, we want to keep anyone else from playing with the object 249 * while it is unlocked. to do this we set UVM_VNODE_ALOCK which 250 * prevents anyone from attaching to the vnode until we are done with 251 * it. 252 */ 253 uvn->u_flags = UVM_VNODE_ALOCK; 254 simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock in case we sleep */ 255 /* XXX: curproc? */ 256 257 if (vp->v_type == VBLK) { 258 /* 259 * We could implement this as a specfs getattr call, but: 260 * 261 * (1) VOP_GETATTR() would get the file system 262 * vnode operation, not the specfs operation. 263 * 264 * (2) All we want is the size, anyhow. 265 */ 266 result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, 267 DIOCGPART, (caddr_t)&pi, FREAD, curproc); 268 if (result == 0) { 269 /* XXX should remember blocksize */ 270 used_vnode_size = (u_quad_t)pi.disklab->d_secsize * 271 (u_quad_t)pi.part->p_size; 272 } 273 } else { 274 result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc); 275 if (result == 0) 276 used_vnode_size = vattr.va_size; 277 } 278 279 /* relock object */ 280 simple_lock(&uvn->u_obj.vmobjlock); 281 282 if (result != 0) { 283 if (uvn->u_flags & UVM_VNODE_WANTED) 284 wakeup(uvn); 285 uvn->u_flags = 0; 286 simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */ 287 UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0); 288 return(NULL); 289 } 290 291 /* 292 * make sure that the newsize fits within a vaddr_t 293 * XXX: need to revise addressing data types 294 */ 295 if (vp->v_type == VBLK) printf("used_vnode_size = %qu\n", used_vnode_size); 296 if (used_vnode_size > (vaddr_t) -PAGE_SIZE) { 297 #ifdef DEBUG 298 printf("uvn_attach: vn %p size truncated %qx->%x\n", vp, 299 used_vnode_size, -PAGE_SIZE); 300 #endif 301 used_vnode_size = (vaddr_t) -PAGE_SIZE; 302 } 303 304 /* 305 * now set up the uvn. 306 */ 307 uvn->u_obj.pgops = &uvm_vnodeops; 308 TAILQ_INIT(&uvn->u_obj.memq); 309 uvn->u_obj.uo_npages = 0; 310 uvn->u_obj.uo_refs = 1; /* just us... */ 311 oldflags = uvn->u_flags; 312 uvn->u_flags = UVM_VNODE_VALID|UVM_VNODE_CANPERSIST; 313 uvn->u_nio = 0; 314 uvn->u_size = used_vnode_size; 315 316 /* if write access, we need to add it to the wlist */ 317 if (accessprot & VM_PROT_WRITE) { 318 simple_lock(&uvn_wl_lock); 319 LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist); 320 simple_unlock(&uvn_wl_lock); 321 uvn->u_flags |= UVM_VNODE_WRITEABLE; /* we are on wlist! */ 322 } 323 324 /* 325 * add a reference to the vnode. this reference will stay as long 326 * as there is a valid mapping of the vnode. dropped when the 327 * reference count goes to zero [and we either free or persist]. 328 */ 329 VREF(vp); 330 simple_unlock(&uvn->u_obj.vmobjlock); 331 if (oldflags & UVM_VNODE_WANTED) 332 wakeup(uvn); 333 334 UVMHIST_LOG(maphist,"<- done/VREF, ret 0x%x", &uvn->u_obj,0,0,0); 335 return(&uvn->u_obj); 336 } 337 338 339 /* 340 * uvn_reference 341 * 342 * duplicate a reference to a VM object. Note that the reference 343 * count must already be at least one (the passed in reference) so 344 * there is no chance of the uvn being killed or locked out here. 345 * 346 * => caller must call with object unlocked. 347 * => caller must be using the same accessprot as was used at attach time 348 */ 349 350 351 static void 352 uvn_reference(uobj) 353 struct uvm_object *uobj; 354 { 355 #ifdef DIAGNOSTIC 356 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj; 357 #endif 358 UVMHIST_FUNC("uvn_reference"); UVMHIST_CALLED(maphist); 359 360 simple_lock(&uobj->vmobjlock); 361 #ifdef DIAGNOSTIC 362 if ((uvn->u_flags & UVM_VNODE_VALID) == 0) { 363 printf("uvn_reference: ref=%d, flags=0x%x\n", uvn->u_flags, 364 uobj->uo_refs); 365 panic("uvn_reference: invalid state"); 366 } 367 #endif 368 uobj->uo_refs++; 369 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)", 370 uobj, uobj->uo_refs,0,0); 371 simple_unlock(&uobj->vmobjlock); 372 } 373 374 /* 375 * uvn_detach 376 * 377 * remove a reference to a VM object. 378 * 379 * => caller must call with object unlocked and map locked. 380 * => this starts the detach process, but doesn't have to finish it 381 * (async i/o could still be pending). 382 */ 383 static void 384 uvn_detach(uobj) 385 struct uvm_object *uobj; 386 { 387 struct uvm_vnode *uvn; 388 struct vnode *vp; 389 int oldflags; 390 UVMHIST_FUNC("uvn_detach"); UVMHIST_CALLED(maphist); 391 392 simple_lock(&uobj->vmobjlock); 393 394 UVMHIST_LOG(maphist," (uobj=0x%x) ref=%d", uobj,uobj->uo_refs,0,0); 395 uobj->uo_refs--; /* drop ref! */ 396 if (uobj->uo_refs) { /* still more refs */ 397 simple_unlock(&uobj->vmobjlock); 398 UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0); 399 return; 400 } 401 402 /* 403 * get other pointers ... 404 */ 405 406 uvn = (struct uvm_vnode *) uobj; 407 vp = (struct vnode *) uobj; 408 409 /* 410 * clear VTEXT flag now that there are no mappings left (VTEXT is used 411 * to keep an active text file from being overwritten). 412 */ 413 vp->v_flag &= ~VTEXT; 414 415 /* 416 * we just dropped the last reference to the uvn. see if we can 417 * let it "stick around". 418 */ 419 420 if (uvn->u_flags & UVM_VNODE_CANPERSIST) { 421 /* won't block */ 422 uvn_flush(uobj, 0, 0, PGO_DEACTIVATE|PGO_ALLPAGES); 423 simple_unlock(&uobj->vmobjlock); 424 vrele(vp); /* drop vnode reference */ 425 UVMHIST_LOG(maphist,"<- done/vrele! (persist)", 0,0,0,0); 426 return; 427 } 428 429 /* 430 * its a goner! 431 */ 432 433 UVMHIST_LOG(maphist," its a goner (flushing)!", 0,0,0,0); 434 435 uvn->u_flags |= UVM_VNODE_DYING; 436 437 /* 438 * even though we may unlock in flush, no one can gain a reference 439 * to us until we clear the "dying" flag [because it blocks 440 * attaches]. we will not do that until after we've disposed of all 441 * the pages with uvn_flush(). note that before the flush the only 442 * pages that could be marked PG_BUSY are ones that are in async 443 * pageout by the daemon. (there can't be any pending "get"'s 444 * because there are no references to the object). 445 */ 446 447 (void) uvn_flush(uobj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES); 448 449 UVMHIST_LOG(maphist," its a goner (done flush)!", 0,0,0,0); 450 451 /* 452 * given the structure of this pager, the above flush request will 453 * create the following state: all the pages that were in the object 454 * have either been free'd or they are marked PG_BUSY|PG_RELEASED. 455 * the PG_BUSY bit was set either by us or the daemon for async I/O. 456 * in either case, if we have pages left we can't kill the object 457 * yet because i/o is pending. in this case we set the "relkill" 458 * flag which will cause pgo_releasepg to kill the object once all 459 * the I/O's are done [pgo_releasepg will be called from the aiodone 460 * routine or from the page daemon]. 461 */ 462 463 if (uobj->uo_npages) { /* I/O pending. iodone will free */ 464 #ifdef DIAGNOSTIC 465 /* 466 * XXXCDC: very unlikely to happen until we have async i/o 467 * so print a little info message in case it does. 468 */ 469 printf("uvn_detach: vn %p has pages left after flush - " 470 "relkill mode\n", uobj); 471 #endif 472 uvn->u_flags |= UVM_VNODE_RELKILL; 473 simple_unlock(&uobj->vmobjlock); 474 UVMHIST_LOG(maphist,"<- done! (releasepg will kill obj)", 0, 0, 475 0, 0); 476 return; 477 } 478 479 /* 480 * kill object now. note that we can't be on the sync q because 481 * all references are gone. 482 */ 483 if (uvn->u_flags & UVM_VNODE_WRITEABLE) { 484 simple_lock(&uvn_wl_lock); /* protect uvn_wlist */ 485 LIST_REMOVE(uvn, u_wlist); 486 simple_unlock(&uvn_wl_lock); 487 } 488 #ifdef DIAGNOSTIC 489 if (uobj->memq.tqh_first != NULL) 490 panic("uvn_deref: vnode VM object still has pages afer " 491 "syncio/free flush"); 492 #endif 493 oldflags = uvn->u_flags; 494 uvn->u_flags = 0; 495 simple_unlock(&uobj->vmobjlock); 496 497 /* wake up any sleepers */ 498 if (oldflags & UVM_VNODE_WANTED) 499 wakeup(uvn); 500 501 /* 502 * drop our reference to the vnode. 503 */ 504 vrele(vp); 505 UVMHIST_LOG(maphist,"<- done (vrele) final", 0,0,0,0); 506 507 return; 508 } 509 510 /* 511 * uvm_vnp_terminate: external hook to clear out a vnode's VM 512 * 513 * called in two cases: 514 * [1] when a persisting vnode vm object (i.e. one with a zero reference 515 * count) needs to be freed so that a vnode can be reused. this 516 * happens under "getnewvnode" in vfs_subr.c. if the vnode from 517 * the free list is still attached (i.e. not VBAD) then vgone is 518 * called. as part of the vgone trace this should get called to 519 * free the vm object. this is the common case. 520 * [2] when a filesystem is being unmounted by force (MNT_FORCE, 521 * "umount -f") the vgone() function is called on active vnodes 522 * on the mounted file systems to kill their data (the vnodes become 523 * "dead" ones [see src/sys/miscfs/deadfs/...]). that results in a 524 * call here (even if the uvn is still in use -- i.e. has a non-zero 525 * reference count). this case happens at "umount -f" and during a 526 * "reboot/halt" operation. 527 * 528 * => the caller must XLOCK and VOP_LOCK the vnode before calling us 529 * [protects us from getting a vnode that is already in the DYING 530 * state...] 531 * => unlike uvn_detach, this function must not return until all the 532 * uvn's pages are disposed of. 533 * => in case [2] the uvn is still alive after this call, but all I/O 534 * ops will fail (due to the backing vnode now being "dead"). this 535 * will prob. kill any process using the uvn due to pgo_get failing. 536 */ 537 538 void 539 uvm_vnp_terminate(vp) 540 struct vnode *vp; 541 { 542 struct uvm_vnode *uvn = &vp->v_uvm; 543 int oldflags; 544 UVMHIST_FUNC("uvm_vnp_terminate"); UVMHIST_CALLED(maphist); 545 546 /* 547 * lock object and check if it is valid 548 */ 549 simple_lock(&uvn->u_obj.vmobjlock); 550 UVMHIST_LOG(maphist, " vp=0x%x, ref=%d, flag=0x%x", vp, 551 uvn->u_obj.uo_refs, uvn->u_flags, 0); 552 if ((uvn->u_flags & UVM_VNODE_VALID) == 0) { 553 simple_unlock(&uvn->u_obj.vmobjlock); 554 UVMHIST_LOG(maphist, "<- done (not active)", 0, 0, 0, 0); 555 return; 556 } 557 558 /* 559 * must be a valid uvn that is not already dying (because XLOCK 560 * protects us from that). the uvn can't in the the ALOCK state 561 * because it is valid, and uvn's that are in the ALOCK state haven't 562 * been marked valid yet. 563 */ 564 565 #ifdef DEBUG 566 /* 567 * debug check: are we yanking the vnode out from under our uvn? 568 */ 569 if (uvn->u_obj.uo_refs) { 570 printf("uvm_vnp_terminate(%p): terminating active vnode " 571 "(refs=%d)\n", uvn, uvn->u_obj.uo_refs); 572 } 573 #endif 574 575 /* 576 * it is possible that the uvn was detached and is in the relkill 577 * state [i.e. waiting for async i/o to finish so that releasepg can 578 * kill object]. we take over the vnode now and cancel the relkill. 579 * we want to know when the i/o is done so we can recycle right 580 * away. note that a uvn can only be in the RELKILL state if it 581 * has a zero reference count. 582 */ 583 584 if (uvn->u_flags & UVM_VNODE_RELKILL) 585 uvn->u_flags &= ~UVM_VNODE_RELKILL; /* cancel RELKILL */ 586 587 /* 588 * block the uvn by setting the dying flag, and then flush the 589 * pages. (note that flush may unlock object while doing I/O, but 590 * it will re-lock it before it returns control here). 591 * 592 * also, note that we tell I/O that we are already VOP_LOCK'd so 593 * that uvn_io doesn't attempt to VOP_LOCK again. 594 * 595 * XXXCDC: setting VNISLOCKED on an active uvn which is being terminated 596 * due to a forceful unmount might not be a good idea. maybe we 597 * need a way to pass in this info to uvn_flush through a 598 * pager-defined PGO_ constant [currently there are none]. 599 */ 600 uvn->u_flags |= UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED; 601 602 (void) uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES); 603 604 /* 605 * as we just did a flush we expect all the pages to be gone or in 606 * the process of going. sleep to wait for the rest to go [via iosync]. 607 */ 608 609 while (uvn->u_obj.uo_npages) { 610 #ifdef DIAGNOSTIC 611 struct vm_page *pp; 612 for (pp = uvn->u_obj.memq.tqh_first ; pp != NULL ; 613 pp = pp->listq.tqe_next) { 614 if ((pp->flags & PG_BUSY) == 0) 615 panic("uvm_vnp_terminate: detected unbusy pg"); 616 } 617 if (uvn->u_nio == 0) 618 panic("uvm_vnp_terminate: no I/O to wait for?"); 619 printf("uvm_vnp_terminate: waiting for I/O to fin.\n"); 620 /* 621 * XXXCDC: this is unlikely to happen without async i/o so we 622 * put a printf in just to keep an eye on it. 623 */ 624 #endif 625 uvn->u_flags |= UVM_VNODE_IOSYNC; 626 UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock, FALSE, 627 "uvn_term",0); 628 simple_lock(&uvn->u_obj.vmobjlock); 629 } 630 631 /* 632 * done. now we free the uvn if its reference count is zero 633 * (true if we are zapping a persisting uvn). however, if we are 634 * terminating a uvn with active mappings we let it live ... future 635 * calls down to the vnode layer will fail. 636 */ 637 638 oldflags = uvn->u_flags; 639 if (uvn->u_obj.uo_refs) { 640 641 /* 642 * uvn must live on it is dead-vnode state until all references 643 * are gone. restore flags. clear CANPERSIST state. 644 */ 645 646 uvn->u_flags &= ~(UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED| 647 UVM_VNODE_WANTED|UVM_VNODE_CANPERSIST); 648 649 } else { 650 651 /* 652 * free the uvn now. note that the VREF reference is already 653 * gone [it is dropped when we enter the persist state]. 654 */ 655 if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED) 656 panic("uvm_vnp_terminate: io sync wanted bit set"); 657 658 if (uvn->u_flags & UVM_VNODE_WRITEABLE) { 659 simple_lock(&uvn_wl_lock); 660 LIST_REMOVE(uvn, u_wlist); 661 simple_unlock(&uvn_wl_lock); 662 } 663 uvn->u_flags = 0; /* uvn is history, clear all bits */ 664 } 665 666 if (oldflags & UVM_VNODE_WANTED) 667 wakeup(uvn); /* object lock still held */ 668 669 simple_unlock(&uvn->u_obj.vmobjlock); 670 UVMHIST_LOG(maphist, "<- done", 0, 0, 0, 0); 671 672 } 673 674 /* 675 * uvn_releasepg: handled a released page in a uvn 676 * 677 * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need 678 * to dispose of. 679 * => caller must handled PG_WANTED case 680 * => called with page's object locked, pageq's unlocked 681 * => returns TRUE if page's object is still alive, FALSE if we 682 * killed the page's object. if we return TRUE, then we 683 * return with the object locked. 684 * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return 685 * with the page queues locked [for pagedaemon] 686 * => if (nextpgp == NULL) => we return with page queues unlocked [normal case] 687 * => we kill the uvn if it is not referenced and we are suppose to 688 * kill it ("relkill"). 689 */ 690 691 boolean_t 692 uvn_releasepg(pg, nextpgp) 693 struct vm_page *pg; 694 struct vm_page **nextpgp; /* OUT */ 695 { 696 struct uvm_vnode *uvn = (struct uvm_vnode *) pg->uobject; 697 #ifdef DIAGNOSTIC 698 if ((pg->flags & PG_RELEASED) == 0) 699 panic("uvn_releasepg: page not released!"); 700 #endif 701 702 /* 703 * dispose of the page [caller handles PG_WANTED] 704 */ 705 pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE); 706 uvm_lock_pageq(); 707 if (nextpgp) 708 *nextpgp = pg->pageq.tqe_next; /* next page for daemon */ 709 uvm_pagefree(pg); 710 if (!nextpgp) 711 uvm_unlock_pageq(); 712 713 /* 714 * now see if we need to kill the object 715 */ 716 if (uvn->u_flags & UVM_VNODE_RELKILL) { 717 if (uvn->u_obj.uo_refs) 718 panic("uvn_releasepg: kill flag set on referenced " 719 "object!"); 720 if (uvn->u_obj.uo_npages == 0) { 721 if (uvn->u_flags & UVM_VNODE_WRITEABLE) { 722 simple_lock(&uvn_wl_lock); 723 LIST_REMOVE(uvn, u_wlist); 724 simple_unlock(&uvn_wl_lock); 725 } 726 #ifdef DIAGNOSTIC 727 if (uvn->u_obj.memq.tqh_first) 728 panic("uvn_releasepg: pages in object with npages == 0"); 729 #endif 730 if (uvn->u_flags & UVM_VNODE_WANTED) 731 /* still holding object lock */ 732 wakeup(uvn); 733 734 uvn->u_flags = 0; /* DEAD! */ 735 simple_unlock(&uvn->u_obj.vmobjlock); 736 return (FALSE); 737 } 738 } 739 return (TRUE); 740 } 741 742 /* 743 * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go 744 * through the buffer cache and allow I/O in any size. These VOPs use 745 * synchronous i/o. [vs. VOP_STRATEGY which can be async, but doesn't 746 * go through the buffer cache or allow I/O sizes larger than a 747 * block]. we will eventually want to change this. 748 * 749 * issues to consider: 750 * uvm provides the uvm_aiodesc structure for async i/o management. 751 * there are two tailq's in the uvm. structure... one for pending async 752 * i/o and one for "done" async i/o. to do an async i/o one puts 753 * an aiodesc on the "pending" list (protected by splbio()), starts the 754 * i/o and returns VM_PAGER_PEND. when the i/o is done, we expect 755 * some sort of "i/o done" function to be called (at splbio(), interrupt 756 * time). this function should remove the aiodesc from the pending list 757 * and place it on the "done" list and wakeup the daemon. the daemon 758 * will run at normal spl() and will remove all items from the "done" 759 * list and call the "aiodone" hook for each done request (see uvm_pager.c). 760 * [in the old vm code, this was done by calling the "put" routine with 761 * null arguments which made the code harder to read and understand because 762 * you had one function ("put") doing two things.] 763 * 764 * so the current pager needs: 765 * int uvn_aiodone(struct uvm_aiodesc *) 766 * 767 * => return KERN_SUCCESS (aio finished, free it). otherwise requeue for 768 * later collection. 769 * => called with pageq's locked by the daemon. 770 * 771 * general outline: 772 * - "try" to lock object. if fail, just return (will try again later) 773 * - drop "u_nio" (this req is done!) 774 * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio } 775 * - get "page" structures (atop?). 776 * - handle "wanted" pages 777 * - handle "released" pages [using pgo_releasepg] 778 * >>> pgo_releasepg may kill the object 779 * dont forget to look at "object" wanted flag in all cases. 780 */ 781 782 783 /* 784 * uvn_flush: flush pages out of a uvm object. 785 * 786 * => object should be locked by caller. we may _unlock_ the object 787 * if (and only if) we need to clean a page (PGO_CLEANIT). 788 * we return with the object locked. 789 * => if PGO_CLEANIT is set, we may block (due to I/O). thus, a caller 790 * might want to unlock higher level resources (e.g. vm_map) 791 * before calling flush. 792 * => if PGO_CLEANIT is not set, then we will neither unlock the object 793 * or block. 794 * => if PGO_ALLPAGE is set, then all pages in the object are valid targets 795 * for flushing. 796 * => NOTE: we rely on the fact that the object's memq is a TAILQ and 797 * that new pages are inserted on the tail end of the list. thus, 798 * we can make a complete pass through the object in one go by starting 799 * at the head and working towards the tail (new pages are put in 800 * front of us). 801 * => NOTE: we are allowed to lock the page queues, so the caller 802 * must not be holding the lock on them [e.g. pagedaemon had 803 * better not call us with the queues locked] 804 * => we return TRUE unless we encountered some sort of I/O error 805 * 806 * comment on "cleaning" object and PG_BUSY pages: 807 * this routine is holding the lock on the object. the only time 808 * that it can run into a PG_BUSY page that it does not own is if 809 * some other process has started I/O on the page (e.g. either 810 * a pagein, or a pageout). if the PG_BUSY page is being paged 811 * in, then it can not be dirty (!PG_CLEAN) because no one has 812 * had a chance to modify it yet. if the PG_BUSY page is being 813 * paged out then it means that someone else has already started 814 * cleaning the page for us (how nice!). in this case, if we 815 * have syncio specified, then after we make our pass through the 816 * object we need to wait for the other PG_BUSY pages to clear 817 * off (i.e. we need to do an iosync). also note that once a 818 * page is PG_BUSY it must stay in its object until it is un-busyed. 819 * 820 * note on page traversal: 821 * we can traverse the pages in an object either by going down the 822 * linked list in "uobj->memq", or we can go over the address range 823 * by page doing hash table lookups for each address. depending 824 * on how many pages are in the object it may be cheaper to do one 825 * or the other. we set "by_list" to true if we are using memq. 826 * if the cost of a hash lookup was equal to the cost of the list 827 * traversal we could compare the number of pages in the start->stop 828 * range to the total number of pages in the object. however, it 829 * seems that a hash table lookup is more expensive than the linked 830 * list traversal, so we multiply the number of pages in the 831 * start->stop range by a penalty which we define below. 832 */ 833 834 #define UVN_HASH_PENALTY 4 /* XXX: a guess */ 835 836 static boolean_t 837 uvn_flush(uobj, start, stop, flags) 838 struct uvm_object *uobj; 839 vaddr_t start, stop; 840 int flags; 841 { 842 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj; 843 struct vm_page *pp, *ppnext, *ptmp; 844 struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp; 845 int npages, result, lcv; 846 boolean_t retval, need_iosync, by_list, needs_clean; 847 vaddr_t curoff; 848 u_short pp_version; 849 UVMHIST_FUNC("uvn_flush"); UVMHIST_CALLED(maphist); 850 851 curoff = 0; /* XXX: shut up gcc */ 852 /* 853 * get init vals and determine how we are going to traverse object 854 */ 855 856 need_iosync = FALSE; 857 retval = TRUE; /* return value */ 858 if (flags & PGO_ALLPAGES) { 859 start = 0; 860 stop = round_page(uvn->u_size); 861 by_list = TRUE; /* always go by the list */ 862 } else { 863 start = trunc_page(start); 864 stop = round_page(stop); 865 if (stop > round_page(uvn->u_size)) 866 printf("uvn_flush: strange, got an out of range " 867 "flush (fixed)\n"); 868 869 by_list = (uobj->uo_npages <= 870 ((stop - start) >> PAGE_SHIFT) * UVN_HASH_PENALTY); 871 } 872 873 UVMHIST_LOG(maphist, 874 " flush start=0x%x, stop=0x%x, by_list=%d, flags=0x%x", 875 start, stop, by_list, flags); 876 877 /* 878 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as 879 * a _hint_ as to how up to date the PG_CLEAN bit is. if the hint 880 * is wrong it will only prevent us from clustering... it won't break 881 * anything. we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster 882 * will set them as it syncs PG_CLEAN. This is only an issue if we 883 * are looking at non-inactive pages (because inactive page's PG_CLEAN 884 * bit is always up to date since there are no mappings). 885 * [borrowed PG_CLEANCHK idea from FreeBSD VM] 886 */ 887 888 if ((flags & PGO_CLEANIT) != 0 && 889 uobj->pgops->pgo_mk_pcluster != NULL) { 890 if (by_list) { 891 for (pp = uobj->memq.tqh_first ; pp != NULL ; 892 pp = pp->listq.tqe_next) { 893 if (pp->offset < start || pp->offset >= stop) 894 continue; 895 pp->flags &= ~PG_CLEANCHK; 896 } 897 898 } else { /* by hash */ 899 for (curoff = start ; curoff < stop; 900 curoff += PAGE_SIZE) { 901 pp = uvm_pagelookup(uobj, curoff); 902 if (pp) 903 pp->flags &= ~PG_CLEANCHK; 904 } 905 } 906 } 907 908 /* 909 * now do it. note: we must update ppnext in body of loop or we 910 * will get stuck. we need to use ppnext because we may free "pp" 911 * before doing the next loop. 912 */ 913 914 if (by_list) { 915 pp = uobj->memq.tqh_first; 916 } else { 917 curoff = start; 918 pp = uvm_pagelookup(uobj, curoff); 919 } 920 921 ppnext = NULL; /* XXX: shut up gcc */ 922 ppsp = NULL; /* XXX: shut up gcc */ 923 uvm_lock_pageq(); /* page queues locked */ 924 925 /* locked: both page queues and uobj */ 926 for ( ; (by_list && pp != NULL) || 927 (!by_list && curoff < stop) ; pp = ppnext) { 928 929 if (by_list) { 930 931 /* 932 * range check 933 */ 934 935 if (pp->offset < start || pp->offset >= stop) { 936 ppnext = pp->listq.tqe_next; 937 continue; 938 } 939 940 } else { 941 942 /* 943 * null check 944 */ 945 946 curoff += PAGE_SIZE; 947 if (pp == NULL) { 948 if (curoff < stop) 949 ppnext = uvm_pagelookup(uobj, curoff); 950 continue; 951 } 952 953 } 954 955 /* 956 * handle case where we do not need to clean page (either 957 * because we are not clean or because page is not dirty or 958 * is busy): 959 * 960 * NOTE: we are allowed to deactivate a non-wired active 961 * PG_BUSY page, but once a PG_BUSY page is on the inactive 962 * queue it must stay put until it is !PG_BUSY (so as not to 963 * confuse pagedaemon). 964 */ 965 966 if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) { 967 needs_clean = FALSE; 968 if ((pp->flags & PG_BUSY) != 0 && 969 (flags & (PGO_CLEANIT|PGO_SYNCIO)) == 970 (PGO_CLEANIT|PGO_SYNCIO)) 971 need_iosync = TRUE; 972 } else { 973 /* 974 * freeing: nuke all mappings so we can sync 975 * PG_CLEAN bit with no race 976 */ 977 if ((pp->flags & PG_CLEAN) != 0 && 978 (flags & PGO_FREE) != 0 && 979 (pp->pqflags & PQ_ACTIVE) != 0) 980 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE); 981 if ((pp->flags & PG_CLEAN) != 0 && 982 pmap_is_modified(PMAP_PGARG(pp))) 983 pp->flags &= ~(PG_CLEAN); 984 pp->flags |= PG_CLEANCHK; /* update "hint" */ 985 986 needs_clean = ((pp->flags & PG_CLEAN) == 0); 987 } 988 989 /* 990 * if we don't need a clean... load ppnext and dispose of pp 991 */ 992 if (!needs_clean) { 993 /* load ppnext */ 994 if (by_list) 995 ppnext = pp->listq.tqe_next; 996 else { 997 if (curoff < stop) 998 ppnext = uvm_pagelookup(uobj, curoff); 999 } 1000 1001 /* now dispose of pp */ 1002 if (flags & PGO_DEACTIVATE) { 1003 if ((pp->pqflags & PQ_INACTIVE) == 0 && 1004 pp->wire_count == 0) { 1005 pmap_page_protect(PMAP_PGARG(pp), 1006 VM_PROT_NONE); 1007 uvm_pagedeactivate(pp); 1008 } 1009 1010 } else if (flags & PGO_FREE) { 1011 if (pp->flags & PG_BUSY) { 1012 /* release busy pages */ 1013 pp->flags |= PG_RELEASED; 1014 } else { 1015 pmap_page_protect(PMAP_PGARG(pp), 1016 VM_PROT_NONE); 1017 /* removed page from object */ 1018 uvm_pagefree(pp); 1019 } 1020 } 1021 /* ppnext is valid so we can continue... */ 1022 continue; 1023 } 1024 1025 /* 1026 * pp points to a page in the locked object that we are 1027 * working on. if it is !PG_CLEAN,!PG_BUSY and we asked 1028 * for cleaning (PGO_CLEANIT). we clean it now. 1029 * 1030 * let uvm_pager_put attempted a clustered page out. 1031 * note: locked: uobj and page queues. 1032 */ 1033 1034 pp->flags |= PG_BUSY; /* we 'own' page now */ 1035 UVM_PAGE_OWN(pp, "uvn_flush"); 1036 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_READ); 1037 pp_version = pp->version; 1038 ReTry: 1039 ppsp = pps; 1040 npages = sizeof(pps) / sizeof(struct vm_page *); 1041 1042 /* locked: page queues, uobj */ 1043 result = uvm_pager_put(uobj, pp, &ppsp, &npages, 1044 flags | PGO_DOACTCLUST, start, stop); 1045 /* unlocked: page queues, uobj */ 1046 1047 /* 1048 * at this point nothing is locked. if we did an async I/O 1049 * it is remotely possible for the async i/o to complete and 1050 * the page "pp" be freed or what not before we get a chance 1051 * to relock the object. in order to detect this, we have 1052 * saved the version number of the page in "pp_version". 1053 */ 1054 1055 /* relock! */ 1056 simple_lock(&uobj->vmobjlock); 1057 uvm_lock_pageq(); 1058 1059 /* 1060 * VM_PAGER_AGAIN: given the structure of this pager, this 1061 * can only happen when we are doing async I/O and can't 1062 * map the pages into kernel memory (pager_map) due to lack 1063 * of vm space. if this happens we drop back to sync I/O. 1064 */ 1065 1066 if (result == VM_PAGER_AGAIN) { 1067 /* 1068 * it is unlikely, but page could have been released 1069 * while we had the object lock dropped. we ignore 1070 * this now and retry the I/O. we will detect and 1071 * handle the released page after the syncio I/O 1072 * completes. 1073 */ 1074 #ifdef DIAGNOSTIC 1075 if (flags & PGO_SYNCIO) 1076 panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)"); 1077 #endif 1078 flags |= PGO_SYNCIO; 1079 goto ReTry; 1080 } 1081 1082 /* 1083 * the cleaning operation is now done. finish up. note that 1084 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us. 1085 * if success (OK, PEND) then uvm_pager_put returns the cluster 1086 * to us in ppsp/npages. 1087 */ 1088 1089 /* 1090 * for pending async i/o if we are not deactivating/freeing 1091 * we can move on to the next page. 1092 */ 1093 1094 if (result == VM_PAGER_PEND) { 1095 1096 if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) { 1097 /* 1098 * no per-page ops: refresh ppnext and continue 1099 */ 1100 if (by_list) { 1101 if (pp->version == pp_version) 1102 ppnext = pp->listq.tqe_next; 1103 else 1104 /* reset */ 1105 ppnext = uobj->memq.tqh_first; 1106 } else { 1107 if (curoff < stop) 1108 ppnext = uvm_pagelookup(uobj, 1109 curoff); 1110 } 1111 continue; 1112 } 1113 1114 /* need to do anything here? */ 1115 } 1116 1117 /* 1118 * need to look at each page of the I/O operation. we defer 1119 * processing "pp" until the last trip through this "for" loop 1120 * so that we can load "ppnext" for the main loop after we 1121 * play with the cluster pages [thus the "npages + 1" in the 1122 * loop below]. 1123 */ 1124 1125 for (lcv = 0 ; lcv < npages + 1 ; lcv++) { 1126 1127 /* 1128 * handle ppnext for outside loop, and saving pp 1129 * until the end. 1130 */ 1131 if (lcv < npages) { 1132 if (ppsp[lcv] == pp) 1133 continue; /* skip pp until the end */ 1134 ptmp = ppsp[lcv]; 1135 } else { 1136 ptmp = pp; 1137 1138 /* set up next page for outer loop */ 1139 if (by_list) { 1140 if (pp->version == pp_version) 1141 ppnext = pp->listq.tqe_next; 1142 else 1143 /* reset */ 1144 ppnext = uobj->memq.tqh_first; 1145 } else { 1146 if (curoff < stop) 1147 ppnext = uvm_pagelookup(uobj, curoff); 1148 } 1149 } 1150 1151 /* 1152 * verify the page didn't get moved while obj was 1153 * unlocked 1154 */ 1155 if (result == VM_PAGER_PEND && ptmp->uobject != uobj) 1156 continue; 1157 1158 /* 1159 * unbusy the page if I/O is done. note that for 1160 * pending I/O it is possible that the I/O op 1161 * finished before we relocked the object (in 1162 * which case the page is no longer busy). 1163 */ 1164 1165 if (result != VM_PAGER_PEND) { 1166 if (ptmp->flags & PG_WANTED) 1167 /* still holding object lock */ 1168 thread_wakeup(ptmp); 1169 1170 ptmp->flags &= ~(PG_WANTED|PG_BUSY); 1171 UVM_PAGE_OWN(ptmp, NULL); 1172 if (ptmp->flags & PG_RELEASED) { 1173 1174 /* pgo_releasepg wants this */ 1175 uvm_unlock_pageq(); 1176 if (!uvn_releasepg(ptmp, NULL)) 1177 return (TRUE); 1178 1179 uvm_lock_pageq(); /* relock */ 1180 continue; /* next page */ 1181 1182 } else { 1183 ptmp->flags |= (PG_CLEAN|PG_CLEANCHK); 1184 if ((flags & PGO_FREE) == 0) 1185 pmap_clear_modify( 1186 PMAP_PGARG(ptmp)); 1187 } 1188 } 1189 1190 /* 1191 * dispose of page 1192 */ 1193 1194 if (flags & PGO_DEACTIVATE) { 1195 if ((pp->pqflags & PQ_INACTIVE) == 0 && 1196 pp->wire_count == 0) { 1197 pmap_page_protect(PMAP_PGARG(ptmp), 1198 VM_PROT_NONE); 1199 uvm_pagedeactivate(ptmp); 1200 } 1201 1202 } else if (flags & PGO_FREE) { 1203 if (result == VM_PAGER_PEND) { 1204 if ((ptmp->flags & PG_BUSY) != 0) 1205 /* signal for i/o done */ 1206 ptmp->flags |= PG_RELEASED; 1207 } else { 1208 if (result != VM_PAGER_OK) { 1209 printf("uvn_flush: obj=%p, " 1210 "offset=0x%lx. error " 1211 "during pageout.\n", 1212 pp->uobject, pp->offset); 1213 printf("uvn_flush: WARNING: " 1214 "changes to page may be " 1215 "lost!\n"); 1216 retval = FALSE; 1217 } 1218 pmap_page_protect(PMAP_PGARG(ptmp), 1219 VM_PROT_NONE); 1220 uvm_pagefree(ptmp); 1221 } 1222 } 1223 1224 } /* end of "lcv" for loop */ 1225 1226 } /* end of "pp" for loop */ 1227 1228 /* 1229 * done with pagequeues: unlock 1230 */ 1231 uvm_unlock_pageq(); 1232 1233 /* 1234 * now wait for all I/O if required. 1235 */ 1236 if (need_iosync) { 1237 1238 UVMHIST_LOG(maphist," <<DOING IOSYNC>>",0,0,0,0); 1239 while (uvn->u_nio != 0) { 1240 uvn->u_flags |= UVM_VNODE_IOSYNC; 1241 UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock, 1242 FALSE, "uvn_flush",0); 1243 simple_lock(&uvn->u_obj.vmobjlock); 1244 } 1245 if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED) 1246 wakeup(&uvn->u_flags); 1247 uvn->u_flags &= ~(UVM_VNODE_IOSYNC|UVM_VNODE_IOSYNCWANTED); 1248 } 1249 1250 /* return, with object locked! */ 1251 UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0); 1252 return(retval); 1253 } 1254 1255 /* 1256 * uvn_cluster 1257 * 1258 * we are about to do I/O in an object at offset. this function is called 1259 * to establish a range of offsets around "offset" in which we can cluster 1260 * I/O. 1261 * 1262 * - currently doesn't matter if obj locked or not. 1263 */ 1264 1265 static void 1266 uvn_cluster(uobj, offset, loffset, hoffset) 1267 struct uvm_object *uobj; 1268 vaddr_t offset; 1269 vaddr_t *loffset, *hoffset; /* OUT */ 1270 { 1271 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj; 1272 *loffset = offset; 1273 1274 if (*loffset >= uvn->u_size) 1275 panic("uvn_cluster: offset out of range"); 1276 1277 /* 1278 * XXX: old pager claims we could use VOP_BMAP to get maxcontig value. 1279 */ 1280 *hoffset = *loffset + MAXBSIZE; 1281 if (*hoffset > round_page(uvn->u_size)) /* past end? */ 1282 *hoffset = round_page(uvn->u_size); 1283 1284 return; 1285 } 1286 1287 /* 1288 * uvn_put: flush page data to backing store. 1289 * 1290 * => prefer map unlocked (not required) 1291 * => object must be locked! we will _unlock_ it before starting I/O. 1292 * => flags: PGO_SYNCIO -- use sync. I/O 1293 * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed) 1294 * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync. 1295 * [thus we never do async i/o! see iodone comment] 1296 */ 1297 1298 static int 1299 uvn_put(uobj, pps, npages, flags) 1300 struct uvm_object *uobj; 1301 struct vm_page **pps; 1302 int npages, flags; 1303 { 1304 int retval; 1305 1306 /* note: object locked */ 1307 retval = uvn_io((struct uvm_vnode*)uobj, pps, npages, flags, UIO_WRITE); 1308 /* note: object unlocked */ 1309 1310 return(retval); 1311 } 1312 1313 1314 /* 1315 * uvn_get: get pages (synchronously) from backing store 1316 * 1317 * => prefer map unlocked (not required) 1318 * => object must be locked! we will _unlock_ it before starting any I/O. 1319 * => flags: PGO_ALLPAGES: get all of the pages 1320 * PGO_LOCKED: fault data structures are locked 1321 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx] 1322 * => NOTE: caller must check for released pages!! 1323 */ 1324 1325 static int 1326 uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags) 1327 struct uvm_object *uobj; 1328 vaddr_t offset; 1329 struct vm_page **pps; /* IN/OUT */ 1330 int *npagesp; /* IN (OUT if PGO_LOCKED) */ 1331 int centeridx, advice, flags; 1332 vm_prot_t access_type; 1333 { 1334 vaddr_t current_offset; 1335 struct vm_page *ptmp; 1336 int lcv, result, gotpages; 1337 boolean_t done; 1338 UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(maphist); 1339 UVMHIST_LOG(maphist, "flags=%d", flags,0,0,0); 1340 1341 /* 1342 * step 1: handled the case where fault data structures are locked. 1343 */ 1344 1345 if (flags & PGO_LOCKED) { 1346 1347 /* 1348 * gotpages is the current number of pages we've gotten (which 1349 * we pass back up to caller via *npagesp. 1350 */ 1351 1352 gotpages = 0; 1353 1354 /* 1355 * step 1a: get pages that are already resident. only do this 1356 * if the data structures are locked (i.e. the first time 1357 * through). 1358 */ 1359 1360 done = TRUE; /* be optimistic */ 1361 1362 for (lcv = 0, current_offset = offset ; lcv < *npagesp ; 1363 lcv++, current_offset += PAGE_SIZE) { 1364 1365 /* do we care about this page? if not, skip it */ 1366 if (pps[lcv] == PGO_DONTCARE) 1367 continue; 1368 1369 /* lookup page */ 1370 ptmp = uvm_pagelookup(uobj, current_offset); 1371 1372 /* to be useful must get a non-busy, non-released pg */ 1373 if (ptmp == NULL || 1374 (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) { 1375 if (lcv == centeridx || (flags & PGO_ALLPAGES) 1376 != 0) 1377 done = FALSE; /* need to do a wait or I/O! */ 1378 continue; 1379 } 1380 1381 /* 1382 * useful page: busy/lock it and plug it in our 1383 * result array 1384 */ 1385 ptmp->flags |= PG_BUSY; /* loan up to caller */ 1386 UVM_PAGE_OWN(ptmp, "uvn_get1"); 1387 pps[lcv] = ptmp; 1388 gotpages++; 1389 1390 } /* "for" lcv loop */ 1391 1392 /* 1393 * XXX: given the "advice", should we consider async read-ahead? 1394 * XXX: fault current does deactive of pages behind us. is 1395 * this good (other callers might now). 1396 */ 1397 /* 1398 * XXX: read-ahead currently handled by buffer cache (bread) 1399 * level. 1400 * XXX: no async i/o available. 1401 * XXX: so we don't do anything now. 1402 */ 1403 1404 /* 1405 * step 1c: now we've either done everything needed or we to 1406 * unlock and do some waiting or I/O. 1407 */ 1408 1409 *npagesp = gotpages; /* let caller know */ 1410 if (done) 1411 return(VM_PAGER_OK); /* bingo! */ 1412 else 1413 /* EEK! Need to unlock and I/O */ 1414 return(VM_PAGER_UNLOCK); 1415 } 1416 1417 /* 1418 * step 2: get non-resident or busy pages. 1419 * object is locked. data structures are unlocked. 1420 * 1421 * XXX: because we can't do async I/O at this level we get things 1422 * page at a time (otherwise we'd chunk). the VOP_READ() will do 1423 * async-read-ahead for us at a lower level. 1424 */ 1425 1426 for (lcv = 0, current_offset = offset ; 1427 lcv < *npagesp ; lcv++, current_offset += PAGE_SIZE) { 1428 1429 /* skip over pages we've already gotten or don't want */ 1430 /* skip over pages we don't _have_ to get */ 1431 if (pps[lcv] != NULL || (lcv != centeridx && 1432 (flags & PGO_ALLPAGES) == 0)) 1433 continue; 1434 1435 /* 1436 * we have yet to locate the current page (pps[lcv]). we first 1437 * look for a page that is already at the current offset. if 1438 * we fine a page, we check to see if it is busy or released. 1439 * if that is the case, then we sleep on the page until it is 1440 * no longer busy or released and repeat the lookup. if the 1441 * page we found is neither busy nor released, then we busy it 1442 * (so we own it) and plug it into pps[lcv]. this breaks the 1443 * following while loop and indicates we are ready to move on 1444 * to the next page in the "lcv" loop above. 1445 * 1446 * if we exit the while loop with pps[lcv] still set to NULL, 1447 * then it means that we allocated a new busy/fake/clean page 1448 * ptmp in the object and we need to do I/O to fill in the data. 1449 */ 1450 1451 while (pps[lcv] == NULL) { /* top of "pps" while loop */ 1452 1453 /* look for a current page */ 1454 ptmp = uvm_pagelookup(uobj, current_offset); 1455 1456 /* nope? allocate one now (if we can) */ 1457 if (ptmp == NULL) { 1458 1459 ptmp = uvm_pagealloc(uobj, current_offset, 1460 NULL); /* alloc */ 1461 1462 /* out of RAM? */ 1463 if (ptmp == NULL) { 1464 simple_unlock(&uobj->vmobjlock); 1465 uvm_wait("uvn_getpage"); 1466 simple_lock(&uobj->vmobjlock); 1467 1468 /* goto top of pps while loop */ 1469 continue; 1470 } 1471 1472 /* 1473 * got new page ready for I/O. break pps 1474 * while loop. pps[lcv] is still NULL. 1475 */ 1476 break; 1477 } 1478 1479 /* page is there, see if we need to wait on it */ 1480 if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) { 1481 ptmp->flags |= PG_WANTED; 1482 UVM_UNLOCK_AND_WAIT(ptmp, 1483 &uobj->vmobjlock, 0, "uvn_get",0); 1484 simple_lock(&uobj->vmobjlock); 1485 continue; /* goto top of pps while loop */ 1486 } 1487 1488 /* 1489 * if we get here then the page has become resident 1490 * and unbusy between steps 1 and 2. we busy it 1491 * now (so we own it) and set pps[lcv] (so that we 1492 * exit the while loop). 1493 */ 1494 ptmp->flags |= PG_BUSY; 1495 UVM_PAGE_OWN(ptmp, "uvn_get2"); 1496 pps[lcv] = ptmp; 1497 } 1498 1499 /* 1500 * if we own the a valid page at the correct offset, pps[lcv] 1501 * will point to it. nothing more to do except go to the 1502 * next page. 1503 */ 1504 1505 if (pps[lcv]) 1506 continue; /* next lcv */ 1507 1508 /* 1509 * we have a "fake/busy/clean" page that we just allocated. do 1510 * I/O to fill it with valid data. note that object must be 1511 * locked going into uvn_io, but will be unlocked afterwards. 1512 */ 1513 1514 result = uvn_io((struct uvm_vnode *) uobj, &ptmp, 1, 1515 PGO_SYNCIO, UIO_READ); 1516 1517 /* 1518 * I/O done. object is unlocked (by uvn_io). because we used 1519 * syncio the result can not be PEND or AGAIN. we must relock 1520 * and check for errors. 1521 */ 1522 1523 /* lock object. check for errors. */ 1524 simple_lock(&uobj->vmobjlock); 1525 if (result != VM_PAGER_OK) { 1526 if (ptmp->flags & PG_WANTED) 1527 /* object lock still held */ 1528 thread_wakeup(ptmp); 1529 1530 ptmp->flags &= ~(PG_WANTED|PG_BUSY); 1531 UVM_PAGE_OWN(ptmp, NULL); 1532 uvm_lock_pageq(); 1533 uvm_pagefree(ptmp); 1534 uvm_unlock_pageq(); 1535 simple_unlock(&uobj->vmobjlock); 1536 return(result); 1537 } 1538 1539 /* 1540 * we got the page! clear the fake flag (indicates valid 1541 * data now in page) and plug into our result array. note 1542 * that page is still busy. 1543 * 1544 * it is the callers job to: 1545 * => check if the page is released 1546 * => unbusy the page 1547 * => activate the page 1548 */ 1549 1550 ptmp->flags &= ~PG_FAKE; /* data is valid ... */ 1551 pmap_clear_modify(PMAP_PGARG(ptmp)); /* ... and clean */ 1552 pps[lcv] = ptmp; 1553 1554 } /* lcv loop */ 1555 1556 /* 1557 * finally, unlock object and return. 1558 */ 1559 1560 simple_unlock(&uobj->vmobjlock); 1561 return (VM_PAGER_OK); 1562 } 1563 1564 /* 1565 * uvn_asyncget: start async I/O to bring pages into ram 1566 * 1567 * => caller must lock object(???XXX: see if this is best) 1568 * => could be called from uvn_get or a madvise() fault-ahead. 1569 * => if it fails, it doesn't matter. 1570 */ 1571 1572 static int 1573 uvn_asyncget(uobj, offset, npages) 1574 struct uvm_object *uobj; 1575 vaddr_t offset; 1576 int npages; 1577 { 1578 1579 /* 1580 * XXXCDC: we can't do async I/O yet 1581 */ 1582 printf("uvn_asyncget called\n"); 1583 return (KERN_SUCCESS); 1584 } 1585 1586 /* 1587 * uvn_io: do I/O to a vnode 1588 * 1589 * => prefer map unlocked (not required) 1590 * => object must be locked! we will _unlock_ it before starting I/O. 1591 * => flags: PGO_SYNCIO -- use sync. I/O 1592 * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync. 1593 * [thus we never do async i/o! see iodone comment] 1594 */ 1595 1596 static int 1597 uvn_io(uvn, pps, npages, flags, rw) 1598 struct uvm_vnode *uvn; 1599 vm_page_t *pps; 1600 int npages, flags, rw; 1601 { 1602 struct vnode *vn; 1603 struct uio uio; 1604 struct iovec iov; 1605 vaddr_t kva, file_offset; 1606 int waitf, result, got, wanted; 1607 UVMHIST_FUNC("uvn_io"); UVMHIST_CALLED(maphist); 1608 1609 UVMHIST_LOG(maphist, "rw=%d", rw,0,0,0); 1610 1611 /* 1612 * init values 1613 */ 1614 1615 waitf = (flags & PGO_SYNCIO) ? M_WAITOK : M_NOWAIT; 1616 vn = (struct vnode *) uvn; 1617 file_offset = pps[0]->offset; 1618 1619 /* 1620 * check for sync'ing I/O. 1621 */ 1622 1623 while (uvn->u_flags & UVM_VNODE_IOSYNC) { 1624 if (waitf == M_NOWAIT) { 1625 simple_unlock(&uvn->u_obj.vmobjlock); 1626 UVMHIST_LOG(maphist,"<- try again (iosync)",0,0,0,0); 1627 return(VM_PAGER_AGAIN); 1628 } 1629 uvn->u_flags |= UVM_VNODE_IOSYNCWANTED; 1630 UVM_UNLOCK_AND_WAIT(&uvn->u_flags, &uvn->u_obj.vmobjlock, 1631 FALSE, "uvn_iosync",0); 1632 simple_lock(&uvn->u_obj.vmobjlock); 1633 } 1634 1635 /* 1636 * check size 1637 */ 1638 1639 if (file_offset >= uvn->u_size) { 1640 simple_unlock(&uvn->u_obj.vmobjlock); 1641 UVMHIST_LOG(maphist,"<- BAD (size check)",0,0,0,0); 1642 #ifdef DIAGNOSTIC 1643 printf("uvn_io: note: size check fired\n"); 1644 #endif 1645 return(VM_PAGER_BAD); 1646 } 1647 1648 /* 1649 * first try and map the pages in (without waiting) 1650 */ 1651 1652 kva = uvm_pagermapin(pps, npages, NULL, M_NOWAIT); 1653 if (kva == NULL && waitf == M_NOWAIT) { 1654 simple_unlock(&uvn->u_obj.vmobjlock); 1655 UVMHIST_LOG(maphist,"<- mapin failed (try again)",0,0,0,0); 1656 return(VM_PAGER_AGAIN); 1657 } 1658 1659 /* 1660 * ok, now bump u_nio up. at this point we are done with uvn 1661 * and can unlock it. if we still don't have a kva, try again 1662 * (this time with sleep ok). 1663 */ 1664 1665 uvn->u_nio++; /* we have an I/O in progress! */ 1666 simple_unlock(&uvn->u_obj.vmobjlock); 1667 /* NOTE: object now unlocked */ 1668 if (kva == NULL) { 1669 kva = uvm_pagermapin(pps, npages, NULL, M_WAITOK); 1670 } 1671 1672 /* 1673 * ok, mapped in. our pages are PG_BUSY so they are not going to 1674 * get touched (so we can look at "offset" without having to lock 1675 * the object). set up for I/O. 1676 */ 1677 1678 /* 1679 * fill out uio/iov 1680 */ 1681 1682 iov.iov_base = (caddr_t) kva; 1683 wanted = npages << PAGE_SHIFT; 1684 if (file_offset + wanted > uvn->u_size) 1685 wanted = uvn->u_size - file_offset; /* XXX: needed? */ 1686 iov.iov_len = wanted; 1687 uio.uio_iov = &iov; 1688 uio.uio_iovcnt = 1; 1689 uio.uio_offset = file_offset; 1690 uio.uio_segflg = UIO_SYSSPACE; 1691 uio.uio_rw = rw; 1692 uio.uio_resid = wanted; 1693 uio.uio_procp = NULL; 1694 1695 /* 1696 * do the I/O! (XXX: curproc?) 1697 */ 1698 1699 UVMHIST_LOG(maphist, "calling VOP",0,0,0,0); 1700 1701 if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0) 1702 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY); 1703 /* NOTE: vnode now locked! */ 1704 1705 if (rw == UIO_READ) 1706 result = VOP_READ(vn, &uio, 0, curproc->p_ucred); 1707 else 1708 result = VOP_WRITE(vn, &uio, 0, curproc->p_ucred); 1709 1710 if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0) 1711 VOP_UNLOCK(vn, 0); 1712 /* NOTE: vnode now unlocked (unless vnislocked) */ 1713 1714 UVMHIST_LOG(maphist, "done calling VOP",0,0,0,0); 1715 1716 /* 1717 * result == unix style errno (0 == OK!) 1718 * 1719 * zero out rest of buffer (if needed) 1720 */ 1721 1722 if (result == 0) { 1723 got = wanted - uio.uio_resid; 1724 1725 if (wanted && got == 0) { 1726 result = EIO; /* XXX: error? */ 1727 } else if (got < PAGE_SIZE * npages && rw == UIO_READ) { 1728 memset((void *) (kva + got), 0, 1729 (npages << PAGE_SHIFT) - got); 1730 } 1731 } 1732 1733 /* 1734 * now remove pager mapping 1735 */ 1736 uvm_pagermapout(kva, npages); 1737 1738 /* 1739 * now clean up the object (i.e. drop I/O count) 1740 */ 1741 1742 simple_lock(&uvn->u_obj.vmobjlock); 1743 /* NOTE: object now locked! */ 1744 1745 uvn->u_nio--; /* I/O DONE! */ 1746 if ((uvn->u_flags & UVM_VNODE_IOSYNC) != 0 && uvn->u_nio == 0) { 1747 wakeup(&uvn->u_nio); 1748 } 1749 simple_unlock(&uvn->u_obj.vmobjlock); 1750 /* NOTE: object now unlocked! */ 1751 1752 /* 1753 * done! 1754 */ 1755 1756 UVMHIST_LOG(maphist, "<- done (result %d)", result,0,0,0); 1757 if (result == 0) 1758 return(VM_PAGER_OK); 1759 else 1760 return(VM_PAGER_ERROR); 1761 } 1762 1763 /* 1764 * uvm_vnp_uncache: disable "persisting" in a vnode... when last reference 1765 * is gone we will kill the object (flushing dirty pages back to the vnode 1766 * if needed). 1767 * 1768 * => returns TRUE if there was no uvm_object attached or if there was 1769 * one and we killed it [i.e. if there is no active uvn] 1770 * => called with the vnode VOP_LOCK'd [we will unlock it for I/O, if 1771 * needed] 1772 * 1773 * => XXX: given that we now kill uvn's when a vnode is recycled (without 1774 * having to hold a reference on the vnode) and given a working 1775 * uvm_vnp_sync(), how does that effect the need for this function? 1776 * [XXXCDC: seems like it can die?] 1777 * 1778 * => XXX: this function should DIE once we merge the VM and buffer 1779 * cache. 1780 * 1781 * research shows that this is called in the following places: 1782 * ext2fs_truncate, ffs_truncate, detrunc[msdosfs]: called when vnode 1783 * changes sizes 1784 * ext2fs_write, WRITE [ufs_readwrite], msdosfs_write: called when we 1785 * are written to 1786 * ex2fs_chmod, ufs_chmod: called if VTEXT vnode and the sticky bit 1787 * is off 1788 * ffs_realloccg: when we can't extend the current block and have 1789 * to allocate a new one we call this [XXX: why?] 1790 * nfsrv_rename, rename_files: called when the target filename is there 1791 * and we want to remove it 1792 * nfsrv_remove, sys_unlink: called on file we are removing 1793 * nfsrv_access: if VTEXT and we want WRITE access and we don't uncache 1794 * then return "text busy" 1795 * nfs_open: seems to uncache any file opened with nfs 1796 * vn_writechk: if VTEXT vnode and can't uncache return "text busy" 1797 */ 1798 1799 boolean_t 1800 uvm_vnp_uncache(vp) 1801 struct vnode *vp; 1802 { 1803 struct uvm_vnode *uvn = &vp->v_uvm; 1804 1805 /* 1806 * lock uvn part of the vnode and check to see if we need to do anything 1807 */ 1808 1809 simple_lock(&uvn->u_obj.vmobjlock); 1810 if ((uvn->u_flags & UVM_VNODE_VALID) == 0 || 1811 (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) { 1812 simple_unlock(&uvn->u_obj.vmobjlock); 1813 return(TRUE); 1814 } 1815 1816 /* 1817 * we have a valid, non-blocked uvn. clear persist flag. 1818 * if uvn is currently active we can return now. 1819 */ 1820 1821 uvn->u_flags &= ~UVM_VNODE_CANPERSIST; 1822 if (uvn->u_obj.uo_refs) { 1823 simple_unlock(&uvn->u_obj.vmobjlock); 1824 return(FALSE); 1825 } 1826 1827 /* 1828 * uvn is currently persisting! we have to gain a reference to 1829 * it so that we can call uvn_detach to kill the uvn. 1830 */ 1831 1832 VREF(vp); /* seems ok, even with VOP_LOCK */ 1833 uvn->u_obj.uo_refs++; /* value is now 1 */ 1834 simple_unlock(&uvn->u_obj.vmobjlock); 1835 1836 1837 #ifdef DEBUG 1838 /* 1839 * carry over sanity check from old vnode pager: the vnode should 1840 * be VOP_LOCK'd, and we confirm it here. 1841 */ 1842 if (!VOP_ISLOCKED(vp)) { 1843 boolean_t is_ok_anyway = FALSE; 1844 #ifdef NFS 1845 extern int (**nfsv2_vnodeop_p) __P((void *)); 1846 extern int (**spec_nfsv2nodeop_p) __P((void *)); 1847 extern int (**fifo_nfsv2nodeop_p) __P((void *)); 1848 1849 /* vnode is NOT VOP_LOCKed: some vnode types _never_ lock */ 1850 if (vp->v_op == nfsv2_vnodeop_p || 1851 vp->v_op == spec_nfsv2nodeop_p) { 1852 is_ok_anyway = TRUE; 1853 } 1854 if (vp->v_op == fifo_nfsv2nodeop_p) { 1855 is_ok_anyway = TRUE; 1856 } 1857 #endif /* NFS */ 1858 if (!is_ok_anyway) 1859 panic("uvm_vnp_uncache: vnode not locked!"); 1860 } 1861 #endif /* DEBUG */ 1862 1863 /* 1864 * now drop our reference to the vnode. if we have the sole 1865 * reference to the vnode then this will cause it to die [as we 1866 * just cleared the persist flag]. we have to unlock the vnode 1867 * while we are doing this as it may trigger I/O. 1868 * 1869 * XXX: it might be possible for uvn to get reclaimed while we are 1870 * unlocked causing us to return TRUE when we should not. we ignore 1871 * this as a false-positive return value doesn't hurt us. 1872 */ 1873 VOP_UNLOCK(vp, 0); 1874 uvn_detach(&uvn->u_obj); 1875 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1876 1877 /* 1878 * and return... 1879 */ 1880 1881 return(TRUE); 1882 } 1883 1884 /* 1885 * uvm_vnp_setsize: grow or shrink a vnode uvn 1886 * 1887 * grow => just update size value 1888 * shrink => toss un-needed pages 1889 * 1890 * => we assume that the caller has a reference of some sort to the 1891 * vnode in question so that it will not be yanked out from under 1892 * us. 1893 * 1894 * called from: 1895 * => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos]) 1896 * => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write) 1897 * => ffs_balloc [XXX: why? doesn't WRITE handle?] 1898 * => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr 1899 * => union fs: union_newsize 1900 */ 1901 1902 void 1903 uvm_vnp_setsize(vp, newsize) 1904 struct vnode *vp; 1905 u_quad_t newsize; 1906 { 1907 struct uvm_vnode *uvn = &vp->v_uvm; 1908 1909 /* 1910 * lock uvn and check for valid object, and if valid: do it! 1911 */ 1912 simple_lock(&uvn->u_obj.vmobjlock); 1913 if (uvn->u_flags & UVM_VNODE_VALID) { 1914 1915 /* 1916 * make sure that the newsize fits within a vaddr_t 1917 * XXX: need to revise addressing data types 1918 */ 1919 1920 if (newsize > (vaddr_t) -PAGE_SIZE) { 1921 #ifdef DEBUG 1922 printf("uvm_vnp_setsize: vn %p size truncated " 1923 "%qx->%lx\n", vp, newsize, (vaddr_t)-PAGE_SIZE); 1924 #endif 1925 newsize = (vaddr_t)-PAGE_SIZE; 1926 } 1927 1928 /* 1929 * now check if the size has changed: if we shrink we had better 1930 * toss some pages... 1931 */ 1932 1933 if (uvn->u_size > newsize) { 1934 (void)uvn_flush(&uvn->u_obj, (vaddr_t) newsize, 1935 uvn->u_size, PGO_FREE); 1936 } 1937 uvn->u_size = (vaddr_t)newsize; 1938 } 1939 simple_unlock(&uvn->u_obj.vmobjlock); 1940 1941 /* 1942 * done 1943 */ 1944 return; 1945 } 1946 1947 /* 1948 * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes. 1949 * 1950 * => called from sys_sync with no VM structures locked 1951 * => only one process can do a sync at a time (because the uvn 1952 * structure only has one queue for sync'ing). we ensure this 1953 * by holding the uvn_sync_lock while the sync is in progress. 1954 * other processes attempting a sync will sleep on this lock 1955 * until we are done. 1956 */ 1957 1958 void 1959 uvm_vnp_sync(mp) 1960 struct mount *mp; 1961 { 1962 struct uvm_vnode *uvn; 1963 struct vnode *vp; 1964 boolean_t got_lock; 1965 1966 /* 1967 * step 1: ensure we are only ones using the uvn_sync_q by locking 1968 * our lock... 1969 */ 1970 lockmgr(&uvn_sync_lock, LK_EXCLUSIVE, (void *)0); 1971 1972 /* 1973 * step 2: build up a simpleq of uvns of interest based on the 1974 * write list. we gain a reference to uvns of interest. must 1975 * be careful about locking uvn's since we will be holding uvn_wl_lock 1976 * in the body of the loop. 1977 */ 1978 SIMPLEQ_INIT(&uvn_sync_q); 1979 simple_lock(&uvn_wl_lock); 1980 for (uvn = uvn_wlist.lh_first ; uvn != NULL ; 1981 uvn = uvn->u_wlist.le_next) { 1982 1983 vp = (struct vnode *) uvn; 1984 if (mp && vp->v_mount != mp) 1985 continue; 1986 1987 /* attempt to gain reference */ 1988 while ((got_lock = simple_lock_try(&uvn->u_obj.vmobjlock)) == 1989 FALSE && 1990 (uvn->u_flags & UVM_VNODE_BLOCKED) == 0) 1991 /* spin */ ; 1992 1993 /* 1994 * we will exit the loop if either if the following are true: 1995 * - we got the lock [always true if NCPU == 1] 1996 * - we failed to get the lock but noticed the vnode was 1997 * "blocked" -- in this case the vnode must be a dying 1998 * vnode, and since dying vnodes are in the process of 1999 * being flushed out, we can safely skip this one 2000 * 2001 * we want to skip over the vnode if we did not get the lock, 2002 * or if the vnode is already dying (due to the above logic). 2003 * 2004 * note that uvn must already be valid because we found it on 2005 * the wlist (this also means it can't be ALOCK'd). 2006 */ 2007 if (!got_lock || (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) { 2008 if (got_lock) 2009 simple_unlock(&uvn->u_obj.vmobjlock); 2010 continue; /* skip it */ 2011 } 2012 2013 /* 2014 * gain reference. watch out for persisting uvns (need to 2015 * regain vnode REF). 2016 */ 2017 if (uvn->u_obj.uo_refs == 0) 2018 VREF(vp); 2019 uvn->u_obj.uo_refs++; 2020 simple_unlock(&uvn->u_obj.vmobjlock); 2021 2022 /* 2023 * got it! 2024 */ 2025 SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq); 2026 } 2027 simple_unlock(&uvn_wl_lock); 2028 2029 /* 2030 * step 3: we now have a list of uvn's that may need cleaning. 2031 * we are holding the uvn_sync_lock, but have dropped the uvn_wl_lock 2032 * (so we can now safely lock uvn's again). 2033 */ 2034 2035 for (uvn = uvn_sync_q.sqh_first ; uvn ; uvn = uvn->u_syncq.sqe_next) { 2036 simple_lock(&uvn->u_obj.vmobjlock); 2037 #ifdef DIAGNOSTIC 2038 if (uvn->u_flags & UVM_VNODE_DYING) { 2039 printf("uvm_vnp_sync: dying vnode on sync list\n"); 2040 } 2041 #endif 2042 uvn_flush(&uvn->u_obj, 0, 0, 2043 PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST); 2044 2045 /* 2046 * if we have the only reference and we just cleaned the uvn, 2047 * then we can pull it out of the UVM_VNODE_WRITEABLE state 2048 * thus allowing us to avoid thinking about flushing it again 2049 * on later sync ops. 2050 */ 2051 if (uvn->u_obj.uo_refs == 1 && 2052 (uvn->u_flags & UVM_VNODE_WRITEABLE)) { 2053 LIST_REMOVE(uvn, u_wlist); 2054 uvn->u_flags &= ~UVM_VNODE_WRITEABLE; 2055 } 2056 2057 simple_unlock(&uvn->u_obj.vmobjlock); 2058 2059 /* now drop our reference to the uvn */ 2060 uvn_detach(&uvn->u_obj); 2061 } 2062 2063 /* 2064 * done! release sync lock 2065 */ 2066 lockmgr(&uvn_sync_lock, LK_RELEASE, (void *)0); 2067 } 2068