1 /* $OpenBSD: uvm_vnode.c,v 1.85 2014/11/16 12:31:01 deraadt Exp $ */ 2 /* $NetBSD: uvm_vnode.c,v 1.36 2000/11/24 20:34:01 chs Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1991, 1993 7 * The Regents of the University of California. 8 * Copyright (c) 1990 University of Utah. 9 * 10 * All rights reserved. 11 * 12 * This code is derived from software contributed to Berkeley by 13 * the Systems Programming Group of the University of Utah Computer 14 * Science Department. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. 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 * @(#)vnode_pager.c 8.8 (Berkeley) 2/13/94 41 * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp 42 */ 43 44 /* 45 * uvm_vnode.c: the vnode pager. 46 */ 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/proc.h> 51 #include <sys/malloc.h> 52 #include <sys/vnode.h> 53 #include <sys/disklabel.h> 54 #include <sys/ioctl.h> 55 #include <sys/fcntl.h> 56 #include <sys/conf.h> 57 #include <sys/rwlock.h> 58 #include <sys/dkio.h> 59 #include <sys/specdev.h> 60 61 #include <uvm/uvm.h> 62 #include <uvm/uvm_vnode.h> 63 64 /* 65 * private global data structure 66 * 67 * we keep a list of writeable active vnode-backed VM objects for sync op. 68 * we keep a simpleq of vnodes that are currently being sync'd. 69 */ 70 71 LIST_HEAD(uvn_list_struct, uvm_vnode); 72 struct uvn_list_struct uvn_wlist; /* writeable uvns */ 73 74 SIMPLEQ_HEAD(uvn_sq_struct, uvm_vnode); 75 struct uvn_sq_struct uvn_sync_q; /* sync'ing uvns */ 76 struct rwlock uvn_sync_lock; /* locks sync operation */ 77 78 /* 79 * functions 80 */ 81 void uvn_cluster(struct uvm_object *, voff_t, voff_t *, voff_t *); 82 void uvn_detach(struct uvm_object *); 83 boolean_t uvn_flush(struct uvm_object *, voff_t, voff_t, int); 84 int uvn_get(struct uvm_object *, voff_t, vm_page_t *, int *, int, 85 vm_prot_t, int, int); 86 void uvn_init(void); 87 int uvn_io(struct uvm_vnode *, vm_page_t *, int, int, int); 88 int uvn_put(struct uvm_object *, vm_page_t *, int, boolean_t); 89 void uvn_reference(struct uvm_object *); 90 91 /* 92 * master pager structure 93 */ 94 struct uvm_pagerops uvm_vnodeops = { 95 uvn_init, 96 uvn_reference, 97 uvn_detach, 98 NULL, /* no specialized fault routine required */ 99 uvn_flush, 100 uvn_get, 101 uvn_put, 102 uvn_cluster, 103 uvm_mk_pcluster, /* use generic version of this: see uvm_pager.c */ 104 }; 105 106 /* 107 * the ops! 108 */ 109 /* 110 * uvn_init 111 * 112 * init pager private data structures. 113 */ 114 void 115 uvn_init(void) 116 { 117 118 LIST_INIT(&uvn_wlist); 119 /* note: uvn_sync_q init'd in uvm_vnp_sync() */ 120 rw_init(&uvn_sync_lock, "uvnsync"); 121 } 122 123 /* 124 * uvn_attach 125 * 126 * attach a vnode structure to a VM object. if the vnode is already 127 * attached, then just bump the reference count by one and return the 128 * VM object. if not already attached, attach and return the new VM obj. 129 * the "accessprot" tells the max access the attaching thread wants to 130 * our pages. 131 * 132 * => in fact, nothing should be locked so that we can sleep here. 133 * => note that uvm_object is first thing in vnode structure, so their 134 * pointers are equiv. 135 */ 136 struct uvm_object * 137 uvn_attach(struct vnode *vp, vm_prot_t accessprot) 138 { 139 struct uvm_vnode *uvn = &vp->v_uvm; 140 struct vattr vattr; 141 int oldflags, result; 142 struct partinfo pi; 143 u_quad_t used_vnode_size; 144 145 used_vnode_size = (u_quad_t)0; /* XXX gcc -Wuninitialized */ 146 147 /* first get a lock on the uvn. */ 148 while (uvn->u_flags & UVM_VNODE_BLOCKED) { 149 uvn->u_flags |= UVM_VNODE_WANTED; 150 UVM_WAIT(uvn, FALSE, "uvn_attach", 0); 151 } 152 153 /* if we're mapping a BLK device, make sure it is a disk. */ 154 if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) { 155 return(NULL); 156 } 157 158 /* 159 * now uvn must not be in a blocked state. 160 * first check to see if it is already active, in which case 161 * we can bump the reference count, check to see if we need to 162 * add it to the writeable list, and then return. 163 */ 164 if (uvn->u_flags & UVM_VNODE_VALID) { /* already active? */ 165 166 /* regain vref if we were persisting */ 167 if (uvn->u_obj.uo_refs == 0) { 168 vref(vp); 169 } 170 uvn->u_obj.uo_refs++; /* bump uvn ref! */ 171 172 /* check for new writeable uvn */ 173 if ((accessprot & PROT_WRITE) != 0 && 174 (uvn->u_flags & UVM_VNODE_WRITEABLE) == 0) { 175 LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist); 176 /* we are now on wlist! */ 177 uvn->u_flags |= UVM_VNODE_WRITEABLE; 178 } 179 180 return (&uvn->u_obj); 181 } 182 183 /* 184 * need to call VOP_GETATTR() to get the attributes, but that could 185 * block (due to I/O), so we want to unlock the object before calling. 186 * however, we want to keep anyone else from playing with the object 187 * while it is unlocked. to do this we set UVM_VNODE_ALOCK which 188 * prevents anyone from attaching to the vnode until we are done with 189 * it. 190 */ 191 uvn->u_flags = UVM_VNODE_ALOCK; 192 193 if (vp->v_type == VBLK) { 194 /* 195 * We could implement this as a specfs getattr call, but: 196 * 197 * (1) VOP_GETATTR() would get the file system 198 * vnode operation, not the specfs operation. 199 * 200 * (2) All we want is the size, anyhow. 201 */ 202 result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, 203 DIOCGPART, (caddr_t)&pi, FREAD, curproc); 204 if (result == 0) { 205 /* XXX should remember blocksize */ 206 used_vnode_size = (u_quad_t)pi.disklab->d_secsize * 207 (u_quad_t)DL_GETPSIZE(pi.part); 208 } 209 } else { 210 result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc); 211 if (result == 0) 212 used_vnode_size = vattr.va_size; 213 } 214 215 if (result != 0) { 216 if (uvn->u_flags & UVM_VNODE_WANTED) 217 wakeup(uvn); 218 uvn->u_flags = 0; 219 return(NULL); 220 } 221 222 /* 223 * make sure that the newsize fits within a vaddr_t 224 * XXX: need to revise addressing data types 225 */ 226 #ifdef DEBUG 227 if (vp->v_type == VBLK) 228 printf("used_vnode_size = %llu\n", (long long)used_vnode_size); 229 #endif 230 231 /* now set up the uvn. */ 232 uvm_objinit(&uvn->u_obj, &uvm_vnodeops, 1); 233 oldflags = uvn->u_flags; 234 uvn->u_flags = UVM_VNODE_VALID|UVM_VNODE_CANPERSIST; 235 uvn->u_nio = 0; 236 uvn->u_size = used_vnode_size; 237 238 /* if write access, we need to add it to the wlist */ 239 if (accessprot & PROT_WRITE) { 240 LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist); 241 uvn->u_flags |= UVM_VNODE_WRITEABLE; /* we are on wlist! */ 242 } 243 244 /* 245 * add a reference to the vnode. this reference will stay as long 246 * as there is a valid mapping of the vnode. dropped when the 247 * reference count goes to zero [and we either free or persist]. 248 */ 249 vref(vp); 250 if (oldflags & UVM_VNODE_WANTED) 251 wakeup(uvn); 252 253 return(&uvn->u_obj); 254 } 255 256 257 /* 258 * uvn_reference 259 * 260 * duplicate a reference to a VM object. Note that the reference 261 * count must already be at least one (the passed in reference) so 262 * there is no chance of the uvn being killed out here. 263 * 264 * => caller must be using the same accessprot as was used at attach time 265 */ 266 267 268 void 269 uvn_reference(struct uvm_object *uobj) 270 { 271 #ifdef DEBUG 272 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj; 273 #endif 274 275 #ifdef DEBUG 276 if ((uvn->u_flags & UVM_VNODE_VALID) == 0) { 277 printf("uvn_reference: ref=%d, flags=0x%x\n", uvn->u_flags, 278 uobj->uo_refs); 279 panic("uvn_reference: invalid state"); 280 } 281 #endif 282 uobj->uo_refs++; 283 } 284 285 /* 286 * uvn_detach 287 * 288 * remove a reference to a VM object. 289 * 290 * => caller must call with map locked. 291 * => this starts the detach process, but doesn't have to finish it 292 * (async i/o could still be pending). 293 */ 294 void 295 uvn_detach(struct uvm_object *uobj) 296 { 297 struct uvm_vnode *uvn; 298 struct vnode *vp; 299 int oldflags; 300 301 302 uobj->uo_refs--; /* drop ref! */ 303 if (uobj->uo_refs) { /* still more refs */ 304 return; 305 } 306 307 /* get other pointers ... */ 308 uvn = (struct uvm_vnode *) uobj; 309 vp = (struct vnode *) uobj; 310 311 /* 312 * clear VTEXT flag now that there are no mappings left (VTEXT is used 313 * to keep an active text file from being overwritten). 314 */ 315 vp->v_flag &= ~VTEXT; 316 317 /* 318 * we just dropped the last reference to the uvn. see if we can 319 * let it "stick around". 320 */ 321 if (uvn->u_flags & UVM_VNODE_CANPERSIST) { 322 /* won't block */ 323 uvn_flush(uobj, 0, 0, PGO_DEACTIVATE|PGO_ALLPAGES); 324 vrele(vp); /* drop vnode reference */ 325 return; 326 } 327 328 /* its a goner! */ 329 uvn->u_flags |= UVM_VNODE_DYING; 330 331 /* 332 * even though we may unlock in flush, no one can gain a reference 333 * to us until we clear the "dying" flag [because it blocks 334 * attaches]. we will not do that until after we've disposed of all 335 * the pages with uvn_flush(). note that before the flush the only 336 * pages that could be marked PG_BUSY are ones that are in async 337 * pageout by the daemon. (there can't be any pending "get"'s 338 * because there are no references to the object). 339 */ 340 (void) uvn_flush(uobj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES); 341 342 /* 343 * given the structure of this pager, the above flush request will 344 * create the following state: all the pages that were in the object 345 * have either been free'd or they are marked PG_BUSY and in the 346 * middle of an async io. If we still have pages we set the "relkill" 347 * state, so that in the case the vnode gets terminated we know 348 * to leave it alone. Otherwise we'll kill the vnode when it's empty. 349 */ 350 uvn->u_flags |= UVM_VNODE_RELKILL; 351 /* wait on any outstanding io */ 352 while (uobj->uo_npages && uvn->u_flags & UVM_VNODE_RELKILL) { 353 uvn->u_flags |= UVM_VNODE_IOSYNC; 354 UVM_WAIT(&uvn->u_nio, FALSE, "uvn_term", 0); 355 } 356 357 if ((uvn->u_flags & UVM_VNODE_RELKILL) == 0) 358 return; 359 360 /* 361 * kill object now. note that we can't be on the sync q because 362 * all references are gone. 363 */ 364 if (uvn->u_flags & UVM_VNODE_WRITEABLE) { 365 LIST_REMOVE(uvn, u_wlist); 366 } 367 KASSERT(RB_EMPTY(&uobj->memt)); 368 oldflags = uvn->u_flags; 369 uvn->u_flags = 0; 370 371 /* wake up any sleepers */ 372 if (oldflags & UVM_VNODE_WANTED) 373 wakeup(uvn); 374 375 /* drop our reference to the vnode. */ 376 vrele(vp); 377 378 return; 379 } 380 381 /* 382 * uvm_vnp_terminate: external hook to clear out a vnode's VM 383 * 384 * called in two cases: 385 * [1] when a persisting vnode vm object (i.e. one with a zero reference 386 * count) needs to be freed so that a vnode can be reused. this 387 * happens under "getnewvnode" in vfs_subr.c. if the vnode from 388 * the free list is still attached (i.e. not VBAD) then vgone is 389 * called. as part of the vgone trace this should get called to 390 * free the vm object. this is the common case. 391 * [2] when a filesystem is being unmounted by force (MNT_FORCE, 392 * "umount -f") the vgone() function is called on active vnodes 393 * on the mounted file systems to kill their data (the vnodes become 394 * "dead" ones [see src/sys/miscfs/deadfs/...]). that results in a 395 * call here (even if the uvn is still in use -- i.e. has a non-zero 396 * reference count). this case happens at "umount -f" and during a 397 * "reboot/halt" operation. 398 * 399 * => the caller must XLOCK and VOP_LOCK the vnode before calling us 400 * [protects us from getting a vnode that is already in the DYING 401 * state...] 402 * => in case [2] the uvn is still alive after this call, but all I/O 403 * ops will fail (due to the backing vnode now being "dead"). this 404 * will prob. kill any process using the uvn due to pgo_get failing. 405 */ 406 void 407 uvm_vnp_terminate(struct vnode *vp) 408 { 409 struct uvm_vnode *uvn = &vp->v_uvm; 410 int oldflags; 411 412 /* check if it is valid */ 413 if ((uvn->u_flags & UVM_VNODE_VALID) == 0) { 414 return; 415 } 416 417 /* 418 * must be a valid uvn that is not already dying (because XLOCK 419 * protects us from that). the uvn can't in the ALOCK state 420 * because it is valid, and uvn's that are in the ALOCK state haven't 421 * been marked valid yet. 422 */ 423 #ifdef DEBUG 424 /* 425 * debug check: are we yanking the vnode out from under our uvn? 426 */ 427 if (uvn->u_obj.uo_refs) { 428 printf("uvm_vnp_terminate(%p): terminating active vnode " 429 "(refs=%d)\n", uvn, uvn->u_obj.uo_refs); 430 } 431 #endif 432 433 /* 434 * it is possible that the uvn was detached and is in the relkill 435 * state [i.e. waiting for async i/o to finish]. 436 * we take over the vnode now and cancel the relkill. 437 * we want to know when the i/o is done so we can recycle right 438 * away. note that a uvn can only be in the RELKILL state if it 439 * has a zero reference count. 440 */ 441 if (uvn->u_flags & UVM_VNODE_RELKILL) 442 uvn->u_flags &= ~UVM_VNODE_RELKILL; /* cancel RELKILL */ 443 444 /* 445 * block the uvn by setting the dying flag, and then flush the 446 * pages. 447 * 448 * also, note that we tell I/O that we are already VOP_LOCK'd so 449 * that uvn_io doesn't attempt to VOP_LOCK again. 450 * 451 * XXXCDC: setting VNISLOCKED on an active uvn which is being terminated 452 * due to a forceful unmount might not be a good idea. maybe we 453 * need a way to pass in this info to uvn_flush through a 454 * pager-defined PGO_ constant [currently there are none]. 455 */ 456 uvn->u_flags |= UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED; 457 458 (void) uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES); 459 460 /* 461 * as we just did a flush we expect all the pages to be gone or in 462 * the process of going. sleep to wait for the rest to go [via iosync]. 463 */ 464 while (uvn->u_obj.uo_npages) { 465 #ifdef DEBUG 466 struct vm_page *pp; 467 RB_FOREACH(pp, uvm_objtree, &uvn->u_obj.memt) { 468 if ((pp->pg_flags & PG_BUSY) == 0) 469 panic("uvm_vnp_terminate: detected unbusy pg"); 470 } 471 if (uvn->u_nio == 0) 472 panic("uvm_vnp_terminate: no I/O to wait for?"); 473 printf("uvm_vnp_terminate: waiting for I/O to fin.\n"); 474 /* 475 * XXXCDC: this is unlikely to happen without async i/o so we 476 * put a printf in just to keep an eye on it. 477 */ 478 #endif 479 uvn->u_flags |= UVM_VNODE_IOSYNC; 480 UVM_WAIT(&uvn->u_nio, FALSE, "uvn_term", 0); 481 } 482 483 /* 484 * done. now we free the uvn if its reference count is zero 485 * (true if we are zapping a persisting uvn). however, if we are 486 * terminating a uvn with active mappings we let it live ... future 487 * calls down to the vnode layer will fail. 488 */ 489 oldflags = uvn->u_flags; 490 if (uvn->u_obj.uo_refs) { 491 /* 492 * uvn must live on it is dead-vnode state until all references 493 * are gone. restore flags. clear CANPERSIST state. 494 */ 495 uvn->u_flags &= ~(UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED| 496 UVM_VNODE_WANTED|UVM_VNODE_CANPERSIST); 497 } else { 498 /* 499 * free the uvn now. note that the vref reference is already 500 * gone [it is dropped when we enter the persist state]. 501 */ 502 if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED) 503 panic("uvm_vnp_terminate: io sync wanted bit set"); 504 505 if (uvn->u_flags & UVM_VNODE_WRITEABLE) { 506 LIST_REMOVE(uvn, u_wlist); 507 } 508 uvn->u_flags = 0; /* uvn is history, clear all bits */ 509 } 510 511 if (oldflags & UVM_VNODE_WANTED) 512 wakeup(uvn); 513 } 514 515 /* 516 * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go 517 * through the buffer cache and allow I/O in any size. These VOPs use 518 * synchronous i/o. [vs. VOP_STRATEGY which can be async, but doesn't 519 * go through the buffer cache or allow I/O sizes larger than a 520 * block]. we will eventually want to change this. 521 * 522 * issues to consider: 523 * uvm provides the uvm_aiodesc structure for async i/o management. 524 * there are two tailq's in the uvm. structure... one for pending async 525 * i/o and one for "done" async i/o. to do an async i/o one puts 526 * an aiodesc on the "pending" list (protected by splbio()), starts the 527 * i/o and returns VM_PAGER_PEND. when the i/o is done, we expect 528 * some sort of "i/o done" function to be called (at splbio(), interrupt 529 * time). this function should remove the aiodesc from the pending list 530 * and place it on the "done" list and wakeup the daemon. the daemon 531 * will run at normal spl() and will remove all items from the "done" 532 * list and call the "aiodone" hook for each done request (see uvm_pager.c). 533 * [in the old vm code, this was done by calling the "put" routine with 534 * null arguments which made the code harder to read and understand because 535 * you had one function ("put") doing two things.] 536 * 537 * so the current pager needs: 538 * int uvn_aiodone(struct uvm_aiodesc *) 539 * 540 * => return 0 (aio finished, free it). otherwise requeue for later collection. 541 * => called with pageq's locked by the daemon. 542 * 543 * general outline: 544 * - drop "u_nio" (this req is done!) 545 * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio } 546 * - get "page" structures (atop?). 547 * - handle "wanted" pages 548 * dont forget to look at "object" wanted flag in all cases. 549 */ 550 551 /* 552 * uvn_flush: flush pages out of a uvm object. 553 * 554 * => if PGO_CLEANIT is set, we may block (due to I/O). thus, a caller 555 * might want to unlock higher level resources (e.g. vm_map) 556 * before calling flush. 557 * => if PGO_CLEANIT is not set, then we will not block 558 * => if PGO_ALLPAGE is set, then all pages in the object are valid targets 559 * for flushing. 560 * => NOTE: we are allowed to lock the page queues, so the caller 561 * must not be holding the lock on them [e.g. pagedaemon had 562 * better not call us with the queues locked] 563 * => we return TRUE unless we encountered some sort of I/O error 564 * 565 * comment on "cleaning" object and PG_BUSY pages: 566 * this routine is holding the lock on the object. the only time 567 * that it can run into a PG_BUSY page that it does not own is if 568 * some other process has started I/O on the page (e.g. either 569 * a pagein, or a pageout). if the PG_BUSY page is being paged 570 * in, then it can not be dirty (!PG_CLEAN) because no one has 571 * had a chance to modify it yet. if the PG_BUSY page is being 572 * paged out then it means that someone else has already started 573 * cleaning the page for us (how nice!). in this case, if we 574 * have syncio specified, then after we make our pass through the 575 * object we need to wait for the other PG_BUSY pages to clear 576 * off (i.e. we need to do an iosync). also note that once a 577 * page is PG_BUSY it must stay in its object until it is un-busyed. 578 */ 579 #define UVN_HASH_PENALTY 4 /* XXX: a guess */ 580 581 boolean_t 582 uvn_flush(struct uvm_object *uobj, voff_t start, voff_t stop, int flags) 583 { 584 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj; 585 struct vm_page *pp, *ptmp; 586 struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp; 587 int npages, result, lcv; 588 boolean_t retval, need_iosync, needs_clean; 589 voff_t curoff; 590 591 /* get init vals and determine how we are going to traverse object */ 592 need_iosync = FALSE; 593 retval = TRUE; /* return value */ 594 if (flags & PGO_ALLPAGES) { 595 start = 0; 596 stop = round_page(uvn->u_size); 597 } else { 598 start = trunc_page(start); 599 stop = MIN(round_page(stop), round_page(uvn->u_size)); 600 } 601 602 /* 603 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as 604 * a _hint_ as to how up to date the PG_CLEAN bit is. if the hint 605 * is wrong it will only prevent us from clustering... it won't break 606 * anything. we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster 607 * will set them as it syncs PG_CLEAN. This is only an issue if we 608 * are looking at non-inactive pages (because inactive page's PG_CLEAN 609 * bit is always up to date since there are no mappings). 610 * [borrowed PG_CLEANCHK idea from FreeBSD VM] 611 */ 612 if ((flags & PGO_CLEANIT) != 0) { 613 KASSERT(uobj->pgops->pgo_mk_pcluster != 0); 614 for (curoff = start ; curoff < stop; curoff += PAGE_SIZE) { 615 if ((pp = uvm_pagelookup(uobj, curoff)) != NULL) 616 atomic_clearbits_int(&pp->pg_flags, 617 PG_CLEANCHK); 618 } 619 } 620 621 ppsp = NULL; /* XXX: shut up gcc */ 622 uvm_lock_pageq(); 623 /* locked: both page queues */ 624 for (curoff = start; curoff < stop; curoff += PAGE_SIZE) { 625 if ((pp = uvm_pagelookup(uobj, curoff)) == NULL) 626 continue; 627 /* 628 * handle case where we do not need to clean page (either 629 * because we are not clean or because page is not dirty or 630 * is busy): 631 * 632 * NOTE: we are allowed to deactivate a non-wired active 633 * PG_BUSY page, but once a PG_BUSY page is on the inactive 634 * queue it must stay put until it is !PG_BUSY (so as not to 635 * confuse pagedaemon). 636 */ 637 if ((flags & PGO_CLEANIT) == 0 || (pp->pg_flags & PG_BUSY) != 0) { 638 needs_clean = FALSE; 639 if ((pp->pg_flags & PG_BUSY) != 0 && 640 (flags & (PGO_CLEANIT|PGO_SYNCIO)) == 641 (PGO_CLEANIT|PGO_SYNCIO)) 642 need_iosync = TRUE; 643 } else { 644 /* 645 * freeing: nuke all mappings so we can sync 646 * PG_CLEAN bit with no race 647 */ 648 if ((pp->pg_flags & PG_CLEAN) != 0 && 649 (flags & PGO_FREE) != 0 && 650 (pp->pg_flags & PQ_ACTIVE) != 0) 651 pmap_page_protect(pp, PROT_NONE); 652 if ((pp->pg_flags & PG_CLEAN) != 0 && 653 pmap_is_modified(pp)) 654 atomic_clearbits_int(&pp->pg_flags, PG_CLEAN); 655 atomic_setbits_int(&pp->pg_flags, PG_CLEANCHK); 656 657 needs_clean = ((pp->pg_flags & PG_CLEAN) == 0); 658 } 659 660 /* if we don't need a clean, deactivate/free pages then cont. */ 661 if (!needs_clean) { 662 if (flags & PGO_DEACTIVATE) { 663 if (pp->wire_count == 0) { 664 pmap_page_protect(pp, PROT_NONE); 665 uvm_pagedeactivate(pp); 666 } 667 } else if (flags & PGO_FREE) { 668 if (pp->pg_flags & PG_BUSY) { 669 atomic_setbits_int(&pp->pg_flags, 670 PG_WANTED); 671 uvm_unlock_pageq(); 672 UVM_WAIT(pp, 0, "uvn_flsh", 0); 673 uvm_lock_pageq(); 674 curoff -= PAGE_SIZE; 675 continue; 676 } else { 677 pmap_page_protect(pp, PROT_NONE); 678 /* removed page from object */ 679 uvm_pagefree(pp); 680 } 681 } 682 continue; 683 } 684 685 /* 686 * pp points to a page in the object that we are 687 * working on. if it is !PG_CLEAN,!PG_BUSY and we asked 688 * for cleaning (PGO_CLEANIT). we clean it now. 689 * 690 * let uvm_pager_put attempted a clustered page out. 691 * note: locked: page queues. 692 */ 693 atomic_setbits_int(&pp->pg_flags, PG_BUSY); 694 UVM_PAGE_OWN(pp, "uvn_flush"); 695 pmap_page_protect(pp, PROT_READ); 696 /* if we're async, free the page in aiodoned */ 697 if ((flags & (PGO_FREE|PGO_SYNCIO)) == PGO_FREE) 698 atomic_setbits_int(&pp->pg_flags, PG_RELEASED); 699 ReTry: 700 ppsp = pps; 701 npages = sizeof(pps) / sizeof(struct vm_page *); 702 703 result = uvm_pager_put(uobj, pp, &ppsp, &npages, 704 flags | PGO_DOACTCLUST, start, stop); 705 706 /* 707 * if we did an async I/O it is remotely possible for the 708 * async i/o to complete and the page "pp" be freed or what 709 * not before we get a chance to relock the object. Therefore, 710 * we only touch it when it won't be freed, RELEASED took care 711 * of the rest. 712 */ 713 uvm_lock_pageq(); 714 715 /* 716 * VM_PAGER_AGAIN: given the structure of this pager, this 717 * can only happen when we are doing async I/O and can't 718 * map the pages into kernel memory (pager_map) due to lack 719 * of vm space. if this happens we drop back to sync I/O. 720 */ 721 if (result == VM_PAGER_AGAIN) { 722 /* 723 * it is unlikely, but page could have been released 724 * we ignore this now and retry the I/O. 725 * we will detect and 726 * handle the released page after the syncio I/O 727 * completes. 728 */ 729 #ifdef DIAGNOSTIC 730 if (flags & PGO_SYNCIO) 731 panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)"); 732 #endif 733 flags |= PGO_SYNCIO; 734 if (flags & PGO_FREE) 735 atomic_clearbits_int(&pp->pg_flags, 736 PG_RELEASED); 737 738 goto ReTry; 739 } 740 741 /* 742 * the cleaning operation is now done. finish up. note that 743 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us. 744 * if success (OK, PEND) then uvm_pager_put returns the cluster 745 * to us in ppsp/npages. 746 */ 747 /* 748 * for pending async i/o if we are not deactivating 749 * we can move on to the next page. aiodoned deals with 750 * the freeing case for us. 751 */ 752 if (result == VM_PAGER_PEND && (flags & PGO_DEACTIVATE) == 0) 753 continue; 754 755 /* 756 * need to look at each page of the I/O operation, and do what 757 * we gotta do. 758 */ 759 for (lcv = 0 ; lcv < npages; lcv++) { 760 ptmp = ppsp[lcv]; 761 /* 762 * verify the page didn't get moved 763 */ 764 if (result == VM_PAGER_PEND && ptmp->uobject != uobj) 765 continue; 766 767 /* 768 * unbusy the page if I/O is done. note that for 769 * pending I/O it is possible that the I/O op 770 * finished 771 * (in which case the page is no longer busy). 772 */ 773 if (result != VM_PAGER_PEND) { 774 if (ptmp->pg_flags & PG_WANTED) 775 wakeup(ptmp); 776 777 atomic_clearbits_int(&ptmp->pg_flags, 778 PG_WANTED|PG_BUSY); 779 UVM_PAGE_OWN(ptmp, NULL); 780 atomic_setbits_int(&ptmp->pg_flags, 781 PG_CLEAN|PG_CLEANCHK); 782 if ((flags & PGO_FREE) == 0) 783 pmap_clear_modify(ptmp); 784 } 785 786 /* dispose of page */ 787 if (flags & PGO_DEACTIVATE) { 788 if (ptmp->wire_count == 0) { 789 pmap_page_protect(ptmp, PROT_NONE); 790 uvm_pagedeactivate(ptmp); 791 } 792 } else if (flags & PGO_FREE && 793 result != VM_PAGER_PEND) { 794 if (result != VM_PAGER_OK) { 795 printf("uvn_flush: obj=%p, " 796 "offset=0x%llx. error " 797 "during pageout.\n", 798 pp->uobject, 799 (long long)pp->offset); 800 printf("uvn_flush: WARNING: " 801 "changes to page may be " 802 "lost!\n"); 803 retval = FALSE; 804 } 805 pmap_page_protect(ptmp, PROT_NONE); 806 uvm_pagefree(ptmp); 807 } 808 809 } /* end of "lcv" for loop */ 810 811 } /* end of "pp" for loop */ 812 813 /* done with pagequeues: unlock */ 814 uvm_unlock_pageq(); 815 816 /* now wait for all I/O if required. */ 817 if (need_iosync) { 818 while (uvn->u_nio != 0) { 819 uvn->u_flags |= UVM_VNODE_IOSYNC; 820 UVM_WAIT(&uvn->u_nio, FALSE, "uvn_flush", 0); 821 } 822 if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED) 823 wakeup(&uvn->u_flags); 824 uvn->u_flags &= ~(UVM_VNODE_IOSYNC|UVM_VNODE_IOSYNCWANTED); 825 } 826 827 return(retval); 828 } 829 830 /* 831 * uvn_cluster 832 * 833 * we are about to do I/O in an object at offset. this function is called 834 * to establish a range of offsets around "offset" in which we can cluster 835 * I/O. 836 */ 837 838 void 839 uvn_cluster(struct uvm_object *uobj, voff_t offset, voff_t *loffset, 840 voff_t *hoffset) 841 { 842 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj; 843 *loffset = offset; 844 845 if (*loffset >= uvn->u_size) 846 panic("uvn_cluster: offset out of range"); 847 848 /* 849 * XXX: old pager claims we could use VOP_BMAP to get maxcontig value. 850 */ 851 *hoffset = *loffset + MAXBSIZE; 852 if (*hoffset > round_page(uvn->u_size)) /* past end? */ 853 *hoffset = round_page(uvn->u_size); 854 855 return; 856 } 857 858 /* 859 * uvn_put: flush page data to backing store. 860 * 861 * => prefer map unlocked (not required) 862 * => flags: PGO_SYNCIO -- use sync. I/O 863 * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed) 864 * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync. 865 * [thus we never do async i/o! see iodone comment] 866 */ 867 int 868 uvn_put(struct uvm_object *uobj, struct vm_page **pps, int npages, int flags) 869 { 870 int retval; 871 872 retval = uvn_io((struct uvm_vnode*)uobj, pps, npages, flags, UIO_WRITE); 873 874 return(retval); 875 } 876 877 /* 878 * uvn_get: get pages (synchronously) from backing store 879 * 880 * => prefer map unlocked (not required) 881 * => flags: PGO_ALLPAGES: get all of the pages 882 * PGO_LOCKED: fault data structures are locked 883 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx] 884 * => NOTE: caller must check for released pages!! 885 */ 886 int 887 uvn_get(struct uvm_object *uobj, voff_t offset, struct vm_page **pps, 888 int *npagesp, int centeridx, vm_prot_t access_type, int advice, int flags) 889 { 890 voff_t current_offset; 891 struct vm_page *ptmp; 892 int lcv, result, gotpages; 893 boolean_t done; 894 895 /* step 1: handled the case where fault data structures are locked. */ 896 if (flags & PGO_LOCKED) { 897 /* 898 * gotpages is the current number of pages we've gotten (which 899 * we pass back up to caller via *npagesp. 900 */ 901 gotpages = 0; 902 903 /* 904 * step 1a: get pages that are already resident. only do this 905 * if the data structures are locked (i.e. the first time 906 * through). 907 */ 908 done = TRUE; /* be optimistic */ 909 910 for (lcv = 0, current_offset = offset ; lcv < *npagesp ; 911 lcv++, current_offset += PAGE_SIZE) { 912 913 /* do we care about this page? if not, skip it */ 914 if (pps[lcv] == PGO_DONTCARE) 915 continue; 916 917 /* lookup page */ 918 ptmp = uvm_pagelookup(uobj, current_offset); 919 920 /* to be useful must get a non-busy, non-released pg */ 921 if (ptmp == NULL || 922 (ptmp->pg_flags & PG_BUSY) != 0) { 923 if (lcv == centeridx || (flags & PGO_ALLPAGES) 924 != 0) 925 done = FALSE; /* need to do a wait or I/O! */ 926 continue; 927 } 928 929 /* 930 * useful page: busy it and plug it in our 931 * result array 932 */ 933 atomic_setbits_int(&ptmp->pg_flags, PG_BUSY); 934 UVM_PAGE_OWN(ptmp, "uvn_get1"); 935 pps[lcv] = ptmp; 936 gotpages++; 937 938 } 939 940 /* 941 * XXX: given the "advice", should we consider async read-ahead? 942 * XXX: fault current does deactive of pages behind us. is 943 * this good (other callers might now). 944 */ 945 /* 946 * XXX: read-ahead currently handled by buffer cache (bread) 947 * level. 948 * XXX: no async i/o available. 949 * XXX: so we don't do anything now. 950 */ 951 952 /* 953 * step 1c: now we've either done everything needed or we to 954 * unlock and do some waiting or I/O. 955 */ 956 957 *npagesp = gotpages; /* let caller know */ 958 if (done) 959 return(VM_PAGER_OK); /* bingo! */ 960 else 961 return(VM_PAGER_UNLOCK); 962 } 963 964 /* 965 * step 2: get non-resident or busy pages. 966 * data structures are unlocked. 967 * 968 * XXX: because we can't do async I/O at this level we get things 969 * page at a time (otherwise we'd chunk). the VOP_READ() will do 970 * async-read-ahead for us at a lower level. 971 */ 972 for (lcv = 0, current_offset = offset; 973 lcv < *npagesp ; lcv++, current_offset += PAGE_SIZE) { 974 975 /* skip over pages we've already gotten or don't want */ 976 /* skip over pages we don't _have_ to get */ 977 if (pps[lcv] != NULL || (lcv != centeridx && 978 (flags & PGO_ALLPAGES) == 0)) 979 continue; 980 981 /* 982 * we have yet to locate the current page (pps[lcv]). we first 983 * look for a page that is already at the current offset. if 984 * we fine a page, we check to see if it is busy or released. 985 * if that is the case, then we sleep on the page until it is 986 * no longer busy or released and repeat the lookup. if the 987 * page we found is neither busy nor released, then we busy it 988 * (so we own it) and plug it into pps[lcv]. this breaks the 989 * following while loop and indicates we are ready to move on 990 * to the next page in the "lcv" loop above. 991 * 992 * if we exit the while loop with pps[lcv] still set to NULL, 993 * then it means that we allocated a new busy/fake/clean page 994 * ptmp in the object and we need to do I/O to fill in the data. 995 */ 996 while (pps[lcv] == NULL) { /* top of "pps" while loop */ 997 /* look for a current page */ 998 ptmp = uvm_pagelookup(uobj, current_offset); 999 1000 /* nope? allocate one now (if we can) */ 1001 if (ptmp == NULL) { 1002 ptmp = uvm_pagealloc(uobj, current_offset, 1003 NULL, 0); 1004 1005 /* out of RAM? */ 1006 if (ptmp == NULL) { 1007 uvm_wait("uvn_getpage"); 1008 1009 /* goto top of pps while loop */ 1010 continue; 1011 } 1012 1013 /* 1014 * got new page ready for I/O. break pps 1015 * while loop. pps[lcv] is still NULL. 1016 */ 1017 break; 1018 } 1019 1020 /* page is there, see if we need to wait on it */ 1021 if ((ptmp->pg_flags & PG_BUSY) != 0) { 1022 atomic_setbits_int(&ptmp->pg_flags, PG_WANTED); 1023 UVM_WAIT(ptmp, FALSE, "uvn_get", 0); 1024 continue; /* goto top of pps while loop */ 1025 } 1026 1027 /* 1028 * if we get here then the page has become resident 1029 * and unbusy between steps 1 and 2. we busy it 1030 * now (so we own it) and set pps[lcv] (so that we 1031 * exit the while loop). 1032 */ 1033 atomic_setbits_int(&ptmp->pg_flags, PG_BUSY); 1034 UVM_PAGE_OWN(ptmp, "uvn_get2"); 1035 pps[lcv] = ptmp; 1036 } 1037 1038 /* 1039 * if we own the a valid page at the correct offset, pps[lcv] 1040 * will point to it. nothing more to do except go to the 1041 * next page. 1042 */ 1043 if (pps[lcv]) 1044 continue; /* next lcv */ 1045 1046 /* 1047 * we have a "fake/busy/clean" page that we just allocated. do 1048 * I/O to fill it with valid data. 1049 */ 1050 result = uvn_io((struct uvm_vnode *) uobj, &ptmp, 1, 1051 PGO_SYNCIO, UIO_READ); 1052 1053 /* 1054 * I/O done. because we used syncio the result can not be 1055 * PEND or AGAIN. 1056 */ 1057 if (result != VM_PAGER_OK) { 1058 if (ptmp->pg_flags & PG_WANTED) 1059 wakeup(ptmp); 1060 1061 atomic_clearbits_int(&ptmp->pg_flags, 1062 PG_WANTED|PG_BUSY); 1063 UVM_PAGE_OWN(ptmp, NULL); 1064 uvm_lock_pageq(); 1065 uvm_pagefree(ptmp); 1066 uvm_unlock_pageq(); 1067 return(result); 1068 } 1069 1070 /* 1071 * we got the page! clear the fake flag (indicates valid 1072 * data now in page) and plug into our result array. note 1073 * that page is still busy. 1074 * 1075 * it is the callers job to: 1076 * => check if the page is released 1077 * => unbusy the page 1078 * => activate the page 1079 */ 1080 1081 /* data is valid ... */ 1082 atomic_clearbits_int(&ptmp->pg_flags, PG_FAKE); 1083 pmap_clear_modify(ptmp); /* ... and clean */ 1084 pps[lcv] = ptmp; 1085 1086 } 1087 1088 return (VM_PAGER_OK); 1089 } 1090 1091 /* 1092 * uvn_io: do I/O to a vnode 1093 * 1094 * => prefer map unlocked (not required) 1095 * => flags: PGO_SYNCIO -- use sync. I/O 1096 * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync. 1097 * [thus we never do async i/o! see iodone comment] 1098 */ 1099 1100 int 1101 uvn_io(struct uvm_vnode *uvn, vm_page_t *pps, int npages, int flags, int rw) 1102 { 1103 struct vnode *vn; 1104 struct uio uio; 1105 struct iovec iov; 1106 vaddr_t kva; 1107 off_t file_offset; 1108 int waitf, result, mapinflags; 1109 size_t got, wanted; 1110 1111 /* init values */ 1112 waitf = (flags & PGO_SYNCIO) ? M_WAITOK : M_NOWAIT; 1113 vn = (struct vnode *) uvn; 1114 file_offset = pps[0]->offset; 1115 1116 /* check for sync'ing I/O. */ 1117 while (uvn->u_flags & UVM_VNODE_IOSYNC) { 1118 if (waitf == M_NOWAIT) { 1119 return(VM_PAGER_AGAIN); 1120 } 1121 uvn->u_flags |= UVM_VNODE_IOSYNCWANTED; 1122 UVM_WAIT(&uvn->u_flags, FALSE, "uvn_iosync", 0); 1123 } 1124 1125 /* check size */ 1126 if (file_offset >= uvn->u_size) { 1127 return(VM_PAGER_BAD); 1128 } 1129 1130 /* first try and map the pages in (without waiting) */ 1131 mapinflags = (rw == UIO_READ) ? 1132 UVMPAGER_MAPIN_READ : UVMPAGER_MAPIN_WRITE; 1133 1134 kva = uvm_pagermapin(pps, npages, mapinflags); 1135 if (kva == 0 && waitf == M_NOWAIT) { 1136 return(VM_PAGER_AGAIN); 1137 } 1138 1139 /* 1140 * ok, now bump u_nio up. at this point we are done with uvn 1141 * and can unlock it. if we still don't have a kva, try again 1142 * (this time with sleep ok). 1143 */ 1144 uvn->u_nio++; /* we have an I/O in progress! */ 1145 if (kva == 0) 1146 kva = uvm_pagermapin(pps, npages, 1147 mapinflags | UVMPAGER_MAPIN_WAITOK); 1148 1149 /* 1150 * ok, mapped in. our pages are PG_BUSY so they are not going to 1151 * get touched (so we can look at "offset" without having to lock 1152 * the object). set up for I/O. 1153 */ 1154 /* fill out uio/iov */ 1155 iov.iov_base = (caddr_t) kva; 1156 wanted = (size_t)npages << PAGE_SHIFT; 1157 if (file_offset + wanted > uvn->u_size) 1158 wanted = uvn->u_size - file_offset; /* XXX: needed? */ 1159 iov.iov_len = wanted; 1160 uio.uio_iov = &iov; 1161 uio.uio_iovcnt = 1; 1162 uio.uio_offset = file_offset; 1163 uio.uio_segflg = UIO_SYSSPACE; 1164 uio.uio_rw = rw; 1165 uio.uio_resid = wanted; 1166 uio.uio_procp = curproc; 1167 1168 /* do the I/O! (XXX: curproc?) */ 1169 /* 1170 * This process may already have this vnode locked, if we faulted in 1171 * copyin() or copyout() on a region backed by this vnode 1172 * while doing I/O to the vnode. If this is the case, don't 1173 * panic.. instead, return the error to the user. 1174 * 1175 * XXX this is a stopgap to prevent a panic. 1176 * Ideally, this kind of operation *should* work. 1177 */ 1178 result = 0; 1179 if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0) 1180 result = vn_lock(vn, LK_EXCLUSIVE | LK_RECURSEFAIL, curproc); 1181 1182 if (result == 0) { 1183 /* NOTE: vnode now locked! */ 1184 if (rw == UIO_READ) 1185 result = VOP_READ(vn, &uio, 0, curproc->p_ucred); 1186 else 1187 result = VOP_WRITE(vn, &uio, 1188 (flags & PGO_PDFREECLUST) ? IO_NOCACHE : 0, 1189 curproc->p_ucred); 1190 1191 if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0) 1192 VOP_UNLOCK(vn, 0, curproc); 1193 } 1194 1195 /* NOTE: vnode now unlocked (unless vnislocked) */ 1196 /* 1197 * result == unix style errno (0 == OK!) 1198 * 1199 * zero out rest of buffer (if needed) 1200 */ 1201 if (result == 0) { 1202 got = wanted - uio.uio_resid; 1203 1204 if (wanted && got == 0) { 1205 result = EIO; /* XXX: error? */ 1206 } else if (got < PAGE_SIZE * npages && rw == UIO_READ) { 1207 memset((void *) (kva + got), 0, 1208 ((size_t)npages << PAGE_SHIFT) - got); 1209 } 1210 } 1211 1212 /* now remove pager mapping */ 1213 uvm_pagermapout(kva, npages); 1214 1215 /* now clean up the object (i.e. drop I/O count) */ 1216 uvn->u_nio--; /* I/O DONE! */ 1217 if ((uvn->u_flags & UVM_VNODE_IOSYNC) != 0 && uvn->u_nio == 0) { 1218 wakeup(&uvn->u_nio); 1219 } 1220 1221 if (result == 0) 1222 return(VM_PAGER_OK); 1223 else 1224 return(VM_PAGER_ERROR); 1225 } 1226 1227 /* 1228 * uvm_vnp_uncache: disable "persisting" in a vnode... when last reference 1229 * is gone we will kill the object (flushing dirty pages back to the vnode 1230 * if needed). 1231 * 1232 * => returns TRUE if there was no uvm_object attached or if there was 1233 * one and we killed it [i.e. if there is no active uvn] 1234 * => called with the vnode VOP_LOCK'd [we will unlock it for I/O, if 1235 * needed] 1236 * 1237 * => XXX: given that we now kill uvn's when a vnode is recycled (without 1238 * having to hold a reference on the vnode) and given a working 1239 * uvm_vnp_sync(), how does that effect the need for this function? 1240 * [XXXCDC: seems like it can die?] 1241 * 1242 * => XXX: this function should DIE once we merge the VM and buffer 1243 * cache. 1244 * 1245 * research shows that this is called in the following places: 1246 * ext2fs_truncate, ffs_truncate, detrunc[msdosfs]: called when vnode 1247 * changes sizes 1248 * ext2fs_write, WRITE [ufs_readwrite], msdosfs_write: called when we 1249 * are written to 1250 * ex2fs_chmod, ufs_chmod: called if VTEXT vnode and the sticky bit 1251 * is off 1252 * ffs_realloccg: when we can't extend the current block and have 1253 * to allocate a new one we call this [XXX: why?] 1254 * nfsrv_rename, rename_files: called when the target filename is there 1255 * and we want to remove it 1256 * nfsrv_remove, sys_unlink: called on file we are removing 1257 * nfsrv_access: if VTEXT and we want WRITE access and we don't uncache 1258 * then return "text busy" 1259 * nfs_open: seems to uncache any file opened with nfs 1260 * vn_writechk: if VTEXT vnode and can't uncache return "text busy" 1261 */ 1262 1263 boolean_t 1264 uvm_vnp_uncache(struct vnode *vp) 1265 { 1266 struct uvm_vnode *uvn = &vp->v_uvm; 1267 1268 /* lock uvn part of the vnode and check if we need to do anything */ 1269 1270 if ((uvn->u_flags & UVM_VNODE_VALID) == 0 || 1271 (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) { 1272 return(TRUE); 1273 } 1274 1275 /* 1276 * we have a valid, non-blocked uvn. clear persist flag. 1277 * if uvn is currently active we can return now. 1278 */ 1279 uvn->u_flags &= ~UVM_VNODE_CANPERSIST; 1280 if (uvn->u_obj.uo_refs) { 1281 return(FALSE); 1282 } 1283 1284 /* 1285 * uvn is currently persisting! we have to gain a reference to 1286 * it so that we can call uvn_detach to kill the uvn. 1287 */ 1288 vref(vp); /* seems ok, even with VOP_LOCK */ 1289 uvn->u_obj.uo_refs++; /* value is now 1 */ 1290 1291 #ifdef VFSLCKDEBUG 1292 /* 1293 * carry over sanity check from old vnode pager: the vnode should 1294 * be VOP_LOCK'd, and we confirm it here. 1295 */ 1296 if ((vp->v_flag & VLOCKSWORK) && !VOP_ISLOCKED(vp)) 1297 panic("uvm_vnp_uncache: vnode not locked!"); 1298 #endif 1299 1300 /* 1301 * now drop our reference to the vnode. if we have the sole 1302 * reference to the vnode then this will cause it to die [as we 1303 * just cleared the persist flag]. we have to unlock the vnode 1304 * while we are doing this as it may trigger I/O. 1305 * 1306 * XXX: it might be possible for uvn to get reclaimed while we are 1307 * unlocked causing us to return TRUE when we should not. we ignore 1308 * this as a false-positive return value doesn't hurt us. 1309 */ 1310 VOP_UNLOCK(vp, 0, curproc); 1311 uvn_detach(&uvn->u_obj); 1312 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc); 1313 1314 return(TRUE); 1315 } 1316 1317 /* 1318 * uvm_vnp_setsize: grow or shrink a vnode uvn 1319 * 1320 * grow => just update size value 1321 * shrink => toss un-needed pages 1322 * 1323 * => we assume that the caller has a reference of some sort to the 1324 * vnode in question so that it will not be yanked out from under 1325 * us. 1326 * 1327 * called from: 1328 * => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos]) 1329 * => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write) 1330 * => ffs_balloc [XXX: why? doesn't WRITE handle?] 1331 * => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr 1332 * => union fs: union_newsize 1333 */ 1334 1335 void 1336 uvm_vnp_setsize(struct vnode *vp, voff_t newsize) 1337 { 1338 struct uvm_vnode *uvn = &vp->v_uvm; 1339 1340 /* lock uvn and check for valid object, and if valid: do it! */ 1341 if (uvn->u_flags & UVM_VNODE_VALID) { 1342 1343 /* 1344 * now check if the size has changed: if we shrink we had better 1345 * toss some pages... 1346 */ 1347 1348 if (uvn->u_size > newsize) { 1349 (void)uvn_flush(&uvn->u_obj, newsize, 1350 uvn->u_size, PGO_FREE); 1351 } 1352 uvn->u_size = newsize; 1353 } 1354 } 1355 1356 /* 1357 * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes. 1358 * 1359 * => called from sys_sync with no VM structures locked 1360 * => only one process can do a sync at a time (because the uvn 1361 * structure only has one queue for sync'ing). we ensure this 1362 * by holding the uvn_sync_lock while the sync is in progress. 1363 * other processes attempting a sync will sleep on this lock 1364 * until we are done. 1365 */ 1366 void 1367 uvm_vnp_sync(struct mount *mp) 1368 { 1369 struct uvm_vnode *uvn; 1370 struct vnode *vp; 1371 1372 /* 1373 * step 1: ensure we are only ones using the uvn_sync_q by locking 1374 * our lock... 1375 */ 1376 rw_enter_write(&uvn_sync_lock); 1377 1378 /* 1379 * step 2: build up a simpleq of uvns of interest based on the 1380 * write list. we gain a reference to uvns of interest. 1381 */ 1382 SIMPLEQ_INIT(&uvn_sync_q); 1383 LIST_FOREACH(uvn, &uvn_wlist, u_wlist) { 1384 vp = (struct vnode *)uvn; 1385 if (mp && vp->v_mount != mp) 1386 continue; 1387 1388 /* 1389 * If the vnode is "blocked" it means it must be dying, which 1390 * in turn means its in the process of being flushed out so 1391 * we can safely skip it. 1392 * 1393 * note that uvn must already be valid because we found it on 1394 * the wlist (this also means it can't be ALOCK'd). 1395 */ 1396 if ((uvn->u_flags & UVM_VNODE_BLOCKED) != 0) 1397 continue; 1398 1399 /* 1400 * gain reference. watch out for persisting uvns (need to 1401 * regain vnode REF). 1402 */ 1403 if (uvn->u_obj.uo_refs == 0) 1404 vref(vp); 1405 uvn->u_obj.uo_refs++; 1406 1407 SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq); 1408 } 1409 1410 /* step 3: we now have a list of uvn's that may need cleaning. */ 1411 SIMPLEQ_FOREACH(uvn, &uvn_sync_q, u_syncq) { 1412 #ifdef DEBUG 1413 if (uvn->u_flags & UVM_VNODE_DYING) { 1414 printf("uvm_vnp_sync: dying vnode on sync list\n"); 1415 } 1416 #endif 1417 uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST); 1418 1419 /* 1420 * if we have the only reference and we just cleaned the uvn, 1421 * then we can pull it out of the UVM_VNODE_WRITEABLE state 1422 * thus allowing us to avoid thinking about flushing it again 1423 * on later sync ops. 1424 */ 1425 if (uvn->u_obj.uo_refs == 1 && 1426 (uvn->u_flags & UVM_VNODE_WRITEABLE)) { 1427 LIST_REMOVE(uvn, u_wlist); 1428 uvn->u_flags &= ~UVM_VNODE_WRITEABLE; 1429 } 1430 1431 /* now drop our reference to the uvn */ 1432 uvn_detach(&uvn->u_obj); 1433 } 1434 1435 rw_exit_write(&uvn_sync_lock); 1436 } 1437