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