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