1 /* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)lfs_segment.c 8.7 (Berkeley) 03/21/95 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/namei.h> 13 #include <sys/kernel.h> 14 #include <sys/resourcevar.h> 15 #include <sys/file.h> 16 #include <sys/stat.h> 17 #include <sys/buf.h> 18 #include <sys/proc.h> 19 #include <sys/conf.h> 20 #include <sys/vnode.h> 21 #include <sys/malloc.h> 22 #include <sys/mount.h> 23 24 #include <miscfs/specfs/specdev.h> 25 #include <miscfs/fifofs/fifo.h> 26 27 #include <ufs/ufs/quota.h> 28 #include <ufs/ufs/inode.h> 29 #include <ufs/ufs/dir.h> 30 #include <ufs/ufs/ufsmount.h> 31 #include <ufs/ufs/ufs_extern.h> 32 33 #include <ufs/lfs/lfs.h> 34 #include <ufs/lfs/lfs_extern.h> 35 36 extern int count_lock_queue __P((void)); 37 38 #define MAX_ACTIVE 10 39 /* 40 * Determine if it's OK to start a partial in this segment, or if we need 41 * to go on to a new segment. 42 */ 43 #define LFS_PARTIAL_FITS(fs) \ 44 ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \ 45 1 << (fs)->lfs_fsbtodb) 46 47 void lfs_callback __P((struct buf *)); 48 void lfs_gather __P((struct lfs *, struct segment *, 49 struct vnode *, int (*) __P((struct lfs *, struct buf *)))); 50 int lfs_gatherblock __P((struct segment *, struct buf *, int *)); 51 void lfs_iset __P((struct inode *, ufs_daddr_t, time_t)); 52 int lfs_match_data __P((struct lfs *, struct buf *)); 53 int lfs_match_dindir __P((struct lfs *, struct buf *)); 54 int lfs_match_indir __P((struct lfs *, struct buf *)); 55 int lfs_match_tindir __P((struct lfs *, struct buf *)); 56 void lfs_newseg __P((struct lfs *)); 57 void lfs_shellsort __P((struct buf **, ufs_daddr_t *, register int)); 58 void lfs_supercallback __P((struct buf *)); 59 void lfs_updatemeta __P((struct segment *)); 60 int lfs_vref __P((struct vnode *)); 61 void lfs_vunref __P((struct vnode *)); 62 void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *)); 63 int lfs_writeinode __P((struct lfs *, struct segment *, struct inode *)); 64 int lfs_writeseg __P((struct lfs *, struct segment *)); 65 void lfs_writesuper __P((struct lfs *)); 66 void lfs_writevnodes __P((struct lfs *fs, struct mount *mp, 67 struct segment *sp, int dirops)); 68 69 int lfs_allclean_wakeup; /* Cleaner wakeup address. */ 70 71 /* Statistics Counters */ 72 #define DOSTATS 73 struct lfs_stats lfs_stats; 74 75 /* op values to lfs_writevnodes */ 76 #define VN_REG 0 77 #define VN_DIROP 1 78 #define VN_EMPTY 2 79 80 /* 81 * Ifile and meta data blocks are not marked busy, so segment writes MUST be 82 * single threaded. Currently, there are two paths into lfs_segwrite, sync() 83 * and getnewbuf(). They both mark the file system busy. Lfs_vflush() 84 * explicitly marks the file system busy. So lfs_segwrite is safe. I think. 85 */ 86 87 int 88 lfs_vflush(vp) 89 struct vnode *vp; 90 { 91 struct inode *ip; 92 struct lfs *fs; 93 struct segment *sp; 94 95 fs = VFSTOUFS(vp->v_mount)->um_lfs; 96 if (fs->lfs_nactive > MAX_ACTIVE) 97 return(lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP)); 98 lfs_seglock(fs, SEGM_SYNC); 99 sp = fs->lfs_sp; 100 101 102 ip = VTOI(vp); 103 if (vp->v_dirtyblkhd.lh_first == NULL) 104 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY); 105 106 do { 107 do { 108 if (vp->v_dirtyblkhd.lh_first != NULL) 109 lfs_writefile(fs, sp, vp); 110 } while (lfs_writeinode(fs, sp, ip)); 111 112 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM); 113 114 #ifdef DOSTATS 115 ++lfs_stats.nwrites; 116 if (sp->seg_flags & SEGM_SYNC) 117 ++lfs_stats.nsync_writes; 118 if (sp->seg_flags & SEGM_CKP) 119 ++lfs_stats.ncheckpoints; 120 #endif 121 lfs_segunlock(fs); 122 return (0); 123 } 124 125 void 126 lfs_writevnodes(fs, mp, sp, op) 127 struct lfs *fs; 128 struct mount *mp; 129 struct segment *sp; 130 int op; 131 { 132 struct inode *ip; 133 struct vnode *vp; 134 135 loop: 136 for (vp = mp->mnt_vnodelist.lh_first; 137 vp != NULL; 138 vp = vp->v_mntvnodes.le_next) { 139 /* 140 * If the vnode that we are about to sync is no longer 141 * associated with this mount point, start over. 142 */ 143 if (vp->v_mount != mp) 144 goto loop; 145 146 /* XXX ignore dirops for now 147 if (op == VN_DIROP && !(vp->v_flag & VDIROP) || 148 op != VN_DIROP && (vp->v_flag & VDIROP)) 149 continue; 150 */ 151 152 if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) 153 continue; 154 155 if (vp->v_type == VNON) 156 continue; 157 158 if (lfs_vref(vp)) 159 continue; 160 161 /* 162 * Write the inode/file if dirty and it's not the 163 * the IFILE. 164 */ 165 ip = VTOI(vp); 166 if ((ip->i_flag & 167 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE) || 168 vp->v_dirtyblkhd.lh_first != NULL) && 169 ip->i_number != LFS_IFILE_INUM) { 170 if (vp->v_dirtyblkhd.lh_first != NULL) 171 lfs_writefile(fs, sp, vp); 172 (void) lfs_writeinode(fs, sp, ip); 173 } 174 vp->v_flag &= ~VDIROP; 175 lfs_vunref(vp); 176 } 177 } 178 179 int 180 lfs_segwrite(mp, flags) 181 struct mount *mp; 182 int flags; /* Do a checkpoint. */ 183 { 184 struct buf *bp; 185 struct inode *ip; 186 struct lfs *fs; 187 struct segment *sp; 188 struct vnode *vp; 189 SEGUSE *segusep; 190 ufs_daddr_t ibno; 191 CLEANERINFO *cip; 192 int clean, do_ckp, error, i; 193 194 fs = VFSTOUFS(mp)->um_lfs; 195 196 /* 197 * If we have fewer than 2 clean segments, wait until cleaner 198 * writes. 199 */ 200 do { 201 LFS_CLEANERINFO(cip, fs, bp); 202 clean = cip->clean; 203 brelse(bp); 204 if (clean <= 2) { 205 printf ("segs clean: %d\n", clean); 206 wakeup(&lfs_allclean_wakeup); 207 if (error = tsleep(&fs->lfs_avail, PRIBIO + 1, 208 "lfs writer", 0)) 209 return (error); 210 } 211 } while (clean <= 2 ); 212 213 /* 214 * Allocate a segment structure and enough space to hold pointers to 215 * the maximum possible number of buffers which can be described in a 216 * single summary block. 217 */ 218 do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE; 219 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0)); 220 sp = fs->lfs_sp; 221 222 lfs_writevnodes(fs, mp, sp, VN_REG); 223 224 /* XXX ignore ordering of dirops for now */ 225 /* XXX 226 fs->lfs_writer = 1; 227 if (fs->lfs_dirops && (error = 228 tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) { 229 free(sp->bpp, M_SEGMENT); 230 free(sp, M_SEGMENT); 231 fs->lfs_writer = 0; 232 return (error); 233 } 234 235 lfs_writevnodes(fs, mp, sp, VN_DIROP); 236 */ 237 238 /* 239 * If we are doing a checkpoint, mark everything since the 240 * last checkpoint as no longer ACTIVE. 241 */ 242 if (do_ckp) 243 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz; 244 --ibno >= fs->lfs_cleansz; ) { 245 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, 246 NOCRED, &bp)) 247 248 panic("lfs: ifile read"); 249 segusep = (SEGUSE *)bp->b_data; 250 for (i = fs->lfs_sepb; i--; segusep++) 251 segusep->su_flags &= ~SEGUSE_ACTIVE; 252 253 error = VOP_BWRITE(bp); 254 } 255 256 if (do_ckp || fs->lfs_doifile) { 257 redo: 258 vp = fs->lfs_ivnode; 259 while (vget(vp, 1)); 260 ip = VTOI(vp); 261 if (vp->v_dirtyblkhd.lh_first != NULL) 262 lfs_writefile(fs, sp, vp); 263 (void)lfs_writeinode(fs, sp, ip); 264 vput(vp); 265 if (lfs_writeseg(fs, sp) && do_ckp) 266 goto redo; 267 } else 268 (void) lfs_writeseg(fs, sp); 269 270 /* 271 * If the I/O count is non-zero, sleep until it reaches zero. At the 272 * moment, the user's process hangs around so we can sleep. 273 */ 274 /* XXX ignore dirops for now 275 fs->lfs_writer = 0; 276 fs->lfs_doifile = 0; 277 wakeup(&fs->lfs_dirops); 278 */ 279 280 #ifdef DOSTATS 281 ++lfs_stats.nwrites; 282 if (sp->seg_flags & SEGM_SYNC) 283 ++lfs_stats.nsync_writes; 284 if (sp->seg_flags & SEGM_CKP) 285 ++lfs_stats.ncheckpoints; 286 #endif 287 lfs_segunlock(fs); 288 return (0); 289 } 290 291 /* 292 * Write the dirty blocks associated with a vnode. 293 */ 294 void 295 lfs_writefile(fs, sp, vp) 296 struct lfs *fs; 297 struct segment *sp; 298 struct vnode *vp; 299 { 300 struct buf *bp; 301 struct finfo *fip; 302 IFILE *ifp; 303 304 if (sp->seg_bytes_left < fs->lfs_bsize || 305 sp->sum_bytes_left < sizeof(struct finfo)) 306 (void) lfs_writeseg(fs, sp); 307 308 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t); 309 ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 310 311 fip = sp->fip; 312 fip->fi_nblocks = 0; 313 fip->fi_ino = VTOI(vp)->i_number; 314 LFS_IENTRY(ifp, fs, fip->fi_ino, bp); 315 fip->fi_version = ifp->if_version; 316 brelse(bp); 317 318 /* 319 * It may not be necessary to write the meta-data blocks at this point, 320 * as the roll-forward recovery code should be able to reconstruct the 321 * list. 322 */ 323 lfs_gather(fs, sp, vp, lfs_match_data); 324 lfs_gather(fs, sp, vp, lfs_match_indir); 325 lfs_gather(fs, sp, vp, lfs_match_dindir); 326 #ifdef TRIPLE 327 lfs_gather(fs, sp, vp, lfs_match_tindir); 328 #endif 329 330 fip = sp->fip; 331 if (fip->fi_nblocks != 0) { 332 sp->fip = 333 (struct finfo *)((caddr_t)fip + sizeof(struct finfo) + 334 sizeof(ufs_daddr_t) * (fip->fi_nblocks - 1)); 335 sp->start_lbp = &sp->fip->fi_blocks[0]; 336 } else { 337 sp->sum_bytes_left += sizeof(struct finfo) - sizeof(ufs_daddr_t); 338 --((SEGSUM *)(sp->segsum))->ss_nfinfo; 339 } 340 } 341 342 int 343 lfs_writeinode(fs, sp, ip) 344 struct lfs *fs; 345 struct segment *sp; 346 struct inode *ip; 347 { 348 struct buf *bp, *ibp; 349 IFILE *ifp; 350 SEGUSE *sup; 351 ufs_daddr_t daddr; 352 ino_t ino; 353 int error, i, ndx; 354 int redo_ifile = 0; 355 356 if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE))) 357 return(0); 358 359 /* Allocate a new inode block if necessary. */ 360 if (sp->ibp == NULL) { 361 /* Allocate a new segment if necessary. */ 362 if (sp->seg_bytes_left < fs->lfs_bsize || 363 sp->sum_bytes_left < sizeof(ufs_daddr_t)) 364 (void) lfs_writeseg(fs, sp); 365 366 /* Get next inode block. */ 367 daddr = fs->lfs_offset; 368 fs->lfs_offset += fsbtodb(fs, 1); 369 sp->ibp = *sp->cbpp++ = 370 lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, 371 fs->lfs_bsize); 372 /* Zero out inode numbers */ 373 for (i = 0; i < INOPB(fs); ++i) 374 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0; 375 ++sp->start_bpp; 376 fs->lfs_avail -= fsbtodb(fs, 1); 377 /* Set remaining space counters. */ 378 sp->seg_bytes_left -= fs->lfs_bsize; 379 sp->sum_bytes_left -= sizeof(ufs_daddr_t); 380 ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) - 381 sp->ninodes / INOPB(fs) - 1; 382 ((ufs_daddr_t *)(sp->segsum))[ndx] = daddr; 383 } 384 385 /* Update the inode times and copy the inode onto the inode page. */ 386 if (ip->i_flag & IN_MODIFIED) 387 --fs->lfs_uinodes; 388 ITIMES(ip, &time, &time); 389 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE); 390 bp = sp->ibp; 391 ((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] = ip->i_din; 392 /* Increment inode count in segment summary block. */ 393 ++((SEGSUM *)(sp->segsum))->ss_ninos; 394 395 /* If this page is full, set flag to allocate a new page. */ 396 if (++sp->ninodes % INOPB(fs) == 0) 397 sp->ibp = NULL; 398 399 /* 400 * If updating the ifile, update the super-block. Update the disk 401 * address and access times for this inode in the ifile. 402 */ 403 ino = ip->i_number; 404 if (ino == LFS_IFILE_INUM) { 405 daddr = fs->lfs_idaddr; 406 fs->lfs_idaddr = bp->b_blkno; 407 } else { 408 LFS_IENTRY(ifp, fs, ino, ibp); 409 daddr = ifp->if_daddr; 410 ifp->if_daddr = bp->b_blkno; 411 error = VOP_BWRITE(ibp); 412 } 413 414 /* 415 * No need to update segment usage if there was no former inode address 416 * or if the last inode address is in the current partial segment. 417 */ 418 if (daddr != LFS_UNUSED_DADDR && 419 !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) { 420 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 421 #ifdef DIAGNOSTIC 422 if (sup->su_nbytes < sizeof(struct dinode)) { 423 /* XXX -- Change to a panic. */ 424 printf("lfs: negative bytes (segment %d)\n", 425 datosn(fs, daddr)); 426 panic("negative bytes"); 427 } 428 #endif 429 sup->su_nbytes -= sizeof(struct dinode); 430 redo_ifile = 431 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED)); 432 error = VOP_BWRITE(bp); 433 } 434 return (redo_ifile); 435 } 436 437 int 438 lfs_gatherblock(sp, bp, sptr) 439 struct segment *sp; 440 struct buf *bp; 441 int *sptr; 442 { 443 struct lfs *fs; 444 int version; 445 446 /* 447 * If full, finish this segment. We may be doing I/O, so 448 * release and reacquire the splbio(). 449 */ 450 #ifdef DIAGNOSTIC 451 if (sp->vp == NULL) 452 panic ("lfs_gatherblock: Null vp in segment"); 453 #endif 454 fs = sp->fs; 455 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) || 456 sp->seg_bytes_left < fs->lfs_bsize) { 457 if (sptr) 458 splx(*sptr); 459 lfs_updatemeta(sp); 460 461 version = sp->fip->fi_version; 462 (void) lfs_writeseg(fs, sp); 463 464 sp->fip->fi_version = version; 465 sp->fip->fi_ino = VTOI(sp->vp)->i_number; 466 /* Add the current file to the segment summary. */ 467 ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 468 sp->sum_bytes_left -= 469 sizeof(struct finfo) - sizeof(ufs_daddr_t); 470 471 if (sptr) 472 *sptr = splbio(); 473 return(1); 474 } 475 476 /* Insert into the buffer list, update the FINFO block. */ 477 bp->b_flags |= B_GATHERED; 478 *sp->cbpp++ = bp; 479 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno; 480 481 sp->sum_bytes_left -= sizeof(ufs_daddr_t); 482 sp->seg_bytes_left -= fs->lfs_bsize; 483 return(0); 484 } 485 486 void 487 lfs_gather(fs, sp, vp, match) 488 struct lfs *fs; 489 struct segment *sp; 490 struct vnode *vp; 491 int (*match) __P((struct lfs *, struct buf *)); 492 { 493 struct buf *bp; 494 int s; 495 496 sp->vp = vp; 497 s = splbio(); 498 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) { 499 if (bp->b_flags & B_BUSY || !match(fs, bp) || 500 bp->b_flags & B_GATHERED) 501 continue; 502 #ifdef DIAGNOSTIC 503 if (!(bp->b_flags & B_DELWRI)) 504 panic("lfs_gather: bp not B_DELWRI"); 505 if (!(bp->b_flags & B_LOCKED)) 506 panic("lfs_gather: bp not B_LOCKED"); 507 #endif 508 if (lfs_gatherblock(sp, bp, &s)) 509 goto loop; 510 } 511 splx(s); 512 lfs_updatemeta(sp); 513 sp->vp = NULL; 514 } 515 516 517 /* 518 * Update the metadata that points to the blocks listed in the FINFO 519 * array. 520 */ 521 void 522 lfs_updatemeta(sp) 523 struct segment *sp; 524 { 525 SEGUSE *sup; 526 struct buf *bp; 527 struct lfs *fs; 528 struct vnode *vp; 529 struct indir a[NIADDR + 2], *ap; 530 struct inode *ip; 531 ufs_daddr_t daddr, lbn, off; 532 int db_per_fsb, error, i, nblocks, num; 533 534 vp = sp->vp; 535 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp; 536 if (vp == NULL || nblocks == 0) 537 return; 538 539 /* Sort the blocks. */ 540 if (!(sp->seg_flags & SEGM_CLEAN)) 541 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks); 542 543 /* 544 * Assign disk addresses, and update references to the logical 545 * block and the segment usage information. 546 */ 547 fs = sp->fs; 548 db_per_fsb = fsbtodb(fs, 1); 549 for (i = nblocks; i--; ++sp->start_bpp) { 550 lbn = *sp->start_lbp++; 551 (*sp->start_bpp)->b_blkno = off = fs->lfs_offset; 552 fs->lfs_offset += db_per_fsb; 553 554 if (error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL)) 555 panic("lfs_updatemeta: ufs_bmaparray %d", error); 556 ip = VTOI(vp); 557 switch (num) { 558 case 0: 559 ip->i_db[lbn] = off; 560 break; 561 case 1: 562 ip->i_ib[a[0].in_off] = off; 563 break; 564 default: 565 ap = &a[num - 1]; 566 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 567 panic("lfs_updatemeta: bread bno %d", 568 ap->in_lbn); 569 /* 570 * Bread may create a new indirect block which needs 571 * to get counted for the inode. 572 */ 573 if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) { 574 printf ("Updatemeta allocating indirect block: shouldn't happen\n"); 575 ip->i_blocks += btodb(fs->lfs_bsize); 576 fs->lfs_bfree -= btodb(fs->lfs_bsize); 577 } 578 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = off; 579 VOP_BWRITE(bp); 580 } 581 582 /* Update segment usage information. */ 583 if (daddr != UNASSIGNED && 584 !(daddr >= fs->lfs_lastpseg && daddr <= off)) { 585 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 586 #ifdef DIAGNOSTIC 587 if (sup->su_nbytes < fs->lfs_bsize) { 588 /* XXX -- Change to a panic. */ 589 printf("lfs: negative bytes (segment %d)\n", 590 datosn(fs, daddr)); 591 panic ("Negative Bytes"); 592 } 593 #endif 594 sup->su_nbytes -= fs->lfs_bsize; 595 error = VOP_BWRITE(bp); 596 } 597 } 598 } 599 600 /* 601 * Start a new segment. 602 */ 603 int 604 lfs_initseg(fs) 605 struct lfs *fs; 606 { 607 struct segment *sp; 608 SEGUSE *sup; 609 SEGSUM *ssp; 610 struct buf *bp; 611 int repeat; 612 613 sp = fs->lfs_sp; 614 615 repeat = 0; 616 /* Advance to the next segment. */ 617 if (!LFS_PARTIAL_FITS(fs)) { 618 /* Wake up any cleaning procs waiting on this file system. */ 619 wakeup(&lfs_allclean_wakeup); 620 621 lfs_newseg(fs); 622 repeat = 1; 623 fs->lfs_offset = fs->lfs_curseg; 624 sp->seg_number = datosn(fs, fs->lfs_curseg); 625 sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE; 626 627 /* 628 * If the segment contains a superblock, update the offset 629 * and summary address to skip over it. 630 */ 631 LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 632 if (sup->su_flags & SEGUSE_SUPERBLOCK) { 633 fs->lfs_offset += LFS_SBPAD / DEV_BSIZE; 634 sp->seg_bytes_left -= LFS_SBPAD; 635 } 636 brelse(bp); 637 } else { 638 sp->seg_number = datosn(fs, fs->lfs_curseg); 639 sp->seg_bytes_left = (fs->lfs_dbpseg - 640 (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE; 641 } 642 fs->lfs_lastpseg = fs->lfs_offset; 643 644 sp->fs = fs; 645 sp->ibp = NULL; 646 sp->ninodes = 0; 647 648 /* Get a new buffer for SEGSUM and enter it into the buffer list. */ 649 sp->cbpp = sp->bpp; 650 *sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset, 651 LFS_SUMMARY_SIZE); 652 sp->segsum = (*sp->cbpp)->b_data; 653 bzero(sp->segsum, LFS_SUMMARY_SIZE); 654 sp->start_bpp = ++sp->cbpp; 655 fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE; 656 657 /* Set point to SEGSUM, initialize it. */ 658 ssp = sp->segsum; 659 ssp->ss_next = fs->lfs_nextseg; 660 ssp->ss_nfinfo = ssp->ss_ninos = 0; 661 662 /* Set pointer to first FINFO, initialize it. */ 663 sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM)); 664 sp->fip->fi_nblocks = 0; 665 sp->start_lbp = &sp->fip->fi_blocks[0]; 666 667 sp->seg_bytes_left -= LFS_SUMMARY_SIZE; 668 sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM); 669 670 return(repeat); 671 } 672 673 /* 674 * Return the next segment to write. 675 */ 676 void 677 lfs_newseg(fs) 678 struct lfs *fs; 679 { 680 CLEANERINFO *cip; 681 SEGUSE *sup; 682 struct buf *bp; 683 int curseg, isdirty, sn; 684 685 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp); 686 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE; 687 sup->su_nbytes = 0; 688 sup->su_nsums = 0; 689 sup->su_ninos = 0; 690 (void) VOP_BWRITE(bp); 691 692 LFS_CLEANERINFO(cip, fs, bp); 693 --cip->clean; 694 ++cip->dirty; 695 (void) VOP_BWRITE(bp); 696 697 fs->lfs_lastseg = fs->lfs_curseg; 698 fs->lfs_curseg = fs->lfs_nextseg; 699 for (sn = curseg = datosn(fs, fs->lfs_curseg);;) { 700 sn = (sn + 1) % fs->lfs_nseg; 701 if (sn == curseg) 702 panic("lfs_nextseg: no clean segments"); 703 LFS_SEGENTRY(sup, fs, sn, bp); 704 isdirty = sup->su_flags & SEGUSE_DIRTY; 705 brelse(bp); 706 if (!isdirty) 707 break; 708 } 709 710 ++fs->lfs_nactive; 711 fs->lfs_nextseg = sntoda(fs, sn); 712 #ifdef DOSTATS 713 ++lfs_stats.segsused; 714 #endif 715 } 716 717 int 718 lfs_writeseg(fs, sp) 719 struct lfs *fs; 720 struct segment *sp; 721 { 722 extern int locked_queue_count; 723 struct buf **bpp, *bp, *cbp; 724 SEGUSE *sup; 725 SEGSUM *ssp; 726 dev_t i_dev; 727 size_t size; 728 u_long *datap, *dp; 729 int ch_per_blk, do_again, i, nblocks, num, s; 730 int (*strategy)__P((struct vop_strategy_args *)); 731 struct vop_strategy_args vop_strategy_a; 732 u_short ninos; 733 char *p; 734 735 /* 736 * If there are no buffers other than the segment summary to write 737 * and it is not a checkpoint, don't do anything. On a checkpoint, 738 * even if there aren't any buffers, you need to write the superblock. 739 */ 740 if ((nblocks = sp->cbpp - sp->bpp) == 1) 741 return (0); 742 743 ssp = (SEGSUM *)sp->segsum; 744 745 /* Update the segment usage information. */ 746 LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 747 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs); 748 sup->su_nbytes += nblocks - 1 - ninos << fs->lfs_bshift; 749 sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode); 750 sup->su_nbytes += LFS_SUMMARY_SIZE; 751 sup->su_lastmod = time.tv_sec; 752 sup->su_ninos += ninos; 753 ++sup->su_nsums; 754 do_again = !(bp->b_flags & B_GATHERED); 755 (void)VOP_BWRITE(bp); 756 /* 757 * Compute checksum across data and then across summary; the first 758 * block (the summary block) is skipped. Set the create time here 759 * so that it's guaranteed to be later than the inode mod times. 760 * 761 * XXX 762 * Fix this to do it inline, instead of malloc/copy. 763 */ 764 datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK); 765 for (bpp = sp->bpp, i = nblocks - 1; i--;) { 766 if ((*++bpp)->b_flags & B_INVAL) { 767 if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long))) 768 panic("lfs_writeseg: copyin failed"); 769 } else 770 *dp++ = ((u_long *)(*bpp)->b_data)[0]; 771 } 772 ssp->ss_create = time.tv_sec; 773 ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long)); 774 ssp->ss_sumsum = 775 cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum)); 776 free(datap, M_SEGMENT); 777 #ifdef DIAGNOSTIC 778 if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE) 779 panic("lfs_writeseg: No diskspace for summary"); 780 #endif 781 fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE); 782 783 i_dev = VTOI(fs->lfs_ivnode)->i_dev; 784 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)]; 785 786 /* 787 * When we simply write the blocks we lose a rotation for every block 788 * written. To avoid this problem, we allocate memory in chunks, copy 789 * the buffers into the chunk and write the chunk. MAXPHYS is the 790 * largest size I/O devices can handle. 791 * When the data is copied to the chunk, turn off the the B_LOCKED bit 792 * and brelse the buffer (which will move them to the LRU list). Add 793 * the B_CALL flag to the buffer header so we can count I/O's for the 794 * checkpoints and so we can release the allocated memory. 795 * 796 * XXX 797 * This should be removed if the new virtual memory system allows us to 798 * easily make the buffers contiguous in kernel memory and if that's 799 * fast enough. 800 */ 801 ch_per_blk = MAXPHYS / fs->lfs_bsize; 802 for (bpp = sp->bpp, i = nblocks; i;) { 803 num = ch_per_blk; 804 if (num > i) 805 num = i; 806 i -= num; 807 size = num * fs->lfs_bsize; 808 809 cbp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, 810 (*bpp)->b_blkno, size); 811 cbp->b_dev = i_dev; 812 cbp->b_flags |= B_ASYNC | B_BUSY; 813 814 s = splbio(); 815 ++fs->lfs_iocount; 816 for (p = cbp->b_data; num--;) { 817 bp = *bpp++; 818 /* 819 * Fake buffers from the cleaner are marked as B_INVAL. 820 * We need to copy the data from user space rather than 821 * from the buffer indicated. 822 * XXX == what do I do on an error? 823 */ 824 if (bp->b_flags & B_INVAL) { 825 if (copyin(bp->b_saveaddr, p, bp->b_bcount)) 826 panic("lfs_writeseg: copyin failed"); 827 } else 828 bcopy(bp->b_data, p, bp->b_bcount); 829 p += bp->b_bcount; 830 if (bp->b_flags & B_LOCKED) 831 --locked_queue_count; 832 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | 833 B_LOCKED | B_GATHERED); 834 if (bp->b_flags & B_CALL) { 835 /* if B_CALL, it was created with newbuf */ 836 brelvp(bp); 837 if (!(bp->b_flags & B_INVAL)) 838 free(bp->b_data, M_SEGMENT); 839 free(bp, M_SEGMENT); 840 } else { 841 bremfree(bp); 842 bp->b_flags |= B_DONE; 843 reassignbuf(bp, bp->b_vp); 844 brelse(bp); 845 } 846 } 847 ++cbp->b_vp->v_numoutput; 848 splx(s); 849 cbp->b_bcount = p - (char *)cbp->b_data; 850 /* 851 * XXXX This is a gross and disgusting hack. Since these 852 * buffers are physically addressed, they hang off the 853 * device vnode (devvp). As a result, they have no way 854 * of getting to the LFS superblock or lfs structure to 855 * keep track of the number of I/O's pending. So, I am 856 * going to stuff the fs into the saveaddr field of 857 * the buffer (yuk). 858 */ 859 cbp->b_saveaddr = (caddr_t)fs; 860 vop_strategy_a.a_desc = VDESC(vop_strategy); 861 vop_strategy_a.a_bp = cbp; 862 (strategy)(&vop_strategy_a); 863 } 864 /* 865 * XXX 866 * Vinvalbuf can move locked buffers off the locked queue 867 * and we have no way of knowing about this. So, after 868 * doing a big write, we recalculate how many bufers are 869 * really still left on the locked queue. 870 */ 871 locked_queue_count = count_lock_queue(); 872 wakeup(&locked_queue_count); 873 #ifdef DOSTATS 874 ++lfs_stats.psegwrites; 875 lfs_stats.blocktot += nblocks - 1; 876 if (fs->lfs_sp->seg_flags & SEGM_SYNC) 877 ++lfs_stats.psyncwrites; 878 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) { 879 ++lfs_stats.pcleanwrites; 880 lfs_stats.cleanblocks += nblocks - 1; 881 } 882 #endif 883 return (lfs_initseg(fs) || do_again); 884 } 885 886 void 887 lfs_writesuper(fs) 888 struct lfs *fs; 889 { 890 struct buf *bp; 891 dev_t i_dev; 892 int (*strategy) __P((struct vop_strategy_args *)); 893 int s; 894 struct vop_strategy_args vop_strategy_a; 895 896 i_dev = VTOI(fs->lfs_ivnode)->i_dev; 897 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)]; 898 899 /* Checksum the superblock and copy it into a buffer. */ 900 fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum)); 901 bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0], 902 LFS_SBPAD); 903 *(struct lfs *)bp->b_data = *fs; 904 905 /* XXX Toggle between first two superblocks; for now just write first */ 906 bp->b_dev = i_dev; 907 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC; 908 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI); 909 bp->b_iodone = lfs_supercallback; 910 vop_strategy_a.a_desc = VDESC(vop_strategy); 911 vop_strategy_a.a_bp = bp; 912 s = splbio(); 913 ++bp->b_vp->v_numoutput; 914 splx(s); 915 (strategy)(&vop_strategy_a); 916 } 917 918 /* 919 * Logical block number match routines used when traversing the dirty block 920 * chain. 921 */ 922 int 923 lfs_match_data(fs, bp) 924 struct lfs *fs; 925 struct buf *bp; 926 { 927 return (bp->b_lblkno >= 0); 928 } 929 930 int 931 lfs_match_indir(fs, bp) 932 struct lfs *fs; 933 struct buf *bp; 934 { 935 int lbn; 936 937 lbn = bp->b_lblkno; 938 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0); 939 } 940 941 int 942 lfs_match_dindir(fs, bp) 943 struct lfs *fs; 944 struct buf *bp; 945 { 946 int lbn; 947 948 lbn = bp->b_lblkno; 949 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1); 950 } 951 952 int 953 lfs_match_tindir(fs, bp) 954 struct lfs *fs; 955 struct buf *bp; 956 { 957 int lbn; 958 959 lbn = bp->b_lblkno; 960 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2); 961 } 962 963 /* 964 * Allocate a new buffer header. 965 */ 966 struct buf * 967 lfs_newbuf(vp, daddr, size) 968 struct vnode *vp; 969 ufs_daddr_t daddr; 970 size_t size; 971 { 972 struct buf *bp; 973 size_t nbytes; 974 975 nbytes = roundup(size, DEV_BSIZE); 976 bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK); 977 bzero(bp, sizeof(struct buf)); 978 if (nbytes) 979 bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK); 980 bgetvp(vp, bp); 981 bp->b_bufsize = size; 982 bp->b_bcount = size; 983 bp->b_lblkno = daddr; 984 bp->b_blkno = daddr; 985 bp->b_error = 0; 986 bp->b_resid = 0; 987 bp->b_iodone = lfs_callback; 988 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE; 989 return (bp); 990 } 991 992 void 993 lfs_callback(bp) 994 struct buf *bp; 995 { 996 struct lfs *fs; 997 998 fs = (struct lfs *)bp->b_saveaddr; 999 #ifdef DIAGNOSTIC 1000 if (fs->lfs_iocount == 0) 1001 panic("lfs_callback: zero iocount\n"); 1002 #endif 1003 if (--fs->lfs_iocount == 0) 1004 wakeup(&fs->lfs_iocount); 1005 1006 brelvp(bp); 1007 free(bp->b_data, M_SEGMENT); 1008 free(bp, M_SEGMENT); 1009 } 1010 1011 void 1012 lfs_supercallback(bp) 1013 struct buf *bp; 1014 { 1015 brelvp(bp); 1016 free(bp->b_data, M_SEGMENT); 1017 free(bp, M_SEGMENT); 1018 } 1019 1020 /* 1021 * Shellsort (diminishing increment sort) from Data Structures and 1022 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 1023 * see also Knuth Vol. 3, page 84. The increments are selected from 1024 * formula (8), page 95. Roughly O(N^3/2). 1025 */ 1026 /* 1027 * This is our own private copy of shellsort because we want to sort 1028 * two parallel arrays (the array of buffer pointers and the array of 1029 * logical block numbers) simultaneously. Note that we cast the array 1030 * of logical block numbers to a unsigned in this routine so that the 1031 * negative block numbers (meta data blocks) sort AFTER the data blocks. 1032 */ 1033 void 1034 lfs_shellsort(bp_array, lb_array, nmemb) 1035 struct buf **bp_array; 1036 ufs_daddr_t *lb_array; 1037 register int nmemb; 1038 { 1039 static int __rsshell_increments[] = { 4, 1, 0 }; 1040 register int incr, *incrp, t1, t2; 1041 struct buf *bp_temp; 1042 u_long lb_temp; 1043 1044 for (incrp = __rsshell_increments; incr = *incrp++;) 1045 for (t1 = incr; t1 < nmemb; ++t1) 1046 for (t2 = t1 - incr; t2 >= 0;) 1047 if (lb_array[t2] > lb_array[t2 + incr]) { 1048 lb_temp = lb_array[t2]; 1049 lb_array[t2] = lb_array[t2 + incr]; 1050 lb_array[t2 + incr] = lb_temp; 1051 bp_temp = bp_array[t2]; 1052 bp_array[t2] = bp_array[t2 + incr]; 1053 bp_array[t2 + incr] = bp_temp; 1054 t2 -= incr; 1055 } else 1056 break; 1057 } 1058 1059 /* 1060 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it. 1061 */ 1062 lfs_vref(vp) 1063 register struct vnode *vp; 1064 { 1065 1066 if (vp->v_flag & VXLOCK) 1067 return(1); 1068 return (vget(vp, 0)); 1069 } 1070 1071 void 1072 lfs_vunref(vp) 1073 register struct vnode *vp; 1074 { 1075 extern int lfs_no_inactive; 1076 1077 /* 1078 * This is vrele except that we do not want to VOP_INACTIVE 1079 * this vnode. Rather than inline vrele here, we use a global 1080 * flag to tell lfs_inactive not to run. Yes, its gross. 1081 */ 1082 lfs_no_inactive = 1; 1083 vrele(vp); 1084 lfs_no_inactive = 0; 1085 } 1086