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