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