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