1 /* $NetBSD: lfs_segment.c,v 1.236 2014/03/24 13:42:40 hannken 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) 1991, 1993 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_segment.c 8.10 (Berkeley) 6/10/95 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: lfs_segment.c,v 1.236 2014/03/24 13:42:40 hannken Exp $"); 64 65 #define _VFS_VNODE_PRIVATE /* XXX: check for VI_MARKER, this has to go */ 66 67 #ifdef DEBUG 68 # define vndebug(vp, str) do { \ 69 if (VTOI(vp)->i_flag & IN_CLEANING) \ 70 DLOG((DLOG_WVNODE, "not writing ino %d because %s (op %d)\n", \ 71 VTOI(vp)->i_number, (str), op)); \ 72 } while(0) 73 #else 74 # define vndebug(vp, str) 75 #endif 76 #define ivndebug(vp, str) \ 77 DLOG((DLOG_WVNODE, "ino %d: %s\n", VTOI(vp)->i_number, (str))) 78 79 #if defined(_KERNEL_OPT) 80 #include "opt_ddb.h" 81 #endif 82 83 #include <sys/param.h> 84 #include <sys/systm.h> 85 #include <sys/namei.h> 86 #include <sys/kernel.h> 87 #include <sys/resourcevar.h> 88 #include <sys/file.h> 89 #include <sys/stat.h> 90 #include <sys/buf.h> 91 #include <sys/proc.h> 92 #include <sys/vnode.h> 93 #include <sys/mount.h> 94 #include <sys/kauth.h> 95 #include <sys/syslog.h> 96 97 #include <miscfs/specfs/specdev.h> 98 #include <miscfs/fifofs/fifo.h> 99 100 #include <ufs/lfs/ulfs_inode.h> 101 #include <ufs/lfs/ulfsmount.h> 102 #include <ufs/lfs/ulfs_extern.h> 103 104 #include <ufs/lfs/lfs.h> 105 #include <ufs/lfs/lfs_kernel.h> 106 #include <ufs/lfs/lfs_extern.h> 107 108 #include <uvm/uvm.h> 109 #include <uvm/uvm_extern.h> 110 111 MALLOC_JUSTDEFINE(M_SEGMENT, "LFS segment", "Segment for LFS"); 112 113 static void lfs_generic_callback(struct buf *, void (*)(struct buf *)); 114 static void lfs_free_aiodone(struct buf *); 115 static void lfs_super_aiodone(struct buf *); 116 static void lfs_cluster_aiodone(struct buf *); 117 static void lfs_cluster_callback(struct buf *); 118 119 /* 120 * Determine if it's OK to start a partial in this segment, or if we need 121 * to go on to a new segment. 122 */ 123 #define LFS_PARTIAL_FITS(fs) \ 124 ((fs)->lfs_fsbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \ 125 (fs)->lfs_frag) 126 127 /* 128 * Figure out whether we should do a checkpoint write or go ahead with 129 * an ordinary write. 130 */ 131 #define LFS_SHOULD_CHECKPOINT(fs, flags) \ 132 ((flags & SEGM_CLEAN) == 0 && \ 133 ((fs->lfs_nactive > LFS_MAX_ACTIVE || \ 134 (flags & SEGM_CKP) || \ 135 fs->lfs_nclean < LFS_MAX_ACTIVE))) 136 137 int lfs_match_fake(struct lfs *, struct buf *); 138 void lfs_newseg(struct lfs *); 139 /* XXX ondisk32 */ 140 void lfs_shellsort(struct buf **, int32_t *, int, int); 141 void lfs_supercallback(struct buf *); 142 void lfs_updatemeta(struct segment *); 143 void lfs_writesuper(struct lfs *, daddr_t); 144 int lfs_writevnodes(struct lfs *fs, struct mount *mp, 145 struct segment *sp, int dirops); 146 147 int lfs_allclean_wakeup; /* Cleaner wakeup address. */ 148 int lfs_writeindir = 1; /* whether to flush indir on non-ckp */ 149 int lfs_clean_vnhead = 0; /* Allow freeing to head of vn list */ 150 int lfs_dirvcount = 0; /* # active dirops */ 151 152 /* Statistics Counters */ 153 int lfs_dostats = 1; 154 struct lfs_stats lfs_stats; 155 156 /* op values to lfs_writevnodes */ 157 #define VN_REG 0 158 #define VN_DIROP 1 159 #define VN_EMPTY 2 160 #define VN_CLEAN 3 161 162 /* 163 * XXX KS - Set modification time on the Ifile, so the cleaner can 164 * read the fs mod time off of it. We don't set IN_UPDATE here, 165 * since we don't really need this to be flushed to disk (and in any 166 * case that wouldn't happen to the Ifile until we checkpoint). 167 */ 168 void 169 lfs_imtime(struct lfs *fs) 170 { 171 struct timespec ts; 172 struct inode *ip; 173 174 ASSERT_MAYBE_SEGLOCK(fs); 175 vfs_timestamp(&ts); 176 ip = VTOI(fs->lfs_ivnode); 177 ip->i_ffs1_mtime = ts.tv_sec; 178 ip->i_ffs1_mtimensec = ts.tv_nsec; 179 } 180 181 /* 182 * Ifile and meta data blocks are not marked busy, so segment writes MUST be 183 * single threaded. Currently, there are two paths into lfs_segwrite, sync() 184 * and getnewbuf(). They both mark the file system busy. Lfs_vflush() 185 * explicitly marks the file system busy. So lfs_segwrite is safe. I think. 186 */ 187 188 #define IS_FLUSHING(fs,vp) ((fs)->lfs_flushvp == (vp)) 189 190 int 191 lfs_vflush(struct vnode *vp) 192 { 193 struct inode *ip; 194 struct lfs *fs; 195 struct segment *sp; 196 struct buf *bp, *nbp, *tbp, *tnbp; 197 int error; 198 int flushed; 199 int relock; 200 201 ip = VTOI(vp); 202 fs = VFSTOULFS(vp->v_mount)->um_lfs; 203 relock = 0; 204 205 top: 206 KASSERT(mutex_owned(vp->v_interlock) == false); 207 KASSERT(mutex_owned(&lfs_lock) == false); 208 KASSERT(mutex_owned(&bufcache_lock) == false); 209 ASSERT_NO_SEGLOCK(fs); 210 if (ip->i_flag & IN_CLEANING) { 211 ivndebug(vp,"vflush/in_cleaning"); 212 mutex_enter(&lfs_lock); 213 LFS_CLR_UINO(ip, IN_CLEANING); 214 LFS_SET_UINO(ip, IN_MODIFIED); 215 mutex_exit(&lfs_lock); 216 217 /* 218 * Toss any cleaning buffers that have real counterparts 219 * to avoid losing new data. 220 */ 221 mutex_enter(vp->v_interlock); 222 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 223 nbp = LIST_NEXT(bp, b_vnbufs); 224 if (!LFS_IS_MALLOC_BUF(bp)) 225 continue; 226 /* 227 * Look for pages matching the range covered 228 * by cleaning blocks. It's okay if more dirty 229 * pages appear, so long as none disappear out 230 * from under us. 231 */ 232 if (bp->b_lblkno > 0 && vp->v_type == VREG && 233 vp != fs->lfs_ivnode) { 234 struct vm_page *pg; 235 voff_t off; 236 237 for (off = lfs_lblktosize(fs, bp->b_lblkno); 238 off < lfs_lblktosize(fs, bp->b_lblkno + 1); 239 off += PAGE_SIZE) { 240 pg = uvm_pagelookup(&vp->v_uobj, off); 241 if (pg == NULL) 242 continue; 243 if ((pg->flags & PG_CLEAN) == 0 || 244 pmap_is_modified(pg)) { 245 fs->lfs_avail += lfs_btofsb(fs, 246 bp->b_bcount); 247 wakeup(&fs->lfs_avail); 248 mutex_exit(vp->v_interlock); 249 lfs_freebuf(fs, bp); 250 mutex_enter(vp->v_interlock); 251 bp = NULL; 252 break; 253 } 254 } 255 } 256 for (tbp = LIST_FIRST(&vp->v_dirtyblkhd); tbp; 257 tbp = tnbp) 258 { 259 tnbp = LIST_NEXT(tbp, b_vnbufs); 260 if (tbp->b_vp == bp->b_vp 261 && tbp->b_lblkno == bp->b_lblkno 262 && tbp != bp) 263 { 264 fs->lfs_avail += lfs_btofsb(fs, 265 bp->b_bcount); 266 wakeup(&fs->lfs_avail); 267 mutex_exit(vp->v_interlock); 268 lfs_freebuf(fs, bp); 269 mutex_enter(vp->v_interlock); 270 bp = NULL; 271 break; 272 } 273 } 274 } 275 } else { 276 mutex_enter(vp->v_interlock); 277 } 278 279 /* If the node is being written, wait until that is done */ 280 while (WRITEINPROG(vp)) { 281 ivndebug(vp,"vflush/writeinprog"); 282 cv_wait(&vp->v_cv, vp->v_interlock); 283 } 284 error = vdead_check(vp, VDEAD_NOWAIT); 285 mutex_exit(vp->v_interlock); 286 287 /* Protect against deadlock in vinvalbuf() */ 288 lfs_seglock(fs, SEGM_SYNC | ((error != 0) ? SEGM_RECLAIM : 0)); 289 if (error != 0) { 290 fs->lfs_reclino = ip->i_number; 291 } 292 293 /* If we're supposed to flush a freed inode, just toss it */ 294 if (ip->i_lfs_iflags & LFSI_DELETED) { 295 DLOG((DLOG_VNODE, "lfs_vflush: ino %d freed, not flushing\n", 296 ip->i_number)); 297 /* Drain v_numoutput */ 298 mutex_enter(vp->v_interlock); 299 while (vp->v_numoutput > 0) { 300 cv_wait(&vp->v_cv, vp->v_interlock); 301 } 302 KASSERT(vp->v_numoutput == 0); 303 mutex_exit(vp->v_interlock); 304 305 mutex_enter(&bufcache_lock); 306 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 307 nbp = LIST_NEXT(bp, b_vnbufs); 308 309 KASSERT((bp->b_flags & B_GATHERED) == 0); 310 if (bp->b_oflags & BO_DELWRI) { /* XXX always true? */ 311 fs->lfs_avail += lfs_btofsb(fs, bp->b_bcount); 312 wakeup(&fs->lfs_avail); 313 } 314 /* Copied from lfs_writeseg */ 315 if (bp->b_iodone != NULL) { 316 mutex_exit(&bufcache_lock); 317 biodone(bp); 318 mutex_enter(&bufcache_lock); 319 } else { 320 bremfree(bp); 321 LFS_UNLOCK_BUF(bp); 322 mutex_enter(vp->v_interlock); 323 bp->b_flags &= ~(B_READ | B_GATHERED); 324 bp->b_oflags = (bp->b_oflags & ~BO_DELWRI) | BO_DONE; 325 bp->b_error = 0; 326 reassignbuf(bp, vp); 327 mutex_exit(vp->v_interlock); 328 brelse(bp, 0); 329 } 330 } 331 mutex_exit(&bufcache_lock); 332 LFS_CLR_UINO(ip, IN_CLEANING); 333 LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED); 334 ip->i_flag &= ~IN_ALLMOD; 335 DLOG((DLOG_VNODE, "lfs_vflush: done not flushing ino %d\n", 336 ip->i_number)); 337 lfs_segunlock(fs); 338 339 KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL); 340 341 return 0; 342 } 343 344 fs->lfs_flushvp = vp; 345 if (LFS_SHOULD_CHECKPOINT(fs, fs->lfs_sp->seg_flags)) { 346 error = lfs_segwrite(vp->v_mount, SEGM_CKP | SEGM_SYNC); 347 fs->lfs_flushvp = NULL; 348 KASSERT(fs->lfs_flushvp_fakevref == 0); 349 lfs_segunlock(fs); 350 351 /* Make sure that any pending buffers get written */ 352 mutex_enter(vp->v_interlock); 353 while (vp->v_numoutput > 0) { 354 cv_wait(&vp->v_cv, vp->v_interlock); 355 } 356 KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL); 357 KASSERT(vp->v_numoutput == 0); 358 mutex_exit(vp->v_interlock); 359 360 return error; 361 } 362 sp = fs->lfs_sp; 363 364 flushed = 0; 365 if (VPISEMPTY(vp)) { 366 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY); 367 ++flushed; 368 } else if ((ip->i_flag & IN_CLEANING) && 369 (fs->lfs_sp->seg_flags & SEGM_CLEAN)) { 370 ivndebug(vp,"vflush/clean"); 371 lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN); 372 ++flushed; 373 } else if (lfs_dostats) { 374 if (!VPISEMPTY(vp) || (VTOI(vp)->i_flag & IN_ALLMOD)) 375 ++lfs_stats.vflush_invoked; 376 ivndebug(vp,"vflush"); 377 } 378 379 #ifdef DIAGNOSTIC 380 if (vp->v_uflag & VU_DIROP) { 381 DLOG((DLOG_VNODE, "lfs_vflush: flushing VU_DIROP\n")); 382 /* panic("lfs_vflush: VU_DIROP being flushed...this can\'t happen"); */ 383 } 384 #endif 385 386 do { 387 #ifdef DEBUG 388 int loopcount = 0; 389 #endif 390 do { 391 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL) { 392 relock = lfs_writefile(fs, sp, vp); 393 if (relock && vp != fs->lfs_ivnode) { 394 /* 395 * Might have to wait for the 396 * cleaner to run; but we're 397 * still not done with this vnode. 398 * XXX we can do better than this. 399 */ 400 KDASSERT(ip->i_number != LFS_IFILE_INUM); 401 lfs_writeinode(fs, sp, ip); 402 mutex_enter(&lfs_lock); 403 LFS_SET_UINO(ip, IN_MODIFIED); 404 mutex_exit(&lfs_lock); 405 lfs_writeseg(fs, sp); 406 lfs_segunlock(fs); 407 lfs_segunlock_relock(fs); 408 goto top; 409 } 410 } 411 /* 412 * If we begin a new segment in the middle of writing 413 * the Ifile, it creates an inconsistent checkpoint, 414 * since the Ifile information for the new segment 415 * is not up-to-date. Take care of this here by 416 * sending the Ifile through again in case there 417 * are newly dirtied blocks. But wait, there's more! 418 * This second Ifile write could *also* cross a segment 419 * boundary, if the first one was large. The second 420 * one is guaranteed to be no more than 8 blocks, 421 * though (two segment blocks and supporting indirects) 422 * so the third write *will not* cross the boundary. 423 */ 424 if (vp == fs->lfs_ivnode) { 425 lfs_writefile(fs, sp, vp); 426 lfs_writefile(fs, sp, vp); 427 } 428 #ifdef DEBUG 429 if (++loopcount > 2) 430 log(LOG_NOTICE, "lfs_vflush: looping count=%d\n", loopcount); 431 #endif 432 } while (lfs_writeinode(fs, sp, ip)); 433 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM); 434 435 if (lfs_dostats) { 436 ++lfs_stats.nwrites; 437 if (sp->seg_flags & SEGM_SYNC) 438 ++lfs_stats.nsync_writes; 439 if (sp->seg_flags & SEGM_CKP) 440 ++lfs_stats.ncheckpoints; 441 } 442 /* 443 * If we were called from somewhere that has already held the seglock 444 * (e.g., lfs_markv()), the lfs_segunlock will not wait for 445 * the write to complete because we are still locked. 446 * Since lfs_vflush() must return the vnode with no dirty buffers, 447 * we must explicitly wait, if that is the case. 448 * 449 * We compare the iocount against 1, not 0, because it is 450 * artificially incremented by lfs_seglock(). 451 */ 452 mutex_enter(&lfs_lock); 453 if (fs->lfs_seglock > 1) { 454 while (fs->lfs_iocount > 1) 455 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1, 456 "lfs_vflush", 0, &lfs_lock); 457 } 458 mutex_exit(&lfs_lock); 459 460 lfs_segunlock(fs); 461 462 /* Wait for these buffers to be recovered by aiodoned */ 463 mutex_enter(vp->v_interlock); 464 while (vp->v_numoutput > 0) { 465 cv_wait(&vp->v_cv, vp->v_interlock); 466 } 467 KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL); 468 KASSERT(vp->v_numoutput == 0); 469 mutex_exit(vp->v_interlock); 470 471 fs->lfs_flushvp = NULL; 472 KASSERT(fs->lfs_flushvp_fakevref == 0); 473 474 return (0); 475 } 476 477 int 478 lfs_writevnodes(struct lfs *fs, struct mount *mp, struct segment *sp, int op) 479 { 480 struct inode *ip; 481 struct vnode *vp; 482 int inodes_written = 0, only_cleaning; 483 int error = 0; 484 485 ASSERT_SEGLOCK(fs); 486 loop: 487 /* start at last (newest) vnode. */ 488 mutex_enter(&mntvnode_lock); 489 TAILQ_FOREACH_REVERSE(vp, &mp->mnt_vnodelist, vnodelst, v_mntvnodes) { 490 /* 491 * If the vnode that we are about to sync is no longer 492 * associated with this mount point, start over. 493 */ 494 if (vp->v_mount != mp) { 495 DLOG((DLOG_VNODE, "lfs_writevnodes: starting over\n")); 496 /* 497 * After this, pages might be busy 498 * due to our own previous putpages. 499 * Start actual segment write here to avoid deadlock. 500 * If we were just writing one segment and we've done 501 * that, break out. 502 */ 503 mutex_exit(&mntvnode_lock); 504 if (lfs_writeseg(fs, sp) && 505 (sp->seg_flags & SEGM_SINGLE) && 506 fs->lfs_curseg != fs->lfs_startseg) { 507 DLOG((DLOG_VNODE, "lfs_writevnodes: breaking out of segment write at daddr 0x%x\n", fs->lfs_offset)); 508 break; 509 } 510 goto loop; 511 } 512 513 mutex_enter(vp->v_interlock); 514 if (vp->v_type == VNON || (vp->v_iflag & VI_MARKER) || 515 vdead_check(vp, VDEAD_NOWAIT) != 0) { 516 mutex_exit(vp->v_interlock); 517 continue; 518 } 519 520 ip = VTOI(vp); 521 if ((op == VN_DIROP && !(vp->v_uflag & VU_DIROP)) || 522 (op != VN_DIROP && op != VN_CLEAN && 523 (vp->v_uflag & VU_DIROP))) { 524 mutex_exit(vp->v_interlock); 525 vndebug(vp,"dirop"); 526 continue; 527 } 528 529 if (op == VN_EMPTY && !VPISEMPTY(vp)) { 530 mutex_exit(vp->v_interlock); 531 vndebug(vp,"empty"); 532 continue; 533 } 534 535 if (op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM 536 && vp != fs->lfs_flushvp 537 && !(ip->i_flag & IN_CLEANING)) { 538 mutex_exit(vp->v_interlock); 539 vndebug(vp,"cleaning"); 540 continue; 541 } 542 543 mutex_exit(&mntvnode_lock); 544 if (lfs_vref(vp)) { 545 vndebug(vp,"vref"); 546 mutex_enter(&mntvnode_lock); 547 continue; 548 } 549 550 only_cleaning = 0; 551 /* 552 * Write the inode/file if dirty and it's not the IFILE. 553 */ 554 if ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp)) { 555 only_cleaning = 556 ((ip->i_flag & IN_ALLMOD) == IN_CLEANING); 557 558 if (ip->i_number != LFS_IFILE_INUM) { 559 error = lfs_writefile(fs, sp, vp); 560 if (error) { 561 lfs_vunref(vp); 562 if (error == EAGAIN) { 563 /* 564 * This error from lfs_putpages 565 * indicates we need to drop 566 * the segment lock and start 567 * over after the cleaner has 568 * had a chance to run. 569 */ 570 lfs_writeinode(fs, sp, ip); 571 lfs_writeseg(fs, sp); 572 if (!VPISEMPTY(vp) && 573 !WRITEINPROG(vp) && 574 !(ip->i_flag & IN_ALLMOD)) { 575 mutex_enter(&lfs_lock); 576 LFS_SET_UINO(ip, IN_MODIFIED); 577 mutex_exit(&lfs_lock); 578 } 579 mutex_enter(&mntvnode_lock); 580 break; 581 } 582 error = 0; /* XXX not quite right */ 583 mutex_enter(&mntvnode_lock); 584 continue; 585 } 586 587 if (!VPISEMPTY(vp)) { 588 if (WRITEINPROG(vp)) { 589 ivndebug(vp,"writevnodes/write2"); 590 } else if (!(ip->i_flag & IN_ALLMOD)) { 591 mutex_enter(&lfs_lock); 592 LFS_SET_UINO(ip, IN_MODIFIED); 593 mutex_exit(&lfs_lock); 594 } 595 } 596 (void) lfs_writeinode(fs, sp, ip); 597 inodes_written++; 598 } 599 } 600 601 if (lfs_clean_vnhead && only_cleaning) 602 lfs_vunref_head(vp); 603 else 604 lfs_vunref(vp); 605 606 mutex_enter(&mntvnode_lock); 607 } 608 mutex_exit(&mntvnode_lock); 609 return error; 610 } 611 612 /* 613 * Do a checkpoint. 614 */ 615 int 616 lfs_segwrite(struct mount *mp, int flags) 617 { 618 struct buf *bp; 619 struct inode *ip; 620 struct lfs *fs; 621 struct segment *sp; 622 struct vnode *vp; 623 SEGUSE *segusep; 624 int do_ckp, did_ckp, error; 625 unsigned n, segleft, maxseg, sn, i, curseg; 626 int writer_set = 0; 627 int dirty; 628 int redo; 629 int um_error; 630 631 fs = VFSTOULFS(mp)->um_lfs; 632 ASSERT_MAYBE_SEGLOCK(fs); 633 634 if (fs->lfs_ronly) 635 return EROFS; 636 637 lfs_imtime(fs); 638 639 /* 640 * Allocate a segment structure and enough space to hold pointers to 641 * the maximum possible number of buffers which can be described in a 642 * single summary block. 643 */ 644 do_ckp = LFS_SHOULD_CHECKPOINT(fs, flags); 645 646 /* We can't do a partial write and checkpoint at the same time. */ 647 if (do_ckp) 648 flags &= ~SEGM_SINGLE; 649 650 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0)); 651 sp = fs->lfs_sp; 652 if (sp->seg_flags & (SEGM_CLEAN | SEGM_CKP)) 653 do_ckp = 1; 654 655 /* 656 * If lfs_flushvp is non-NULL, we are called from lfs_vflush, 657 * in which case we have to flush *all* buffers off of this vnode. 658 * We don't care about other nodes, but write any non-dirop nodes 659 * anyway in anticipation of another getnewvnode(). 660 * 661 * If we're cleaning we only write cleaning and ifile blocks, and 662 * no dirops, since otherwise we'd risk corruption in a crash. 663 */ 664 if (sp->seg_flags & SEGM_CLEAN) 665 lfs_writevnodes(fs, mp, sp, VN_CLEAN); 666 else if (!(sp->seg_flags & SEGM_FORCE_CKP)) { 667 do { 668 um_error = lfs_writevnodes(fs, mp, sp, VN_REG); 669 if ((sp->seg_flags & SEGM_SINGLE) && 670 fs->lfs_curseg != fs->lfs_startseg) { 671 DLOG((DLOG_SEG, "lfs_segwrite: breaking out of segment write at daddr 0x%x\n", fs->lfs_offset)); 672 break; 673 } 674 675 if (do_ckp || fs->lfs_dirops == 0) { 676 if (!writer_set) { 677 lfs_writer_enter(fs, "lfs writer"); 678 writer_set = 1; 679 } 680 error = lfs_writevnodes(fs, mp, sp, VN_DIROP); 681 if (um_error == 0) 682 um_error = error; 683 /* In case writevnodes errored out */ 684 lfs_flush_dirops(fs); 685 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT); 686 lfs_finalize_fs_seguse(fs); 687 } 688 if (do_ckp && um_error) { 689 lfs_segunlock_relock(fs); 690 sp = fs->lfs_sp; 691 } 692 } while (do_ckp && um_error != 0); 693 } 694 695 /* 696 * If we are doing a checkpoint, mark everything since the 697 * last checkpoint as no longer ACTIVE. 698 */ 699 if (do_ckp || fs->lfs_doifile) { 700 segleft = fs->lfs_nseg; 701 curseg = 0; 702 for (n = 0; n < fs->lfs_segtabsz; n++) { 703 dirty = 0; 704 if (bread(fs->lfs_ivnode, fs->lfs_cleansz + n, 705 fs->lfs_bsize, NOCRED, B_MODIFY, &bp)) 706 panic("lfs_segwrite: ifile read"); 707 segusep = (SEGUSE *)bp->b_data; 708 maxseg = min(segleft, fs->lfs_sepb); 709 for (i = 0; i < maxseg; i++) { 710 sn = curseg + i; 711 if (sn != lfs_dtosn(fs, fs->lfs_curseg) && 712 segusep->su_flags & SEGUSE_ACTIVE) { 713 segusep->su_flags &= ~SEGUSE_ACTIVE; 714 --fs->lfs_nactive; 715 ++dirty; 716 } 717 fs->lfs_suflags[fs->lfs_activesb][sn] = 718 segusep->su_flags; 719 if (fs->lfs_version > 1) 720 ++segusep; 721 else 722 segusep = (SEGUSE *) 723 ((SEGUSE_V1 *)segusep + 1); 724 } 725 726 if (dirty) 727 error = LFS_BWRITE_LOG(bp); /* Ifile */ 728 else 729 brelse(bp, 0); 730 segleft -= fs->lfs_sepb; 731 curseg += fs->lfs_sepb; 732 } 733 } 734 735 KASSERT(LFS_SEGLOCK_HELD(fs)); 736 737 did_ckp = 0; 738 if (do_ckp || fs->lfs_doifile) { 739 vp = fs->lfs_ivnode; 740 #ifdef DEBUG 741 int loopcount = 0; 742 #endif 743 do { 744 #ifdef DEBUG 745 LFS_ENTER_LOG("pretend", __FILE__, __LINE__, 0, 0, curproc->p_pid); 746 #endif 747 mutex_enter(&lfs_lock); 748 fs->lfs_flags &= ~LFS_IFDIRTY; 749 mutex_exit(&lfs_lock); 750 751 ip = VTOI(vp); 752 753 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL) { 754 /* 755 * Ifile has no pages, so we don't need 756 * to check error return here. 757 */ 758 lfs_writefile(fs, sp, vp); 759 /* 760 * Ensure the Ifile takes the current segment 761 * into account. See comment in lfs_vflush. 762 */ 763 lfs_writefile(fs, sp, vp); 764 lfs_writefile(fs, sp, vp); 765 } 766 767 if (ip->i_flag & IN_ALLMOD) 768 ++did_ckp; 769 #if 0 770 redo = (do_ckp ? lfs_writeinode(fs, sp, ip) : 0); 771 #else 772 redo = lfs_writeinode(fs, sp, ip); 773 #endif 774 redo += lfs_writeseg(fs, sp); 775 mutex_enter(&lfs_lock); 776 redo += (fs->lfs_flags & LFS_IFDIRTY); 777 mutex_exit(&lfs_lock); 778 #ifdef DEBUG 779 if (++loopcount > 2) 780 log(LOG_NOTICE, "lfs_segwrite: looping count=%d\n", 781 loopcount); 782 #endif 783 } while (redo && do_ckp); 784 785 /* 786 * Unless we are unmounting, the Ifile may continue to have 787 * dirty blocks even after a checkpoint, due to changes to 788 * inodes' atime. If we're checkpointing, it's "impossible" 789 * for other parts of the Ifile to be dirty after the loop 790 * above, since we hold the segment lock. 791 */ 792 mutex_enter(vp->v_interlock); 793 if (LIST_EMPTY(&vp->v_dirtyblkhd)) { 794 LFS_CLR_UINO(ip, IN_ALLMOD); 795 } 796 #ifdef DIAGNOSTIC 797 else if (do_ckp) { 798 int do_panic = 0; 799 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) { 800 if (bp->b_lblkno < fs->lfs_cleansz + 801 fs->lfs_segtabsz && 802 !(bp->b_flags & B_GATHERED)) { 803 printf("ifile lbn %ld still dirty (flags %lx)\n", 804 (long)bp->b_lblkno, 805 (long)bp->b_flags); 806 ++do_panic; 807 } 808 } 809 if (do_panic) 810 panic("dirty blocks"); 811 } 812 #endif 813 mutex_exit(vp->v_interlock); 814 } else { 815 (void) lfs_writeseg(fs, sp); 816 } 817 818 /* Note Ifile no longer needs to be written */ 819 fs->lfs_doifile = 0; 820 if (writer_set) 821 lfs_writer_leave(fs); 822 823 /* 824 * If we didn't write the Ifile, we didn't really do anything. 825 * That means that (1) there is a checkpoint on disk and (2) 826 * nothing has changed since it was written. 827 * 828 * Take the flags off of the segment so that lfs_segunlock 829 * doesn't have to write the superblock either. 830 */ 831 if (do_ckp && !did_ckp) { 832 sp->seg_flags &= ~SEGM_CKP; 833 } 834 835 if (lfs_dostats) { 836 ++lfs_stats.nwrites; 837 if (sp->seg_flags & SEGM_SYNC) 838 ++lfs_stats.nsync_writes; 839 if (sp->seg_flags & SEGM_CKP) 840 ++lfs_stats.ncheckpoints; 841 } 842 lfs_segunlock(fs); 843 return (0); 844 } 845 846 /* 847 * Write the dirty blocks associated with a vnode. 848 */ 849 int 850 lfs_writefile(struct lfs *fs, struct segment *sp, struct vnode *vp) 851 { 852 struct inode *ip; 853 int i, frag; 854 int error; 855 856 ASSERT_SEGLOCK(fs); 857 error = 0; 858 ip = VTOI(vp); 859 860 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen); 861 862 if (vp->v_uflag & VU_DIROP) 863 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT); 864 865 if (sp->seg_flags & SEGM_CLEAN) { 866 lfs_gather(fs, sp, vp, lfs_match_fake); 867 /* 868 * For a file being flushed, we need to write *all* blocks. 869 * This means writing the cleaning blocks first, and then 870 * immediately following with any non-cleaning blocks. 871 * The same is true of the Ifile since checkpoints assume 872 * that all valid Ifile blocks are written. 873 */ 874 if (IS_FLUSHING(fs, vp) || vp == fs->lfs_ivnode) { 875 lfs_gather(fs, sp, vp, lfs_match_data); 876 /* 877 * Don't call VOP_PUTPAGES: if we're flushing, 878 * we've already done it, and the Ifile doesn't 879 * use the page cache. 880 */ 881 } 882 } else { 883 lfs_gather(fs, sp, vp, lfs_match_data); 884 /* 885 * If we're flushing, we've already called VOP_PUTPAGES 886 * so don't do it again. Otherwise, we want to write 887 * everything we've got. 888 */ 889 if (!IS_FLUSHING(fs, vp)) { 890 mutex_enter(vp->v_interlock); 891 error = VOP_PUTPAGES(vp, 0, 0, 892 PGO_CLEANIT | PGO_ALLPAGES | PGO_LOCKED); 893 } 894 } 895 896 /* 897 * It may not be necessary to write the meta-data blocks at this point, 898 * as the roll-forward recovery code should be able to reconstruct the 899 * list. 900 * 901 * We have to write them anyway, though, under two conditions: (1) the 902 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are 903 * checkpointing. 904 * 905 * BUT if we are cleaning, we might have indirect blocks that refer to 906 * new blocks not being written yet, in addition to fragments being 907 * moved out of a cleaned segment. If that is the case, don't 908 * write the indirect blocks, or the finfo will have a small block 909 * in the middle of it! 910 * XXX in this case isn't the inode size wrong too? 911 */ 912 frag = 0; 913 if (sp->seg_flags & SEGM_CLEAN) { 914 for (i = 0; i < ULFS_NDADDR; i++) 915 if (ip->i_lfs_fragsize[i] > 0 && 916 ip->i_lfs_fragsize[i] < fs->lfs_bsize) 917 ++frag; 918 } 919 #ifdef DIAGNOSTIC 920 if (frag > 1) 921 panic("lfs_writefile: more than one fragment!"); 922 #endif 923 if (IS_FLUSHING(fs, vp) || 924 (frag == 0 && (lfs_writeindir || (sp->seg_flags & SEGM_CKP)))) { 925 lfs_gather(fs, sp, vp, lfs_match_indir); 926 lfs_gather(fs, sp, vp, lfs_match_dindir); 927 lfs_gather(fs, sp, vp, lfs_match_tindir); 928 } 929 lfs_release_finfo(fs); 930 931 return error; 932 } 933 934 /* 935 * Update segment accounting to reflect this inode's change of address. 936 */ 937 static int 938 lfs_update_iaddr(struct lfs *fs, struct segment *sp, struct inode *ip, daddr_t ndaddr) 939 { 940 struct buf *bp; 941 daddr_t daddr; 942 IFILE *ifp; 943 SEGUSE *sup; 944 ino_t ino; 945 int redo_ifile; 946 u_int32_t sn; 947 948 redo_ifile = 0; 949 950 /* 951 * If updating the ifile, update the super-block. Update the disk 952 * address and access times for this inode in the ifile. 953 */ 954 ino = ip->i_number; 955 if (ino == LFS_IFILE_INUM) { 956 daddr = fs->lfs_idaddr; 957 fs->lfs_idaddr = LFS_DBTOFSB(fs, ndaddr); 958 } else { 959 LFS_IENTRY(ifp, fs, ino, bp); 960 daddr = ifp->if_daddr; 961 ifp->if_daddr = LFS_DBTOFSB(fs, ndaddr); 962 (void)LFS_BWRITE_LOG(bp); /* Ifile */ 963 } 964 965 /* 966 * If this is the Ifile and lfs_offset is set to the first block 967 * in the segment, dirty the new segment's accounting block 968 * (XXX should already be dirty?) and tell the caller to do it again. 969 */ 970 if (ip->i_number == LFS_IFILE_INUM) { 971 sn = lfs_dtosn(fs, fs->lfs_offset); 972 if (lfs_sntod(fs, sn) + lfs_btofsb(fs, fs->lfs_sumsize) == 973 fs->lfs_offset) { 974 LFS_SEGENTRY(sup, fs, sn, bp); 975 KASSERT(bp->b_oflags & BO_DELWRI); 976 LFS_WRITESEGENTRY(sup, fs, sn, bp); 977 /* fs->lfs_flags |= LFS_IFDIRTY; */ 978 redo_ifile |= 1; 979 } 980 } 981 982 /* 983 * The inode's last address should not be in the current partial 984 * segment, except under exceptional circumstances (lfs_writevnodes 985 * had to start over, and in the meantime more blocks were written 986 * to a vnode). Both inodes will be accounted to this segment 987 * in lfs_writeseg so we need to subtract the earlier version 988 * here anyway. The segment count can temporarily dip below 989 * zero here; keep track of how many duplicates we have in 990 * "dupino" so we don't panic below. 991 */ 992 if (daddr >= fs->lfs_lastpseg && daddr <= fs->lfs_offset) { 993 ++sp->ndupino; 994 DLOG((DLOG_SEG, "lfs_writeinode: last inode addr in current pseg " 995 "(ino %d daddr 0x%llx) ndupino=%d\n", ino, 996 (long long)daddr, sp->ndupino)); 997 } 998 /* 999 * Account the inode: it no longer belongs to its former segment, 1000 * though it will not belong to the new segment until that segment 1001 * is actually written. 1002 */ 1003 if (daddr != LFS_UNUSED_DADDR) { 1004 u_int32_t oldsn = lfs_dtosn(fs, daddr); 1005 #ifdef DIAGNOSTIC 1006 int ndupino = (sp->seg_number == oldsn) ? sp->ndupino : 0; 1007 #endif 1008 LFS_SEGENTRY(sup, fs, oldsn, bp); 1009 #ifdef DIAGNOSTIC 1010 if (sup->su_nbytes + 1011 sizeof (struct ulfs1_dinode) * ndupino 1012 < sizeof (struct ulfs1_dinode)) { 1013 printf("lfs_writeinode: negative bytes " 1014 "(segment %" PRIu32 " short by %d, " 1015 "oldsn=%" PRIu32 ", cursn=%" PRIu32 1016 ", daddr=%" PRId64 ", su_nbytes=%u, " 1017 "ndupino=%d)\n", 1018 lfs_dtosn(fs, daddr), 1019 (int)sizeof (struct ulfs1_dinode) * 1020 (1 - sp->ndupino) - sup->su_nbytes, 1021 oldsn, sp->seg_number, daddr, 1022 (unsigned int)sup->su_nbytes, 1023 sp->ndupino); 1024 panic("lfs_writeinode: negative bytes"); 1025 sup->su_nbytes = sizeof (struct ulfs1_dinode); 1026 } 1027 #endif 1028 DLOG((DLOG_SU, "seg %d -= %d for ino %d inode\n", 1029 lfs_dtosn(fs, daddr), sizeof (struct ulfs1_dinode), ino)); 1030 sup->su_nbytes -= sizeof (struct ulfs1_dinode); 1031 redo_ifile |= 1032 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED)); 1033 if (redo_ifile) { 1034 mutex_enter(&lfs_lock); 1035 fs->lfs_flags |= LFS_IFDIRTY; 1036 mutex_exit(&lfs_lock); 1037 /* Don't double-account */ 1038 fs->lfs_idaddr = 0x0; 1039 } 1040 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */ 1041 } 1042 1043 return redo_ifile; 1044 } 1045 1046 int 1047 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip) 1048 { 1049 struct buf *bp; 1050 struct ulfs1_dinode *cdp; 1051 struct vnode *vp = ITOV(ip); 1052 daddr_t daddr; 1053 int32_t *daddrp; /* XXX ondisk32 */ 1054 int i, ndx; 1055 int redo_ifile = 0; 1056 int gotblk = 0; 1057 int count; 1058 1059 ASSERT_SEGLOCK(fs); 1060 if (!(ip->i_flag & IN_ALLMOD) && !(vp->v_uflag & VU_DIROP)) 1061 return (0); 1062 1063 /* Can't write ifile when writer is not set */ 1064 KASSERT(ip->i_number != LFS_IFILE_INUM || fs->lfs_writer > 0 || 1065 (sp->seg_flags & SEGM_CLEAN)); 1066 1067 /* 1068 * If this is the Ifile, see if writing it here will generate a 1069 * temporary misaccounting. If it will, do the accounting and write 1070 * the blocks, postponing the inode write until the accounting is 1071 * solid. 1072 */ 1073 count = 0; 1074 while (vp == fs->lfs_ivnode) { 1075 int redo = 0; 1076 1077 if (sp->idp == NULL && sp->ibp == NULL && 1078 (sp->seg_bytes_left < fs->lfs_ibsize || 1079 sp->sum_bytes_left < sizeof(int32_t))) { 1080 (void) lfs_writeseg(fs, sp); 1081 continue; 1082 } 1083 1084 /* Look for dirty Ifile blocks */ 1085 LIST_FOREACH(bp, &fs->lfs_ivnode->v_dirtyblkhd, b_vnbufs) { 1086 if (!(bp->b_flags & B_GATHERED)) { 1087 redo = 1; 1088 break; 1089 } 1090 } 1091 1092 if (redo == 0) 1093 redo = lfs_update_iaddr(fs, sp, ip, 0x0); 1094 if (redo == 0) 1095 break; 1096 1097 if (sp->idp) { 1098 sp->idp->di_inumber = 0; 1099 sp->idp = NULL; 1100 } 1101 ++count; 1102 if (count > 2) 1103 log(LOG_NOTICE, "lfs_writeinode: looping count=%d\n", count); 1104 lfs_writefile(fs, sp, fs->lfs_ivnode); 1105 } 1106 1107 /* Allocate a new inode block if necessary. */ 1108 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) && 1109 sp->ibp == NULL) { 1110 /* Allocate a new segment if necessary. */ 1111 if (sp->seg_bytes_left < fs->lfs_ibsize || 1112 sp->sum_bytes_left < sizeof(int32_t)) 1113 (void) lfs_writeseg(fs, sp); 1114 1115 /* Get next inode block. */ 1116 daddr = fs->lfs_offset; 1117 fs->lfs_offset += lfs_btofsb(fs, fs->lfs_ibsize); 1118 sp->ibp = *sp->cbpp++ = 1119 getblk(VTOI(fs->lfs_ivnode)->i_devvp, 1120 LFS_FSBTODB(fs, daddr), fs->lfs_ibsize, 0, 0); 1121 gotblk++; 1122 1123 /* Zero out inode numbers */ 1124 for (i = 0; i < LFS_INOPB(fs); ++i) 1125 ((struct ulfs1_dinode *)sp->ibp->b_data)[i].di_inumber = 1126 0; 1127 1128 ++sp->start_bpp; 1129 fs->lfs_avail -= lfs_btofsb(fs, fs->lfs_ibsize); 1130 /* Set remaining space counters. */ 1131 sp->seg_bytes_left -= fs->lfs_ibsize; 1132 sp->sum_bytes_left -= sizeof(int32_t); 1133 ndx = fs->lfs_sumsize / sizeof(int32_t) - 1134 sp->ninodes / LFS_INOPB(fs) - 1; 1135 ((int32_t *)(sp->segsum))[ndx] = daddr; 1136 } 1137 1138 /* Check VU_DIROP in case there is a new file with no data blocks */ 1139 if (vp->v_uflag & VU_DIROP) 1140 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT); 1141 1142 /* Update the inode times and copy the inode onto the inode page. */ 1143 /* XXX kludge --- don't redirty the ifile just to put times on it */ 1144 if (ip->i_number != LFS_IFILE_INUM) 1145 LFS_ITIMES(ip, NULL, NULL, NULL); 1146 1147 /* 1148 * If this is the Ifile, and we've already written the Ifile in this 1149 * partial segment, just overwrite it (it's not on disk yet) and 1150 * continue. 1151 * 1152 * XXX we know that the bp that we get the second time around has 1153 * already been gathered. 1154 */ 1155 if (ip->i_number == LFS_IFILE_INUM && sp->idp) { 1156 *(sp->idp) = *ip->i_din.ffs1_din; 1157 ip->i_lfs_osize = ip->i_size; 1158 return 0; 1159 } 1160 1161 bp = sp->ibp; 1162 cdp = ((struct ulfs1_dinode *)bp->b_data) + (sp->ninodes % LFS_INOPB(fs)); 1163 *cdp = *ip->i_din.ffs1_din; 1164 1165 /* 1166 * This inode is on its way to disk; clear its VU_DIROP status when 1167 * the write is complete. 1168 */ 1169 if (vp->v_uflag & VU_DIROP) { 1170 if (!(sp->seg_flags & SEGM_CLEAN)) 1171 ip->i_flag |= IN_CDIROP; 1172 else { 1173 DLOG((DLOG_DIROP, "lfs_writeinode: not clearing dirop for cleaned ino %d\n", (int)ip->i_number)); 1174 } 1175 } 1176 1177 /* 1178 * If cleaning, link counts and directory file sizes cannot change, 1179 * since those would be directory operations---even if the file 1180 * we are writing is marked VU_DIROP we should write the old values. 1181 * If we're not cleaning, of course, update the values so we get 1182 * current values the next time we clean. 1183 */ 1184 if (sp->seg_flags & SEGM_CLEAN) { 1185 if (vp->v_uflag & VU_DIROP) { 1186 cdp->di_nlink = ip->i_lfs_odnlink; 1187 /* if (vp->v_type == VDIR) */ 1188 cdp->di_size = ip->i_lfs_osize; 1189 } 1190 } else { 1191 ip->i_lfs_odnlink = cdp->di_nlink; 1192 ip->i_lfs_osize = ip->i_size; 1193 } 1194 1195 1196 /* We can finish the segment accounting for truncations now */ 1197 lfs_finalize_ino_seguse(fs, ip); 1198 1199 /* 1200 * If we are cleaning, ensure that we don't write UNWRITTEN disk 1201 * addresses to disk; possibly change the on-disk record of 1202 * the inode size, either by reverting to the previous size 1203 * (in the case of cleaning) or by verifying the inode's block 1204 * holdings (in the case of files being allocated as they are being 1205 * written). 1206 * XXX By not writing UNWRITTEN blocks, we are making the lfs_avail 1207 * XXX count on disk wrong by the same amount. We should be 1208 * XXX able to "borrow" from lfs_avail and return it after the 1209 * XXX Ifile is written. See also in lfs_writeseg. 1210 */ 1211 1212 /* Check file size based on highest allocated block */ 1213 if (((ip->i_ffs1_mode & LFS_IFMT) == LFS_IFREG || 1214 (ip->i_ffs1_mode & LFS_IFMT) == LFS_IFDIR) && 1215 ip->i_size > ((ip->i_lfs_hiblk + 1) << fs->lfs_bshift)) { 1216 cdp->di_size = (ip->i_lfs_hiblk + 1) << fs->lfs_bshift; 1217 DLOG((DLOG_SEG, "lfs_writeinode: ino %d size %" PRId64 " -> %" 1218 PRId64 "\n", (int)ip->i_number, ip->i_size, cdp->di_size)); 1219 } 1220 if (ip->i_lfs_effnblks != ip->i_ffs1_blocks) { 1221 DLOG((DLOG_SEG, "lfs_writeinode: cleansing ino %d eff %d != nblk %d)" 1222 " at %x\n", ip->i_number, ip->i_lfs_effnblks, 1223 ip->i_ffs1_blocks, fs->lfs_offset)); 1224 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + ULFS_NIADDR; 1225 daddrp++) { 1226 if (*daddrp == UNWRITTEN) { 1227 DLOG((DLOG_SEG, "lfs_writeinode: wiping UNWRITTEN\n")); 1228 *daddrp = 0; 1229 } 1230 } 1231 } 1232 1233 #ifdef DIAGNOSTIC 1234 /* 1235 * Check dinode held blocks against dinode size. 1236 * This should be identical to the check in lfs_vget(). 1237 */ 1238 for (i = (cdp->di_size + fs->lfs_bsize - 1) >> fs->lfs_bshift; 1239 i < ULFS_NDADDR; i++) { 1240 KASSERT(i >= 0); 1241 if ((cdp->di_mode & LFS_IFMT) == LFS_IFLNK) 1242 continue; 1243 if (((cdp->di_mode & LFS_IFMT) == LFS_IFBLK || 1244 (cdp->di_mode & LFS_IFMT) == LFS_IFCHR) && i == 0) 1245 continue; 1246 if (cdp->di_db[i] != 0) { 1247 # ifdef DEBUG 1248 lfs_dump_dinode(cdp); 1249 # endif 1250 panic("writing inconsistent inode"); 1251 } 1252 } 1253 #endif /* DIAGNOSTIC */ 1254 1255 if (ip->i_flag & IN_CLEANING) 1256 LFS_CLR_UINO(ip, IN_CLEANING); 1257 else { 1258 /* XXX IN_ALLMOD */ 1259 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE | 1260 IN_UPDATE | IN_MODIFY); 1261 if (ip->i_lfs_effnblks == ip->i_ffs1_blocks) 1262 LFS_CLR_UINO(ip, IN_MODIFIED); 1263 else { 1264 DLOG((DLOG_VNODE, "lfs_writeinode: ino %d: real " 1265 "blks=%d, eff=%d\n", ip->i_number, 1266 ip->i_ffs1_blocks, ip->i_lfs_effnblks)); 1267 } 1268 } 1269 1270 if (ip->i_number == LFS_IFILE_INUM) { 1271 /* We know sp->idp == NULL */ 1272 sp->idp = ((struct ulfs1_dinode *)bp->b_data) + 1273 (sp->ninodes % LFS_INOPB(fs)); 1274 1275 /* Not dirty any more */ 1276 mutex_enter(&lfs_lock); 1277 fs->lfs_flags &= ~LFS_IFDIRTY; 1278 mutex_exit(&lfs_lock); 1279 } 1280 1281 if (gotblk) { 1282 mutex_enter(&bufcache_lock); 1283 LFS_LOCK_BUF(bp); 1284 brelsel(bp, 0); 1285 mutex_exit(&bufcache_lock); 1286 } 1287 1288 /* Increment inode count in segment summary block. */ 1289 ++((SEGSUM *)(sp->segsum))->ss_ninos; 1290 1291 /* If this page is full, set flag to allocate a new page. */ 1292 if (++sp->ninodes % LFS_INOPB(fs) == 0) 1293 sp->ibp = NULL; 1294 1295 redo_ifile = lfs_update_iaddr(fs, sp, ip, bp->b_blkno); 1296 1297 KASSERT(redo_ifile == 0); 1298 return (redo_ifile); 1299 } 1300 1301 int 1302 lfs_gatherblock(struct segment *sp, struct buf *bp, kmutex_t *mptr) 1303 { 1304 struct lfs *fs; 1305 int vers; 1306 int j, blksinblk; 1307 1308 ASSERT_SEGLOCK(sp->fs); 1309 /* 1310 * If full, finish this segment. We may be doing I/O, so 1311 * release and reacquire the splbio(). 1312 */ 1313 #ifdef DIAGNOSTIC 1314 if (sp->vp == NULL) 1315 panic ("lfs_gatherblock: Null vp in segment"); 1316 #endif 1317 fs = sp->fs; 1318 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize); 1319 if (sp->sum_bytes_left < sizeof(int32_t) * blksinblk || 1320 sp->seg_bytes_left < bp->b_bcount) { 1321 if (mptr) 1322 mutex_exit(mptr); 1323 lfs_updatemeta(sp); 1324 1325 vers = sp->fip->fi_version; 1326 (void) lfs_writeseg(fs, sp); 1327 1328 /* Add the current file to the segment summary. */ 1329 lfs_acquire_finfo(fs, VTOI(sp->vp)->i_number, vers); 1330 1331 if (mptr) 1332 mutex_enter(mptr); 1333 return (1); 1334 } 1335 1336 if (bp->b_flags & B_GATHERED) { 1337 DLOG((DLOG_SEG, "lfs_gatherblock: already gathered! Ino %d," 1338 " lbn %" PRId64 "\n", 1339 sp->fip->fi_ino, bp->b_lblkno)); 1340 return (0); 1341 } 1342 1343 /* Insert into the buffer list, update the FINFO block. */ 1344 bp->b_flags |= B_GATHERED; 1345 1346 *sp->cbpp++ = bp; 1347 for (j = 0; j < blksinblk; j++) { 1348 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j; 1349 /* This block's accounting moves from lfs_favail to lfs_avail */ 1350 lfs_deregister_block(sp->vp, bp->b_lblkno + j); 1351 } 1352 1353 sp->sum_bytes_left -= sizeof(int32_t) * blksinblk; 1354 sp->seg_bytes_left -= bp->b_bcount; 1355 return (0); 1356 } 1357 1358 int 1359 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp, 1360 int (*match)(struct lfs *, struct buf *)) 1361 { 1362 struct buf *bp, *nbp; 1363 int count = 0; 1364 1365 ASSERT_SEGLOCK(fs); 1366 if (vp->v_type == VBLK) 1367 return 0; 1368 KASSERT(sp->vp == NULL); 1369 sp->vp = vp; 1370 mutex_enter(&bufcache_lock); 1371 1372 #ifndef LFS_NO_BACKBUF_HACK 1373 /* This is a hack to see if ordering the blocks in LFS makes a difference. */ 1374 # define BUF_OFFSET \ 1375 (((char *)&LIST_NEXT(bp, b_vnbufs)) - (char *)bp) 1376 # define BACK_BUF(BP) \ 1377 ((struct buf *)(((char *)(BP)->b_vnbufs.le_prev) - BUF_OFFSET)) 1378 # define BEG_OF_LIST \ 1379 ((struct buf *)(((char *)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET)) 1380 1381 loop: 1382 /* Find last buffer. */ 1383 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); 1384 bp && LIST_NEXT(bp, b_vnbufs) != NULL; 1385 bp = LIST_NEXT(bp, b_vnbufs)) 1386 /* nothing */; 1387 for (; bp && bp != BEG_OF_LIST; bp = nbp) { 1388 nbp = BACK_BUF(bp); 1389 #else /* LFS_NO_BACKBUF_HACK */ 1390 loop: 1391 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 1392 nbp = LIST_NEXT(bp, b_vnbufs); 1393 #endif /* LFS_NO_BACKBUF_HACK */ 1394 if ((bp->b_cflags & BC_BUSY) != 0 || 1395 (bp->b_flags & B_GATHERED) != 0 || !match(fs, bp)) { 1396 #ifdef DEBUG 1397 if (vp == fs->lfs_ivnode && 1398 (bp->b_cflags & BC_BUSY) != 0 && 1399 (bp->b_flags & B_GATHERED) == 0) 1400 log(LOG_NOTICE, "lfs_gather: ifile lbn %" 1401 PRId64 " busy (%x) at 0x%x", 1402 bp->b_lblkno, bp->b_flags, 1403 (unsigned)fs->lfs_offset); 1404 #endif 1405 continue; 1406 } 1407 #ifdef DIAGNOSTIC 1408 # ifdef LFS_USE_B_INVAL 1409 if ((bp->b_flags & BC_INVAL) != 0 && bp->b_iodone == NULL) { 1410 DLOG((DLOG_SEG, "lfs_gather: lbn %" PRId64 1411 " is BC_INVAL\n", bp->b_lblkno)); 1412 VOP_PRINT(bp->b_vp); 1413 } 1414 # endif /* LFS_USE_B_INVAL */ 1415 if (!(bp->b_oflags & BO_DELWRI)) 1416 panic("lfs_gather: bp not BO_DELWRI"); 1417 if (!(bp->b_flags & B_LOCKED)) { 1418 DLOG((DLOG_SEG, "lfs_gather: lbn %" PRId64 1419 " blk %" PRId64 " not B_LOCKED\n", 1420 bp->b_lblkno, 1421 LFS_DBTOFSB(fs, bp->b_blkno))); 1422 VOP_PRINT(bp->b_vp); 1423 panic("lfs_gather: bp not B_LOCKED"); 1424 } 1425 #endif 1426 if (lfs_gatherblock(sp, bp, &bufcache_lock)) { 1427 goto loop; 1428 } 1429 count++; 1430 } 1431 mutex_exit(&bufcache_lock); 1432 lfs_updatemeta(sp); 1433 KASSERT(sp->vp == vp); 1434 sp->vp = NULL; 1435 return count; 1436 } 1437 1438 #if DEBUG 1439 # define DEBUG_OOFF(n) do { \ 1440 if (ooff == 0) { \ 1441 DLOG((DLOG_SEG, "lfs_updatemeta[%d]: warning: writing " \ 1442 "ino %d lbn %" PRId64 " at 0x%" PRIx32 \ 1443 ", was 0x0 (or %" PRId64 ")\n", \ 1444 (n), ip->i_number, lbn, ndaddr, daddr)); \ 1445 } \ 1446 } while (0) 1447 #else 1448 # define DEBUG_OOFF(n) 1449 #endif 1450 1451 /* 1452 * Change the given block's address to ndaddr, finding its previous 1453 * location using ulfs_bmaparray(). 1454 * 1455 * Account for this change in the segment table. 1456 * 1457 * called with sp == NULL by roll-forwarding code. 1458 */ 1459 void 1460 lfs_update_single(struct lfs *fs, struct segment *sp, 1461 struct vnode *vp, daddr_t lbn, int32_t ndaddr, int size) 1462 { 1463 SEGUSE *sup; 1464 struct buf *bp; 1465 struct indir a[ULFS_NIADDR + 2], *ap; 1466 struct inode *ip; 1467 daddr_t daddr, ooff; 1468 int num, error; 1469 int bb, osize, obb; 1470 1471 ASSERT_SEGLOCK(fs); 1472 KASSERT(sp == NULL || sp->vp == vp); 1473 ip = VTOI(vp); 1474 1475 error = ulfs_bmaparray(vp, lbn, &daddr, a, &num, NULL, NULL); 1476 if (error) 1477 panic("lfs_updatemeta: ulfs_bmaparray returned %d", error); 1478 1479 daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */ 1480 KASSERT(daddr <= LFS_MAX_DADDR); 1481 if (daddr > 0) 1482 daddr = LFS_DBTOFSB(fs, daddr); 1483 1484 bb = lfs_numfrags(fs, size); 1485 switch (num) { 1486 case 0: 1487 ooff = ip->i_ffs1_db[lbn]; 1488 DEBUG_OOFF(0); 1489 if (ooff == UNWRITTEN) 1490 ip->i_ffs1_blocks += bb; 1491 else { 1492 /* possible fragment truncation or extension */ 1493 obb = lfs_btofsb(fs, ip->i_lfs_fragsize[lbn]); 1494 ip->i_ffs1_blocks += (bb - obb); 1495 } 1496 ip->i_ffs1_db[lbn] = ndaddr; 1497 break; 1498 case 1: 1499 ooff = ip->i_ffs1_ib[a[0].in_off]; 1500 DEBUG_OOFF(1); 1501 if (ooff == UNWRITTEN) 1502 ip->i_ffs1_blocks += bb; 1503 ip->i_ffs1_ib[a[0].in_off] = ndaddr; 1504 break; 1505 default: 1506 ap = &a[num - 1]; 1507 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, 1508 B_MODIFY, &bp)) 1509 panic("lfs_updatemeta: bread bno %" PRId64, 1510 ap->in_lbn); 1511 1512 /* XXX ondisk32 */ 1513 ooff = ((int32_t *)bp->b_data)[ap->in_off]; 1514 DEBUG_OOFF(num); 1515 if (ooff == UNWRITTEN) 1516 ip->i_ffs1_blocks += bb; 1517 /* XXX ondisk32 */ 1518 ((int32_t *)bp->b_data)[ap->in_off] = ndaddr; 1519 (void) VOP_BWRITE(bp->b_vp, bp); 1520 } 1521 1522 KASSERT(ooff == 0 || ooff == UNWRITTEN || ooff == daddr); 1523 1524 /* Update hiblk when extending the file */ 1525 if (lbn > ip->i_lfs_hiblk) 1526 ip->i_lfs_hiblk = lbn; 1527 1528 /* 1529 * Though we'd rather it couldn't, this *can* happen right now 1530 * if cleaning blocks and regular blocks coexist. 1531 */ 1532 /* KASSERT(daddr < fs->lfs_lastpseg || daddr > ndaddr); */ 1533 1534 /* 1535 * Update segment usage information, based on old size 1536 * and location. 1537 */ 1538 if (daddr > 0) { 1539 u_int32_t oldsn = lfs_dtosn(fs, daddr); 1540 #ifdef DIAGNOSTIC 1541 int ndupino; 1542 1543 if (sp && sp->seg_number == oldsn) { 1544 ndupino = sp->ndupino; 1545 } else { 1546 ndupino = 0; 1547 } 1548 #endif 1549 KASSERT(oldsn < fs->lfs_nseg); 1550 if (lbn >= 0 && lbn < ULFS_NDADDR) 1551 osize = ip->i_lfs_fragsize[lbn]; 1552 else 1553 osize = fs->lfs_bsize; 1554 LFS_SEGENTRY(sup, fs, oldsn, bp); 1555 #ifdef DIAGNOSTIC 1556 if (sup->su_nbytes + sizeof (struct ulfs1_dinode) * ndupino 1557 < osize) { 1558 printf("lfs_updatemeta: negative bytes " 1559 "(segment %" PRIu32 " short by %" PRId64 1560 ")\n", lfs_dtosn(fs, daddr), 1561 (int64_t)osize - 1562 (sizeof (struct ulfs1_dinode) * ndupino + 1563 sup->su_nbytes)); 1564 printf("lfs_updatemeta: ino %llu, lbn %" PRId64 1565 ", addr = 0x%" PRIx64 "\n", 1566 (unsigned long long)ip->i_number, lbn, daddr); 1567 printf("lfs_updatemeta: ndupino=%d\n", ndupino); 1568 panic("lfs_updatemeta: negative bytes"); 1569 sup->su_nbytes = osize - 1570 sizeof (struct ulfs1_dinode) * ndupino; 1571 } 1572 #endif 1573 DLOG((DLOG_SU, "seg %" PRIu32 " -= %d for ino %d lbn %" PRId64 1574 " db 0x%" PRIx64 "\n", 1575 lfs_dtosn(fs, daddr), osize, 1576 ip->i_number, lbn, daddr)); 1577 sup->su_nbytes -= osize; 1578 if (!(bp->b_flags & B_GATHERED)) { 1579 mutex_enter(&lfs_lock); 1580 fs->lfs_flags |= LFS_IFDIRTY; 1581 mutex_exit(&lfs_lock); 1582 } 1583 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); 1584 } 1585 /* 1586 * Now that this block has a new address, and its old 1587 * segment no longer owns it, we can forget about its 1588 * old size. 1589 */ 1590 if (lbn >= 0 && lbn < ULFS_NDADDR) 1591 ip->i_lfs_fragsize[lbn] = size; 1592 } 1593 1594 /* 1595 * Update the metadata that points to the blocks listed in the FINFO 1596 * array. 1597 */ 1598 void 1599 lfs_updatemeta(struct segment *sp) 1600 { 1601 struct buf *sbp; 1602 struct lfs *fs; 1603 struct vnode *vp; 1604 daddr_t lbn; 1605 int i, nblocks, num; 1606 int bb; 1607 int bytesleft, size; 1608 1609 ASSERT_SEGLOCK(sp->fs); 1610 vp = sp->vp; 1611 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp; 1612 KASSERT(nblocks >= 0); 1613 KASSERT(vp != NULL); 1614 if (nblocks == 0) 1615 return; 1616 1617 /* 1618 * This count may be high due to oversize blocks from lfs_gop_write. 1619 * Correct for this. (XXX we should be able to keep track of these.) 1620 */ 1621 fs = sp->fs; 1622 for (i = 0; i < nblocks; i++) { 1623 if (sp->start_bpp[i] == NULL) { 1624 DLOG((DLOG_SEG, "lfs_updatemeta: nblocks = %d, not %d\n", i, nblocks)); 1625 nblocks = i; 1626 break; 1627 } 1628 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize); 1629 KASSERT(sp->start_bpp[i]->b_lblkno >= 0 || num == 1); 1630 nblocks -= num - 1; 1631 } 1632 1633 KASSERT(vp->v_type == VREG || 1634 nblocks == &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp); 1635 KASSERT(nblocks == sp->cbpp - sp->start_bpp); 1636 1637 /* 1638 * Sort the blocks. 1639 * 1640 * We have to sort even if the blocks come from the 1641 * cleaner, because there might be other pending blocks on the 1642 * same inode...and if we don't sort, and there are fragments 1643 * present, blocks may be written in the wrong place. 1644 */ 1645 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize); 1646 1647 /* 1648 * Record the length of the last block in case it's a fragment. 1649 * If there are indirect blocks present, they sort last. An 1650 * indirect block will be lfs_bsize and its presence indicates 1651 * that you cannot have fragments. 1652 * 1653 * XXX This last is a lie. A cleaned fragment can coexist with 1654 * XXX a later indirect block. This will continue to be 1655 * XXX true until lfs_markv is fixed to do everything with 1656 * XXX fake blocks (including fake inodes and fake indirect blocks). 1657 */ 1658 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) & 1659 fs->lfs_bmask) + 1; 1660 1661 /* 1662 * Assign disk addresses, and update references to the logical 1663 * block and the segment usage information. 1664 */ 1665 for (i = nblocks; i--; ++sp->start_bpp) { 1666 sbp = *sp->start_bpp; 1667 lbn = *sp->start_lbp; 1668 KASSERT(sbp->b_lblkno == lbn); 1669 1670 sbp->b_blkno = LFS_FSBTODB(fs, fs->lfs_offset); 1671 1672 /* 1673 * If we write a frag in the wrong place, the cleaner won't 1674 * be able to correctly identify its size later, and the 1675 * segment will be uncleanable. (Even worse, it will assume 1676 * that the indirect block that actually ends the list 1677 * is of a smaller size!) 1678 */ 1679 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0) 1680 panic("lfs_updatemeta: fragment is not last block"); 1681 1682 /* 1683 * For each subblock in this possibly oversized block, 1684 * update its address on disk. 1685 */ 1686 KASSERT(lbn >= 0 || sbp->b_bcount == fs->lfs_bsize); 1687 KASSERT(vp == sbp->b_vp); 1688 for (bytesleft = sbp->b_bcount; bytesleft > 0; 1689 bytesleft -= fs->lfs_bsize) { 1690 size = MIN(bytesleft, fs->lfs_bsize); 1691 bb = lfs_numfrags(fs, size); 1692 lbn = *sp->start_lbp++; 1693 lfs_update_single(fs, sp, sp->vp, lbn, fs->lfs_offset, 1694 size); 1695 fs->lfs_offset += bb; 1696 } 1697 1698 } 1699 1700 /* This inode has been modified */ 1701 LFS_SET_UINO(VTOI(vp), IN_MODIFIED); 1702 } 1703 1704 /* 1705 * Move lfs_offset to a segment earlier than sn. 1706 */ 1707 int 1708 lfs_rewind(struct lfs *fs, int newsn) 1709 { 1710 int sn, osn, isdirty; 1711 struct buf *bp; 1712 SEGUSE *sup; 1713 1714 ASSERT_SEGLOCK(fs); 1715 1716 osn = lfs_dtosn(fs, fs->lfs_offset); 1717 if (osn < newsn) 1718 return 0; 1719 1720 /* lfs_avail eats the remaining space in this segment */ 1721 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset - fs->lfs_curseg); 1722 1723 /* Find a low-numbered segment */ 1724 for (sn = 0; sn < fs->lfs_nseg; ++sn) { 1725 LFS_SEGENTRY(sup, fs, sn, bp); 1726 isdirty = sup->su_flags & SEGUSE_DIRTY; 1727 brelse(bp, 0); 1728 1729 if (!isdirty) 1730 break; 1731 } 1732 if (sn == fs->lfs_nseg) 1733 panic("lfs_rewind: no clean segments"); 1734 if (newsn >= 0 && sn >= newsn) 1735 return ENOENT; 1736 fs->lfs_nextseg = sn; 1737 lfs_newseg(fs); 1738 fs->lfs_offset = fs->lfs_curseg; 1739 1740 return 0; 1741 } 1742 1743 /* 1744 * Start a new partial segment. 1745 * 1746 * Return 1 when we entered to a new segment. 1747 * Otherwise, return 0. 1748 */ 1749 int 1750 lfs_initseg(struct lfs *fs) 1751 { 1752 struct segment *sp = fs->lfs_sp; 1753 SEGSUM *ssp; 1754 struct buf *sbp; /* buffer for SEGSUM */ 1755 int repeat = 0; /* return value */ 1756 1757 ASSERT_SEGLOCK(fs); 1758 /* Advance to the next segment. */ 1759 if (!LFS_PARTIAL_FITS(fs)) { 1760 SEGUSE *sup; 1761 struct buf *bp; 1762 1763 /* lfs_avail eats the remaining space */ 1764 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset - 1765 fs->lfs_curseg); 1766 /* Wake up any cleaning procs waiting on this file system. */ 1767 lfs_wakeup_cleaner(fs); 1768 lfs_newseg(fs); 1769 repeat = 1; 1770 fs->lfs_offset = fs->lfs_curseg; 1771 1772 sp->seg_number = lfs_dtosn(fs, fs->lfs_curseg); 1773 sp->seg_bytes_left = lfs_fsbtob(fs, fs->lfs_fsbpseg); 1774 1775 /* 1776 * If the segment contains a superblock, update the offset 1777 * and summary address to skip over it. 1778 */ 1779 LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 1780 if (sup->su_flags & SEGUSE_SUPERBLOCK) { 1781 fs->lfs_offset += lfs_btofsb(fs, LFS_SBPAD); 1782 sp->seg_bytes_left -= LFS_SBPAD; 1783 } 1784 brelse(bp, 0); 1785 /* Segment zero could also contain the labelpad */ 1786 if (fs->lfs_version > 1 && sp->seg_number == 0 && 1787 fs->lfs_start < lfs_btofsb(fs, LFS_LABELPAD)) { 1788 fs->lfs_offset += 1789 lfs_btofsb(fs, LFS_LABELPAD) - fs->lfs_start; 1790 sp->seg_bytes_left -= 1791 LFS_LABELPAD - lfs_fsbtob(fs, fs->lfs_start); 1792 } 1793 } else { 1794 sp->seg_number = lfs_dtosn(fs, fs->lfs_curseg); 1795 sp->seg_bytes_left = lfs_fsbtob(fs, fs->lfs_fsbpseg - 1796 (fs->lfs_offset - fs->lfs_curseg)); 1797 } 1798 fs->lfs_lastpseg = fs->lfs_offset; 1799 1800 /* Record first address of this partial segment */ 1801 if (sp->seg_flags & SEGM_CLEAN) { 1802 fs->lfs_cleanint[fs->lfs_cleanind] = fs->lfs_offset; 1803 if (++fs->lfs_cleanind >= LFS_MAX_CLEANIND) { 1804 /* "1" is the artificial inc in lfs_seglock */ 1805 mutex_enter(&lfs_lock); 1806 while (fs->lfs_iocount > 1) { 1807 mtsleep(&fs->lfs_iocount, PRIBIO + 1, 1808 "lfs_initseg", 0, &lfs_lock); 1809 } 1810 mutex_exit(&lfs_lock); 1811 fs->lfs_cleanind = 0; 1812 } 1813 } 1814 1815 sp->fs = fs; 1816 sp->ibp = NULL; 1817 sp->idp = NULL; 1818 sp->ninodes = 0; 1819 sp->ndupino = 0; 1820 1821 sp->cbpp = sp->bpp; 1822 1823 /* Get a new buffer for SEGSUM */ 1824 sbp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp, 1825 LFS_FSBTODB(fs, fs->lfs_offset), fs->lfs_sumsize, LFS_NB_SUMMARY); 1826 1827 /* ... and enter it into the buffer list. */ 1828 *sp->cbpp = sbp; 1829 sp->cbpp++; 1830 fs->lfs_offset += lfs_btofsb(fs, fs->lfs_sumsize); 1831 1832 sp->start_bpp = sp->cbpp; 1833 1834 /* Set point to SEGSUM, initialize it. */ 1835 ssp = sp->segsum = sbp->b_data; 1836 memset(ssp, 0, fs->lfs_sumsize); 1837 ssp->ss_next = fs->lfs_nextseg; 1838 ssp->ss_nfinfo = ssp->ss_ninos = 0; 1839 ssp->ss_magic = SS_MAGIC; 1840 1841 /* Set pointer to first FINFO, initialize it. */ 1842 sp->fip = (struct finfo *)((char *)sp->segsum + SEGSUM_SIZE(fs)); 1843 sp->fip->fi_nblocks = 0; 1844 sp->start_lbp = &sp->fip->fi_blocks[0]; 1845 sp->fip->fi_lastlength = 0; 1846 1847 sp->seg_bytes_left -= fs->lfs_sumsize; 1848 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs); 1849 1850 return (repeat); 1851 } 1852 1853 /* 1854 * Remove SEGUSE_INVAL from all segments. 1855 */ 1856 void 1857 lfs_unset_inval_all(struct lfs *fs) 1858 { 1859 SEGUSE *sup; 1860 struct buf *bp; 1861 int i; 1862 1863 for (i = 0; i < fs->lfs_nseg; i++) { 1864 LFS_SEGENTRY(sup, fs, i, bp); 1865 if (sup->su_flags & SEGUSE_INVAL) { 1866 sup->su_flags &= ~SEGUSE_INVAL; 1867 LFS_WRITESEGENTRY(sup, fs, i, bp); 1868 } else 1869 brelse(bp, 0); 1870 } 1871 } 1872 1873 /* 1874 * Return the next segment to write. 1875 */ 1876 void 1877 lfs_newseg(struct lfs *fs) 1878 { 1879 CLEANERINFO *cip; 1880 SEGUSE *sup; 1881 struct buf *bp; 1882 int curseg, isdirty, sn, skip_inval; 1883 1884 ASSERT_SEGLOCK(fs); 1885 1886 /* Honor LFCNWRAPSTOP */ 1887 mutex_enter(&lfs_lock); 1888 while (fs->lfs_nextseg < fs->lfs_curseg && fs->lfs_nowrap) { 1889 if (fs->lfs_wrappass) { 1890 log(LOG_NOTICE, "%s: wrappass=%d\n", 1891 fs->lfs_fsmnt, fs->lfs_wrappass); 1892 fs->lfs_wrappass = 0; 1893 break; 1894 } 1895 fs->lfs_wrapstatus = LFS_WRAP_WAITING; 1896 wakeup(&fs->lfs_nowrap); 1897 log(LOG_NOTICE, "%s: waiting at log wrap\n", fs->lfs_fsmnt); 1898 mtsleep(&fs->lfs_wrappass, PVFS, "newseg", 10 * hz, 1899 &lfs_lock); 1900 } 1901 fs->lfs_wrapstatus = LFS_WRAP_GOING; 1902 mutex_exit(&lfs_lock); 1903 1904 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, fs->lfs_nextseg), bp); 1905 DLOG((DLOG_SU, "lfs_newseg: seg %d := 0 in newseg\n", 1906 lfs_dtosn(fs, fs->lfs_nextseg))); 1907 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE; 1908 sup->su_nbytes = 0; 1909 sup->su_nsums = 0; 1910 sup->su_ninos = 0; 1911 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, fs->lfs_nextseg), bp); 1912 1913 LFS_CLEANERINFO(cip, fs, bp); 1914 --cip->clean; 1915 ++cip->dirty; 1916 fs->lfs_nclean = cip->clean; 1917 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1); 1918 1919 fs->lfs_lastseg = fs->lfs_curseg; 1920 fs->lfs_curseg = fs->lfs_nextseg; 1921 skip_inval = 1; 1922 for (sn = curseg = lfs_dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) { 1923 sn = (sn + 1) % fs->lfs_nseg; 1924 1925 if (sn == curseg) { 1926 if (skip_inval) 1927 skip_inval = 0; 1928 else 1929 panic("lfs_nextseg: no clean segments"); 1930 } 1931 LFS_SEGENTRY(sup, fs, sn, bp); 1932 isdirty = sup->su_flags & (SEGUSE_DIRTY | (skip_inval ? SEGUSE_INVAL : 0)); 1933 /* Check SEGUSE_EMPTY as we go along */ 1934 if (isdirty && sup->su_nbytes == 0 && 1935 !(sup->su_flags & SEGUSE_EMPTY)) 1936 LFS_WRITESEGENTRY(sup, fs, sn, bp); 1937 else 1938 brelse(bp, 0); 1939 1940 if (!isdirty) 1941 break; 1942 } 1943 if (skip_inval == 0) 1944 lfs_unset_inval_all(fs); 1945 1946 ++fs->lfs_nactive; 1947 fs->lfs_nextseg = lfs_sntod(fs, sn); 1948 if (lfs_dostats) { 1949 ++lfs_stats.segsused; 1950 } 1951 } 1952 1953 static struct buf * 1954 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr, 1955 int n) 1956 { 1957 struct lfs_cluster *cl; 1958 struct buf **bpp, *bp; 1959 1960 ASSERT_SEGLOCK(fs); 1961 cl = (struct lfs_cluster *)pool_get(&fs->lfs_clpool, PR_WAITOK); 1962 bpp = (struct buf **)pool_get(&fs->lfs_bpppool, PR_WAITOK); 1963 memset(cl, 0, sizeof(*cl)); 1964 cl->fs = fs; 1965 cl->bpp = bpp; 1966 cl->bufcount = 0; 1967 cl->bufsize = 0; 1968 1969 /* If this segment is being written synchronously, note that */ 1970 if (fs->lfs_sp->seg_flags & SEGM_SYNC) { 1971 cl->flags |= LFS_CL_SYNC; 1972 cl->seg = fs->lfs_sp; 1973 ++cl->seg->seg_iocount; 1974 } 1975 1976 /* Get an empty buffer header, or maybe one with something on it */ 1977 bp = getiobuf(vp, true); 1978 bp->b_dev = NODEV; 1979 bp->b_blkno = bp->b_lblkno = addr; 1980 bp->b_iodone = lfs_cluster_callback; 1981 bp->b_private = cl; 1982 1983 return bp; 1984 } 1985 1986 int 1987 lfs_writeseg(struct lfs *fs, struct segment *sp) 1988 { 1989 struct buf **bpp, *bp, *cbp, *newbp, *unbusybp; 1990 SEGUSE *sup; 1991 SEGSUM *ssp; 1992 int i; 1993 int do_again, nblocks, byteoffset; 1994 size_t el_size; 1995 struct lfs_cluster *cl; 1996 u_short ninos; 1997 struct vnode *devvp; 1998 char *p = NULL; 1999 struct vnode *vp; 2000 int32_t *daddrp; /* XXX ondisk32 */ 2001 int changed; 2002 u_int32_t sum; 2003 #ifdef DEBUG 2004 FINFO *fip; 2005 int findex; 2006 #endif 2007 2008 ASSERT_SEGLOCK(fs); 2009 2010 ssp = (SEGSUM *)sp->segsum; 2011 2012 /* 2013 * If there are no buffers other than the segment summary to write, 2014 * don't do anything. If we are the end of a dirop sequence, however, 2015 * write the empty segment summary anyway, to help out the 2016 * roll-forward agent. 2017 */ 2018 if ((nblocks = sp->cbpp - sp->bpp) == 1) { 2019 if ((ssp->ss_flags & (SS_DIROP | SS_CONT)) != SS_DIROP) 2020 return 0; 2021 } 2022 2023 /* Note if partial segment is being written by the cleaner */ 2024 if (sp->seg_flags & SEGM_CLEAN) 2025 ssp->ss_flags |= SS_CLEAN; 2026 2027 /* Note if we are writing to reclaim */ 2028 if (sp->seg_flags & SEGM_RECLAIM) { 2029 ssp->ss_flags |= SS_RECLAIM; 2030 ssp->ss_reclino = fs->lfs_reclino; 2031 } 2032 2033 devvp = VTOI(fs->lfs_ivnode)->i_devvp; 2034 2035 /* Update the segment usage information. */ 2036 LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 2037 2038 /* Loop through all blocks, except the segment summary. */ 2039 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) { 2040 if ((*bpp)->b_vp != devvp) { 2041 sup->su_nbytes += (*bpp)->b_bcount; 2042 DLOG((DLOG_SU, "seg %" PRIu32 " += %ld for ino %d" 2043 " lbn %" PRId64 " db 0x%" PRIx64 "\n", 2044 sp->seg_number, (*bpp)->b_bcount, 2045 VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno, 2046 (*bpp)->b_blkno)); 2047 } 2048 } 2049 2050 #ifdef DEBUG 2051 /* Check for zero-length and zero-version FINFO entries. */ 2052 fip = (struct finfo *)((char *)ssp + SEGSUM_SIZE(fs)); 2053 for (findex = 0; findex < ssp->ss_nfinfo; findex++) { 2054 KDASSERT(fip->fi_nblocks > 0); 2055 KDASSERT(fip->fi_version > 0); 2056 fip = (FINFO *)((char *)fip + FINFOSIZE + 2057 sizeof(int32_t) * fip->fi_nblocks); 2058 } 2059 #endif /* DEBUG */ 2060 2061 ninos = (ssp->ss_ninos + LFS_INOPB(fs) - 1) / LFS_INOPB(fs); 2062 DLOG((DLOG_SU, "seg %d += %d for %d inodes\n", 2063 sp->seg_number, ssp->ss_ninos * sizeof (struct ulfs1_dinode), 2064 ssp->ss_ninos)); 2065 sup->su_nbytes += ssp->ss_ninos * sizeof (struct ulfs1_dinode); 2066 /* sup->su_nbytes += fs->lfs_sumsize; */ 2067 if (fs->lfs_version == 1) 2068 sup->su_olastmod = time_second; 2069 else 2070 sup->su_lastmod = time_second; 2071 sup->su_ninos += ninos; 2072 ++sup->su_nsums; 2073 fs->lfs_avail -= lfs_btofsb(fs, fs->lfs_sumsize); 2074 2075 do_again = !(bp->b_flags & B_GATHERED); 2076 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */ 2077 2078 /* 2079 * Mark blocks B_BUSY, to prevent then from being changed between 2080 * the checksum computation and the actual write. 2081 * 2082 * If we are cleaning, check indirect blocks for UNWRITTEN, and if 2083 * there are any, replace them with copies that have UNASSIGNED 2084 * instead. 2085 */ 2086 mutex_enter(&bufcache_lock); 2087 for (bpp = sp->bpp, i = nblocks - 1; i--;) { 2088 ++bpp; 2089 bp = *bpp; 2090 if (bp->b_iodone != NULL) { /* UBC or malloced buffer */ 2091 bp->b_cflags |= BC_BUSY; 2092 continue; 2093 } 2094 2095 while (bp->b_cflags & BC_BUSY) { 2096 DLOG((DLOG_SEG, "lfs_writeseg: avoiding potential" 2097 " data summary corruption for ino %d, lbn %" 2098 PRId64 "\n", 2099 VTOI(bp->b_vp)->i_number, bp->b_lblkno)); 2100 bp->b_cflags |= BC_WANTED; 2101 cv_wait(&bp->b_busy, &bufcache_lock); 2102 } 2103 bp->b_cflags |= BC_BUSY; 2104 mutex_exit(&bufcache_lock); 2105 unbusybp = NULL; 2106 2107 /* 2108 * Check and replace indirect block UNWRITTEN bogosity. 2109 * XXX See comment in lfs_writefile. 2110 */ 2111 if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp && 2112 VTOI(bp->b_vp)->i_ffs1_blocks != 2113 VTOI(bp->b_vp)->i_lfs_effnblks) { 2114 DLOG((DLOG_VNODE, "lfs_writeseg: cleansing ino %d (%d != %d)\n", 2115 VTOI(bp->b_vp)->i_number, 2116 VTOI(bp->b_vp)->i_lfs_effnblks, 2117 VTOI(bp->b_vp)->i_ffs1_blocks)); 2118 /* Make a copy we'll make changes to */ 2119 newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno, 2120 bp->b_bcount, LFS_NB_IBLOCK); 2121 newbp->b_blkno = bp->b_blkno; 2122 memcpy(newbp->b_data, bp->b_data, 2123 newbp->b_bcount); 2124 2125 changed = 0; 2126 /* XXX ondisk32 */ 2127 for (daddrp = (int32_t *)(newbp->b_data); 2128 daddrp < (int32_t *)((char *)newbp->b_data + 2129 newbp->b_bcount); daddrp++) { 2130 if (*daddrp == UNWRITTEN) { 2131 ++changed; 2132 *daddrp = 0; 2133 } 2134 } 2135 /* 2136 * Get rid of the old buffer. Don't mark it clean, 2137 * though, if it still has dirty data on it. 2138 */ 2139 if (changed) { 2140 DLOG((DLOG_SEG, "lfs_writeseg: replacing UNWRITTEN(%d):" 2141 " bp = %p newbp = %p\n", changed, bp, 2142 newbp)); 2143 *bpp = newbp; 2144 bp->b_flags &= ~B_GATHERED; 2145 bp->b_error = 0; 2146 if (bp->b_iodone != NULL) { 2147 DLOG((DLOG_SEG, "lfs_writeseg: " 2148 "indir bp should not be B_CALL\n")); 2149 biodone(bp); 2150 bp = NULL; 2151 } else { 2152 /* Still on free list, leave it there */ 2153 unbusybp = bp; 2154 /* 2155 * We have to re-decrement lfs_avail 2156 * since this block is going to come 2157 * back around to us in the next 2158 * segment. 2159 */ 2160 fs->lfs_avail -= 2161 lfs_btofsb(fs, bp->b_bcount); 2162 } 2163 } else { 2164 lfs_freebuf(fs, newbp); 2165 } 2166 } 2167 mutex_enter(&bufcache_lock); 2168 if (unbusybp != NULL) { 2169 unbusybp->b_cflags &= ~BC_BUSY; 2170 if (unbusybp->b_cflags & BC_WANTED) 2171 cv_broadcast(&bp->b_busy); 2172 } 2173 } 2174 mutex_exit(&bufcache_lock); 2175 2176 /* 2177 * Compute checksum across data and then across summary; the first 2178 * block (the summary block) is skipped. Set the create time here 2179 * so that it's guaranteed to be later than the inode mod times. 2180 */ 2181 sum = 0; 2182 if (fs->lfs_version == 1) 2183 el_size = sizeof(u_long); 2184 else 2185 el_size = sizeof(u_int32_t); 2186 for (bpp = sp->bpp, i = nblocks - 1; i--; ) { 2187 ++bpp; 2188 /* Loop through gop_write cluster blocks */ 2189 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount; 2190 byteoffset += fs->lfs_bsize) { 2191 #ifdef LFS_USE_B_INVAL 2192 if (((*bpp)->b_cflags & BC_INVAL) != 0 && 2193 (*bpp)->b_iodone != NULL) { 2194 if (copyin((void *)(*bpp)->b_saveaddr + 2195 byteoffset, dp, el_size)) { 2196 panic("lfs_writeseg: copyin failed [1]:" 2197 " ino %d blk %" PRId64, 2198 VTOI((*bpp)->b_vp)->i_number, 2199 (*bpp)->b_lblkno); 2200 } 2201 } else 2202 #endif /* LFS_USE_B_INVAL */ 2203 { 2204 sum = lfs_cksum_part((char *) 2205 (*bpp)->b_data + byteoffset, el_size, sum); 2206 } 2207 } 2208 } 2209 if (fs->lfs_version == 1) 2210 ssp->ss_ocreate = time_second; 2211 else { 2212 ssp->ss_create = time_second; 2213 ssp->ss_serial = ++fs->lfs_serial; 2214 ssp->ss_ident = fs->lfs_ident; 2215 } 2216 ssp->ss_datasum = lfs_cksum_fold(sum); 2217 ssp->ss_sumsum = cksum(&ssp->ss_datasum, 2218 fs->lfs_sumsize - sizeof(ssp->ss_sumsum)); 2219 2220 mutex_enter(&lfs_lock); 2221 fs->lfs_bfree -= (lfs_btofsb(fs, ninos * fs->lfs_ibsize) + 2222 lfs_btofsb(fs, fs->lfs_sumsize)); 2223 fs->lfs_dmeta += (lfs_btofsb(fs, ninos * fs->lfs_ibsize) + 2224 lfs_btofsb(fs, fs->lfs_sumsize)); 2225 mutex_exit(&lfs_lock); 2226 2227 /* 2228 * When we simply write the blocks we lose a rotation for every block 2229 * written. To avoid this problem, we cluster the buffers into a 2230 * chunk and write the chunk. MAXPHYS is the largest size I/O 2231 * devices can handle, use that for the size of the chunks. 2232 * 2233 * Blocks that are already clusters (from GOP_WRITE), however, we 2234 * don't bother to copy into other clusters. 2235 */ 2236 2237 #define CHUNKSIZE MAXPHYS 2238 2239 if (devvp == NULL) 2240 panic("devvp is NULL"); 2241 for (bpp = sp->bpp, i = nblocks; i;) { 2242 cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i); 2243 cl = cbp->b_private; 2244 2245 cbp->b_flags |= B_ASYNC; 2246 cbp->b_cflags |= BC_BUSY; 2247 cbp->b_bcount = 0; 2248 2249 #if defined(DEBUG) && defined(DIAGNOSTIC) 2250 if (bpp - sp->bpp > (fs->lfs_sumsize - SEGSUM_SIZE(fs)) 2251 / sizeof(int32_t)) { 2252 panic("lfs_writeseg: real bpp overwrite"); 2253 } 2254 if (bpp - sp->bpp > lfs_segsize(fs) / fs->lfs_fsize) { 2255 panic("lfs_writeseg: theoretical bpp overwrite"); 2256 } 2257 #endif 2258 2259 /* 2260 * Construct the cluster. 2261 */ 2262 mutex_enter(&lfs_lock); 2263 ++fs->lfs_iocount; 2264 mutex_exit(&lfs_lock); 2265 while (i && cbp->b_bcount < CHUNKSIZE) { 2266 bp = *bpp; 2267 2268 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount)) 2269 break; 2270 if (cbp->b_bcount > 0 && !(cl->flags & LFS_CL_MALLOC)) 2271 break; 2272 2273 /* Clusters from GOP_WRITE are expedited */ 2274 if (bp->b_bcount > fs->lfs_bsize) { 2275 if (cbp->b_bcount > 0) 2276 /* Put in its own buffer */ 2277 break; 2278 else { 2279 cbp->b_data = bp->b_data; 2280 } 2281 } else if (cbp->b_bcount == 0) { 2282 p = cbp->b_data = lfs_malloc(fs, CHUNKSIZE, 2283 LFS_NB_CLUSTER); 2284 cl->flags |= LFS_CL_MALLOC; 2285 } 2286 #ifdef DIAGNOSTIC 2287 if (lfs_dtosn(fs, LFS_DBTOFSB(fs, bp->b_blkno + 2288 btodb(bp->b_bcount - 1))) != 2289 sp->seg_number) { 2290 printf("blk size %d daddr %" PRIx64 2291 " not in seg %d\n", 2292 bp->b_bcount, bp->b_blkno, 2293 sp->seg_number); 2294 panic("segment overwrite"); 2295 } 2296 #endif 2297 2298 #ifdef LFS_USE_B_INVAL 2299 /* 2300 * Fake buffers from the cleaner are marked as B_INVAL. 2301 * We need to copy the data from user space rather than 2302 * from the buffer indicated. 2303 * XXX == what do I do on an error? 2304 */ 2305 if ((bp->b_cflags & BC_INVAL) != 0 && 2306 bp->b_iodone != NULL) { 2307 if (copyin(bp->b_saveaddr, p, bp->b_bcount)) 2308 panic("lfs_writeseg: " 2309 "copyin failed [2]"); 2310 } else 2311 #endif /* LFS_USE_B_INVAL */ 2312 if (cl->flags & LFS_CL_MALLOC) { 2313 /* copy data into our cluster. */ 2314 memcpy(p, bp->b_data, bp->b_bcount); 2315 p += bp->b_bcount; 2316 } 2317 2318 cbp->b_bcount += bp->b_bcount; 2319 cl->bufsize += bp->b_bcount; 2320 2321 bp->b_flags &= ~B_READ; 2322 bp->b_error = 0; 2323 cl->bpp[cl->bufcount++] = bp; 2324 2325 vp = bp->b_vp; 2326 mutex_enter(&bufcache_lock); 2327 mutex_enter(vp->v_interlock); 2328 bp->b_oflags &= ~(BO_DELWRI | BO_DONE); 2329 reassignbuf(bp, vp); 2330 vp->v_numoutput++; 2331 mutex_exit(vp->v_interlock); 2332 mutex_exit(&bufcache_lock); 2333 2334 bpp++; 2335 i--; 2336 } 2337 if (fs->lfs_sp->seg_flags & SEGM_SYNC) 2338 BIO_SETPRIO(cbp, BPRIO_TIMECRITICAL); 2339 else 2340 BIO_SETPRIO(cbp, BPRIO_TIMELIMITED); 2341 mutex_enter(devvp->v_interlock); 2342 devvp->v_numoutput++; 2343 mutex_exit(devvp->v_interlock); 2344 VOP_STRATEGY(devvp, cbp); 2345 curlwp->l_ru.ru_oublock++; 2346 } 2347 2348 if (lfs_dostats) { 2349 ++lfs_stats.psegwrites; 2350 lfs_stats.blocktot += nblocks - 1; 2351 if (fs->lfs_sp->seg_flags & SEGM_SYNC) 2352 ++lfs_stats.psyncwrites; 2353 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) { 2354 ++lfs_stats.pcleanwrites; 2355 lfs_stats.cleanblocks += nblocks - 1; 2356 } 2357 } 2358 2359 return (lfs_initseg(fs) || do_again); 2360 } 2361 2362 void 2363 lfs_writesuper(struct lfs *fs, daddr_t daddr) 2364 { 2365 struct buf *bp; 2366 struct vnode *devvp = VTOI(fs->lfs_ivnode)->i_devvp; 2367 int s; 2368 2369 ASSERT_MAYBE_SEGLOCK(fs); 2370 #ifdef DIAGNOSTIC 2371 KASSERT(fs->lfs_magic == LFS_MAGIC); 2372 #endif 2373 /* 2374 * If we can write one superblock while another is in 2375 * progress, we risk not having a complete checkpoint if we crash. 2376 * So, block here if a superblock write is in progress. 2377 */ 2378 mutex_enter(&lfs_lock); 2379 s = splbio(); 2380 while (fs->lfs_sbactive) { 2381 mtsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0, 2382 &lfs_lock); 2383 } 2384 fs->lfs_sbactive = daddr; 2385 splx(s); 2386 mutex_exit(&lfs_lock); 2387 2388 /* Set timestamp of this version of the superblock */ 2389 if (fs->lfs_version == 1) 2390 fs->lfs_otstamp = time_second; 2391 fs->lfs_tstamp = time_second; 2392 2393 /* Checksum the superblock and copy it into a buffer. */ 2394 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs)); 2395 bp = lfs_newbuf(fs, devvp, 2396 LFS_FSBTODB(fs, daddr), LFS_SBPAD, LFS_NB_SBLOCK); 2397 memset((char *)bp->b_data + sizeof(struct dlfs), 0, 2398 LFS_SBPAD - sizeof(struct dlfs)); 2399 *(struct dlfs *)bp->b_data = fs->lfs_dlfs; 2400 2401 bp->b_cflags |= BC_BUSY; 2402 bp->b_flags = (bp->b_flags & ~B_READ) | B_ASYNC; 2403 bp->b_oflags &= ~(BO_DONE | BO_DELWRI); 2404 bp->b_error = 0; 2405 bp->b_iodone = lfs_supercallback; 2406 2407 if (fs->lfs_sp != NULL && fs->lfs_sp->seg_flags & SEGM_SYNC) 2408 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL); 2409 else 2410 BIO_SETPRIO(bp, BPRIO_TIMELIMITED); 2411 curlwp->l_ru.ru_oublock++; 2412 2413 mutex_enter(devvp->v_interlock); 2414 devvp->v_numoutput++; 2415 mutex_exit(devvp->v_interlock); 2416 2417 mutex_enter(&lfs_lock); 2418 ++fs->lfs_iocount; 2419 mutex_exit(&lfs_lock); 2420 VOP_STRATEGY(devvp, bp); 2421 } 2422 2423 /* 2424 * Logical block number match routines used when traversing the dirty block 2425 * chain. 2426 */ 2427 int 2428 lfs_match_fake(struct lfs *fs, struct buf *bp) 2429 { 2430 2431 ASSERT_SEGLOCK(fs); 2432 return LFS_IS_MALLOC_BUF(bp); 2433 } 2434 2435 #if 0 2436 int 2437 lfs_match_real(struct lfs *fs, struct buf *bp) 2438 { 2439 2440 ASSERT_SEGLOCK(fs); 2441 return (lfs_match_data(fs, bp) && !lfs_match_fake(fs, bp)); 2442 } 2443 #endif 2444 2445 int 2446 lfs_match_data(struct lfs *fs, struct buf *bp) 2447 { 2448 2449 ASSERT_SEGLOCK(fs); 2450 return (bp->b_lblkno >= 0); 2451 } 2452 2453 int 2454 lfs_match_indir(struct lfs *fs, struct buf *bp) 2455 { 2456 daddr_t lbn; 2457 2458 ASSERT_SEGLOCK(fs); 2459 lbn = bp->b_lblkno; 2460 return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 0); 2461 } 2462 2463 int 2464 lfs_match_dindir(struct lfs *fs, struct buf *bp) 2465 { 2466 daddr_t lbn; 2467 2468 ASSERT_SEGLOCK(fs); 2469 lbn = bp->b_lblkno; 2470 return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 1); 2471 } 2472 2473 int 2474 lfs_match_tindir(struct lfs *fs, struct buf *bp) 2475 { 2476 daddr_t lbn; 2477 2478 ASSERT_SEGLOCK(fs); 2479 lbn = bp->b_lblkno; 2480 return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 2); 2481 } 2482 2483 static void 2484 lfs_free_aiodone(struct buf *bp) 2485 { 2486 struct lfs *fs; 2487 2488 KERNEL_LOCK(1, curlwp); 2489 fs = bp->b_private; 2490 ASSERT_NO_SEGLOCK(fs); 2491 lfs_freebuf(fs, bp); 2492 KERNEL_UNLOCK_LAST(curlwp); 2493 } 2494 2495 static void 2496 lfs_super_aiodone(struct buf *bp) 2497 { 2498 struct lfs *fs; 2499 2500 KERNEL_LOCK(1, curlwp); 2501 fs = bp->b_private; 2502 ASSERT_NO_SEGLOCK(fs); 2503 mutex_enter(&lfs_lock); 2504 fs->lfs_sbactive = 0; 2505 if (--fs->lfs_iocount <= 1) 2506 wakeup(&fs->lfs_iocount); 2507 wakeup(&fs->lfs_sbactive); 2508 mutex_exit(&lfs_lock); 2509 lfs_freebuf(fs, bp); 2510 KERNEL_UNLOCK_LAST(curlwp); 2511 } 2512 2513 static void 2514 lfs_cluster_aiodone(struct buf *bp) 2515 { 2516 struct lfs_cluster *cl; 2517 struct lfs *fs; 2518 struct buf *tbp, *fbp; 2519 struct vnode *vp, *devvp, *ovp; 2520 struct inode *ip; 2521 int error; 2522 2523 KERNEL_LOCK(1, curlwp); 2524 2525 error = bp->b_error; 2526 cl = bp->b_private; 2527 fs = cl->fs; 2528 devvp = VTOI(fs->lfs_ivnode)->i_devvp; 2529 ASSERT_NO_SEGLOCK(fs); 2530 2531 /* Put the pages back, and release the buffer */ 2532 while (cl->bufcount--) { 2533 tbp = cl->bpp[cl->bufcount]; 2534 KASSERT(tbp->b_cflags & BC_BUSY); 2535 if (error) { 2536 tbp->b_error = error; 2537 } 2538 2539 /* 2540 * We're done with tbp. If it has not been re-dirtied since 2541 * the cluster was written, free it. Otherwise, keep it on 2542 * the locked list to be written again. 2543 */ 2544 vp = tbp->b_vp; 2545 2546 tbp->b_flags &= ~B_GATHERED; 2547 2548 LFS_BCLEAN_LOG(fs, tbp); 2549 2550 mutex_enter(&bufcache_lock); 2551 if (tbp->b_iodone == NULL) { 2552 KASSERT(tbp->b_flags & B_LOCKED); 2553 bremfree(tbp); 2554 if (vp) { 2555 mutex_enter(vp->v_interlock); 2556 reassignbuf(tbp, vp); 2557 mutex_exit(vp->v_interlock); 2558 } 2559 tbp->b_flags |= B_ASYNC; /* for biodone */ 2560 } 2561 2562 if (((tbp->b_flags | tbp->b_oflags) & 2563 (B_LOCKED | BO_DELWRI)) == B_LOCKED) 2564 LFS_UNLOCK_BUF(tbp); 2565 2566 if (tbp->b_oflags & BO_DONE) { 2567 DLOG((DLOG_SEG, "blk %d biodone already (flags %lx)\n", 2568 cl->bufcount, (long)tbp->b_flags)); 2569 } 2570 2571 if (tbp->b_iodone != NULL && !LFS_IS_MALLOC_BUF(tbp)) { 2572 /* 2573 * A buffer from the page daemon. 2574 * We use the same iodone as it does, 2575 * so we must manually disassociate its 2576 * buffers from the vp. 2577 */ 2578 if ((ovp = tbp->b_vp) != NULL) { 2579 /* This is just silly */ 2580 mutex_enter(ovp->v_interlock); 2581 brelvp(tbp); 2582 mutex_exit(ovp->v_interlock); 2583 tbp->b_vp = vp; 2584 tbp->b_objlock = vp->v_interlock; 2585 } 2586 /* Put it back the way it was */ 2587 tbp->b_flags |= B_ASYNC; 2588 /* Master buffers have BC_AGE */ 2589 if (tbp->b_private == tbp) 2590 tbp->b_cflags |= BC_AGE; 2591 } 2592 mutex_exit(&bufcache_lock); 2593 2594 biodone(tbp); 2595 2596 /* 2597 * If this is the last block for this vnode, but 2598 * there are other blocks on its dirty list, 2599 * set IN_MODIFIED/IN_CLEANING depending on what 2600 * sort of block. Only do this for our mount point, 2601 * not for, e.g., inode blocks that are attached to 2602 * the devvp. 2603 * XXX KS - Shouldn't we set *both* if both types 2604 * of blocks are present (traverse the dirty list?) 2605 */ 2606 mutex_enter(vp->v_interlock); 2607 mutex_enter(&lfs_lock); 2608 if (vp != devvp && vp->v_numoutput == 0 && 2609 (fbp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL) { 2610 ip = VTOI(vp); 2611 DLOG((DLOG_SEG, "lfs_cluster_aiodone: mark ino %d\n", 2612 ip->i_number)); 2613 if (LFS_IS_MALLOC_BUF(fbp)) 2614 LFS_SET_UINO(ip, IN_CLEANING); 2615 else 2616 LFS_SET_UINO(ip, IN_MODIFIED); 2617 } 2618 cv_broadcast(&vp->v_cv); 2619 mutex_exit(&lfs_lock); 2620 mutex_exit(vp->v_interlock); 2621 } 2622 2623 /* Fix up the cluster buffer, and release it */ 2624 if (cl->flags & LFS_CL_MALLOC) 2625 lfs_free(fs, bp->b_data, LFS_NB_CLUSTER); 2626 putiobuf(bp); 2627 2628 /* Note i/o done */ 2629 if (cl->flags & LFS_CL_SYNC) { 2630 if (--cl->seg->seg_iocount == 0) 2631 wakeup(&cl->seg->seg_iocount); 2632 } 2633 mutex_enter(&lfs_lock); 2634 #ifdef DIAGNOSTIC 2635 if (fs->lfs_iocount == 0) 2636 panic("lfs_cluster_aiodone: zero iocount"); 2637 #endif 2638 if (--fs->lfs_iocount <= 1) 2639 wakeup(&fs->lfs_iocount); 2640 mutex_exit(&lfs_lock); 2641 2642 KERNEL_UNLOCK_LAST(curlwp); 2643 2644 pool_put(&fs->lfs_bpppool, cl->bpp); 2645 cl->bpp = NULL; 2646 pool_put(&fs->lfs_clpool, cl); 2647 } 2648 2649 static void 2650 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *)) 2651 { 2652 /* reset b_iodone for when this is a single-buf i/o. */ 2653 bp->b_iodone = aiodone; 2654 2655 workqueue_enqueue(uvm.aiodone_queue, &bp->b_work, NULL); 2656 } 2657 2658 static void 2659 lfs_cluster_callback(struct buf *bp) 2660 { 2661 2662 lfs_generic_callback(bp, lfs_cluster_aiodone); 2663 } 2664 2665 void 2666 lfs_supercallback(struct buf *bp) 2667 { 2668 2669 lfs_generic_callback(bp, lfs_super_aiodone); 2670 } 2671 2672 /* 2673 * The only buffers that are going to hit these functions are the 2674 * segment write blocks, or the segment summaries, or the superblocks. 2675 * 2676 * All of the above are created by lfs_newbuf, and so do not need to be 2677 * released via brelse. 2678 */ 2679 void 2680 lfs_callback(struct buf *bp) 2681 { 2682 2683 lfs_generic_callback(bp, lfs_free_aiodone); 2684 } 2685 2686 /* 2687 * Shellsort (diminishing increment sort) from Data Structures and 2688 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 2689 * see also Knuth Vol. 3, page 84. The increments are selected from 2690 * formula (8), page 95. Roughly O(N^3/2). 2691 */ 2692 /* 2693 * This is our own private copy of shellsort because we want to sort 2694 * two parallel arrays (the array of buffer pointers and the array of 2695 * logical block numbers) simultaneously. Note that we cast the array 2696 * of logical block numbers to a unsigned in this routine so that the 2697 * negative block numbers (meta data blocks) sort AFTER the data blocks. 2698 */ 2699 2700 void 2701 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb, int size) 2702 { 2703 static int __rsshell_increments[] = { 4, 1, 0 }; 2704 int incr, *incrp, t1, t2; 2705 struct buf *bp_temp; 2706 2707 #ifdef DEBUG 2708 incr = 0; 2709 for (t1 = 0; t1 < nmemb; t1++) { 2710 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) { 2711 if (lb_array[incr++] != bp_array[t1]->b_lblkno + t2) { 2712 /* dump before panic */ 2713 printf("lfs_shellsort: nmemb=%d, size=%d\n", 2714 nmemb, size); 2715 incr = 0; 2716 for (t1 = 0; t1 < nmemb; t1++) { 2717 const struct buf *bp = bp_array[t1]; 2718 2719 printf("bp[%d]: lbn=%" PRIu64 ", size=%" 2720 PRIu64 "\n", t1, 2721 (uint64_t)bp->b_bcount, 2722 (uint64_t)bp->b_lblkno); 2723 printf("lbns:"); 2724 for (t2 = 0; t2 * size < bp->b_bcount; 2725 t2++) { 2726 printf(" %" PRId32, 2727 lb_array[incr++]); 2728 } 2729 printf("\n"); 2730 } 2731 panic("lfs_shellsort: inconsistent input"); 2732 } 2733 } 2734 } 2735 #endif 2736 2737 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;) 2738 for (t1 = incr; t1 < nmemb; ++t1) 2739 for (t2 = t1 - incr; t2 >= 0;) 2740 if ((u_int32_t)bp_array[t2]->b_lblkno > 2741 (u_int32_t)bp_array[t2 + incr]->b_lblkno) { 2742 bp_temp = bp_array[t2]; 2743 bp_array[t2] = bp_array[t2 + incr]; 2744 bp_array[t2 + incr] = bp_temp; 2745 t2 -= incr; 2746 } else 2747 break; 2748 2749 /* Reform the list of logical blocks */ 2750 incr = 0; 2751 for (t1 = 0; t1 < nmemb; t1++) { 2752 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) { 2753 lb_array[incr++] = bp_array[t1]->b_lblkno + t2; 2754 } 2755 } 2756 } 2757 2758 /* 2759 * Call vget with LK_NOWAIT. If we are the one who is dead, 2760 * however, we must press on. Just fake success in that case. 2761 */ 2762 int 2763 lfs_vref(struct vnode *vp) 2764 { 2765 struct lfs *fs; 2766 2767 KASSERT(mutex_owned(vp->v_interlock)); 2768 2769 fs = VTOI(vp)->i_lfs; 2770 2771 ASSERT_MAYBE_SEGLOCK(fs); 2772 2773 /* 2774 * If we return 1 here during a flush, we risk vinvalbuf() not 2775 * being able to flush all of the pages from this vnode, which 2776 * will cause it to panic. So, return 0 if a flush is in progress. 2777 */ 2778 if (IS_FLUSHING(VTOI(vp)->i_lfs, vp)) { 2779 ++fs->lfs_flushvp_fakevref; 2780 mutex_exit(vp->v_interlock); 2781 return 0; 2782 } 2783 2784 return vget(vp, LK_NOWAIT); 2785 } 2786 2787 /* 2788 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We 2789 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end. 2790 */ 2791 void 2792 lfs_vunref(struct vnode *vp) 2793 { 2794 struct lfs *fs; 2795 2796 fs = VTOI(vp)->i_lfs; 2797 ASSERT_MAYBE_SEGLOCK(fs); 2798 2799 /* 2800 * Analogous to lfs_vref, if the node is flushing, fake it. 2801 */ 2802 if (IS_FLUSHING(fs, vp) && fs->lfs_flushvp_fakevref) { 2803 --fs->lfs_flushvp_fakevref; 2804 return; 2805 } 2806 2807 /* does not call inactive XXX sure it does XXX */ 2808 vrele(vp); 2809 } 2810 2811 /* 2812 * We use this when we have vnodes that were loaded in solely for cleaning. 2813 * There is no reason to believe that these vnodes will be referenced again 2814 * soon, since the cleaning process is unrelated to normal filesystem 2815 * activity. Putting cleaned vnodes at the tail of the list has the effect 2816 * of flushing the vnode LRU. So, put vnodes that were loaded only for 2817 * cleaning at the head of the list, instead. 2818 */ 2819 void 2820 lfs_vunref_head(struct vnode *vp) 2821 { 2822 2823 ASSERT_SEGLOCK(VTOI(vp)->i_lfs); 2824 2825 /* does not call inactive XXX sure it does XXX, 2826 inserts non-held vnode at head of freelist */ 2827 vrele(vp); 2828 } 2829 2830 2831 /* 2832 * Set up an FINFO entry for a new file. The fip pointer is assumed to 2833 * point at uninitialized space. 2834 */ 2835 void 2836 lfs_acquire_finfo(struct lfs *fs, ino_t ino, int vers) 2837 { 2838 struct segment *sp = fs->lfs_sp; 2839 2840 KASSERT(vers > 0); 2841 2842 if (sp->seg_bytes_left < fs->lfs_bsize || 2843 sp->sum_bytes_left < sizeof(struct finfo)) 2844 (void) lfs_writeseg(fs, fs->lfs_sp); 2845 2846 sp->sum_bytes_left -= FINFOSIZE; 2847 ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 2848 sp->fip->fi_nblocks = 0; 2849 sp->fip->fi_ino = ino; 2850 sp->fip->fi_version = vers; 2851 } 2852 2853 /* 2854 * Release the FINFO entry, either clearing out an unused entry or 2855 * advancing us to the next available entry. 2856 */ 2857 void 2858 lfs_release_finfo(struct lfs *fs) 2859 { 2860 struct segment *sp = fs->lfs_sp; 2861 2862 if (sp->fip->fi_nblocks != 0) { 2863 sp->fip = (FINFO*)((char *)sp->fip + FINFOSIZE + 2864 sizeof(int32_t) * sp->fip->fi_nblocks); 2865 sp->start_lbp = &sp->fip->fi_blocks[0]; 2866 } else { 2867 sp->sum_bytes_left += FINFOSIZE; 2868 --((SEGSUM *)(sp->segsum))->ss_nfinfo; 2869 } 2870 } 2871