1 /* $NetBSD: lfs_segment.c,v 1.66 2000/12/03 05:56:27 perseant 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 #define ivndebug(vp,str) printf("ino %d: %s\n",VTOI(vp)->i_number,(str)) 74 75 #if defined(_KERNEL) && !defined(_LKM) 76 #include "opt_ddb.h" 77 #endif 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/namei.h> 82 #include <sys/kernel.h> 83 #include <sys/resourcevar.h> 84 #include <sys/file.h> 85 #include <sys/stat.h> 86 #include <sys/buf.h> 87 #include <sys/proc.h> 88 #include <sys/conf.h> 89 #include <sys/vnode.h> 90 #include <sys/malloc.h> 91 #include <sys/mount.h> 92 93 #include <miscfs/specfs/specdev.h> 94 #include <miscfs/fifofs/fifo.h> 95 96 #include <ufs/ufs/quota.h> 97 #include <ufs/ufs/inode.h> 98 #include <ufs/ufs/dir.h> 99 #include <ufs/ufs/ufsmount.h> 100 #include <ufs/ufs/ufs_extern.h> 101 102 #include <ufs/lfs/lfs.h> 103 #include <ufs/lfs/lfs_extern.h> 104 105 extern int count_lock_queue __P((void)); 106 extern struct simplelock vnode_free_list_slock; /* XXX */ 107 108 /* 109 * Determine if it's OK to start a partial in this segment, or if we need 110 * to go on to a new segment. 111 */ 112 #define LFS_PARTIAL_FITS(fs) \ 113 ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \ 114 1 << (fs)->lfs_fsbtodb) 115 116 void lfs_callback __P((struct buf *)); 117 int lfs_gather __P((struct lfs *, struct segment *, 118 struct vnode *, int (*) __P((struct lfs *, struct buf *)))); 119 int lfs_gatherblock __P((struct segment *, struct buf *, int *)); 120 void lfs_iset __P((struct inode *, ufs_daddr_t, time_t)); 121 int lfs_match_fake __P((struct lfs *, struct buf *)); 122 int lfs_match_data __P((struct lfs *, struct buf *)); 123 int lfs_match_dindir __P((struct lfs *, struct buf *)); 124 int lfs_match_indir __P((struct lfs *, struct buf *)); 125 int lfs_match_tindir __P((struct lfs *, struct buf *)); 126 void lfs_newseg __P((struct lfs *)); 127 void lfs_shellsort __P((struct buf **, ufs_daddr_t *, int)); 128 void lfs_supercallback __P((struct buf *)); 129 void lfs_updatemeta __P((struct segment *)); 130 int lfs_vref __P((struct vnode *)); 131 void lfs_vunref __P((struct vnode *)); 132 void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *)); 133 int lfs_writeinode __P((struct lfs *, struct segment *, struct inode *)); 134 int lfs_writeseg __P((struct lfs *, struct segment *)); 135 void lfs_writesuper __P((struct lfs *, daddr_t)); 136 int lfs_writevnodes __P((struct lfs *fs, struct mount *mp, 137 struct segment *sp, int dirops)); 138 139 int lfs_allclean_wakeup; /* Cleaner wakeup address. */ 140 int lfs_writeindir = 1; /* whether to flush indir on non-ckp */ 141 int lfs_clean_vnhead = 0; /* Allow freeing to head of vn list */ 142 int lfs_dirvcount = 0; /* # active dirops */ 143 144 /* Statistics Counters */ 145 int lfs_dostats = 1; 146 struct lfs_stats lfs_stats; 147 148 extern int locked_queue_count; 149 extern long locked_queue_bytes; 150 151 /* op values to lfs_writevnodes */ 152 #define VN_REG 0 153 #define VN_DIROP 1 154 #define VN_EMPTY 2 155 #define VN_CLEAN 3 156 157 #define LFS_MAX_ACTIVE 10 158 159 /* 160 * XXX KS - Set modification time on the Ifile, so the cleaner can 161 * read the fs mod time off of it. We don't set IN_UPDATE here, 162 * since we don't really need this to be flushed to disk (and in any 163 * case that wouldn't happen to the Ifile until we checkpoint). 164 */ 165 void 166 lfs_imtime(fs) 167 struct lfs *fs; 168 { 169 struct timespec ts; 170 struct inode *ip; 171 172 TIMEVAL_TO_TIMESPEC(&time, &ts); 173 ip = VTOI(fs->lfs_ivnode); 174 ip->i_ffs_mtime = ts.tv_sec; 175 ip->i_ffs_mtimensec = ts.tv_nsec; 176 } 177 178 /* 179 * Ifile and meta data blocks are not marked busy, so segment writes MUST be 180 * single threaded. Currently, there are two paths into lfs_segwrite, sync() 181 * and getnewbuf(). They both mark the file system busy. Lfs_vflush() 182 * explicitly marks the file system busy. So lfs_segwrite is safe. I think. 183 */ 184 185 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp) 186 #define IS_FLUSHING(fs,vp) ((fs)->lfs_flushvp == (vp)) 187 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL 188 189 int 190 lfs_vflush(vp) 191 struct vnode *vp; 192 { 193 struct inode *ip; 194 struct lfs *fs; 195 struct segment *sp; 196 struct buf *bp, *nbp, *tbp, *tnbp; 197 int error, s; 198 199 ip = VTOI(vp); 200 fs = VFSTOUFS(vp->v_mount)->um_lfs; 201 202 if(ip->i_flag & IN_CLEANING) { 203 #ifdef DEBUG_LFS 204 ivndebug(vp,"vflush/in_cleaning"); 205 #endif 206 LFS_CLR_UINO(ip, IN_CLEANING); 207 LFS_SET_UINO(ip, IN_MODIFIED); 208 209 /* 210 * Toss any cleaning buffers that have real counterparts 211 * to avoid losing new data 212 */ 213 s = splbio(); 214 for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) { 215 nbp = bp->b_vnbufs.le_next; 216 if(bp->b_flags & B_CALL) { 217 for(tbp=vp->v_dirtyblkhd.lh_first; tbp; 218 tbp=tnbp) 219 { 220 tnbp = tbp->b_vnbufs.le_next; 221 if(tbp->b_vp == bp->b_vp 222 && tbp->b_lblkno == bp->b_lblkno 223 && tbp != bp) 224 { 225 fs->lfs_avail += btodb(bp->b_bcount); 226 wakeup(&fs->lfs_avail); 227 lfs_freebuf(bp); 228 } 229 } 230 } 231 } 232 splx(s); 233 } 234 235 /* If the node is being written, wait until that is done */ 236 if(WRITEINPROG(vp)) { 237 #ifdef DEBUG_LFS 238 ivndebug(vp,"vflush/writeinprog"); 239 #endif 240 tsleep(vp, PRIBIO+1, "lfs_vw", 0); 241 } 242 243 /* Protect against VXLOCK deadlock in vinvalbuf() */ 244 lfs_seglock(fs, SEGM_SYNC); 245 246 /* If we're supposed to flush a freed inode, just toss it */ 247 /* XXX - seglock, so these buffers can't be gathered, right? */ 248 if(ip->i_ffs_mode == 0) { 249 printf("lfs_vflush: ino %d is freed, not flushing\n", 250 ip->i_number); 251 s = splbio(); 252 for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) { 253 nbp = bp->b_vnbufs.le_next; 254 if (bp->b_flags & B_DELWRI) { /* XXX always true? */ 255 fs->lfs_avail += btodb(bp->b_bcount); 256 wakeup(&fs->lfs_avail); 257 } 258 /* Copied from lfs_writeseg */ 259 if (bp->b_flags & B_CALL) { 260 /* if B_CALL, it was created with newbuf */ 261 lfs_freebuf(bp); 262 } else { 263 bremfree(bp); 264 LFS_UNLOCK_BUF(bp); 265 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | 266 B_GATHERED); 267 bp->b_flags |= B_DONE; 268 reassignbuf(bp, vp); 269 brelse(bp); 270 } 271 } 272 splx(s); 273 LFS_CLR_UINO(ip, IN_CLEANING); 274 LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED); 275 ip->i_flag &= ~IN_ALLMOD; 276 printf("lfs_vflush: done not flushing ino %d\n", 277 ip->i_number); 278 lfs_segunlock(fs); 279 return 0; 280 } 281 282 SET_FLUSHING(fs,vp); 283 if (fs->lfs_nactive > LFS_MAX_ACTIVE) { 284 error = lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP); 285 CLR_FLUSHING(fs,vp); 286 lfs_segunlock(fs); 287 return error; 288 } 289 sp = fs->lfs_sp; 290 291 if (vp->v_dirtyblkhd.lh_first == NULL) { 292 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY); 293 } else if((ip->i_flag & IN_CLEANING) && 294 (fs->lfs_sp->seg_flags & SEGM_CLEAN)) { 295 #ifdef DEBUG_LFS 296 ivndebug(vp,"vflush/clean"); 297 #endif 298 lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN); 299 } 300 else if(lfs_dostats) { 301 if(vp->v_dirtyblkhd.lh_first || (VTOI(vp)->i_flag & IN_ALLMOD)) 302 ++lfs_stats.vflush_invoked; 303 #ifdef DEBUG_LFS 304 ivndebug(vp,"vflush"); 305 #endif 306 } 307 308 #ifdef DIAGNOSTIC 309 /* XXX KS This actually can happen right now, though it shouldn't(?) */ 310 if(vp->v_flag & VDIROP) { 311 printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n"); 312 /* panic("VDIROP being flushed...this can\'t happen"); */ 313 } 314 if(vp->v_usecount<0) { 315 printf("usecount=%d\n",vp->v_usecount); 316 panic("lfs_vflush: usecount<0"); 317 } 318 #endif 319 320 do { 321 do { 322 if (vp->v_dirtyblkhd.lh_first != NULL) 323 lfs_writefile(fs, sp, vp); 324 } while (lfs_writeinode(fs, sp, ip)); 325 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM); 326 327 if(lfs_dostats) { 328 ++lfs_stats.nwrites; 329 if (sp->seg_flags & SEGM_SYNC) 330 ++lfs_stats.nsync_writes; 331 if (sp->seg_flags & SEGM_CKP) 332 ++lfs_stats.ncheckpoints; 333 } 334 lfs_segunlock(fs); 335 336 CLR_FLUSHING(fs,vp); 337 return (0); 338 } 339 340 #ifdef DEBUG_LFS_VERBOSE 341 # 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) 342 #else 343 # define vndebug(vp,str) 344 #endif 345 346 int 347 lfs_writevnodes(fs, mp, sp, op) 348 struct lfs *fs; 349 struct mount *mp; 350 struct segment *sp; 351 int op; 352 { 353 struct inode *ip; 354 struct vnode *vp; 355 int inodes_written=0, only_cleaning; 356 int needs_unlock; 357 358 #ifndef LFS_NO_BACKVP_HACK 359 /* BEGIN HACK */ 360 #define VN_OFFSET (((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp) 361 #define BACK_VP(VP) ((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET)) 362 #define BEG_OF_VLIST ((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET)) 363 364 /* Find last vnode. */ 365 loop: for (vp = mp->mnt_vnodelist.lh_first; 366 vp && vp->v_mntvnodes.le_next != NULL; 367 vp = vp->v_mntvnodes.le_next); 368 for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) { 369 #else 370 loop: 371 for (vp = mp->mnt_vnodelist.lh_first; 372 vp != NULL; 373 vp = vp->v_mntvnodes.le_next) { 374 #endif 375 /* 376 * If the vnode that we are about to sync is no longer 377 * associated with this mount point, start over. 378 */ 379 if (vp->v_mount != mp) { 380 printf("lfs_writevnodes: starting over\n"); 381 goto loop; 382 } 383 384 ip = VTOI(vp); 385 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) || 386 (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) { 387 vndebug(vp,"dirop"); 388 continue; 389 } 390 391 if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) { 392 vndebug(vp,"empty"); 393 continue; 394 } 395 396 if (vp->v_type == VNON) { 397 continue; 398 } 399 400 if(op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM 401 && vp != fs->lfs_flushvp 402 && !(ip->i_flag & IN_CLEANING)) { 403 vndebug(vp,"cleaning"); 404 continue; 405 } 406 407 if (lfs_vref(vp)) { 408 vndebug(vp,"vref"); 409 continue; 410 } 411 412 needs_unlock = 0; 413 if (VOP_ISLOCKED(vp)) { 414 if (vp != fs->lfs_ivnode && 415 vp->v_lock.lk_lockholder != curproc->p_pid) { 416 #ifdef DEBUG_LFS 417 printf("lfs_writevnodes: not writing ino %d," 418 " locked by pid %d\n", 419 VTOI(vp)->i_number, 420 vp->v_lock.lk_lockholder); 421 #endif 422 lfs_vunref(vp); 423 continue; 424 } 425 } else if (vp != fs->lfs_ivnode) { 426 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 427 needs_unlock = 1; 428 } 429 430 only_cleaning = 0; 431 /* 432 * Write the inode/file if dirty and it's not the IFILE. 433 */ 434 if ((ip->i_flag & IN_ALLMOD) || 435 (vp->v_dirtyblkhd.lh_first != NULL)) 436 { 437 only_cleaning = ((ip->i_flag & IN_ALLMOD)==IN_CLEANING); 438 439 if(ip->i_number != LFS_IFILE_INUM 440 && vp->v_dirtyblkhd.lh_first != NULL) 441 { 442 lfs_writefile(fs, sp, vp); 443 } 444 if(vp->v_dirtyblkhd.lh_first != NULL) { 445 if(WRITEINPROG(vp)) { 446 #ifdef DEBUG_LFS 447 ivndebug(vp,"writevnodes/write2"); 448 #endif 449 } else if(!(ip->i_flag & IN_ALLMOD)) { 450 #ifdef DEBUG_LFS 451 printf("<%d>",ip->i_number); 452 #endif 453 LFS_SET_UINO(ip, IN_MODIFIED); 454 } 455 } 456 (void) lfs_writeinode(fs, sp, ip); 457 inodes_written++; 458 } 459 460 if (needs_unlock) 461 VOP_UNLOCK(vp, 0); 462 463 if (lfs_clean_vnhead && only_cleaning) 464 lfs_vunref_head(vp); 465 else 466 lfs_vunref(vp); 467 } 468 return inodes_written; 469 } 470 471 int 472 lfs_segwrite(mp, flags) 473 struct mount *mp; 474 int flags; /* Do a checkpoint. */ 475 { 476 struct buf *bp; 477 struct inode *ip; 478 struct lfs *fs; 479 struct segment *sp; 480 struct vnode *vp; 481 SEGUSE *segusep; 482 ufs_daddr_t ibno; 483 int do_ckp, did_ckp, error, i; 484 int writer_set = 0; 485 int dirty; 486 487 fs = VFSTOUFS(mp)->um_lfs; 488 489 if (fs->lfs_ronly) 490 return EROFS; 491 492 lfs_imtime(fs); 493 494 /* printf("lfs_segwrite: ifile flags are 0x%lx\n", 495 (long)(VTOI(fs->lfs_ivnode)->i_flag)); */ 496 497 #if 0 498 /* 499 * If we are not the cleaner, and there is no space available, 500 * wait until cleaner writes. 501 */ 502 if(!(flags & SEGM_CLEAN) && !(fs->lfs_seglock && fs->lfs_sp && 503 (fs->lfs_sp->seg_flags & SEGM_CLEAN))) 504 { 505 while (fs->lfs_avail <= 0) { 506 LFS_CLEANERINFO(cip, fs, bp); 507 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0); 508 509 wakeup(&lfs_allclean_wakeup); 510 wakeup(&fs->lfs_nextseg); 511 error = tsleep(&fs->lfs_avail, PRIBIO + 1, "lfs_av2", 512 0); 513 if (error) { 514 return (error); 515 } 516 } 517 } 518 #endif 519 /* 520 * Allocate a segment structure and enough space to hold pointers to 521 * the maximum possible number of buffers which can be described in a 522 * single summary block. 523 */ 524 do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE; 525 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0)); 526 sp = fs->lfs_sp; 527 528 /* 529 * If lfs_flushvp is non-NULL, we are called from lfs_vflush, 530 * in which case we have to flush *all* buffers off of this vnode. 531 * We don't care about other nodes, but write any non-dirop nodes 532 * anyway in anticipation of another getnewvnode(). 533 * 534 * If we're cleaning we only write cleaning and ifile blocks, and 535 * no dirops, since otherwise we'd risk corruption in a crash. 536 */ 537 if(sp->seg_flags & SEGM_CLEAN) 538 lfs_writevnodes(fs, mp, sp, VN_CLEAN); 539 else { 540 lfs_writevnodes(fs, mp, sp, VN_REG); 541 if(!fs->lfs_dirops || !fs->lfs_flushvp) { 542 while(fs->lfs_dirops) 543 if((error = tsleep(&fs->lfs_writer, PRIBIO + 1, 544 "lfs writer", 0))) 545 { 546 free(sp->bpp, M_SEGMENT); 547 free(sp, M_SEGMENT); 548 return (error); 549 } 550 fs->lfs_writer++; 551 writer_set=1; 552 lfs_writevnodes(fs, mp, sp, VN_DIROP); 553 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT); 554 } 555 } 556 557 /* 558 * If we are doing a checkpoint, mark everything since the 559 * last checkpoint as no longer ACTIVE. 560 */ 561 if (do_ckp) { 562 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz; 563 --ibno >= fs->lfs_cleansz; ) { 564 dirty = 0; 565 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp)) 566 567 panic("lfs_segwrite: ifile read"); 568 segusep = (SEGUSE *)bp->b_data; 569 for (i = fs->lfs_sepb; i--; segusep++) { 570 if (segusep->su_flags & SEGUSE_ACTIVE) { 571 segusep->su_flags &= ~SEGUSE_ACTIVE; 572 ++dirty; 573 } 574 } 575 576 /* But the current segment is still ACTIVE */ 577 segusep = (SEGUSE *)bp->b_data; 578 if (datosn(fs, fs->lfs_curseg) / fs->lfs_sepb == 579 (ibno-fs->lfs_cleansz)) { 580 segusep[datosn(fs, fs->lfs_curseg) % 581 fs->lfs_sepb].su_flags |= SEGUSE_ACTIVE; 582 --dirty; 583 } 584 if (dirty) 585 error = VOP_BWRITE(bp); /* Ifile */ 586 else 587 brelse(bp); 588 } 589 } 590 591 did_ckp = 0; 592 if (do_ckp || fs->lfs_doifile) { 593 do { 594 vp = fs->lfs_ivnode; 595 596 vget(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY); 597 598 ip = VTOI(vp); 599 if (vp->v_dirtyblkhd.lh_first != NULL) 600 lfs_writefile(fs, sp, vp); 601 if (ip->i_flag & IN_ALLMOD) 602 ++did_ckp; 603 (void) lfs_writeinode(fs, sp, ip); 604 605 vput(vp); 606 } while (lfs_writeseg(fs, sp) && do_ckp); 607 608 /* The ifile should now be all clear */ 609 LFS_CLR_UINO(ip, IN_ALLMOD); 610 } else { 611 (void) lfs_writeseg(fs, sp); 612 } 613 614 /* 615 * If the I/O count is non-zero, sleep until it reaches zero. 616 * At the moment, the user's process hangs around so we can 617 * sleep. 618 */ 619 fs->lfs_doifile = 0; 620 if(writer_set && --fs->lfs_writer==0) 621 wakeup(&fs->lfs_dirops); 622 623 /* 624 * If we didn't write the Ifile, we didn't really do anything. 625 * That means that (1) there is a checkpoint on disk and (2) 626 * nothing has changed since it was written. 627 * 628 * Take the flags off of the segment so that lfs_segunlock 629 * doesn't have to write the superblock either. 630 */ 631 if (did_ckp == 0) { 632 sp->seg_flags &= ~(SEGM_SYNC|SEGM_CKP); 633 /* if(do_ckp) printf("lfs_segwrite: no checkpoint\n"); */ 634 } 635 636 if(lfs_dostats) { 637 ++lfs_stats.nwrites; 638 if (sp->seg_flags & SEGM_SYNC) 639 ++lfs_stats.nsync_writes; 640 if (sp->seg_flags & SEGM_CKP) 641 ++lfs_stats.ncheckpoints; 642 } 643 lfs_segunlock(fs); 644 return (0); 645 } 646 647 /* 648 * Write the dirty blocks associated with a vnode. 649 */ 650 void 651 lfs_writefile(fs, sp, vp) 652 struct lfs *fs; 653 struct segment *sp; 654 struct vnode *vp; 655 { 656 struct buf *bp; 657 struct finfo *fip; 658 IFILE *ifp; 659 660 661 if (sp->seg_bytes_left < fs->lfs_bsize || 662 sp->sum_bytes_left < sizeof(struct finfo)) 663 (void) lfs_writeseg(fs, sp); 664 665 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t); 666 ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 667 668 if(vp->v_flag & VDIROP) 669 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT); 670 671 fip = sp->fip; 672 fip->fi_nblocks = 0; 673 fip->fi_ino = VTOI(vp)->i_number; 674 LFS_IENTRY(ifp, fs, fip->fi_ino, bp); 675 fip->fi_version = ifp->if_version; 676 brelse(bp); 677 678 if(sp->seg_flags & SEGM_CLEAN) 679 { 680 lfs_gather(fs, sp, vp, lfs_match_fake); 681 /* 682 * For a file being flushed, we need to write *all* blocks. 683 * This means writing the cleaning blocks first, and then 684 * immediately following with any non-cleaning blocks. 685 * The same is true of the Ifile since checkpoints assume 686 * that all valid Ifile blocks are written. 687 */ 688 if(IS_FLUSHING(fs,vp) || VTOI(vp)->i_number == LFS_IFILE_INUM) 689 lfs_gather(fs, sp, vp, lfs_match_data); 690 } else 691 lfs_gather(fs, sp, vp, lfs_match_data); 692 693 /* 694 * It may not be necessary to write the meta-data blocks at this point, 695 * as the roll-forward recovery code should be able to reconstruct the 696 * list. 697 * 698 * We have to write them anyway, though, under two conditions: (1) the 699 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are 700 * checkpointing. 701 */ 702 if(lfs_writeindir 703 || IS_FLUSHING(fs,vp) 704 || (sp->seg_flags & SEGM_CKP)) 705 { 706 lfs_gather(fs, sp, vp, lfs_match_indir); 707 lfs_gather(fs, sp, vp, lfs_match_dindir); 708 lfs_gather(fs, sp, vp, lfs_match_tindir); 709 } 710 fip = sp->fip; 711 if (fip->fi_nblocks != 0) { 712 sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) + 713 sizeof(ufs_daddr_t) * (fip->fi_nblocks-1)); 714 sp->start_lbp = &sp->fip->fi_blocks[0]; 715 } else { 716 sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t); 717 --((SEGSUM *)(sp->segsum))->ss_nfinfo; 718 } 719 } 720 721 int 722 lfs_writeinode(fs, sp, ip) 723 struct lfs *fs; 724 struct segment *sp; 725 struct inode *ip; 726 { 727 struct buf *bp, *ibp; 728 struct dinode *cdp; 729 IFILE *ifp; 730 SEGUSE *sup; 731 ufs_daddr_t daddr; 732 daddr_t *daddrp; 733 ino_t ino; 734 int error, i, ndx; 735 int redo_ifile = 0; 736 struct timespec ts; 737 int gotblk=0; 738 739 if (!(ip->i_flag & IN_ALLMOD)) 740 return(0); 741 742 /* Allocate a new inode block if necessary. */ 743 if ((ip->i_number != LFS_IFILE_INUM || sp->idp==NULL) && sp->ibp == NULL) { 744 /* Allocate a new segment if necessary. */ 745 if (sp->seg_bytes_left < fs->lfs_bsize || 746 sp->sum_bytes_left < sizeof(ufs_daddr_t)) 747 (void) lfs_writeseg(fs, sp); 748 749 /* Get next inode block. */ 750 daddr = fs->lfs_offset; 751 fs->lfs_offset += fsbtodb(fs, 1); 752 sp->ibp = *sp->cbpp++ = 753 getblk(VTOI(fs->lfs_ivnode)->i_devvp, daddr, fs->lfs_bsize, 0, 0); 754 gotblk++; 755 756 /* Zero out inode numbers */ 757 for (i = 0; i < INOPB(fs); ++i) 758 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0; 759 760 ++sp->start_bpp; 761 fs->lfs_avail -= fsbtodb(fs, 1); 762 /* Set remaining space counters. */ 763 sp->seg_bytes_left -= fs->lfs_bsize; 764 sp->sum_bytes_left -= sizeof(ufs_daddr_t); 765 ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) - 766 sp->ninodes / INOPB(fs) - 1; 767 ((ufs_daddr_t *)(sp->segsum))[ndx] = daddr; 768 } 769 770 /* Update the inode times and copy the inode onto the inode page. */ 771 TIMEVAL_TO_TIMESPEC(&time, &ts); 772 LFS_ITIMES(ip, &ts, &ts, &ts); 773 774 /* 775 * If this is the Ifile, and we've already written the Ifile in this 776 * partial segment, just overwrite it (it's not on disk yet) and 777 * continue. 778 * 779 * XXX we know that the bp that we get the second time around has 780 * already been gathered. 781 */ 782 if(ip->i_number == LFS_IFILE_INUM && sp->idp) { 783 *(sp->idp) = ip->i_din.ffs_din; 784 return 0; 785 } 786 787 bp = sp->ibp; 788 cdp = ((struct dinode *)bp->b_data) + (sp->ninodes % INOPB(fs)); 789 *cdp = ip->i_din.ffs_din; 790 791 /* 792 * If we are cleaning, ensure that we don't write UNWRITTEN disk 793 * addresses to disk. 794 */ 795 if (ip->i_lfs_effnblks != ip->i_ffs_blocks) { 796 #ifdef DEBUG_LFS 797 printf("lfs_writeinode: cleansing ino %d (%d != %d)\n", 798 ip->i_number, ip->i_lfs_effnblks, ip->i_ffs_blocks); 799 #endif 800 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR; 801 daddrp++) { 802 if (*daddrp == UNWRITTEN) { 803 #ifdef DEBUG_LFS 804 printf("lfs_writeinode: wiping UNWRITTEN\n"); 805 #endif 806 *daddrp = 0; 807 } 808 } 809 } 810 811 if(ip->i_flag & IN_CLEANING) 812 LFS_CLR_UINO(ip, IN_CLEANING); 813 else { 814 /* XXX IN_ALLMOD */ 815 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE | 816 IN_UPDATE); 817 if (ip->i_lfs_effnblks == ip->i_ffs_blocks) 818 LFS_CLR_UINO(ip, IN_MODIFIED); 819 #ifdef DEBUG_LFS 820 else 821 printf("lfs_writeinode: ino %d: real blks=%d, " 822 "eff=%d\n", ip->i_number, ip->i_ffs_blocks, 823 ip->i_lfs_effnblks); 824 #endif 825 } 826 827 if(ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */ 828 sp->idp = ((struct dinode *)bp->b_data) + 829 (sp->ninodes % INOPB(fs)); 830 if(gotblk) { 831 LFS_LOCK_BUF(bp); 832 brelse(bp); 833 } 834 835 /* Increment inode count in segment summary block. */ 836 ++((SEGSUM *)(sp->segsum))->ss_ninos; 837 838 /* If this page is full, set flag to allocate a new page. */ 839 if (++sp->ninodes % INOPB(fs) == 0) 840 sp->ibp = NULL; 841 842 /* 843 * If updating the ifile, update the super-block. Update the disk 844 * address and access times for this inode in the ifile. 845 */ 846 ino = ip->i_number; 847 if (ino == LFS_IFILE_INUM) { 848 daddr = fs->lfs_idaddr; 849 fs->lfs_idaddr = bp->b_blkno; 850 } else { 851 LFS_IENTRY(ifp, fs, ino, ibp); 852 daddr = ifp->if_daddr; 853 ifp->if_daddr = bp->b_blkno; 854 #ifdef LFS_DEBUG_NEXTFREE 855 if(ino > 3 && ifp->if_nextfree) { 856 vprint("lfs_writeinode",ITOV(ip)); 857 printf("lfs_writeinode: updating free ino %d\n", 858 ip->i_number); 859 } 860 #endif 861 error = VOP_BWRITE(ibp); /* Ifile */ 862 } 863 864 /* 865 * Account the inode: it no longer belongs to its former segment, 866 * though it will not belong to the new segment until that segment 867 * is actually written. 868 */ 869 #ifdef DEBUG 870 /* 871 * The inode's last address should not be in the current partial 872 * segment, except under exceptional circumstances (lfs_writevnodes 873 * had to start over, and in the meantime more blocks were written 874 * to a vnode). Although the previous inode won't be accounted in 875 * su_nbytes until lfs_writeseg, this shouldn't be a problem as we 876 * have more data blocks in the current partial segment. 877 */ 878 if (daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno) 879 printf("lfs_writeinode: last inode addr in current pseg " 880 "(ino %d daddr 0x%x)\n", ino, daddr); 881 #endif 882 if (daddr != LFS_UNUSED_DADDR) { 883 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 884 #ifdef DIAGNOSTIC 885 if (sup->su_nbytes < DINODE_SIZE) { 886 printf("lfs_writeinode: negative bytes " 887 "(segment %d short by %d)\n", 888 datosn(fs, daddr), 889 (int)DINODE_SIZE - sup->su_nbytes); 890 panic("lfs_writeinode: negative bytes"); 891 sup->su_nbytes = DINODE_SIZE; 892 } 893 #endif 894 sup->su_nbytes -= DINODE_SIZE; 895 redo_ifile = 896 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED)); 897 error = VOP_BWRITE(bp); /* Ifile */ 898 } 899 return (redo_ifile); 900 } 901 902 int 903 lfs_gatherblock(sp, bp, sptr) 904 struct segment *sp; 905 struct buf *bp; 906 int *sptr; 907 { 908 struct lfs *fs; 909 int version; 910 911 /* 912 * If full, finish this segment. We may be doing I/O, so 913 * release and reacquire the splbio(). 914 */ 915 #ifdef DIAGNOSTIC 916 if (sp->vp == NULL) 917 panic ("lfs_gatherblock: Null vp in segment"); 918 #endif 919 fs = sp->fs; 920 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) || 921 sp->seg_bytes_left < bp->b_bcount) { 922 if (sptr) 923 splx(*sptr); 924 lfs_updatemeta(sp); 925 926 version = sp->fip->fi_version; 927 (void) lfs_writeseg(fs, sp); 928 929 sp->fip->fi_version = version; 930 sp->fip->fi_ino = VTOI(sp->vp)->i_number; 931 /* Add the current file to the segment summary. */ 932 ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 933 sp->sum_bytes_left -= 934 sizeof(struct finfo) - sizeof(ufs_daddr_t); 935 936 if (sptr) 937 *sptr = splbio(); 938 return(1); 939 } 940 941 #ifdef DEBUG 942 if(bp->b_flags & B_GATHERED) { 943 printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n", 944 sp->fip->fi_ino, bp->b_lblkno); 945 return(0); 946 } 947 #endif 948 /* Insert into the buffer list, update the FINFO block. */ 949 bp->b_flags |= B_GATHERED; 950 *sp->cbpp++ = bp; 951 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno; 952 953 sp->sum_bytes_left -= sizeof(ufs_daddr_t); 954 sp->seg_bytes_left -= bp->b_bcount; 955 return(0); 956 } 957 958 int 959 lfs_gather(fs, sp, vp, match) 960 struct lfs *fs; 961 struct segment *sp; 962 struct vnode *vp; 963 int (*match) __P((struct lfs *, struct buf *)); 964 { 965 struct buf *bp; 966 int s, count=0; 967 968 sp->vp = vp; 969 s = splbio(); 970 971 #ifndef LFS_NO_BACKBUF_HACK 972 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) { 973 #else /* LFS_NO_BACKBUF_HACK */ 974 /* This is a hack to see if ordering the blocks in LFS makes a difference. */ 975 # define BUF_OFFSET (((void *)&bp->b_vnbufs.le_next) - (void *)bp) 976 # define BACK_BUF(BP) ((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET)) 977 # define BEG_OF_LIST ((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET)) 978 /* Find last buffer. */ 979 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL; 980 bp = bp->b_vnbufs.le_next); 981 for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) { 982 #endif /* LFS_NO_BACKBUF_HACK */ 983 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp)) 984 continue; 985 if(vp->v_type == VBLK) { 986 /* For block devices, just write the blocks. */ 987 /* XXX Do we really need to even do this? */ 988 #ifdef DEBUG_LFS 989 if(count==0) 990 printf("BLK("); 991 printf("."); 992 #endif 993 /* Get the block before bwrite, so we don't corrupt the free list */ 994 bp->b_flags |= B_BUSY; 995 bremfree(bp); 996 bwrite(bp); 997 } else { 998 #ifdef DIAGNOSTIC 999 if ((bp->b_flags & (B_CALL|B_INVAL))==B_INVAL) { 1000 printf("lfs_gather: lbn %d is B_INVAL\n", 1001 bp->b_lblkno); 1002 VOP_PRINT(bp->b_vp); 1003 } 1004 if (!(bp->b_flags & B_DELWRI)) 1005 panic("lfs_gather: bp not B_DELWRI"); 1006 if (!(bp->b_flags & B_LOCKED)) { 1007 printf("lfs_gather: lbn %d blk %d" 1008 " not B_LOCKED\n", bp->b_lblkno, 1009 bp->b_blkno); 1010 VOP_PRINT(bp->b_vp); 1011 panic("lfs_gather: bp not B_LOCKED"); 1012 } 1013 #endif 1014 if (lfs_gatherblock(sp, bp, &s)) { 1015 goto loop; 1016 } 1017 } 1018 count++; 1019 } 1020 splx(s); 1021 #ifdef DEBUG_LFS 1022 if(vp->v_type == VBLK && count) 1023 printf(")\n"); 1024 #endif 1025 lfs_updatemeta(sp); 1026 sp->vp = NULL; 1027 return count; 1028 } 1029 1030 /* 1031 * Update the metadata that points to the blocks listed in the FINFO 1032 * array. 1033 */ 1034 void 1035 lfs_updatemeta(sp) 1036 struct segment *sp; 1037 { 1038 SEGUSE *sup; 1039 struct buf *bp; 1040 struct lfs *fs; 1041 struct vnode *vp; 1042 struct indir a[NIADDR + 2], *ap; 1043 struct inode *ip; 1044 ufs_daddr_t daddr, lbn, off; 1045 daddr_t ooff; 1046 int error, i, nblocks, num; 1047 int bb; 1048 1049 vp = sp->vp; 1050 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp; 1051 if (nblocks < 0) 1052 panic("This is a bad thing\n"); 1053 if (vp == NULL || nblocks == 0) 1054 return; 1055 1056 /* Sort the blocks. */ 1057 /* 1058 * XXX KS - We have to sort even if the blocks come from the 1059 * cleaner, because there might be other pending blocks on the 1060 * same inode...and if we don't sort, and there are fragments 1061 * present, blocks may be written in the wrong place. 1062 */ 1063 /* if (!(sp->seg_flags & SEGM_CLEAN)) */ 1064 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks); 1065 1066 /* 1067 * Record the length of the last block in case it's a fragment. 1068 * If there are indirect blocks present, they sort last. An 1069 * indirect block will be lfs_bsize and its presence indicates 1070 * that you cannot have fragments. 1071 */ 1072 sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount; 1073 1074 /* 1075 * Assign disk addresses, and update references to the logical 1076 * block and the segment usage information. 1077 */ 1078 fs = sp->fs; 1079 for (i = nblocks; i--; ++sp->start_bpp) { 1080 lbn = *sp->start_lbp++; 1081 1082 (*sp->start_bpp)->b_blkno = off = fs->lfs_offset; 1083 if((*sp->start_bpp)->b_blkno == (*sp->start_bpp)->b_lblkno) { 1084 printf("lfs_updatemeta: ino %d blk %d" 1085 " has same lbn and daddr\n", 1086 VTOI(vp)->i_number, off); 1087 } 1088 bb = fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount)); 1089 fs->lfs_offset += bb; 1090 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL); 1091 if (error) 1092 panic("lfs_updatemeta: ufs_bmaparray %d", error); 1093 ip = VTOI(vp); 1094 switch (num) { 1095 case 0: 1096 ooff = ip->i_ffs_db[lbn]; 1097 #ifdef DEBUG 1098 if (ooff == 0) { 1099 printf("lfs_updatemeta[1]: warning: writing " 1100 "ino %d lbn %d at 0x%x, was 0x0\n", 1101 ip->i_number, lbn, off); 1102 } 1103 #endif 1104 if (ooff == UNWRITTEN) 1105 ip->i_ffs_blocks += bb; 1106 ip->i_ffs_db[lbn] = off; 1107 break; 1108 case 1: 1109 ooff = ip->i_ffs_ib[a[0].in_off]; 1110 #ifdef DEBUG 1111 if (ooff == 0) { 1112 printf("lfs_updatemeta[2]: warning: writing " 1113 "ino %d lbn %d at 0x%x, was 0x0\n", 1114 ip->i_number, lbn, off); 1115 } 1116 #endif 1117 if (ooff == UNWRITTEN) 1118 ip->i_ffs_blocks += bb; 1119 ip->i_ffs_ib[a[0].in_off] = off; 1120 break; 1121 default: 1122 ap = &a[num - 1]; 1123 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 1124 panic("lfs_updatemeta: bread bno %d", 1125 ap->in_lbn); 1126 1127 ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off]; 1128 #if DEBUG 1129 if (ooff == 0) { 1130 printf("lfs_updatemeta[3]: warning: writing " 1131 "ino %d lbn %d at 0x%x, was 0x0\n", 1132 ip->i_number, lbn, off); 1133 } 1134 #endif 1135 if (ooff == UNWRITTEN) 1136 ip->i_ffs_blocks += bb; 1137 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = off; 1138 (void) VOP_BWRITE(bp); 1139 } 1140 #ifdef DEBUG 1141 if (daddr >= fs->lfs_lastpseg && daddr <= off) { 1142 printf("lfs_updatemeta: ino %d, lbn %d, addr = %x " 1143 "in same pseg\n", VTOI(sp->vp)->i_number, 1144 (*sp->start_bpp)->b_lblkno, daddr); 1145 } 1146 #endif 1147 /* Update segment usage information. */ 1148 if (daddr > 0) { 1149 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 1150 #ifdef DIAGNOSTIC 1151 if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) { 1152 /* XXX -- Change to a panic. */ 1153 printf("lfs_updatemeta: negative bytes " 1154 "(segment %d short by %ld)\n", 1155 datosn(fs, daddr), 1156 (*sp->start_bpp)->b_bcount - 1157 sup->su_nbytes); 1158 printf("lfs_updatemeta: ino %d, lbn %d, " 1159 "addr = %x\n", VTOI(sp->vp)->i_number, 1160 (*sp->start_bpp)->b_lblkno, daddr); 1161 panic("lfs_updatemeta: negative bytes"); 1162 sup->su_nbytes = (*sp->start_bpp)->b_bcount; 1163 } 1164 #endif 1165 sup->su_nbytes -= (*sp->start_bpp)->b_bcount; 1166 error = VOP_BWRITE(bp); /* Ifile */ 1167 } 1168 } 1169 } 1170 1171 /* 1172 * Start a new segment. 1173 */ 1174 int 1175 lfs_initseg(fs) 1176 struct lfs *fs; 1177 { 1178 struct segment *sp; 1179 SEGUSE *sup; 1180 SEGSUM *ssp; 1181 struct buf *bp; 1182 int repeat; 1183 1184 sp = fs->lfs_sp; 1185 1186 repeat = 0; 1187 /* Advance to the next segment. */ 1188 if (!LFS_PARTIAL_FITS(fs)) { 1189 /* lfs_avail eats the remaining space */ 1190 fs->lfs_avail -= fs->lfs_dbpseg - (fs->lfs_offset - 1191 fs->lfs_curseg); 1192 /* Wake up any cleaning procs waiting on this file system. */ 1193 wakeup(&lfs_allclean_wakeup); 1194 wakeup(&fs->lfs_nextseg); 1195 lfs_newseg(fs); 1196 repeat = 1; 1197 fs->lfs_offset = fs->lfs_curseg; 1198 sp->seg_number = datosn(fs, fs->lfs_curseg); 1199 sp->seg_bytes_left = dbtob(fs->lfs_dbpseg); 1200 /* 1201 * If the segment contains a superblock, update the offset 1202 * and summary address to skip over it. 1203 */ 1204 LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 1205 if (sup->su_flags & SEGUSE_SUPERBLOCK) { 1206 fs->lfs_offset += btodb(LFS_SBPAD); 1207 sp->seg_bytes_left -= LFS_SBPAD; 1208 } 1209 brelse(bp); 1210 } else { 1211 sp->seg_number = datosn(fs, fs->lfs_curseg); 1212 sp->seg_bytes_left = dbtob(fs->lfs_dbpseg - 1213 (fs->lfs_offset - fs->lfs_curseg)); 1214 } 1215 fs->lfs_lastpseg = fs->lfs_offset; 1216 1217 sp->fs = fs; 1218 sp->ibp = NULL; 1219 sp->idp = NULL; 1220 sp->ninodes = 0; 1221 1222 /* Get a new buffer for SEGSUM and enter it into the buffer list. */ 1223 sp->cbpp = sp->bpp; 1224 *sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, 1225 fs->lfs_offset, LFS_SUMMARY_SIZE); 1226 sp->segsum = (*sp->cbpp)->b_data; 1227 bzero(sp->segsum, LFS_SUMMARY_SIZE); 1228 sp->start_bpp = ++sp->cbpp; 1229 fs->lfs_offset += btodb(LFS_SUMMARY_SIZE); 1230 1231 /* Set point to SEGSUM, initialize it. */ 1232 ssp = sp->segsum; 1233 ssp->ss_next = fs->lfs_nextseg; 1234 ssp->ss_nfinfo = ssp->ss_ninos = 0; 1235 ssp->ss_magic = SS_MAGIC; 1236 1237 /* Set pointer to first FINFO, initialize it. */ 1238 sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM)); 1239 sp->fip->fi_nblocks = 0; 1240 sp->start_lbp = &sp->fip->fi_blocks[0]; 1241 sp->fip->fi_lastlength = 0; 1242 1243 sp->seg_bytes_left -= LFS_SUMMARY_SIZE; 1244 sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM); 1245 1246 return(repeat); 1247 } 1248 1249 /* 1250 * Return the next segment to write. 1251 */ 1252 void 1253 lfs_newseg(fs) 1254 struct lfs *fs; 1255 { 1256 CLEANERINFO *cip; 1257 SEGUSE *sup; 1258 struct buf *bp; 1259 int curseg, isdirty, sn; 1260 1261 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp); 1262 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE; 1263 sup->su_nbytes = 0; 1264 sup->su_nsums = 0; 1265 sup->su_ninos = 0; 1266 (void) VOP_BWRITE(bp); /* Ifile */ 1267 1268 LFS_CLEANERINFO(cip, fs, bp); 1269 --cip->clean; 1270 ++cip->dirty; 1271 fs->lfs_nclean = cip->clean; 1272 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1); 1273 1274 fs->lfs_lastseg = fs->lfs_curseg; 1275 fs->lfs_curseg = fs->lfs_nextseg; 1276 for (sn = curseg = datosn(fs, fs->lfs_curseg);;) { 1277 sn = (sn + 1) % fs->lfs_nseg; 1278 if (sn == curseg) 1279 panic("lfs_nextseg: no clean segments"); 1280 LFS_SEGENTRY(sup, fs, sn, bp); 1281 isdirty = sup->su_flags & SEGUSE_DIRTY; 1282 brelse(bp); 1283 if (!isdirty) 1284 break; 1285 } 1286 1287 ++fs->lfs_nactive; 1288 fs->lfs_nextseg = sntoda(fs, sn); 1289 if(lfs_dostats) { 1290 ++lfs_stats.segsused; 1291 } 1292 } 1293 1294 int 1295 lfs_writeseg(fs, sp) 1296 struct lfs *fs; 1297 struct segment *sp; 1298 { 1299 struct buf **bpp, *bp, *cbp, *newbp; 1300 SEGUSE *sup; 1301 SEGSUM *ssp; 1302 dev_t i_dev; 1303 u_long *datap, *dp; 1304 int do_again, i, nblocks, s; 1305 #ifdef LFS_TRACK_IOS 1306 int j; 1307 #endif 1308 int (*strategy)__P((void *)); 1309 struct vop_strategy_args vop_strategy_a; 1310 u_short ninos; 1311 struct vnode *devvp; 1312 char *p; 1313 struct vnode *vn; 1314 struct inode *ip; 1315 daddr_t *daddrp; 1316 int changed; 1317 #if defined(DEBUG) && defined(LFS_PROPELLER) 1318 static int propeller; 1319 char propstring[4] = "-\\|/"; 1320 1321 printf("%c\b",propstring[propeller++]); 1322 if(propeller==4) 1323 propeller = 0; 1324 #endif 1325 1326 /* 1327 * If there are no buffers other than the segment summary to write 1328 * and it is not a checkpoint, don't do anything. On a checkpoint, 1329 * even if there aren't any buffers, you need to write the superblock. 1330 */ 1331 if ((nblocks = sp->cbpp - sp->bpp) == 1) 1332 return (0); 1333 1334 i_dev = VTOI(fs->lfs_ivnode)->i_dev; 1335 devvp = VTOI(fs->lfs_ivnode)->i_devvp; 1336 1337 /* Update the segment usage information. */ 1338 LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 1339 1340 /* Loop through all blocks, except the segment summary. */ 1341 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) { 1342 if((*bpp)->b_vp != devvp) 1343 sup->su_nbytes += (*bpp)->b_bcount; 1344 } 1345 1346 ssp = (SEGSUM *)sp->segsum; 1347 1348 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs); 1349 sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE; 1350 /* sup->su_nbytes += LFS_SUMMARY_SIZE; */ 1351 sup->su_lastmod = time.tv_sec; 1352 sup->su_ninos += ninos; 1353 ++sup->su_nsums; 1354 fs->lfs_dmeta += (btodb(LFS_SUMMARY_SIZE) + fsbtodb(fs, ninos)); 1355 fs->lfs_avail -= btodb(LFS_SUMMARY_SIZE); 1356 1357 do_again = !(bp->b_flags & B_GATHERED); 1358 (void)VOP_BWRITE(bp); /* Ifile */ 1359 /* 1360 * Mark blocks B_BUSY, to prevent then from being changed between 1361 * the checksum computation and the actual write. 1362 * 1363 * If we are cleaning, check indirect blocks for UNWRITTEN, and if 1364 * there are any, replace them with copies that have UNASSIGNED 1365 * instead. 1366 */ 1367 for (bpp = sp->bpp, i = nblocks - 1; i--;) { 1368 ++bpp; 1369 if((*bpp)->b_flags & B_CALL) 1370 continue; 1371 bp = *bpp; 1372 again: 1373 s = splbio(); 1374 if(bp->b_flags & B_BUSY) { 1375 #ifdef DEBUG 1376 printf("lfs_writeseg: avoiding potential data " 1377 "summary corruption for ino %d, lbn %d\n", 1378 VTOI(bp->b_vp)->i_number, bp->b_lblkno); 1379 #endif 1380 bp->b_flags |= B_WANTED; 1381 tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0); 1382 splx(s); 1383 goto again; 1384 } 1385 bp->b_flags |= B_BUSY; 1386 splx(s); 1387 /* Check and replace indirect block UNWRITTEN bogosity */ 1388 if(bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp && 1389 VTOI(bp->b_vp)->i_ffs_blocks != 1390 VTOI(bp->b_vp)->i_lfs_effnblks) { 1391 #ifdef DEBUG_LFS 1392 printf("lfs_writeseg: cleansing ino %d (%d != %d)\n", 1393 VTOI(bp->b_vp)->i_number, 1394 VTOI(bp->b_vp)->i_lfs_effnblks, 1395 VTOI(bp->b_vp)->i_ffs_blocks); 1396 #endif 1397 /* Make a copy we'll make changes to */ 1398 newbp = lfs_newbuf(bp->b_vp, bp->b_lblkno, 1399 bp->b_bcount); 1400 newbp->b_blkno = bp->b_blkno; 1401 memcpy(newbp->b_data, bp->b_data, 1402 newbp->b_bcount); 1403 *bpp = newbp; 1404 1405 changed = 0; 1406 for (daddrp = (daddr_t *)(newbp->b_data); 1407 daddrp < (daddr_t *)(newbp->b_data + 1408 newbp->b_bcount); daddrp++) { 1409 if (*daddrp == UNWRITTEN) { 1410 ++changed; 1411 #ifdef DEBUG_LFS 1412 printf("lfs_writeseg: replacing UNWRITTEN\n"); 1413 #endif 1414 *daddrp = 0; 1415 } 1416 } 1417 /* 1418 * Get rid of the old buffer. Don't mark it clean, 1419 * though, if it still has dirty data on it. 1420 */ 1421 if (changed) { 1422 bp->b_flags &= ~(B_ERROR | B_GATHERED); 1423 if (bp->b_flags & B_CALL) 1424 lfs_freebuf(bp); 1425 else { 1426 /* Still on free list, leave it there */ 1427 s = splbio(); 1428 bp->b_flags &= ~B_BUSY; 1429 if (bp->b_flags & B_WANTED) 1430 wakeup(bp); 1431 splx(s); 1432 /* 1433 * We have to re-decrement lfs_avail 1434 * since this block is going to come 1435 * back around to us in the next 1436 * segment. 1437 */ 1438 fs->lfs_avail -= btodb(bp->b_bcount); 1439 } 1440 } else { 1441 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | 1442 B_GATHERED); 1443 LFS_UNLOCK_BUF(bp); 1444 if (bp->b_flags & B_CALL) 1445 lfs_freebuf(bp); 1446 else { 1447 bremfree(bp); 1448 bp->b_flags |= B_DONE; 1449 reassignbuf(bp, bp->b_vp); 1450 brelse(bp); 1451 } 1452 } 1453 1454 } 1455 } 1456 /* 1457 * Compute checksum across data and then across summary; the first 1458 * block (the summary block) is skipped. Set the create time here 1459 * so that it's guaranteed to be later than the inode mod times. 1460 * 1461 * XXX 1462 * Fix this to do it inline, instead of malloc/copy. 1463 */ 1464 datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK); 1465 for (bpp = sp->bpp, i = nblocks - 1; i--;) { 1466 if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) { 1467 if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long))) 1468 panic("lfs_writeseg: copyin failed [1]: " 1469 "ino %d blk %d", 1470 VTOI((*bpp)->b_vp)->i_number, 1471 (*bpp)->b_lblkno); 1472 } else 1473 *dp++ = ((u_long *)(*bpp)->b_data)[0]; 1474 } 1475 ssp->ss_create = time.tv_sec; 1476 ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long)); 1477 ssp->ss_sumsum = 1478 cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum)); 1479 free(datap, M_SEGMENT); 1480 1481 fs->lfs_bfree -= (fsbtodb(fs, ninos) + btodb(LFS_SUMMARY_SIZE)); 1482 1483 strategy = devvp->v_op[VOFFSET(vop_strategy)]; 1484 1485 /* 1486 * When we simply write the blocks we lose a rotation for every block 1487 * written. To avoid this problem, we allocate memory in chunks, copy 1488 * the buffers into the chunk and write the chunk. CHUNKSIZE is the 1489 * largest size I/O devices can handle. 1490 * When the data is copied to the chunk, turn off the B_LOCKED bit 1491 * and brelse the buffer (which will move them to the LRU list). Add 1492 * the B_CALL flag to the buffer header so we can count I/O's for the 1493 * checkpoints and so we can release the allocated memory. 1494 * 1495 * XXX 1496 * This should be removed if the new virtual memory system allows us to 1497 * easily make the buffers contiguous in kernel memory and if that's 1498 * fast enough. 1499 */ 1500 1501 #define CHUNKSIZE MAXPHYS 1502 1503 if(devvp==NULL) 1504 panic("devvp is NULL"); 1505 for (bpp = sp->bpp,i = nblocks; i;) { 1506 cbp = lfs_newbuf(devvp, (*bpp)->b_blkno, CHUNKSIZE); 1507 cbp->b_dev = i_dev; 1508 cbp->b_flags |= B_ASYNC | B_BUSY; 1509 cbp->b_bcount = 0; 1510 1511 #ifdef DIAGNOSTIC 1512 if(datosn(fs, (*bpp)->b_blkno + btodb((*bpp)->b_bcount) - 1) != 1513 datosn(fs, cbp->b_blkno)) { 1514 panic("lfs_writeseg: Segment overwrite"); 1515 } 1516 #endif 1517 1518 s = splbio(); 1519 if(fs->lfs_iocount >= LFS_THROTTLE) { 1520 tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0); 1521 } 1522 ++fs->lfs_iocount; 1523 #ifdef LFS_TRACK_IOS 1524 for(j=0;j<LFS_THROTTLE;j++) { 1525 if(fs->lfs_pending[j]==LFS_UNUSED_DADDR) { 1526 fs->lfs_pending[j] = cbp->b_blkno; 1527 break; 1528 } 1529 } 1530 #endif /* LFS_TRACK_IOS */ 1531 for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) { 1532 bp = *bpp; 1533 1534 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount)) 1535 break; 1536 1537 /* 1538 * Fake buffers from the cleaner are marked as B_INVAL. 1539 * We need to copy the data from user space rather than 1540 * from the buffer indicated. 1541 * XXX == what do I do on an error? 1542 */ 1543 if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) { 1544 if (copyin(bp->b_saveaddr, p, bp->b_bcount)) 1545 panic("lfs_writeseg: copyin failed [2]"); 1546 } else 1547 bcopy(bp->b_data, p, bp->b_bcount); 1548 p += bp->b_bcount; 1549 cbp->b_bcount += bp->b_bcount; 1550 LFS_UNLOCK_BUF(bp); 1551 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | 1552 B_GATHERED); 1553 vn = bp->b_vp; 1554 if (bp->b_flags & B_CALL) { 1555 /* if B_CALL, it was created with newbuf */ 1556 lfs_freebuf(bp); 1557 } else { 1558 bremfree(bp); 1559 bp->b_flags |= B_DONE; 1560 if(vn) 1561 reassignbuf(bp, vn); 1562 brelse(bp); 1563 } 1564 1565 bpp++; 1566 1567 /* 1568 * If this is the last block for this vnode, but 1569 * there are other blocks on its dirty list, 1570 * set IN_MODIFIED/IN_CLEANING depending on what 1571 * sort of block. Only do this for our mount point, 1572 * not for, e.g., inode blocks that are attached to 1573 * the devvp. 1574 */ 1575 if(i>1 && vn && *bpp && (*bpp)->b_vp != vn 1576 && (*bpp)->b_vp && (bp=vn->v_dirtyblkhd.lh_first)!=NULL && 1577 vn->v_mount == fs->lfs_ivnode->v_mount) 1578 { 1579 ip = VTOI(vn); 1580 #ifdef DEBUG_LFS 1581 printf("lfs_writeseg: marking ino %d\n",ip->i_number); 1582 #endif 1583 if(bp->b_flags & B_CALL) 1584 LFS_SET_UINO(ip, IN_CLEANING); 1585 else 1586 LFS_SET_UINO(ip, IN_MODIFIED); 1587 } 1588 /* if(vn->v_dirtyblkhd.lh_first == NULL) */ 1589 wakeup(vn); 1590 } 1591 ++cbp->b_vp->v_numoutput; 1592 splx(s); 1593 /* 1594 * XXXX This is a gross and disgusting hack. Since these 1595 * buffers are physically addressed, they hang off the 1596 * device vnode (devvp). As a result, they have no way 1597 * of getting to the LFS superblock or lfs structure to 1598 * keep track of the number of I/O's pending. So, I am 1599 * going to stuff the fs into the saveaddr field of 1600 * the buffer (yuk). 1601 */ 1602 cbp->b_saveaddr = (caddr_t)fs; 1603 vop_strategy_a.a_desc = VDESC(vop_strategy); 1604 vop_strategy_a.a_bp = cbp; 1605 (strategy)(&vop_strategy_a); 1606 } 1607 #if 1 || defined(DEBUG) 1608 /* 1609 * After doing a big write, we recalculate how many buffers are 1610 * really still left on the locked queue. 1611 */ 1612 s = splbio(); 1613 lfs_countlocked(&locked_queue_count, &locked_queue_bytes); 1614 splx(s); 1615 wakeup(&locked_queue_count); 1616 #endif /* 1 || DEBUG */ 1617 if(lfs_dostats) { 1618 ++lfs_stats.psegwrites; 1619 lfs_stats.blocktot += nblocks - 1; 1620 if (fs->lfs_sp->seg_flags & SEGM_SYNC) 1621 ++lfs_stats.psyncwrites; 1622 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) { 1623 ++lfs_stats.pcleanwrites; 1624 lfs_stats.cleanblocks += nblocks - 1; 1625 } 1626 } 1627 return (lfs_initseg(fs) || do_again); 1628 } 1629 1630 void 1631 lfs_writesuper(fs, daddr) 1632 struct lfs *fs; 1633 daddr_t daddr; 1634 { 1635 struct buf *bp; 1636 dev_t i_dev; 1637 int (*strategy) __P((void *)); 1638 int s; 1639 struct vop_strategy_args vop_strategy_a; 1640 1641 #ifdef LFS_CANNOT_ROLLFW 1642 /* 1643 * If we can write one superblock while another is in 1644 * progress, we risk not having a complete checkpoint if we crash. 1645 * So, block here if a superblock write is in progress. 1646 */ 1647 s = splbio(); 1648 while(fs->lfs_sbactive) { 1649 tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0); 1650 } 1651 fs->lfs_sbactive = daddr; 1652 splx(s); 1653 #endif 1654 i_dev = VTOI(fs->lfs_ivnode)->i_dev; 1655 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)]; 1656 1657 /* Set timestamp of this version of the superblock */ 1658 fs->lfs_tstamp = time.tv_sec; 1659 1660 /* Checksum the superblock and copy it into a buffer. */ 1661 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs)); 1662 bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, LFS_SBPAD); 1663 *(struct dlfs *)bp->b_data = fs->lfs_dlfs; 1664 1665 bp->b_dev = i_dev; 1666 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC; 1667 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 1668 bp->b_iodone = lfs_supercallback; 1669 /* XXX KS - same nasty hack as above */ 1670 bp->b_saveaddr = (caddr_t)fs; 1671 1672 vop_strategy_a.a_desc = VDESC(vop_strategy); 1673 vop_strategy_a.a_bp = bp; 1674 s = splbio(); 1675 ++bp->b_vp->v_numoutput; 1676 ++fs->lfs_iocount; 1677 splx(s); 1678 (strategy)(&vop_strategy_a); 1679 } 1680 1681 /* 1682 * Logical block number match routines used when traversing the dirty block 1683 * chain. 1684 */ 1685 int 1686 lfs_match_fake(fs, bp) 1687 struct lfs *fs; 1688 struct buf *bp; 1689 { 1690 return (bp->b_flags & B_CALL); 1691 } 1692 1693 int 1694 lfs_match_data(fs, bp) 1695 struct lfs *fs; 1696 struct buf *bp; 1697 { 1698 return (bp->b_lblkno >= 0); 1699 } 1700 1701 int 1702 lfs_match_indir(fs, bp) 1703 struct lfs *fs; 1704 struct buf *bp; 1705 { 1706 int lbn; 1707 1708 lbn = bp->b_lblkno; 1709 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0); 1710 } 1711 1712 int 1713 lfs_match_dindir(fs, bp) 1714 struct lfs *fs; 1715 struct buf *bp; 1716 { 1717 int lbn; 1718 1719 lbn = bp->b_lblkno; 1720 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1); 1721 } 1722 1723 int 1724 lfs_match_tindir(fs, bp) 1725 struct lfs *fs; 1726 struct buf *bp; 1727 { 1728 int lbn; 1729 1730 lbn = bp->b_lblkno; 1731 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2); 1732 } 1733 1734 /* 1735 * XXX - The only buffers that are going to hit these functions are the 1736 * segment write blocks, or the segment summaries, or the superblocks. 1737 * 1738 * All of the above are created by lfs_newbuf, and so do not need to be 1739 * released via brelse. 1740 */ 1741 void 1742 lfs_callback(bp) 1743 struct buf *bp; 1744 { 1745 struct lfs *fs; 1746 #ifdef LFS_TRACK_IOS 1747 int j; 1748 #endif 1749 1750 fs = (struct lfs *)bp->b_saveaddr; 1751 #ifdef DIAGNOSTIC 1752 if (fs->lfs_iocount == 0) 1753 panic("lfs_callback: zero iocount\n"); 1754 #endif 1755 if (--fs->lfs_iocount < LFS_THROTTLE) 1756 wakeup(&fs->lfs_iocount); 1757 #ifdef LFS_TRACK_IOS 1758 for(j=0;j<LFS_THROTTLE;j++) { 1759 if(fs->lfs_pending[j]==bp->b_blkno) { 1760 fs->lfs_pending[j] = LFS_UNUSED_DADDR; 1761 wakeup(&(fs->lfs_pending[j])); 1762 break; 1763 } 1764 } 1765 #endif /* LFS_TRACK_IOS */ 1766 1767 lfs_freebuf(bp); 1768 } 1769 1770 void 1771 lfs_supercallback(bp) 1772 struct buf *bp; 1773 { 1774 struct lfs *fs; 1775 1776 fs = (struct lfs *)bp->b_saveaddr; 1777 #ifdef LFS_CANNOT_ROLLFW 1778 fs->lfs_sbactive = 0; 1779 wakeup(&fs->lfs_sbactive); 1780 #endif 1781 if (--fs->lfs_iocount < LFS_THROTTLE) 1782 wakeup(&fs->lfs_iocount); 1783 lfs_freebuf(bp); 1784 } 1785 1786 /* 1787 * Shellsort (diminishing increment sort) from Data Structures and 1788 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 1789 * see also Knuth Vol. 3, page 84. The increments are selected from 1790 * formula (8), page 95. Roughly O(N^3/2). 1791 */ 1792 /* 1793 * This is our own private copy of shellsort because we want to sort 1794 * two parallel arrays (the array of buffer pointers and the array of 1795 * logical block numbers) simultaneously. Note that we cast the array 1796 * of logical block numbers to a unsigned in this routine so that the 1797 * negative block numbers (meta data blocks) sort AFTER the data blocks. 1798 */ 1799 1800 void 1801 lfs_shellsort(bp_array, lb_array, nmemb) 1802 struct buf **bp_array; 1803 ufs_daddr_t *lb_array; 1804 int nmemb; 1805 { 1806 static int __rsshell_increments[] = { 4, 1, 0 }; 1807 int incr, *incrp, t1, t2; 1808 struct buf *bp_temp; 1809 u_long lb_temp; 1810 1811 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;) 1812 for (t1 = incr; t1 < nmemb; ++t1) 1813 for (t2 = t1 - incr; t2 >= 0;) 1814 if (lb_array[t2] > lb_array[t2 + incr]) { 1815 lb_temp = lb_array[t2]; 1816 lb_array[t2] = lb_array[t2 + incr]; 1817 lb_array[t2 + incr] = lb_temp; 1818 bp_temp = bp_array[t2]; 1819 bp_array[t2] = bp_array[t2 + incr]; 1820 bp_array[t2 + incr] = bp_temp; 1821 t2 -= incr; 1822 } else 1823 break; 1824 } 1825 1826 /* 1827 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it. 1828 */ 1829 int 1830 lfs_vref(vp) 1831 struct vnode *vp; 1832 { 1833 /* 1834 * If we return 1 here during a flush, we risk vinvalbuf() not 1835 * being able to flush all of the pages from this vnode, which 1836 * will cause it to panic. So, return 0 if a flush is in progress. 1837 */ 1838 if (vp->v_flag & VXLOCK) { 1839 if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) { 1840 return 0; 1841 } 1842 return(1); 1843 } 1844 return (vget(vp, 0)); 1845 } 1846 1847 /* 1848 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We 1849 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end. 1850 */ 1851 void 1852 lfs_vunref(vp) 1853 struct vnode *vp; 1854 { 1855 /* 1856 * Analogous to lfs_vref, if the node is flushing, fake it. 1857 */ 1858 if((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) { 1859 return; 1860 } 1861 1862 simple_lock(&vp->v_interlock); 1863 #ifdef DIAGNOSTIC 1864 if(vp->v_usecount<=0) { 1865 printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number); 1866 printf("lfs_vunref: flags are 0x%x\n", vp->v_flag); 1867 printf("lfs_vunref: usecount = %d\n", vp->v_usecount); 1868 panic("lfs_vunref: v_usecount<0"); 1869 } 1870 #endif 1871 vp->v_usecount--; 1872 if (vp->v_usecount > 0) { 1873 simple_unlock(&vp->v_interlock); 1874 return; 1875 } 1876 /* 1877 * insert at tail of LRU list 1878 */ 1879 simple_lock(&vnode_free_list_slock); 1880 if (vp->v_holdcnt > 0) 1881 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist); 1882 else 1883 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 1884 simple_unlock(&vnode_free_list_slock); 1885 simple_unlock(&vp->v_interlock); 1886 } 1887 1888 /* 1889 * We use this when we have vnodes that were loaded in solely for cleaning. 1890 * There is no reason to believe that these vnodes will be referenced again 1891 * soon, since the cleaning process is unrelated to normal filesystem 1892 * activity. Putting cleaned vnodes at the tail of the list has the effect 1893 * of flushing the vnode LRU. So, put vnodes that were loaded only for 1894 * cleaning at the head of the list, instead. 1895 */ 1896 void 1897 lfs_vunref_head(vp) 1898 struct vnode *vp; 1899 { 1900 simple_lock(&vp->v_interlock); 1901 #ifdef DIAGNOSTIC 1902 if(vp->v_usecount==0) { 1903 panic("lfs_vunref: v_usecount<0"); 1904 } 1905 #endif 1906 vp->v_usecount--; 1907 if (vp->v_usecount > 0) { 1908 simple_unlock(&vp->v_interlock); 1909 return; 1910 } 1911 /* 1912 * insert at head of LRU list 1913 */ 1914 simple_lock(&vnode_free_list_slock); 1915 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 1916 simple_unlock(&vnode_free_list_slock); 1917 simple_unlock(&vp->v_interlock); 1918 } 1919 1920