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