1 /* $NetBSD: lfs_pages.c,v 1.8 2016/07/21 18:10:47 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Konrad E. Schroder <perseant@hhhh.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /* 32 * Copyright (c) 1986, 1989, 1991, 1993, 1995 33 * The Regents of the University of California. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 * 59 * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: lfs_pages.c,v 1.8 2016/07/21 18:10:47 christos Exp $"); 64 65 #ifdef _KERNEL_OPT 66 #include "opt_compat_netbsd.h" 67 #include "opt_uvm_page_trkown.h" 68 #endif 69 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/namei.h> 73 #include <sys/resourcevar.h> 74 #include <sys/kernel.h> 75 #include <sys/file.h> 76 #include <sys/stat.h> 77 #include <sys/buf.h> 78 #include <sys/proc.h> 79 #include <sys/mount.h> 80 #include <sys/vnode.h> 81 #include <sys/pool.h> 82 #include <sys/signalvar.h> 83 #include <sys/kauth.h> 84 #include <sys/syslog.h> 85 #include <sys/fstrans.h> 86 87 #include <miscfs/fifofs/fifo.h> 88 #include <miscfs/genfs/genfs.h> 89 #include <miscfs/specfs/specdev.h> 90 91 #include <ufs/lfs/ulfs_inode.h> 92 #include <ufs/lfs/ulfsmount.h> 93 #include <ufs/lfs/ulfs_bswap.h> 94 #include <ufs/lfs/ulfs_extern.h> 95 96 #include <uvm/uvm.h> 97 #include <uvm/uvm_pmap.h> 98 #include <uvm/uvm_stat.h> 99 #include <uvm/uvm_pager.h> 100 101 #include <ufs/lfs/lfs.h> 102 #include <ufs/lfs/lfs_accessors.h> 103 #include <ufs/lfs/lfs_kernel.h> 104 #include <ufs/lfs/lfs_extern.h> 105 106 extern pid_t lfs_writer_daemon; 107 108 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int, struct vm_page **); 109 110 int 111 lfs_getpages(void *v) 112 { 113 struct vop_getpages_args /* { 114 struct vnode *a_vp; 115 voff_t a_offset; 116 struct vm_page **a_m; 117 int *a_count; 118 int a_centeridx; 119 vm_prot_t a_access_type; 120 int a_advice; 121 int a_flags; 122 } */ *ap = v; 123 124 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM && 125 (ap->a_access_type & VM_PROT_WRITE) != 0) { 126 return EPERM; 127 } 128 if ((ap->a_access_type & VM_PROT_WRITE) != 0) { 129 mutex_enter(&lfs_lock); 130 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED); 131 mutex_exit(&lfs_lock); 132 } 133 134 /* 135 * we're relying on the fact that genfs_getpages() always read in 136 * entire filesystem blocks. 137 */ 138 return genfs_getpages(v); 139 } 140 141 /* 142 * Wait for a page to become unbusy, possibly printing diagnostic messages 143 * as well. 144 * 145 * Called with vp->v_interlock held; return with it held. 146 */ 147 static void 148 wait_for_page(struct vnode *vp, struct vm_page *pg, const char *label) 149 { 150 KASSERT(mutex_owned(vp->v_interlock)); 151 if ((pg->flags & PG_BUSY) == 0) 152 return; /* Nothing to wait for! */ 153 154 #if defined(DEBUG) && defined(UVM_PAGE_TRKOWN) 155 static struct vm_page *lastpg; 156 157 if (label != NULL && pg != lastpg) { 158 if (pg->owner_tag) { 159 printf("lfs_putpages[%d.%d]: %s: page %p owner %d.%d [%s]\n", 160 curproc->p_pid, curlwp->l_lid, label, 161 pg, pg->owner, pg->lowner, pg->owner_tag); 162 } else { 163 printf("lfs_putpages[%d.%d]: %s: page %p unowned?!\n", 164 curproc->p_pid, curlwp->l_lid, label, pg); 165 } 166 } 167 lastpg = pg; 168 #endif 169 170 pg->flags |= PG_WANTED; 171 UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0, "lfsput", 0); 172 mutex_enter(vp->v_interlock); 173 } 174 175 /* 176 * This routine is called by lfs_putpages() when it can't complete the 177 * write because a page is busy. This means that either (1) someone, 178 * possibly the pagedaemon, is looking at this page, and will give it up 179 * presently; or (2) we ourselves are holding the page busy in the 180 * process of being written (either gathered or actually on its way to 181 * disk). We don't need to give up the segment lock, but we might need 182 * to call lfs_writeseg() to expedite the page's journey to disk. 183 * 184 * Called with vp->v_interlock held; return with it held. 185 */ 186 /* #define BUSYWAIT */ 187 static void 188 write_and_wait(struct lfs *fs, struct vnode *vp, struct vm_page *pg, 189 int seglocked, const char *label) 190 { 191 KASSERT(mutex_owned(vp->v_interlock)); 192 #ifndef BUSYWAIT 193 struct inode *ip = VTOI(vp); 194 struct segment *sp = fs->lfs_sp; 195 int count = 0; 196 197 if (pg == NULL) 198 return; 199 200 while (pg->flags & PG_BUSY && 201 pg->uobject == &vp->v_uobj) { 202 mutex_exit(vp->v_interlock); 203 if (sp->cbpp - sp->bpp > 1) { 204 /* Write gathered pages */ 205 lfs_updatemeta(sp); 206 lfs_release_finfo(fs); 207 (void) lfs_writeseg(fs, sp); 208 209 /* 210 * Reinitialize FIP 211 */ 212 KASSERT(sp->vp == vp); 213 lfs_acquire_finfo(fs, ip->i_number, 214 ip->i_gen); 215 } 216 ++count; 217 mutex_enter(vp->v_interlock); 218 wait_for_page(vp, pg, label); 219 } 220 if (label != NULL && count > 1) { 221 DLOG((DLOG_PAGE, "lfs_putpages[%d]: %s: %sn = %d\n", 222 curproc->p_pid, label, (count > 0 ? "looping, " : ""), 223 count)); 224 } 225 #else 226 preempt(1); 227 #endif 228 KASSERT(mutex_owned(vp->v_interlock)); 229 } 230 231 /* 232 * Make sure that for all pages in every block in the given range, 233 * either all are dirty or all are clean. If any of the pages 234 * we've seen so far are dirty, put the vnode on the paging chain, 235 * and mark it IN_PAGING. 236 * 237 * If checkfirst != 0, don't check all the pages but return at the 238 * first dirty page. 239 */ 240 static int 241 check_dirty(struct lfs *fs, struct vnode *vp, 242 off_t startoffset, off_t endoffset, off_t blkeof, 243 int flags, int checkfirst, struct vm_page **pgp) 244 { 245 int by_list; 246 struct vm_page *curpg = NULL; /* XXX: gcc */ 247 struct vm_page *pgs[MAXBSIZE / 248 (__builtin_constant_p(PAGE_SIZE) ? PAGE_SIZE : 1024)], *pg; 249 off_t soff = 0; /* XXX: gcc */ 250 voff_t off; 251 int i; 252 int nonexistent; 253 int any_dirty; /* number of dirty pages */ 254 int dirty; /* number of dirty pages in a block */ 255 int tdirty; 256 int pages_per_block = lfs_sb_getbsize(fs) >> PAGE_SHIFT; 257 int pagedaemon = (curlwp == uvm.pagedaemon_lwp); 258 259 KASSERT(mutex_owned(vp->v_interlock)); 260 ASSERT_MAYBE_SEGLOCK(fs); 261 top: 262 by_list = (vp->v_uobj.uo_npages <= 263 ((endoffset - startoffset) >> PAGE_SHIFT) * 264 UVM_PAGE_TREE_PENALTY); 265 any_dirty = 0; 266 267 if (by_list) { 268 curpg = TAILQ_FIRST(&vp->v_uobj.memq); 269 } else { 270 soff = startoffset; 271 } 272 while (by_list || soff < MIN(blkeof, endoffset)) { 273 if (by_list) { 274 /* 275 * Find the first page in a block. Skip 276 * blocks outside our area of interest or beyond 277 * the end of file. 278 */ 279 KASSERT(curpg == NULL 280 || (curpg->flags & PG_MARKER) == 0); 281 if (pages_per_block > 1) { 282 while (curpg && 283 ((curpg->offset & lfs_sb_getbmask(fs)) || 284 curpg->offset >= vp->v_size || 285 curpg->offset >= endoffset)) { 286 curpg = TAILQ_NEXT(curpg, listq.queue); 287 KASSERT(curpg == NULL || 288 (curpg->flags & PG_MARKER) == 0); 289 } 290 } 291 if (curpg == NULL) 292 break; 293 soff = curpg->offset; 294 } 295 296 /* 297 * Mark all pages in extended range busy; find out if any 298 * of them are dirty. 299 */ 300 nonexistent = dirty = 0; 301 for (i = 0; i == 0 || i < pages_per_block; i++) { 302 KASSERT(mutex_owned(vp->v_interlock)); 303 if (by_list && pages_per_block <= 1) { 304 pgs[i] = pg = curpg; 305 } else { 306 off = soff + (i << PAGE_SHIFT); 307 pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off); 308 if (pg == NULL) { 309 ++nonexistent; 310 continue; 311 } 312 } 313 KASSERT(pg != NULL); 314 315 /* 316 * If we're holding the segment lock, we can deadlock 317 * against a process that has our page and is waiting 318 * for the cleaner, while the cleaner waits for the 319 * segment lock. Just bail in that case. 320 */ 321 if ((pg->flags & PG_BUSY) && 322 (pagedaemon || LFS_SEGLOCK_HELD(fs))) { 323 if (i > 0) 324 uvm_page_unbusy(pgs, i); 325 DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n")); 326 if (pgp) 327 *pgp = pg; 328 KASSERT(mutex_owned(vp->v_interlock)); 329 return -1; 330 } 331 332 while (pg->flags & PG_BUSY) { 333 wait_for_page(vp, pg, NULL); 334 KASSERT(mutex_owned(vp->v_interlock)); 335 if (i > 0) 336 uvm_page_unbusy(pgs, i); 337 KASSERT(mutex_owned(vp->v_interlock)); 338 goto top; 339 } 340 pg->flags |= PG_BUSY; 341 UVM_PAGE_OWN(pg, "lfs_putpages"); 342 343 pmap_page_protect(pg, VM_PROT_NONE); 344 tdirty = (pmap_clear_modify(pg) || 345 (pg->flags & PG_CLEAN) == 0); 346 dirty += tdirty; 347 } 348 if (pages_per_block > 0 && nonexistent >= pages_per_block) { 349 if (by_list) { 350 curpg = TAILQ_NEXT(curpg, listq.queue); 351 } else { 352 soff += lfs_sb_getbsize(fs); 353 } 354 continue; 355 } 356 357 any_dirty += dirty; 358 KASSERT(nonexistent == 0); 359 KASSERT(mutex_owned(vp->v_interlock)); 360 361 /* 362 * If any are dirty make all dirty; unbusy them, 363 * but if we were asked to clean, wire them so that 364 * the pagedaemon doesn't bother us about them while 365 * they're on their way to disk. 366 */ 367 for (i = 0; i == 0 || i < pages_per_block; i++) { 368 KASSERT(mutex_owned(vp->v_interlock)); 369 pg = pgs[i]; 370 KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI))); 371 KASSERT(pg->flags & PG_BUSY); 372 if (dirty) { 373 pg->flags &= ~PG_CLEAN; 374 if (flags & PGO_FREE) { 375 /* 376 * Wire the page so that 377 * pdaemon doesn't see it again. 378 */ 379 mutex_enter(&uvm_pageqlock); 380 uvm_pagewire(pg); 381 mutex_exit(&uvm_pageqlock); 382 383 /* Suspended write flag */ 384 pg->flags |= PG_DELWRI; 385 } 386 } 387 if (pg->flags & PG_WANTED) 388 wakeup(pg); 389 pg->flags &= ~(PG_WANTED|PG_BUSY); 390 UVM_PAGE_OWN(pg, NULL); 391 } 392 393 if (checkfirst && any_dirty) 394 break; 395 396 if (by_list) { 397 curpg = TAILQ_NEXT(curpg, listq.queue); 398 } else { 399 soff += MAX(PAGE_SIZE, lfs_sb_getbsize(fs)); 400 } 401 } 402 403 KASSERT(mutex_owned(vp->v_interlock)); 404 return any_dirty; 405 } 406 407 /* 408 * lfs_putpages functions like genfs_putpages except that 409 * 410 * (1) It needs to bounds-check the incoming requests to ensure that 411 * they are block-aligned; if they are not, expand the range and 412 * do the right thing in case, e.g., the requested range is clean 413 * but the expanded range is dirty. 414 * 415 * (2) It needs to explicitly send blocks to be written when it is done. 416 * If VOP_PUTPAGES is called without the seglock held, we simply take 417 * the seglock and let lfs_segunlock wait for us. 418 * XXX There might be a bad situation if we have to flush a vnode while 419 * XXX lfs_markv is in operation. As of this writing we panic in this 420 * XXX case. 421 * 422 * Assumptions: 423 * 424 * (1) The caller does not hold any pages in this vnode busy. If it does, 425 * there is a danger that when we expand the page range and busy the 426 * pages we will deadlock. 427 * 428 * (2) We are called with vp->v_interlock held; we must return with it 429 * released. 430 * 431 * (3) We don't absolutely have to free pages right away, provided that 432 * the request does not have PGO_SYNCIO. When the pagedaemon gives 433 * us a request with PGO_FREE, we take the pages out of the paging 434 * queue and wake up the writer, which will handle freeing them for us. 435 * 436 * We ensure that for any filesystem block, all pages for that 437 * block are either resident or not, even if those pages are higher 438 * than EOF; that means that we will be getting requests to free 439 * "unused" pages above EOF all the time, and should ignore them. 440 * 441 * (4) If we are called with PGO_LOCKED, the finfo array we are to write 442 * into has been set up for us by lfs_writefile. If not, we will 443 * have to handle allocating and/or freeing an finfo entry. 444 * 445 * XXX note that we're (ab)using PGO_LOCKED as "seglock held". 446 */ 447 448 /* How many times to loop before we should start to worry */ 449 #define TOOMANY 4 450 451 int 452 lfs_putpages(void *v) 453 { 454 int error; 455 struct vop_putpages_args /* { 456 struct vnode *a_vp; 457 voff_t a_offlo; 458 voff_t a_offhi; 459 int a_flags; 460 } */ *ap = v; 461 struct vnode *vp; 462 struct inode *ip; 463 struct lfs *fs; 464 struct segment *sp; 465 off_t origoffset, startoffset, endoffset, origendoffset, blkeof; 466 off_t off, max_endoffset; 467 bool seglocked, sync, pagedaemon, reclaim; 468 struct vm_page *pg, *busypg; 469 UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist); 470 int oreclaim = 0; 471 int donewriting = 0; 472 #ifdef DEBUG 473 int debug_n_again, debug_n_dirtyclean; 474 #endif 475 476 vp = ap->a_vp; 477 ip = VTOI(vp); 478 fs = ip->i_lfs; 479 sync = (ap->a_flags & PGO_SYNCIO) != 0; 480 reclaim = (ap->a_flags & PGO_RECLAIM) != 0; 481 pagedaemon = (curlwp == uvm.pagedaemon_lwp); 482 483 KASSERT(mutex_owned(vp->v_interlock)); 484 485 /* Putpages does nothing for metadata. */ 486 if (vp == fs->lfs_ivnode || vp->v_type != VREG) { 487 mutex_exit(vp->v_interlock); 488 return 0; 489 } 490 491 /* 492 * If there are no pages, don't do anything. 493 */ 494 if (vp->v_uobj.uo_npages == 0) { 495 if (TAILQ_EMPTY(&vp->v_uobj.memq) && 496 (vp->v_iflag & VI_ONWORKLST) && 497 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) { 498 vp->v_iflag &= ~VI_WRMAPDIRTY; 499 vn_syncer_remove_from_worklist(vp); 500 } 501 mutex_exit(vp->v_interlock); 502 503 /* Remove us from paging queue, if we were on it */ 504 mutex_enter(&lfs_lock); 505 if (ip->i_flags & IN_PAGING) { 506 ip->i_flags &= ~IN_PAGING; 507 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain); 508 } 509 mutex_exit(&lfs_lock); 510 511 KASSERT(!mutex_owned(vp->v_interlock)); 512 return 0; 513 } 514 515 blkeof = lfs_blkroundup(fs, ip->i_size); 516 517 /* 518 * Ignore requests to free pages past EOF but in the same block 519 * as EOF, unless the vnode is being reclaimed or the request 520 * is synchronous. (If the request is sync, it comes from 521 * lfs_truncate.) 522 * 523 * To avoid being flooded with this request, make these pages 524 * look "active". 525 */ 526 if (!sync && !reclaim && 527 ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) { 528 origoffset = ap->a_offlo; 529 for (off = origoffset; off < blkeof; off += lfs_sb_getbsize(fs)) { 530 pg = uvm_pagelookup(&vp->v_uobj, off); 531 KASSERT(pg != NULL); 532 while (pg->flags & PG_BUSY) { 533 pg->flags |= PG_WANTED; 534 UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0, 535 "lfsput2", 0); 536 mutex_enter(vp->v_interlock); 537 } 538 mutex_enter(&uvm_pageqlock); 539 uvm_pageactivate(pg); 540 mutex_exit(&uvm_pageqlock); 541 } 542 ap->a_offlo = blkeof; 543 if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) { 544 mutex_exit(vp->v_interlock); 545 return 0; 546 } 547 } 548 549 /* 550 * Extend page range to start and end at block boundaries. 551 * (For the purposes of VOP_PUTPAGES, fragments don't exist.) 552 */ 553 origoffset = ap->a_offlo; 554 origendoffset = ap->a_offhi; 555 startoffset = origoffset & ~(lfs_sb_getbmask(fs)); 556 max_endoffset = (trunc_page(LLONG_MAX) >> lfs_sb_getbshift(fs)) 557 << lfs_sb_getbshift(fs); 558 559 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) { 560 endoffset = max_endoffset; 561 origendoffset = endoffset; 562 } else { 563 origendoffset = round_page(ap->a_offhi); 564 endoffset = round_page(lfs_blkroundup(fs, origendoffset)); 565 } 566 567 KASSERT(startoffset > 0 || endoffset >= startoffset); 568 if (startoffset == endoffset) { 569 /* Nothing to do, why were we called? */ 570 mutex_exit(vp->v_interlock); 571 DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %" 572 PRId64 "\n", startoffset)); 573 return 0; 574 } 575 576 ap->a_offlo = startoffset; 577 ap->a_offhi = endoffset; 578 579 /* 580 * If not cleaning, just send the pages through genfs_putpages 581 * to be returned to the pool. 582 */ 583 if (!(ap->a_flags & PGO_CLEANIT)) { 584 DLOG((DLOG_PAGE, "lfs_putpages: no cleanit vn %p ino %d (flags %x)\n", 585 vp, (int)ip->i_number, ap->a_flags)); 586 int r = genfs_putpages(v); 587 KASSERT(!mutex_owned(vp->v_interlock)); 588 return r; 589 } 590 591 /* Set PGO_BUSYFAIL to avoid deadlocks */ 592 ap->a_flags |= PGO_BUSYFAIL; 593 594 /* 595 * Likewise, if we are asked to clean but the pages are not 596 * dirty, we can just free them using genfs_putpages. 597 */ 598 #ifdef DEBUG 599 debug_n_dirtyclean = 0; 600 #endif 601 do { 602 int r; 603 KASSERT(mutex_owned(vp->v_interlock)); 604 605 /* Count the number of dirty pages */ 606 r = check_dirty(fs, vp, startoffset, endoffset, blkeof, 607 ap->a_flags, 1, NULL); 608 if (r < 0) { 609 /* Pages are busy with another process */ 610 mutex_exit(vp->v_interlock); 611 return EDEADLK; 612 } 613 if (r > 0) /* Some pages are dirty */ 614 break; 615 616 /* 617 * Sometimes pages are dirtied between the time that 618 * we check and the time we try to clean them. 619 * Instruct lfs_gop_write to return EDEADLK in this case 620 * so we can write them properly. 621 */ 622 ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE; 623 r = genfs_do_putpages(vp, startoffset, endoffset, 624 ap->a_flags & ~PGO_SYNCIO, &busypg); 625 ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE; 626 if (r != EDEADLK) { 627 KASSERT(!mutex_owned(vp->v_interlock)); 628 return r; 629 } 630 631 /* One of the pages was busy. Start over. */ 632 mutex_enter(vp->v_interlock); 633 wait_for_page(vp, busypg, "dirtyclean"); 634 #ifdef DEBUG 635 ++debug_n_dirtyclean; 636 #endif 637 } while(1); 638 639 #ifdef DEBUG 640 if (debug_n_dirtyclean > TOOMANY) 641 DLOG((DLOG_PAGE, "lfs_putpages: dirtyclean: looping, n = %d\n", 642 debug_n_dirtyclean)); 643 #endif 644 645 /* 646 * Dirty and asked to clean. 647 * 648 * Pagedaemon can't actually write LFS pages; wake up 649 * the writer to take care of that. The writer will 650 * notice the pager inode queue and act on that. 651 * 652 * XXX We must drop the vp->interlock before taking the lfs_lock or we 653 * get a nasty deadlock with lfs_flush_pchain(). 654 */ 655 if (pagedaemon) { 656 mutex_exit(vp->v_interlock); 657 mutex_enter(&lfs_lock); 658 if (!(ip->i_flags & IN_PAGING)) { 659 ip->i_flags |= IN_PAGING; 660 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain); 661 } 662 wakeup(&lfs_writer_daemon); 663 mutex_exit(&lfs_lock); 664 preempt(); 665 KASSERT(!mutex_owned(vp->v_interlock)); 666 return EWOULDBLOCK; 667 } 668 669 /* 670 * If this is a file created in a recent dirop, we can't flush its 671 * inode until the dirop is complete. Drain dirops, then flush the 672 * filesystem (taking care of any other pending dirops while we're 673 * at it). 674 */ 675 if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT && 676 (vp->v_uflag & VU_DIROP)) { 677 DLOG((DLOG_PAGE, "lfs_putpages: flushing VU_DIROP\n")); 678 679 lfs_writer_enter(fs, "ppdirop"); 680 681 /* Note if we hold the vnode locked */ 682 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 683 { 684 DLOG((DLOG_PAGE, "lfs_putpages: dirop inode already locked\n")); 685 } else { 686 DLOG((DLOG_PAGE, "lfs_putpages: dirop inode not locked\n")); 687 } 688 mutex_exit(vp->v_interlock); 689 690 mutex_enter(&lfs_lock); 691 lfs_flush_fs(fs, sync ? SEGM_SYNC : 0); 692 mutex_exit(&lfs_lock); 693 694 mutex_enter(vp->v_interlock); 695 lfs_writer_leave(fs); 696 697 /* The flush will have cleaned out this vnode as well, 698 no need to do more to it. */ 699 } 700 701 /* 702 * This is it. We are going to write some pages. From here on 703 * down it's all just mechanics. 704 * 705 * Don't let genfs_putpages wait; lfs_segunlock will wait for us. 706 */ 707 ap->a_flags &= ~PGO_SYNCIO; 708 709 /* 710 * If we've already got the seglock, flush the node and return. 711 * The FIP has already been set up for us by lfs_writefile, 712 * and FIP cleanup and lfs_updatemeta will also be done there, 713 * unless genfs_putpages returns EDEADLK; then we must flush 714 * what we have, and correct FIP and segment header accounting. 715 */ 716 get_seglock: 717 /* 718 * If we are not called with the segment locked, lock it. 719 * Account for a new FIP in the segment header, and set sp->vp. 720 * (This should duplicate the setup at the top of lfs_writefile().) 721 */ 722 seglocked = (ap->a_flags & PGO_LOCKED) != 0; 723 if (!seglocked) { 724 mutex_exit(vp->v_interlock); 725 error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0)); 726 if (error != 0) { 727 KASSERT(!mutex_owned(vp->v_interlock)); 728 return error; 729 } 730 mutex_enter(vp->v_interlock); 731 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen); 732 } 733 sp = fs->lfs_sp; 734 KASSERT(sp->vp == NULL); 735 sp->vp = vp; 736 737 /* Note segments written by reclaim; only for debugging */ 738 if (vdead_check(vp, VDEAD_NOWAIT) != 0) { 739 sp->seg_flags |= SEGM_RECLAIM; 740 fs->lfs_reclino = ip->i_number; 741 } 742 743 /* 744 * Ensure that the partial segment is marked SS_DIROP if this 745 * vnode is a DIROP. 746 */ 747 if (!seglocked && vp->v_uflag & VU_DIROP) { 748 SEGSUM *ssp = sp->segsum; 749 750 lfs_ss_setflags(fs, ssp, 751 lfs_ss_getflags(fs, ssp) | (SS_DIROP|SS_CONT)); 752 } 753 754 /* 755 * Loop over genfs_putpages until all pages are gathered. 756 * genfs_putpages() drops the interlock, so reacquire it if necessary. 757 * Whenever we lose the interlock we have to rerun check_dirty, as 758 * well, since more pages might have been dirtied in our absence. 759 */ 760 #ifdef DEBUG 761 debug_n_again = 0; 762 #endif 763 do { 764 busypg = NULL; 765 KASSERT(mutex_owned(vp->v_interlock)); 766 if (check_dirty(fs, vp, startoffset, endoffset, blkeof, 767 ap->a_flags, 0, &busypg) < 0) { 768 mutex_exit(vp->v_interlock); 769 /* XXX why? --ks */ 770 mutex_enter(vp->v_interlock); 771 write_and_wait(fs, vp, busypg, seglocked, NULL); 772 if (!seglocked) { 773 mutex_exit(vp->v_interlock); 774 lfs_release_finfo(fs); 775 lfs_segunlock(fs); 776 mutex_enter(vp->v_interlock); 777 } 778 sp->vp = NULL; 779 goto get_seglock; 780 } 781 782 busypg = NULL; 783 KASSERT(!mutex_owned(&uvm_pageqlock)); 784 oreclaim = (ap->a_flags & PGO_RECLAIM); 785 ap->a_flags &= ~PGO_RECLAIM; 786 error = genfs_do_putpages(vp, startoffset, endoffset, 787 ap->a_flags, &busypg); 788 ap->a_flags |= oreclaim; 789 790 if (error == EDEADLK || error == EAGAIN) { 791 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned" 792 " %d ino %d off %jx (seg %d)\n", error, 793 ip->i_number, (uintmax_t)lfs_sb_getoffset(fs), 794 lfs_dtosn(fs, lfs_sb_getoffset(fs)))); 795 796 if (oreclaim) { 797 mutex_enter(vp->v_interlock); 798 write_and_wait(fs, vp, busypg, seglocked, "again"); 799 mutex_exit(vp->v_interlock); 800 } else { 801 if ((sp->seg_flags & SEGM_SINGLE) && 802 lfs_sb_getcurseg(fs) != fs->lfs_startseg) 803 donewriting = 1; 804 } 805 } else if (error) { 806 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned" 807 " %d ino %d off %jx (seg %d)\n", error, 808 (int)ip->i_number, (uintmax_t)lfs_sb_getoffset(fs), 809 lfs_dtosn(fs, lfs_sb_getoffset(fs)))); 810 } 811 /* genfs_do_putpages loses the interlock */ 812 #ifdef DEBUG 813 ++debug_n_again; 814 #endif 815 if (oreclaim && error == EAGAIN) { 816 DLOG((DLOG_PAGE, "vp %p ino %d vi_flags %x a_flags %x avoiding vclean panic\n", 817 vp, (int)ip->i_number, vp->v_iflag, ap->a_flags)); 818 mutex_enter(vp->v_interlock); 819 } 820 if (error == EDEADLK) 821 mutex_enter(vp->v_interlock); 822 } while (error == EDEADLK || (oreclaim && error == EAGAIN)); 823 #ifdef DEBUG 824 if (debug_n_again > TOOMANY) 825 DLOG((DLOG_PAGE, "lfs_putpages: again: looping, n = %d\n", debug_n_again)); 826 #endif 827 828 KASSERT(sp != NULL && sp->vp == vp); 829 if (!seglocked && !donewriting) { 830 sp->vp = NULL; 831 832 /* Write indirect blocks as well */ 833 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir); 834 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir); 835 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir); 836 837 KASSERT(sp->vp == NULL); 838 sp->vp = vp; 839 } 840 841 /* 842 * Blocks are now gathered into a segment waiting to be written. 843 * All that's left to do is update metadata, and write them. 844 */ 845 lfs_updatemeta(sp); 846 KASSERT(sp->vp == vp); 847 sp->vp = NULL; 848 849 /* 850 * If we were called from lfs_writefile, we don't need to clean up 851 * the FIP or unlock the segment lock. We're done. 852 */ 853 if (seglocked) { 854 KASSERT(!mutex_owned(vp->v_interlock)); 855 return error; 856 } 857 858 /* Clean up FIP and send it to disk. */ 859 lfs_release_finfo(fs); 860 lfs_writeseg(fs, fs->lfs_sp); 861 862 /* 863 * Remove us from paging queue if we wrote all our pages. 864 */ 865 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) { 866 mutex_enter(&lfs_lock); 867 if (ip->i_flags & IN_PAGING) { 868 ip->i_flags &= ~IN_PAGING; 869 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain); 870 } 871 mutex_exit(&lfs_lock); 872 } 873 874 /* 875 * XXX - with the malloc/copy writeseg, the pages are freed by now 876 * even if we don't wait (e.g. if we hold a nested lock). This 877 * will not be true if we stop using malloc/copy. 878 */ 879 KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT); 880 lfs_segunlock(fs); 881 882 /* 883 * Wait for v_numoutput to drop to zero. The seglock should 884 * take care of this, but there is a slight possibility that 885 * aiodoned might not have got around to our buffers yet. 886 */ 887 if (sync) { 888 mutex_enter(vp->v_interlock); 889 while (vp->v_numoutput > 0) { 890 DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on" 891 " num %d\n", ip->i_number, vp->v_numoutput)); 892 cv_wait(&vp->v_cv, vp->v_interlock); 893 } 894 mutex_exit(vp->v_interlock); 895 } 896 KASSERT(!mutex_owned(vp->v_interlock)); 897 return error; 898 } 899 900