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