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