1 /* $NetBSD: segwrite.c,v 1.3 2003/04/02 10:39:28 fvdl Exp $ */ 2 /*- 3 * Copyright (c) 2003 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Konrad E. Schroder <perseant@hhhh.org>. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the NetBSD 20 * Foundation, Inc. and its contributors. 21 * 4. Neither the name of The NetBSD Foundation nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 /* 38 * Copyright (c) 1991, 1993 39 * The Regents of the University of California. All rights reserved. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. All advertising materials mentioning features or use of this software 50 * must display the following acknowledgement: 51 * This product includes software developed by the University of 52 * California, Berkeley and its contributors. 53 * 4. Neither the name of the University nor the names of its contributors 54 * may be used to endorse or promote products derived from this software 55 * without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * SUCH DAMAGE. 68 * 69 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95 70 */ 71 72 /* 73 * Partial segment writer, taken from the kernel and adapted for userland. 74 */ 75 #include <sys/types.h> 76 #include <sys/param.h> 77 #include <sys/time.h> 78 #include <sys/buf.h> 79 #include <sys/mount.h> 80 81 #include <ufs/ufs/inode.h> 82 #include <ufs/ufs/ufsmount.h> 83 84 /* Override certain things to make <ufs/lfs/lfs.h> work */ 85 #undef simple_lock 86 #define simple_lock(x) 87 #undef simple_unlock 88 #define simple_unlock(x) 89 #define vnode uvnode 90 #define buf ubuf 91 #define panic call_panic 92 93 #include <ufs/lfs/lfs.h> 94 95 #include <assert.h> 96 #include <stdio.h> 97 #include <stdlib.h> 98 #include <string.h> 99 #include <err.h> 100 #include <errno.h> 101 102 #include "bufcache.h" 103 #include "vnode.h" 104 #include "lfs.h" 105 #include "segwrite.h" 106 107 /* Compatibility definitions */ 108 extern off_t locked_queue_bytes; 109 int locked_queue_count; 110 off_t written_bytes = 0; 111 off_t written_data = 0; 112 off_t written_indir = 0; 113 off_t written_dev = 0; 114 int written_inodes = 0; 115 116 /* Global variables */ 117 time_t write_time; 118 119 extern u_int32_t cksum(void *, size_t); 120 extern u_int32_t lfs_sb_cksum(struct dlfs *); 121 122 /* 123 * Logical block number match routines used when traversing the dirty block 124 * chain. 125 */ 126 int 127 lfs_match_data(struct lfs * fs, struct ubuf * bp) 128 { 129 return (bp->b_lblkno >= 0); 130 } 131 132 int 133 lfs_match_indir(struct lfs * fs, struct ubuf * bp) 134 { 135 daddr_t lbn; 136 137 lbn = bp->b_lblkno; 138 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0); 139 } 140 141 int 142 lfs_match_dindir(struct lfs * fs, struct ubuf * bp) 143 { 144 daddr_t lbn; 145 146 lbn = bp->b_lblkno; 147 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1); 148 } 149 150 int 151 lfs_match_tindir(struct lfs * fs, struct ubuf * bp) 152 { 153 daddr_t lbn; 154 155 lbn = bp->b_lblkno; 156 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2); 157 } 158 159 /* 160 * Do a checkpoint. 161 */ 162 int 163 lfs_segwrite(struct lfs * fs, int flags) 164 { 165 struct inode *ip; 166 struct segment *sp; 167 struct uvnode *vp; 168 int redo; 169 170 lfs_seglock(fs, flags | SEGM_CKP); 171 sp = fs->lfs_sp; 172 173 lfs_writevnodes(fs, sp, VN_REG); 174 lfs_writevnodes(fs, sp, VN_DIROP); 175 ((SEGSUM *) (sp->segsum))->ss_flags &= ~(SS_CONT); 176 177 do { 178 vp = fs->lfs_ivnode; 179 fs->lfs_flags &= ~LFS_IFDIRTY; 180 ip = VTOI(vp); 181 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL) 182 lfs_writefile(fs, sp, vp); 183 184 redo = lfs_writeinode(fs, sp, ip); 185 redo += lfs_writeseg(fs, sp); 186 redo += (fs->lfs_flags & LFS_IFDIRTY); 187 } while (redo); 188 189 lfs_segunlock(fs); 190 #if 0 191 printf("wrote %" PRId64 " bytes (%" PRId32 " fsb)\n", 192 written_bytes, (ufs_daddr_t)btofsb(fs, written_bytes)); 193 printf("wrote %" PRId64 " bytes data (%" PRId32 " fsb)\n", 194 written_data, (ufs_daddr_t)btofsb(fs, written_data)); 195 printf("wrote %" PRId64 " bytes indir (%" PRId32 " fsb)\n", 196 written_indir, (ufs_daddr_t)btofsb(fs, written_indir)); 197 printf("wrote %" PRId64 " bytes dev (%" PRId32 " fsb)\n", 198 written_dev, (ufs_daddr_t)btofsb(fs, written_dev)); 199 printf("wrote %d inodes (%" PRId32 " fsb)\n", 200 written_inodes, btofsb(fs, written_inodes * fs->lfs_ibsize)); 201 #endif 202 return 0; 203 } 204 205 /* 206 * Write the dirty blocks associated with a vnode. 207 */ 208 void 209 lfs_writefile(struct lfs * fs, struct segment * sp, struct uvnode * vp) 210 { 211 struct ubuf *bp; 212 struct finfo *fip; 213 struct inode *ip; 214 IFILE *ifp; 215 216 ip = VTOI(vp); 217 218 if (sp->seg_bytes_left < fs->lfs_bsize || 219 sp->sum_bytes_left < sizeof(struct finfo)) 220 (void) lfs_writeseg(fs, sp); 221 222 sp->sum_bytes_left -= FINFOSIZE; 223 ++((SEGSUM *) (sp->segsum))->ss_nfinfo; 224 225 if (vp->v_flag & VDIROP) 226 ((SEGSUM *) (sp->segsum))->ss_flags |= (SS_DIROP | SS_CONT); 227 228 fip = sp->fip; 229 fip->fi_nblocks = 0; 230 fip->fi_ino = ip->i_number; 231 LFS_IENTRY(ifp, fs, fip->fi_ino, bp); 232 fip->fi_version = ifp->if_version; 233 brelse(bp); 234 235 lfs_gather(fs, sp, vp, lfs_match_data); 236 lfs_gather(fs, sp, vp, lfs_match_indir); 237 lfs_gather(fs, sp, vp, lfs_match_dindir); 238 lfs_gather(fs, sp, vp, lfs_match_tindir); 239 240 fip = sp->fip; 241 if (fip->fi_nblocks != 0) { 242 sp->fip = (FINFO *) ((caddr_t) fip + FINFOSIZE + 243 sizeof(ufs_daddr_t) * (fip->fi_nblocks)); 244 sp->start_lbp = &sp->fip->fi_blocks[0]; 245 } else { 246 sp->sum_bytes_left += FINFOSIZE; 247 --((SEGSUM *) (sp->segsum))->ss_nfinfo; 248 } 249 } 250 251 int 252 lfs_writeinode(struct lfs * fs, struct segment * sp, struct inode * ip) 253 { 254 struct ubuf *bp, *ibp; 255 struct ufs1_dinode *cdp; 256 IFILE *ifp; 257 SEGUSE *sup; 258 daddr_t daddr; 259 ino_t ino; 260 int error, i, ndx, fsb = 0; 261 int redo_ifile = 0; 262 struct timespec ts; 263 int gotblk = 0; 264 265 /* Allocate a new inode block if necessary. */ 266 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) && 267 sp->ibp == NULL) { 268 /* Allocate a new segment if necessary. */ 269 if (sp->seg_bytes_left < fs->lfs_ibsize || 270 sp->sum_bytes_left < sizeof(ufs_daddr_t)) 271 (void) lfs_writeseg(fs, sp); 272 273 /* Get next inode block. */ 274 daddr = fs->lfs_offset; 275 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize); 276 sp->ibp = *sp->cbpp++ = 277 getblk(fs->lfs_unlockvp, fsbtodb(fs, daddr), 278 fs->lfs_ibsize); 279 sp->ibp->b_flags |= B_GATHERED; 280 gotblk++; 281 282 /* Zero out inode numbers */ 283 for (i = 0; i < INOPB(fs); ++i) 284 ((struct ufs1_dinode *) sp->ibp->b_data)[i].di_inumber = 0; 285 286 ++sp->start_bpp; 287 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize); 288 /* Set remaining space counters. */ 289 sp->seg_bytes_left -= fs->lfs_ibsize; 290 sp->sum_bytes_left -= sizeof(ufs_daddr_t); 291 ndx = fs->lfs_sumsize / sizeof(ufs_daddr_t) - 292 sp->ninodes / INOPB(fs) - 1; 293 ((ufs_daddr_t *) (sp->segsum))[ndx] = daddr; 294 } 295 /* Update the inode times and copy the inode onto the inode page. */ 296 ts.tv_nsec = 0; 297 ts.tv_sec = write_time; 298 /* XXX kludge --- don't redirty the ifile just to put times on it */ 299 if (ip->i_number != LFS_IFILE_INUM) 300 LFS_ITIMES(ip, &ts, &ts, &ts); 301 302 /* 303 * If this is the Ifile, and we've already written the Ifile in this 304 * partial segment, just overwrite it (it's not on disk yet) and 305 * continue. 306 * 307 * XXX we know that the bp that we get the second time around has 308 * already been gathered. 309 */ 310 if (ip->i_number == LFS_IFILE_INUM && sp->idp) { 311 *(sp->idp) = *ip->i_din.ffs1_din; 312 ip->i_lfs_osize = ip->i_ffs1_size; 313 return 0; 314 } 315 bp = sp->ibp; 316 cdp = ((struct ufs1_dinode *) bp->b_data) + (sp->ninodes % INOPB(fs)); 317 *cdp = *ip->i_din.ffs1_din; 318 319 /* If all blocks are goig to disk, update the "size on disk" */ 320 ip->i_lfs_osize = ip->i_ffs1_size; 321 322 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */ 323 sp->idp = ((struct ufs1_dinode *) bp->b_data) + 324 (sp->ninodes % INOPB(fs)); 325 if (gotblk) { 326 LFS_LOCK_BUF(bp); 327 brelse(bp); 328 } 329 /* Increment inode count in segment summary block. */ 330 ++((SEGSUM *) (sp->segsum))->ss_ninos; 331 332 /* If this page is full, set flag to allocate a new page. */ 333 if (++sp->ninodes % INOPB(fs) == 0) 334 sp->ibp = NULL; 335 336 /* 337 * If updating the ifile, update the super-block. Update the disk 338 * address and access times for this inode in the ifile. 339 */ 340 ino = ip->i_number; 341 if (ino == LFS_IFILE_INUM) { 342 daddr = fs->lfs_idaddr; 343 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno); 344 } else { 345 LFS_IENTRY(ifp, fs, ino, ibp); 346 daddr = ifp->if_daddr; 347 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb; 348 error = LFS_BWRITE_LOG(ibp); /* Ifile */ 349 } 350 351 /* 352 * Account the inode: it no longer belongs to its former segment, 353 * though it will not belong to the new segment until that segment 354 * is actually written. 355 */ 356 if (daddr != LFS_UNUSED_DADDR) { 357 u_int32_t oldsn = dtosn(fs, daddr); 358 LFS_SEGENTRY(sup, fs, oldsn, bp); 359 sup->su_nbytes -= DINODE1_SIZE; 360 redo_ifile = 361 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED)); 362 if (redo_ifile) 363 fs->lfs_flags |= LFS_IFDIRTY; 364 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */ 365 } 366 return redo_ifile; 367 } 368 369 int 370 lfs_gatherblock(struct segment * sp, struct ubuf * bp) 371 { 372 struct lfs *fs; 373 int version; 374 int j, blksinblk; 375 376 /* 377 * If full, finish this segment. We may be doing I/O, so 378 * release and reacquire the splbio(). 379 */ 380 fs = sp->fs; 381 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize); 382 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) * blksinblk || 383 sp->seg_bytes_left < bp->b_bcount) { 384 lfs_updatemeta(sp); 385 386 version = sp->fip->fi_version; 387 (void) lfs_writeseg(fs, sp); 388 389 sp->fip->fi_version = version; 390 sp->fip->fi_ino = VTOI(sp->vp)->i_number; 391 /* Add the current file to the segment summary. */ 392 ++((SEGSUM *) (sp->segsum))->ss_nfinfo; 393 sp->sum_bytes_left -= FINFOSIZE; 394 395 return 1; 396 } 397 /* Insert into the buffer list, update the FINFO block. */ 398 bp->b_flags |= B_GATHERED; 399 /* bp->b_flags &= ~B_DONE; */ 400 401 *sp->cbpp++ = bp; 402 for (j = 0; j < blksinblk; j++) 403 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j; 404 405 sp->sum_bytes_left -= sizeof(ufs_daddr_t) * blksinblk; 406 sp->seg_bytes_left -= bp->b_bcount; 407 return 0; 408 } 409 410 int 411 lfs_gather(struct lfs * fs, struct segment * sp, struct uvnode * vp, int (*match) (struct lfs *, struct ubuf *)) 412 { 413 struct ubuf *bp, *nbp; 414 int count = 0; 415 416 sp->vp = vp; 417 loop: 418 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 419 nbp = LIST_NEXT(bp, b_vnbufs); 420 421 assert(bp->b_flags & B_DELWRI); 422 if ((bp->b_flags & (B_BUSY | B_GATHERED)) || !match(fs, bp)) { 423 continue; 424 } 425 if (lfs_gatherblock(sp, bp)) { 426 goto loop; 427 } 428 count++; 429 } 430 431 lfs_updatemeta(sp); 432 sp->vp = NULL; 433 return count; 434 } 435 436 437 /* 438 * Change the given block's address to ndaddr, finding its previous 439 * location using ufs_bmaparray(). 440 * 441 * Account for this change in the segment table. 442 */ 443 void 444 lfs_update_single(struct lfs * fs, struct segment * sp, daddr_t lbn, 445 ufs_daddr_t ndaddr, int size) 446 { 447 SEGUSE *sup; 448 struct ubuf *bp; 449 struct indir a[NIADDR + 2], *ap; 450 struct inode *ip; 451 struct uvnode *vp; 452 daddr_t daddr, ooff; 453 int num, error; 454 int bb, osize, obb; 455 456 vp = sp->vp; 457 ip = VTOI(vp); 458 459 error = ufs_bmaparray(fs, vp, lbn, &daddr, a, &num); 460 if (error) 461 errx(1, "lfs_updatemeta: ufs_bmaparray returned %d looking up lbn %" PRId64 "\n", error, lbn); 462 if (daddr > 0) 463 daddr = dbtofsb(fs, daddr); 464 465 bb = fragstofsb(fs, numfrags(fs, size)); 466 switch (num) { 467 case 0: 468 ooff = ip->i_ffs1_db[lbn]; 469 if (ooff == UNWRITTEN) 470 ip->i_ffs1_blocks += bb; 471 else { 472 /* possible fragment truncation or extension */ 473 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]); 474 ip->i_ffs1_blocks += (bb - obb); 475 } 476 ip->i_ffs1_db[lbn] = ndaddr; 477 break; 478 case 1: 479 ooff = ip->i_ffs1_ib[a[0].in_off]; 480 if (ooff == UNWRITTEN) 481 ip->i_ffs1_blocks += bb; 482 ip->i_ffs1_ib[a[0].in_off] = ndaddr; 483 break; 484 default: 485 ap = &a[num - 1]; 486 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NULL, &bp)) 487 errx(1, "lfs_updatemeta: bread bno %" PRId64, 488 ap->in_lbn); 489 490 ooff = ((ufs_daddr_t *) bp->b_data)[ap->in_off]; 491 if (ooff == UNWRITTEN) 492 ip->i_ffs1_blocks += bb; 493 ((ufs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr; 494 (void) VOP_BWRITE(bp); 495 } 496 497 /* 498 * Update segment usage information, based on old size 499 * and location. 500 */ 501 if (daddr > 0) { 502 u_int32_t oldsn = dtosn(fs, daddr); 503 if (lbn >= 0 && lbn < NDADDR) 504 osize = ip->i_lfs_fragsize[lbn]; 505 else 506 osize = fs->lfs_bsize; 507 LFS_SEGENTRY(sup, fs, oldsn, bp); 508 sup->su_nbytes -= osize; 509 if (!(bp->b_flags & B_GATHERED)) 510 fs->lfs_flags |= LFS_IFDIRTY; 511 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); 512 } 513 /* 514 * Now that this block has a new address, and its old 515 * segment no longer owns it, we can forget about its 516 * old size. 517 */ 518 if (lbn >= 0 && lbn < NDADDR) 519 ip->i_lfs_fragsize[lbn] = size; 520 } 521 522 /* 523 * Update the metadata that points to the blocks listed in the FINFO 524 * array. 525 */ 526 void 527 lfs_updatemeta(struct segment * sp) 528 { 529 struct ubuf *sbp; 530 struct lfs *fs; 531 struct uvnode *vp; 532 daddr_t lbn; 533 int i, nblocks, num; 534 int bb; 535 int bytesleft, size; 536 537 vp = sp->vp; 538 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp; 539 540 if (vp == NULL || nblocks == 0) 541 return; 542 543 /* 544 * This count may be high due to oversize blocks from lfs_gop_write. 545 * Correct for this. (XXX we should be able to keep track of these.) 546 */ 547 fs = sp->fs; 548 for (i = 0; i < nblocks; i++) { 549 if (sp->start_bpp[i] == NULL) { 550 printf("nblocks = %d, not %d\n", i, nblocks); 551 nblocks = i; 552 break; 553 } 554 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize); 555 nblocks -= num - 1; 556 } 557 558 /* 559 * Sort the blocks. 560 */ 561 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize); 562 563 /* 564 * Record the length of the last block in case it's a fragment. 565 * If there are indirect blocks present, they sort last. An 566 * indirect block will be lfs_bsize and its presence indicates 567 * that you cannot have fragments. 568 */ 569 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) & 570 fs->lfs_bmask) + 1; 571 572 /* 573 * Assign disk addresses, and update references to the logical 574 * block and the segment usage information. 575 */ 576 for (i = nblocks; i--; ++sp->start_bpp) { 577 sbp = *sp->start_bpp; 578 lbn = *sp->start_lbp; 579 580 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset); 581 582 /* 583 * If we write a frag in the wrong place, the cleaner won't 584 * be able to correctly identify its size later, and the 585 * segment will be uncleanable. (Even worse, it will assume 586 * that the indirect block that actually ends the list 587 * is of a smaller size!) 588 */ 589 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0) 590 errx(1, "lfs_updatemeta: fragment is not last block"); 591 592 /* 593 * For each subblock in this possibly oversized block, 594 * update its address on disk. 595 */ 596 for (bytesleft = sbp->b_bcount; bytesleft > 0; 597 bytesleft -= fs->lfs_bsize) { 598 size = MIN(bytesleft, fs->lfs_bsize); 599 bb = fragstofsb(fs, numfrags(fs, size)); 600 lbn = *sp->start_lbp++; 601 lfs_update_single(fs, sp, lbn, fs->lfs_offset, size); 602 fs->lfs_offset += bb; 603 } 604 605 } 606 } 607 608 /* 609 * Start a new segment. 610 */ 611 int 612 lfs_initseg(struct lfs * fs) 613 { 614 struct segment *sp; 615 SEGUSE *sup; 616 SEGSUM *ssp; 617 struct ubuf *bp, *sbp; 618 int repeat; 619 620 sp = fs->lfs_sp; 621 622 repeat = 0; 623 624 /* Advance to the next segment. */ 625 if (!LFS_PARTIAL_FITS(fs)) { 626 /* lfs_avail eats the remaining space */ 627 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset - 628 fs->lfs_curseg); 629 lfs_newseg(fs); 630 repeat = 1; 631 fs->lfs_offset = fs->lfs_curseg; 632 633 sp->seg_number = dtosn(fs, fs->lfs_curseg); 634 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg); 635 636 /* 637 * If the segment contains a superblock, update the offset 638 * and summary address to skip over it. 639 */ 640 LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 641 if (sup->su_flags & SEGUSE_SUPERBLOCK) { 642 fs->lfs_offset += btofsb(fs, LFS_SBPAD); 643 sp->seg_bytes_left -= LFS_SBPAD; 644 } 645 brelse(bp); 646 /* Segment zero could also contain the labelpad */ 647 if (fs->lfs_version > 1 && sp->seg_number == 0 && 648 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) { 649 fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start; 650 sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start); 651 } 652 } else { 653 sp->seg_number = dtosn(fs, fs->lfs_curseg); 654 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg - 655 (fs->lfs_offset - fs->lfs_curseg)); 656 } 657 fs->lfs_lastpseg = fs->lfs_offset; 658 659 sp->fs = fs; 660 sp->ibp = NULL; 661 sp->idp = NULL; 662 sp->ninodes = 0; 663 sp->ndupino = 0; 664 665 /* Get a new buffer for SEGSUM and enter it into the buffer list. */ 666 sp->cbpp = sp->bpp; 667 sbp = *sp->cbpp = getblk(fs->lfs_unlockvp, 668 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize); 669 sp->segsum = sbp->b_data; 670 memset(sp->segsum, 0, fs->lfs_sumsize); 671 sp->start_bpp = ++sp->cbpp; 672 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize); 673 674 /* Set point to SEGSUM, initialize it. */ 675 ssp = sp->segsum; 676 ssp->ss_next = fs->lfs_nextseg; 677 ssp->ss_nfinfo = ssp->ss_ninos = 0; 678 ssp->ss_magic = SS_MAGIC; 679 680 /* Set pointer to first FINFO, initialize it. */ 681 sp->fip = (struct finfo *) ((caddr_t) sp->segsum + SEGSUM_SIZE(fs)); 682 sp->fip->fi_nblocks = 0; 683 sp->start_lbp = &sp->fip->fi_blocks[0]; 684 sp->fip->fi_lastlength = 0; 685 686 sp->seg_bytes_left -= fs->lfs_sumsize; 687 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs); 688 689 LFS_LOCK_BUF(sbp); 690 brelse(sbp); 691 return repeat; 692 } 693 694 /* 695 * Return the next segment to write. 696 */ 697 void 698 lfs_newseg(struct lfs * fs) 699 { 700 CLEANERINFO *cip; 701 SEGUSE *sup; 702 struct ubuf *bp; 703 int curseg, isdirty, sn; 704 705 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp); 706 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE; 707 sup->su_nbytes = 0; 708 sup->su_nsums = 0; 709 sup->su_ninos = 0; 710 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp); 711 712 LFS_CLEANERINFO(cip, fs, bp); 713 --cip->clean; 714 ++cip->dirty; 715 fs->lfs_nclean = cip->clean; 716 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1); 717 718 fs->lfs_lastseg = fs->lfs_curseg; 719 fs->lfs_curseg = fs->lfs_nextseg; 720 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) { 721 sn = (sn + 1) % fs->lfs_nseg; 722 if (sn == curseg) 723 errx(1, "lfs_nextseg: no clean segments"); 724 LFS_SEGENTRY(sup, fs, sn, bp); 725 isdirty = sup->su_flags & SEGUSE_DIRTY; 726 brelse(bp); 727 728 if (!isdirty) 729 break; 730 } 731 732 ++fs->lfs_nactive; 733 fs->lfs_nextseg = sntod(fs, sn); 734 } 735 736 737 int 738 lfs_writeseg(struct lfs * fs, struct segment * sp) 739 { 740 struct ubuf **bpp, *bp; 741 SEGUSE *sup; 742 SEGSUM *ssp; 743 char *datap, *dp; 744 int i; 745 int do_again, nblocks, byteoffset; 746 size_t el_size; 747 u_short ninos; 748 struct uvnode *devvp; 749 750 /* 751 * If there are no buffers other than the segment summary to write 752 * and it is not a checkpoint, don't do anything. On a checkpoint, 753 * even if there aren't any buffers, you need to write the superblock. 754 */ 755 if ((nblocks = sp->cbpp - sp->bpp) == 1) 756 return 0; 757 758 devvp = fs->lfs_unlockvp; 759 760 /* Update the segment usage information. */ 761 LFS_SEGENTRY(sup, fs, sp->seg_number, bp); 762 763 /* Loop through all blocks, except the segment summary. */ 764 for (bpp = sp->bpp; ++bpp < sp->cbpp;) { 765 if ((*bpp)->b_vp != devvp) { 766 sup->su_nbytes += (*bpp)->b_bcount; 767 } 768 } 769 770 ssp = (SEGSUM *) sp->segsum; 771 772 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs); 773 sup->su_nbytes += ssp->ss_ninos * DINODE1_SIZE; 774 775 if (fs->lfs_version == 1) 776 sup->su_olastmod = write_time; 777 else 778 sup->su_lastmod = write_time; 779 sup->su_ninos += ninos; 780 ++sup->su_nsums; 781 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos * 782 fs->lfs_ibsize)); 783 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize); 784 785 do_again = !(bp->b_flags & B_GATHERED); 786 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */ 787 788 /* 789 * Compute checksum across data and then across summary; the first 790 * block (the summary block) is skipped. Set the create time here 791 * so that it's guaranteed to be later than the inode mod times. 792 */ 793 if (fs->lfs_version == 1) 794 el_size = sizeof(u_long); 795 else 796 el_size = sizeof(u_int32_t); 797 datap = dp = malloc(nblocks * el_size); 798 for (bpp = sp->bpp, i = nblocks - 1; i--;) { 799 ++bpp; 800 /* Loop through gop_write cluster blocks */ 801 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount; 802 byteoffset += fs->lfs_bsize) { 803 memcpy(dp, (*bpp)->b_data + byteoffset, el_size); 804 dp += el_size; 805 } 806 bremfree(*bpp); 807 (*bpp)->b_flags |= B_BUSY; 808 } 809 if (fs->lfs_version == 1) 810 ssp->ss_ocreate = write_time; 811 else { 812 ssp->ss_create = write_time; 813 ssp->ss_serial = ++fs->lfs_serial; 814 ssp->ss_ident = fs->lfs_ident; 815 } 816 /* Set the summary block busy too */ 817 bremfree(*(sp->bpp)); 818 (*(sp->bpp))->b_flags |= B_BUSY; 819 820 ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size); 821 ssp->ss_sumsum = 822 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum)); 823 free(datap); 824 datap = dp = NULL; 825 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) + 826 btofsb(fs, fs->lfs_sumsize)); 827 828 if (devvp == NULL) 829 errx(1, "devvp is NULL"); 830 for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) { 831 bp = *bpp; 832 #if 0 833 printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n", 834 nblocks - i, bp, bp->b_flags, bp->b_blkno); 835 printf(" vp = %p\n", bp->b_vp); 836 if (bp->b_vp != fs->lfs_unlockvp) 837 printf(" ino = %d lbn = %" PRId64 "\n", 838 VTOI(bp->b_vp)->i_number, bp->b_lblkno); 839 #endif 840 if (bp->b_vp == fs->lfs_unlockvp) 841 written_dev += bp->b_bcount; 842 else { 843 if (bp->b_lblkno >= 0) 844 written_data += bp->b_bcount; 845 else 846 written_indir += bp->b_bcount; 847 } 848 bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR | 849 B_LOCKED); 850 bwrite(bp); 851 written_bytes += bp->b_bcount; 852 } 853 written_inodes += ninos; 854 855 return (lfs_initseg(fs) || do_again); 856 } 857 858 /* 859 * Our own copy of shellsort. XXX use qsort or heapsort. 860 */ 861 void 862 lfs_shellsort(struct ubuf ** bp_array, ufs_daddr_t * lb_array, int nmemb, int size) 863 { 864 static int __rsshell_increments[] = {4, 1, 0}; 865 int incr, *incrp, t1, t2; 866 struct ubuf *bp_temp; 867 868 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;) 869 for (t1 = incr; t1 < nmemb; ++t1) 870 for (t2 = t1 - incr; t2 >= 0;) 871 if ((u_int32_t) bp_array[t2]->b_lblkno > 872 (u_int32_t) bp_array[t2 + incr]->b_lblkno) { 873 bp_temp = bp_array[t2]; 874 bp_array[t2] = bp_array[t2 + incr]; 875 bp_array[t2 + incr] = bp_temp; 876 t2 -= incr; 877 } else 878 break; 879 880 /* Reform the list of logical blocks */ 881 incr = 0; 882 for (t1 = 0; t1 < nmemb; t1++) { 883 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) { 884 lb_array[incr++] = bp_array[t1]->b_lblkno + t2; 885 } 886 } 887 } 888 889 890 /* 891 * lfs_seglock -- 892 * Single thread the segment writer. 893 */ 894 int 895 lfs_seglock(struct lfs * fs, unsigned long flags) 896 { 897 struct segment *sp; 898 899 if (fs->lfs_seglock) { 900 ++fs->lfs_seglock; 901 fs->lfs_sp->seg_flags |= flags; 902 return 0; 903 } 904 fs->lfs_seglock = 1; 905 906 sp = fs->lfs_sp = (struct segment *) malloc(sizeof(*sp)); 907 sp->bpp = (struct ubuf **) malloc(fs->lfs_ssize * sizeof(struct ubuf *)); 908 sp->seg_flags = flags; 909 sp->vp = NULL; 910 sp->seg_iocount = 0; 911 (void) lfs_initseg(fs); 912 913 /* 914 * Keep a cumulative count of the outstanding I/O operations. If the 915 * disk drive catches up with us it could go to zero before we finish, 916 * so we artificially increment it by one until we've scheduled all of 917 * the writes we intend to do. 918 */ 919 ++fs->lfs_iocount; 920 return 0; 921 } 922 923 /* 924 * lfs_segunlock -- 925 * Single thread the segment writer. 926 */ 927 void 928 lfs_segunlock(struct lfs * fs) 929 { 930 struct segment *sp; 931 struct ubuf *bp; 932 933 sp = fs->lfs_sp; 934 935 if (fs->lfs_seglock == 1) { 936 if (sp->bpp != sp->cbpp) { 937 /* Free allocated segment summary */ 938 fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize); 939 bp = *sp->bpp; 940 bremfree(bp); 941 bp->b_flags |= B_DONE | B_INVAL; 942 bp->b_flags &= ~B_DELWRI; 943 reassignbuf(bp, bp->b_vp); 944 bp->b_flags |= B_BUSY; /* XXX */ 945 brelse(bp); 946 } else 947 printf("unlock to 0 with no summary"); 948 949 free(sp->bpp); 950 sp->bpp = NULL; 951 free(sp); 952 fs->lfs_sp = NULL; 953 954 fs->lfs_nactive = 0; 955 956 /* Since we *know* everything's on disk, write both sbs */ 957 lfs_writesuper(fs, fs->lfs_sboffs[fs->lfs_activesb]); 958 lfs_writesuper(fs, fs->lfs_sboffs[1 - fs->lfs_activesb]); 959 960 --fs->lfs_seglock; 961 fs->lfs_lockpid = 0; 962 } else if (fs->lfs_seglock == 0) { 963 errx(1, "Seglock not held"); 964 } else { 965 --fs->lfs_seglock; 966 } 967 } 968 969 int 970 lfs_writevnodes(struct lfs *fs, struct segment *sp, int op) 971 { 972 struct inode *ip; 973 struct uvnode *vp; 974 int inodes_written = 0; 975 976 LIST_FOREACH(vp, &vnodelist, v_mntvnodes) { 977 if (vp->v_bmap_op != lfs_vop_bmap) 978 continue; 979 980 ip = VTOI(vp); 981 982 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) || 983 (op != VN_DIROP && (vp->v_flag & VDIROP))) { 984 continue; 985 } 986 /* 987 * Write the inode/file if dirty and it's not the IFILE. 988 */ 989 if (ip->i_flag & IN_ALLMOD || !LIST_EMPTY(&vp->v_dirtyblkhd)) { 990 if (ip->i_number != LFS_IFILE_INUM) 991 lfs_writefile(fs, sp, vp); 992 (void) lfs_writeinode(fs, sp, ip); 993 inodes_written++; 994 } 995 } 996 return inodes_written; 997 } 998 999 void 1000 lfs_writesuper(struct lfs *fs, ufs_daddr_t daddr) 1001 { 1002 struct ubuf *bp; 1003 1004 /* Set timestamp of this version of the superblock */ 1005 if (fs->lfs_version == 1) 1006 fs->lfs_otstamp = write_time; 1007 fs->lfs_tstamp = write_time; 1008 1009 /* Checksum the superblock and copy it into a buffer. */ 1010 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs)); 1011 assert(daddr > 0); 1012 bp = getblk(fs->lfs_unlockvp, fsbtodb(fs, daddr), LFS_SBPAD); 1013 memset(bp->b_data + sizeof(struct dlfs), 0, 1014 LFS_SBPAD - sizeof(struct dlfs)); 1015 *(struct dlfs *) bp->b_data = fs->lfs_dlfs; 1016 1017 bwrite(bp); 1018 } 1019