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