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