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