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