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