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.1 (Berkeley) 06/11/93 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 *, 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 **, 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 int error, s; 95 96 fs = VFSTOUFS(vp->v_mount)->um_lfs; 97 if (fs->lfs_nactive > MAX_ACTIVE) 98 return(lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP)); 99 lfs_seglock(fs, SEGM_SYNC); 100 sp = fs->lfs_sp; 101 102 103 ip = VTOI(vp); 104 if (vp->v_dirtyblkhd.le_next == NULL) 105 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY); 106 107 do { 108 do { 109 if (vp->v_dirtyblkhd.le_next != NULL) 110 lfs_writefile(fs, sp, vp); 111 } while (lfs_writeinode(fs, sp, ip)); 112 113 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM); 114 115 #ifdef DOSTATS 116 ++lfs_stats.nwrites; 117 if (sp->seg_flags & SEGM_SYNC) 118 ++lfs_stats.nsync_writes; 119 if (sp->seg_flags & SEGM_CKP) 120 ++lfs_stats.ncheckpoints; 121 #endif 122 lfs_segunlock(fs); 123 return (0); 124 } 125 126 void 127 lfs_writevnodes(fs, mp, sp, op) 128 struct lfs *fs; 129 struct mount *mp; 130 struct segment *sp; 131 int op; 132 { 133 struct inode *ip; 134 struct vnode *vp; 135 int error, s, active; 136 137 loop: for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) { 138 /* 139 * If the vnode that we are about to sync is no longer 140 * associated with this mount point, start over. 141 */ 142 if (vp->v_mount != mp) 143 goto loop; 144 145 /* XXX ignore dirops for now 146 if (op == VN_DIROP && !(vp->v_flag & VDIROP) || 147 op != VN_DIROP && (vp->v_flag & VDIROP)) 148 continue; 149 */ 150 151 if (op == VN_EMPTY && vp->v_dirtyblkhd.le_next) 152 continue; 153 154 if (vp->v_type == VNON) 155 continue; 156 157 if (lfs_vref(vp)) 158 continue; 159 160 /* 161 * Write the inode/file if dirty and it's not the 162 * the IFILE. 163 */ 164 ip = VTOI(vp); 165 if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) || 166 vp->v_dirtyblkhd.le_next != NULL) && 167 ip->i_number != LFS_IFILE_INUM) { 168 if (vp->v_dirtyblkhd.le_next != NULL) 169 lfs_writefile(fs, sp, vp); 170 (void) lfs_writeinode(fs, sp, ip); 171 } 172 vp->v_flag &= ~VDIROP; 173 lfs_vunref(vp); 174 } 175 } 176 177 int 178 lfs_segwrite(mp, flags) 179 struct mount *mp; 180 int flags; /* Do a checkpoint. */ 181 { 182 struct buf *bp; 183 struct inode *ip; 184 struct lfs *fs; 185 struct segment *sp; 186 struct vnode *vp; 187 SEGUSE *segusep; 188 daddr_t ibno; 189 CLEANERINFO *cip; 190 int clean, error, i, s; 191 int do_ckp; 192 193 fs = VFSTOUFS(mp)->um_lfs; 194 195 /* 196 * If we have fewer than 2 clean segments, wait until cleaner 197 * writes. 198 */ 199 do { 200 LFS_CLEANERINFO(cip, fs, bp); 201 clean = cip->clean; 202 brelse(bp); 203 if (clean <= 2) { 204 printf ("segs clean: %d\n", clean); 205 wakeup(&lfs_allclean_wakeup); 206 if (error = tsleep(&fs->lfs_avail, PRIBIO + 1, 207 "lfs writer", 0)) 208 return (error); 209 } 210 } while (clean <= 2 ); 211 212 /* 213 * Allocate a segment structure and enough space to hold pointers to 214 * the maximum possible number of buffers which can be described in a 215 * single summary block. 216 */ 217 do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE; 218 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0)); 219 sp = fs->lfs_sp; 220 221 lfs_writevnodes(fs, mp, sp, VN_REG); 222 223 /* XXX ignore ordering of dirops for now */ 224 /* XXX 225 fs->lfs_writer = 1; 226 if (fs->lfs_dirops && (error = 227 tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) { 228 free(sp->bpp, M_SEGMENT); 229 free(sp, M_SEGMENT); 230 fs->lfs_writer = 0; 231 return (error); 232 } 233 234 lfs_writevnodes(fs, mp, sp, VN_DIROP); 235 */ 236 237 /* 238 * If we are doing a checkpoint, mark everything since the 239 * last checkpoint as no longer ACTIVE. 240 */ 241 if (do_ckp) 242 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz; 243 --ibno >= fs->lfs_cleansz; ) { 244 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, 245 NOCRED, &bp)) 246 247 panic("lfs: ifile read"); 248 segusep = (SEGUSE *)bp->b_un.b_addr; 249 for (i = fs->lfs_sepb; i--; segusep++) 250 segusep->su_flags &= ~SEGUSE_ACTIVE; 251 252 error = VOP_BWRITE(bp); 253 } 254 255 if (do_ckp || fs->lfs_doifile) { 256 redo: 257 vp = fs->lfs_ivnode; 258 while (vget(vp)); 259 ip = VTOI(vp); 260 if (vp->v_dirtyblkhd.le_next != NULL) 261 lfs_writefile(fs, sp, vp); 262 (void)lfs_writeinode(fs, sp, ip); 263 vput(vp); 264 if (lfs_writeseg(fs, sp) && do_ckp) 265 goto redo; 266 } else 267 (void) lfs_writeseg(fs, sp); 268 269 /* 270 * If the I/O count is non-zero, sleep until it reaches zero. At the 271 * moment, the user's process hangs around so we can sleep. 272 */ 273 /* XXX ignore dirops for now 274 fs->lfs_writer = 0; 275 fs->lfs_doifile = 0; 276 wakeup(&fs->lfs_dirops); 277 */ 278 279 #ifdef DOSTATS 280 ++lfs_stats.nwrites; 281 if (sp->seg_flags & SEGM_SYNC) 282 ++lfs_stats.nsync_writes; 283 if (sp->seg_flags & SEGM_CKP) 284 ++lfs_stats.ncheckpoints; 285 #endif 286 lfs_segunlock(fs); 287 return (0); 288 } 289 290 /* 291 * Write the dirty blocks associated with a vnode. 292 */ 293 void 294 lfs_writefile(fs, sp, vp) 295 struct lfs *fs; 296 struct segment *sp; 297 struct vnode *vp; 298 { 299 struct buf *bp; 300 struct finfo *fip; 301 IFILE *ifp; 302 303 if (sp->seg_bytes_left < fs->lfs_bsize || 304 sp->sum_bytes_left < sizeof(struct finfo)) 305 (void) lfs_writeseg(fs, sp); 306 307 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t); 308 ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 309 310 fip = sp->fip; 311 fip->fi_nblocks = 0; 312 fip->fi_ino = VTOI(vp)->i_number; 313 LFS_IENTRY(ifp, fs, fip->fi_ino, bp); 314 fip->fi_version = ifp->if_version; 315 brelse(bp); 316 317 /* 318 * It may not be necessary to write the meta-data blocks at this point, 319 * as the roll-forward recovery code should be able to reconstruct the 320 * list. 321 */ 322 lfs_gather(fs, sp, vp, lfs_match_data); 323 lfs_gather(fs, sp, vp, lfs_match_indir); 324 lfs_gather(fs, sp, vp, lfs_match_dindir); 325 #ifdef TRIPLE 326 lfs_gather(fs, sp, vp, lfs_match_tindir); 327 #endif 328 329 fip = sp->fip; 330 if (fip->fi_nblocks != 0) { 331 sp->fip = 332 (struct finfo *)((caddr_t)fip + sizeof(struct finfo) + 333 sizeof(daddr_t) * (fip->fi_nblocks - 1)); 334 sp->start_lbp = &sp->fip->fi_blocks[0]; 335 } else { 336 sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t); 337 --((SEGSUM *)(sp->segsum))->ss_nfinfo; 338 } 339 } 340 341 int 342 lfs_writeinode(fs, sp, ip) 343 struct lfs *fs; 344 struct segment *sp; 345 struct inode *ip; 346 { 347 struct buf *bp, *ibp; 348 IFILE *ifp; 349 SEGUSE *sup; 350 daddr_t daddr; 351 ino_t ino; 352 int error, i, ndx; 353 int redo_ifile = 0; 354 355 if (!(ip->i_flag & (IMOD | IACC | IUPD | ICHG))) 356 return(0); 357 358 /* Allocate a new inode block if necessary. */ 359 if (sp->ibp == NULL) { 360 /* Allocate a new segment if necessary. */ 361 if (sp->seg_bytes_left < fs->lfs_bsize || 362 sp->sum_bytes_left < sizeof(daddr_t)) 363 (void) lfs_writeseg(fs, sp); 364 365 /* Get next inode block. */ 366 daddr = fs->lfs_offset; 367 fs->lfs_offset += fsbtodb(fs, 1); 368 sp->ibp = *sp->cbpp++ = 369 lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, 370 fs->lfs_bsize); 371 /* Zero out inode numbers */ 372 for (i = 0; i < INOPB(fs); ++i) 373 sp->ibp->b_un.b_dino[i].di_inumber = 0; 374 ++sp->start_bpp; 375 fs->lfs_avail -= fsbtodb(fs, 1); 376 /* Set remaining space counters. */ 377 sp->seg_bytes_left -= fs->lfs_bsize; 378 sp->sum_bytes_left -= sizeof(daddr_t); 379 ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) - 380 sp->ninodes / INOPB(fs) - 1; 381 ((daddr_t *)(sp->segsum))[ndx] = daddr; 382 } 383 384 /* Update the inode times and copy the inode onto the inode page. */ 385 if (ip->i_flag & IMOD) 386 --fs->lfs_uinodes; 387 ITIMES(ip, &time, &time); 388 ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG); 389 bp = sp->ibp; 390 bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din; 391 /* Increment inode count in segment summary block. */ 392 ++((SEGSUM *)(sp->segsum))->ss_ninos; 393 394 /* If this page is full, set flag to allocate a new page. */ 395 if (++sp->ninodes % INOPB(fs) == 0) 396 sp->ibp = NULL; 397 398 /* 399 * If updating the ifile, update the super-block. Update the disk 400 * address and access times for this inode in the ifile. 401 */ 402 ino = ip->i_number; 403 if (ino == LFS_IFILE_INUM) { 404 daddr = fs->lfs_idaddr; 405 fs->lfs_idaddr = bp->b_blkno; 406 } else { 407 LFS_IENTRY(ifp, fs, ino, ibp); 408 daddr = ifp->if_daddr; 409 ifp->if_daddr = bp->b_blkno; 410 error = VOP_BWRITE(ibp); 411 } 412 413 /* 414 * No need to update segment usage if there was no former inode address 415 * or if the last inode address is in the current partial segment. 416 */ 417 if (daddr != LFS_UNUSED_DADDR && 418 !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) { 419 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 420 #ifdef DIAGNOSTIC 421 if (sup->su_nbytes < sizeof(struct dinode)) { 422 /* XXX -- Change to a panic. */ 423 printf("lfs: negative bytes (segment %d)\n", 424 datosn(fs, daddr)); 425 panic("negative bytes"); 426 } 427 #endif 428 sup->su_nbytes -= sizeof(struct dinode); 429 redo_ifile = 430 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED)); 431 error = VOP_BWRITE(bp); 432 } 433 return (redo_ifile); 434 } 435 436 int 437 lfs_gatherblock(sp, bp, sptr) 438 struct segment *sp; 439 struct buf *bp; 440 int *sptr; 441 { 442 struct lfs *fs; 443 int version; 444 445 /* 446 * If full, finish this segment. We may be doing I/O, so 447 * release and reacquire the splbio(). 448 */ 449 #ifdef DIAGNOSTIC 450 if (sp->vp == NULL) 451 panic ("lfs_gatherblock: Null vp in segment"); 452 #endif 453 fs = sp->fs; 454 if (sp->sum_bytes_left < sizeof(daddr_t) || 455 sp->seg_bytes_left < fs->lfs_bsize) { 456 if (sptr) 457 splx(*sptr); 458 lfs_updatemeta(sp); 459 460 version = sp->fip->fi_version; 461 (void) lfs_writeseg(fs, sp); 462 463 sp->fip->fi_version = version; 464 sp->fip->fi_ino = VTOI(sp->vp)->i_number; 465 /* Add the current file to the segment summary. */ 466 ++((SEGSUM *)(sp->segsum))->ss_nfinfo; 467 sp->sum_bytes_left -= 468 sizeof(struct finfo) - sizeof(daddr_t); 469 470 if (sptr) 471 *sptr = splbio(); 472 return(1); 473 } 474 475 /* Insert into the buffer list, update the FINFO block. */ 476 bp->b_flags |= B_GATHERED; 477 *sp->cbpp++ = bp; 478 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno; 479 480 sp->sum_bytes_left -= sizeof(daddr_t); 481 sp->seg_bytes_left -= fs->lfs_bsize; 482 return(0); 483 } 484 485 void 486 lfs_gather(fs, sp, vp, match) 487 struct lfs *fs; 488 struct segment *sp; 489 struct vnode *vp; 490 int (*match) __P((struct lfs *, struct buf *)); 491 { 492 struct buf *bp; 493 int s; 494 495 sp->vp = vp; 496 s = splbio(); 497 loop: for (bp = vp->v_dirtyblkhd.le_next; bp; bp = bp->b_vnbufs.qe_next) { 498 if (bp->b_flags & B_BUSY || !match(fs, bp) || 499 bp->b_flags & B_GATHERED) 500 continue; 501 #ifdef DIAGNOSTIC 502 if (!(bp->b_flags & B_DELWRI)) 503 panic("lfs_gather: bp not B_DELWRI"); 504 if (!(bp->b_flags & B_LOCKED)) 505 panic("lfs_gather: bp not B_LOCKED"); 506 #endif 507 if (lfs_gatherblock(sp, bp, &s)) 508 goto loop; 509 } 510 splx(s); 511 lfs_updatemeta(sp); 512 sp->vp = NULL; 513 } 514 515 516 /* 517 * Update the metadata that points to the blocks listed in the FINFO 518 * array. 519 */ 520 void 521 lfs_updatemeta(sp) 522 struct segment *sp; 523 { 524 SEGUSE *sup; 525 struct buf *bp; 526 struct lfs *fs; 527 struct vnode *vp; 528 struct indir a[NIADDR + 2], *ap; 529 struct inode *ip; 530 daddr_t daddr, lbn, off; 531 int db_per_fsb, error, i, nblocks, num; 532 533 vp = sp->vp; 534 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp; 535 if (vp == NULL || nblocks == 0) 536 return; 537 538 /* Sort the blocks. */ 539 if (!(sp->seg_flags & SEGM_CLEAN)) 540 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks); 541 542 /* 543 * Assign disk addresses, and update references to the logical 544 * block and the segment usage information. 545 */ 546 fs = sp->fs; 547 db_per_fsb = fsbtodb(fs, 1); 548 for (i = nblocks; i--; ++sp->start_bpp) { 549 lbn = *sp->start_lbp++; 550 (*sp->start_bpp)->b_blkno = off = fs->lfs_offset; 551 fs->lfs_offset += db_per_fsb; 552 553 if (error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL)) 554 panic("lfs_updatemeta: ufs_bmaparray %d", error); 555 ip = VTOI(vp); 556 switch (num) { 557 case 0: 558 ip->i_db[lbn] = off; 559 break; 560 case 1: 561 ip->i_ib[a[0].in_off] = off; 562 break; 563 default: 564 ap = &a[num - 1]; 565 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 566 panic("lfs_updatemeta: bread bno %d", 567 ap->in_lbn); 568 /* 569 * Bread may create a new indirect block which needs 570 * to get counted for the inode. 571 */ 572 if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) { 573 printf ("Updatemeta allocating indirect block: shouldn't happen\n"); 574 ip->i_blocks += btodb(fs->lfs_bsize); 575 fs->lfs_bfree -= btodb(fs->lfs_bsize); 576 } 577 bp->b_un.b_daddr[ap->in_off] = off; 578 VOP_BWRITE(bp); 579 } 580 581 /* Update segment usage information. */ 582 if (daddr != UNASSIGNED && 583 !(daddr >= fs->lfs_lastpseg && daddr <= off)) { 584 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp); 585 #ifdef DIAGNOSTIC 586 if (sup->su_nbytes < fs->lfs_bsize) { 587 /* XXX -- Change to a panic. */ 588 printf("lfs: negative bytes (segment %d)\n", 589 datosn(fs, daddr)); 590 panic ("Negative Bytes"); 591 } 592 #endif 593 sup->su_nbytes -= fs->lfs_bsize; 594 error = VOP_BWRITE(bp); 595 } 596 } 597 } 598 599 /* 600 * Start a new segment. 601 */ 602 int 603 lfs_initseg(fs) 604 struct lfs *fs; 605 { 606 struct segment *sp; 607 SEGUSE *sup; 608 SEGSUM *ssp; 609 struct buf *bp; 610 daddr_t lbn, *lbnp; 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_un.b_addr; 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 *)(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, error, 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, error, 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++ = (*bpp)->b_un.b_words[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_un.b_addr; 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_un.b_addr, 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_un.b_addr, 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 - cbp->b_un.b_addr; 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 *bp->b_un.b_lfs = *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 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_un.b_addr = 980 malloc(nbytes, M_SEGMENT, M_WAITOK); 981 bgetvp(vp, bp); 982 bp->b_bufsize = size; 983 bp->b_bcount = size; 984 bp->b_lblkno = daddr; 985 bp->b_blkno = daddr; 986 bp->b_error = 0; 987 bp->b_resid = 0; 988 bp->b_iodone = lfs_callback; 989 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE; 990 return (bp); 991 } 992 993 void 994 lfs_callback(bp) 995 struct buf *bp; 996 { 997 struct lfs *fs; 998 999 fs = (struct lfs *)bp->b_saveaddr; 1000 #ifdef DIAGNOSTIC 1001 if (fs->lfs_iocount == 0) 1002 panic("lfs_callback: zero iocount\n"); 1003 #endif 1004 if (--fs->lfs_iocount == 0) 1005 wakeup(&fs->lfs_iocount); 1006 1007 brelvp(bp); 1008 free(bp->b_un.b_addr, M_SEGMENT); 1009 free(bp, M_SEGMENT); 1010 } 1011 1012 void 1013 lfs_supercallback(bp) 1014 struct buf *bp; 1015 { 1016 brelvp(bp); 1017 free(bp->b_un.b_addr, M_SEGMENT); 1018 free(bp, M_SEGMENT); 1019 } 1020 1021 /* 1022 * Shellsort (diminishing increment sort) from Data Structures and 1023 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 1024 * see also Knuth Vol. 3, page 84. The increments are selected from 1025 * formula (8), page 95. Roughly O(N^3/2). 1026 */ 1027 /* 1028 * This is our own private copy of shellsort because we want to sort 1029 * two parallel arrays (the array of buffer pointers and the array of 1030 * logical block numbers) simultaneously. Note that we cast the array 1031 * of logical block numbers to a unsigned in this routine so that the 1032 * negative block numbers (meta data blocks) sort AFTER the data blocks. 1033 */ 1034 void 1035 lfs_shellsort(bp_array, lb_array, nmemb) 1036 struct buf **bp_array; 1037 daddr_t *lb_array; 1038 register int nmemb; 1039 { 1040 static int __rsshell_increments[] = { 4, 1, 0 }; 1041 register int incr, *incrp, t1, t2; 1042 struct buf *bp_temp; 1043 u_long lb_temp; 1044 1045 for (incrp = __rsshell_increments; incr = *incrp++;) 1046 for (t1 = incr; t1 < nmemb; ++t1) 1047 for (t2 = t1 - incr; t2 >= 0;) 1048 if (lb_array[t2] > lb_array[t2 + incr]) { 1049 lb_temp = lb_array[t2]; 1050 lb_array[t2] = lb_array[t2 + incr]; 1051 lb_array[t2 + incr] = lb_temp; 1052 bp_temp = bp_array[t2]; 1053 bp_array[t2] = bp_array[t2 + incr]; 1054 bp_array[t2 + incr] = bp_temp; 1055 t2 -= incr; 1056 } else 1057 break; 1058 } 1059 1060 /* 1061 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, bump the 1062 * ref count, removing the vnode from the free list if it is on it. 1063 */ 1064 lfs_vref(vp) 1065 register struct vnode *vp; 1066 { 1067 register struct vnode *vq; 1068 extern struct vnode *vfreeh; 1069 extern struct vnode **vfreet; 1070 1071 if (vp->v_flag & VXLOCK) 1072 return(1); 1073 1074 if (vp->v_usecount == 0) { 1075 if (vq = vp->v_freef) 1076 vq->v_freeb = vp->v_freeb; 1077 else 1078 vfreet = vp->v_freeb; 1079 *vp->v_freeb = vq; 1080 vp->v_freef = NULL; 1081 vp->v_freeb = NULL; 1082 } 1083 VREF(vp); 1084 return (0); 1085 } 1086 1087 void 1088 lfs_vunref(vp) 1089 register struct vnode *vp; 1090 { 1091 extern struct vnode *vfreeh; 1092 extern struct vnode **vfreet; 1093 1094 --vp->v_usecount; 1095 1096 /* 1097 * return to free list 1098 */ 1099 if (vp->v_usecount == 0) { 1100 *vfreet = vp; 1101 vp->v_freeb = vfreet; 1102 vp->v_freef = NULL; 1103 vfreet = &vp->v_freef; 1104 } 1105 return; 1106 } 1107