1 /* $NetBSD: lfs.c,v 1.75 2020/04/03 19:36:33 joerg 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) 1989, 1991, 1993 32 * The Regents of the University of California. All rights reserved. 33 * (c) UNIX System Laboratories, Inc. 34 * All or some portions of this file are derived from material licensed 35 * to the University of California by American Telephone and Telegraph 36 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 37 * the permission of UNIX System Laboratories, Inc. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95 64 */ 65 66 67 #include <sys/types.h> 68 #include <sys/param.h> 69 #include <sys/time.h> 70 #include <sys/buf.h> 71 #include <sys/mount.h> 72 73 #define vnode uvnode 74 #include <ufs/lfs/lfs.h> 75 #include <ufs/lfs/lfs_inode.h> 76 #include <ufs/lfs/lfs_accessors.h> 77 #undef vnode 78 79 #include <assert.h> 80 #include <err.h> 81 #include <errno.h> 82 #include <stdarg.h> 83 #include <stdbool.h> 84 #include <stdio.h> 85 #include <stdlib.h> 86 #include <string.h> 87 #include <unistd.h> 88 #include <util.h> 89 90 #include "bufcache.h" 91 #include "extern.h" 92 #include "lfs_user.h" 93 #include "segwrite.h" 94 #include "kernelops.h" 95 96 #define panic call_panic 97 98 long dev_bsize = DEV_BSIZE; 99 100 static int 101 lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **); 102 103 int fsdirty = 0; 104 void (*panic_func)(int, const char *, va_list) = my_vpanic; 105 106 /* 107 * LFS buffer and uvnode operations 108 */ 109 110 int 111 lfs_vop_strategy(struct ubuf * bp) 112 { 113 int count; 114 115 if (bp->b_flags & B_READ) { 116 count = kops.ko_pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount, 117 bp->b_blkno * dev_bsize); 118 if (count == bp->b_bcount) 119 bp->b_flags |= B_DONE; 120 } else { 121 count = kops.ko_pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount, 122 bp->b_blkno * dev_bsize); 123 if (count == 0) { 124 perror("pwrite"); 125 return -1; 126 } 127 bp->b_flags &= ~B_DELWRI; 128 reassignbuf(bp, bp->b_vp); 129 } 130 return 0; 131 } 132 133 int 134 lfs_vop_bwrite(struct ubuf * bp) 135 { 136 struct lfs *fs; 137 138 fs = bp->b_vp->v_fs; 139 if (!(bp->b_flags & B_DELWRI)) { 140 lfs_sb_subavail(fs, lfs_btofsb(fs, bp->b_bcount)); 141 } 142 bp->b_flags |= B_DELWRI | B_LOCKED; 143 reassignbuf(bp, bp->b_vp); 144 brelse(bp, 0); 145 return 0; 146 } 147 148 /* 149 * ulfs_bmaparray does the bmap conversion, and if requested returns the 150 * array of logical blocks which must be traversed to get to a block. 151 * Each entry contains the offset into that block that gets you to the 152 * next block and the disk address of the block (if it is assigned). 153 */ 154 int 155 ulfs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump) 156 { 157 struct inode *ip; 158 struct ubuf *bp; 159 struct indir a[ULFS_NIADDR + 1], *xap; 160 daddr_t daddr; 161 daddr_t metalbn; 162 int error, num; 163 164 ip = VTOI(vp); 165 166 if (bn >= 0 && bn < ULFS_NDADDR) { 167 if (nump != NULL) 168 *nump = 0; 169 *bnp = LFS_FSBTODB(fs, lfs_dino_getdb(fs, ip->i_din, bn)); 170 if (*bnp == 0) 171 *bnp = -1; 172 return (0); 173 } 174 xap = ap == NULL ? a : ap; 175 if (!nump) 176 nump = # 177 if ((error = ulfs_getlbns(fs, vp, bn, xap, nump)) != 0) 178 return (error); 179 180 num = *nump; 181 182 /* Get disk address out of indirect block array */ 183 daddr = lfs_dino_getib(fs, ip->i_din, xap->in_off); 184 185 for (bp = NULL, ++xap; --num; ++xap) { 186 /* Exit the loop if there is no disk address assigned yet and 187 * the indirect block isn't in the cache, or if we were 188 * looking for an indirect block and we've found it. */ 189 190 metalbn = xap->in_lbn; 191 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn) 192 break; 193 /* 194 * If we get here, we've either got the block in the cache 195 * or we have a disk address for it, go fetch it. 196 */ 197 if (bp) 198 brelse(bp, 0); 199 200 xap->in_exists = 1; 201 bp = getblk(vp, metalbn, lfs_sb_getbsize(fs)); 202 203 if (!(bp->b_flags & (B_DONE | B_DELWRI))) { 204 bp->b_blkno = LFS_FSBTODB(fs, daddr); 205 bp->b_flags |= B_READ; 206 VOP_STRATEGY(bp); 207 } 208 daddr = lfs_iblock_get(fs, bp->b_data, xap->in_off); 209 } 210 if (bp) 211 brelse(bp, 0); 212 213 daddr = LFS_FSBTODB(fs, daddr); 214 *bnp = daddr == 0 ? -1 : daddr; 215 return (0); 216 } 217 218 /* 219 * Create an array of logical block number/offset pairs which represent the 220 * path of indirect blocks required to access a data block. The first "pair" 221 * contains the logical block number of the appropriate single, double or 222 * triple indirect block and the offset into the inode indirect block array. 223 * Note, the logical block number of the inode single/double/triple indirect 224 * block appears twice in the array, once with the offset into di_ib and 225 * once with the offset into the page itself. 226 */ 227 int 228 ulfs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump) 229 { 230 daddr_t metalbn, realbn; 231 int64_t blockcnt; 232 int lbc; 233 int i, numlevels, off; 234 int lognindir, indir; 235 236 metalbn = 0; /* XXXGCC -Wuninitialized [sh3] */ 237 238 if (nump) 239 *nump = 0; 240 numlevels = 0; 241 realbn = bn; 242 if (bn < 0) 243 bn = -bn; 244 245 lognindir = -1; 246 for (indir = lfs_sb_getnindir(fs); indir; indir >>= 1) 247 ++lognindir; 248 249 /* Determine the number of levels of indirection. After this loop is 250 * done, blockcnt indicates the number of data blocks possible at the 251 * given level of indirection, and ULFS_NIADDR - i is the number of levels 252 * of indirection needed to locate the requested block. */ 253 254 bn -= ULFS_NDADDR; 255 for (lbc = 0, i = ULFS_NIADDR;; i--, bn -= blockcnt) { 256 if (i == 0) 257 return (EFBIG); 258 259 lbc += lognindir; 260 blockcnt = (int64_t) 1 << lbc; 261 262 if (bn < blockcnt) 263 break; 264 } 265 266 /* Calculate the address of the first meta-block. */ 267 metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + ULFS_NIADDR - i); 268 269 /* At each iteration, off is the offset into the bap array which is an 270 * array of disk addresses at the current level of indirection. The 271 * logical block number and the offset in that block are stored into 272 * the argument array. */ 273 ap->in_lbn = metalbn; 274 ap->in_off = off = ULFS_NIADDR - i; 275 ap->in_exists = 0; 276 ap++; 277 for (++numlevels; i <= ULFS_NIADDR; i++) { 278 /* If searching for a meta-data block, quit when found. */ 279 if (metalbn == realbn) 280 break; 281 282 lbc -= lognindir; 283 /*blockcnt = (int64_t) 1 << lbc;*/ 284 off = (bn >> lbc) & (lfs_sb_getnindir(fs) - 1); 285 286 ++numlevels; 287 ap->in_lbn = metalbn; 288 ap->in_off = off; 289 ap->in_exists = 0; 290 ++ap; 291 292 metalbn -= -1 + (off << lbc); 293 } 294 if (nump) 295 *nump = numlevels; 296 return (0); 297 } 298 299 int 300 lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp) 301 { 302 return ulfs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL); 303 } 304 305 /* Search a block for a specific dinode. */ 306 union lfs_dinode * 307 lfs_ifind(struct lfs *fs, ino_t ino, struct ubuf *bp) 308 { 309 union lfs_dinode *ldip; 310 unsigned i, num; 311 312 num = LFS_INOPB(fs); 313 314 /* 315 * Read the inode block backwards, since later versions of the 316 * inode will supercede earlier ones. Though it is unlikely, it is 317 * possible that the same inode will appear in the same inode block. 318 */ 319 for (i = num; i-- > 0; ) { 320 ldip = DINO_IN_BLOCK(fs, bp->b_data, i); 321 if (lfs_dino_getinumber(fs, ldip) == ino) 322 return (ldip); 323 } 324 return NULL; 325 } 326 327 /* 328 * lfs_raw_vget makes us a new vnode from the inode at the given disk address. 329 * XXX it currently loses atime information. 330 */ 331 struct uvnode * 332 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, daddr_t daddr) 333 { 334 struct uvnode *vp; 335 struct inode *ip; 336 union lfs_dinode *dip; 337 struct ubuf *bp; 338 int i, hash; 339 340 vp = ecalloc(1, sizeof(*vp)); 341 vp->v_fd = fd; 342 vp->v_fs = fs; 343 vp->v_usecount = 0; 344 vp->v_strategy_op = lfs_vop_strategy; 345 vp->v_bwrite_op = lfs_vop_bwrite; 346 vp->v_bmap_op = lfs_vop_bmap; 347 LIST_INIT(&vp->v_cleanblkhd); 348 LIST_INIT(&vp->v_dirtyblkhd); 349 350 ip = ecalloc(1, sizeof(*ip)); 351 352 ip->i_din = dip = ecalloc(1, sizeof(*dip)); 353 354 /* Initialize the inode -- from lfs_vcreate. */ 355 ip->inode_ext.lfs = ecalloc(1, sizeof(*ip->inode_ext.lfs)); 356 vp->v_data = ip; 357 /* ip->i_vnode = vp; */ 358 ip->i_lockf = 0; 359 ip->i_state = 0; 360 361 /* Load inode block and find inode */ 362 if (daddr > 0) { 363 bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs), 364 0, &bp); 365 bp->b_flags |= B_AGE; 366 dip = lfs_ifind(fs, ino, bp); 367 if (dip == NULL) { 368 brelse(bp, 0); 369 free(ip->i_din); 370 free(ip->inode_ext.lfs); 371 free(ip); 372 free(vp); 373 return NULL; 374 } 375 lfs_copy_dinode(fs, ip->i_din, dip); 376 brelse(bp, 0); 377 } 378 ip->i_number = ino; 379 /* ip->i_devvp = fs->lfs_devvp; */ 380 ip->i_lfs = fs; 381 382 ip->i_lfs_effnblks = lfs_dino_getblocks(fs, ip->i_din); 383 ip->i_lfs_osize = lfs_dino_getsize(fs, ip->i_din); 384 #if 0 385 if (lfs_sb_getversion(fs) > 1) { 386 lfs_dino_setatime(fs, ip->i_din, ts.tv_sec); 387 lfs_dino_setatimensec(fs, ip->i_din, ts.tv_nsec); 388 } 389 #endif 390 391 memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize)); 392 for (i = 0; i < ULFS_NDADDR; i++) 393 if (lfs_dino_getdb(fs, ip->i_din, i) != 0) 394 ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i); 395 396 ++nvnodes; 397 hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1); 398 LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes); 399 LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes); 400 401 return vp; 402 } 403 404 static struct uvnode * 405 lfs_vget(void *vfs, ino_t ino) 406 { 407 struct lfs *fs = (struct lfs *)vfs; 408 daddr_t daddr; 409 struct ubuf *bp; 410 IFILE *ifp; 411 412 LFS_IENTRY(ifp, fs, ino, bp); 413 daddr = lfs_if_getdaddr(fs, ifp); 414 brelse(bp, 0); 415 if (daddr <= 0 || lfs_dtosn(fs, daddr) >= lfs_sb_getnseg(fs)) 416 return NULL; 417 return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr); 418 } 419 420 /* 421 * Check superblock magic number and checksum. 422 * Sets lfs_is64 and lfs_dobyteswap. 423 */ 424 static int 425 check_sb(struct lfs *fs) 426 { 427 u_int32_t checksum; 428 u_int32_t magic; 429 430 /* we can read the magic out of either the 32-bit or 64-bit dlfs */ 431 magic = fs->lfs_dlfs_u.u_32.dlfs_magic; 432 433 switch (magic) { 434 case LFS_MAGIC: 435 fs->lfs_is64 = false; 436 fs->lfs_dobyteswap = false; 437 break; 438 case LFS_MAGIC_SWAPPED: 439 fs->lfs_is64 = false; 440 fs->lfs_dobyteswap = true; 441 break; 442 case LFS64_MAGIC: 443 fs->lfs_is64 = true; 444 fs->lfs_dobyteswap = false; 445 break; 446 case LFS64_MAGIC_SWAPPED: 447 fs->lfs_is64 = true; 448 fs->lfs_dobyteswap = true; 449 break; 450 default: 451 printf("Superblock magic number (0x%lx) does not match " 452 "expected 0x%lx\n", (unsigned long) magic, 453 (unsigned long) LFS_MAGIC); 454 return 1; 455 } 456 457 /* checksum */ 458 checksum = lfs_sb_cksum(fs); 459 if (lfs_sb_getcksum(fs) != checksum) { 460 printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n", 461 (unsigned long) lfs_sb_getcksum(fs), (unsigned long) checksum); 462 return 1; 463 } 464 return 0; 465 } 466 467 /* Initialize LFS library; load superblocks and choose which to use. */ 468 struct lfs * 469 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug) 470 { 471 struct uvnode *devvp; 472 struct ubuf *bp; 473 int tryalt; 474 struct lfs *fs, *altfs; 475 476 vfs_init(); 477 478 devvp = ecalloc(1, sizeof(*devvp)); 479 devvp->v_fs = NULL; 480 devvp->v_fd = devfd; 481 devvp->v_strategy_op = raw_vop_strategy; 482 devvp->v_bwrite_op = raw_vop_bwrite; 483 devvp->v_bmap_op = raw_vop_bmap; 484 LIST_INIT(&devvp->v_cleanblkhd); 485 LIST_INIT(&devvp->v_dirtyblkhd); 486 487 tryalt = 0; 488 if (dummy_read) { 489 if (sblkno == 0) 490 sblkno = LFS_LABELPAD / dev_bsize; 491 fs = ecalloc(1, sizeof(*fs)); 492 fs->lfs_devvp = devvp; 493 } else { 494 if (sblkno == 0) { 495 sblkno = LFS_LABELPAD / dev_bsize; 496 tryalt = 1; 497 } else if (debug) { 498 printf("No -b flag given, not attempting to verify checkpoint\n"); 499 } 500 501 dev_bsize = DEV_BSIZE; 502 503 (void)bread(devvp, sblkno, LFS_SBPAD, 0, &bp); 504 fs = ecalloc(1, sizeof(*fs)); 505 __CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64)); 506 memcpy(&fs->lfs_dlfs_u, bp->b_data, sizeof(struct dlfs)); 507 fs->lfs_devvp = devvp; 508 bp->b_flags |= B_INVAL; 509 brelse(bp, 0); 510 511 dev_bsize = lfs_sb_getfsize(fs) >> lfs_sb_getfsbtodb(fs); 512 513 if (tryalt) { 514 (void)bread(devvp, LFS_FSBTODB(fs, lfs_sb_getsboff(fs, 1)), 515 LFS_SBPAD, 0, &bp); 516 altfs = ecalloc(1, sizeof(*altfs)); 517 memcpy(&altfs->lfs_dlfs_u, bp->b_data, 518 sizeof(struct dlfs)); 519 altfs->lfs_devvp = devvp; 520 bp->b_flags |= B_INVAL; 521 brelse(bp, 0); 522 523 if (check_sb(fs) || lfs_sb_getidaddr(fs) <= 0) { 524 if (debug) 525 printf("Primary superblock is no good, using first alternate\n"); 526 free(fs); 527 fs = altfs; 528 } else { 529 /* If both superblocks check out, try verification */ 530 if (check_sb(altfs)) { 531 if (debug) 532 printf("First alternate superblock is no good, using primary\n"); 533 free(altfs); 534 } else { 535 if (lfs_verify(fs, altfs, devvp, debug) == fs) { 536 free(altfs); 537 } else { 538 free(fs); 539 fs = altfs; 540 } 541 } 542 } 543 } 544 if (check_sb(fs)) { 545 free(fs); 546 return NULL; 547 } 548 } 549 550 /* Compatibility */ 551 if (lfs_sb_getversion(fs) < 2) { 552 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE); 553 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs)); 554 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0)); 555 lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs)); 556 lfs_sb_setfsbtodb(fs, 0); 557 } 558 559 if (!dummy_read) { 560 fs->lfs_suflags = emalloc(2 * sizeof(u_int32_t *)); 561 fs->lfs_suflags[0] = emalloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t)); 562 fs->lfs_suflags[1] = emalloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t)); 563 } 564 565 if (idaddr == 0) 566 idaddr = lfs_sb_getidaddr(fs); 567 else 568 lfs_sb_setidaddr(fs, idaddr); 569 /* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */ 570 fs->lfs_ivnode = lfs_raw_vget(fs, LFS_IFILE_INUM, 571 devvp->v_fd, idaddr); 572 if (fs->lfs_ivnode == NULL) 573 return NULL; 574 575 register_vget((void *)fs, lfs_vget); 576 577 return fs; 578 } 579 580 /* 581 * Check partial segment validity between fs->lfs_offset and the given goal. 582 * 583 * If goal == 0, just keep on going until the segments stop making sense, 584 * and return the address of the last valid partial segment. 585 * 586 * If goal != 0, return the address of the first partial segment that failed, 587 * or "goal" if we reached it without failure (the partial segment *at* goal 588 * need not be valid). 589 */ 590 daddr_t 591 try_verify(struct lfs *osb, struct uvnode *devvp, daddr_t goal, int debug) 592 { 593 daddr_t daddr, odaddr; 594 SEGSUM *sp; 595 int i, bc, hitclean; 596 struct ubuf *bp; 597 daddr_t nodirop_daddr; 598 u_int64_t serial; 599 600 bc = 0; 601 hitclean = 0; 602 odaddr = -1; 603 daddr = lfs_sb_getoffset(osb); 604 nodirop_daddr = daddr; 605 serial = lfs_sb_getserial(osb); 606 while (daddr != goal) { 607 /* 608 * Don't mistakenly read a superblock, if there is one here. 609 */ 610 if (lfs_sntod(osb, lfs_dtosn(osb, daddr)) == daddr) { 611 if (daddr == lfs_sb_gets0addr(osb)) 612 daddr += lfs_btofsb(osb, LFS_LABELPAD); 613 for (i = 0; i < LFS_MAXNUMSB; i++) { 614 /* XXX dholland 20150828 I think this is wrong */ 615 if (lfs_sb_getsboff(osb, i) < daddr) 616 break; 617 if (lfs_sb_getsboff(osb, i) == daddr) 618 daddr += lfs_btofsb(osb, LFS_SBPAD); 619 } 620 } 621 622 /* Read in summary block */ 623 bread(devvp, LFS_FSBTODB(osb, daddr), lfs_sb_getsumsize(osb), 624 0, &bp); 625 sp = (SEGSUM *)bp->b_data; 626 627 /* 628 * Check for a valid segment summary belonging to our fs. 629 */ 630 if (lfs_ss_getmagic(osb, sp) != SS_MAGIC || 631 lfs_ss_getident(osb, sp) != lfs_sb_getident(osb) || 632 lfs_ss_getserial(osb, sp) < serial || /* XXX strengthen this */ 633 lfs_ss_getsumsum(osb, sp) != 634 cksum((char *)sp + lfs_ss_getsumstart(osb), 635 lfs_sb_getsumsize(osb) - lfs_ss_getsumstart(osb))) { 636 brelse(bp, 0); 637 if (debug) { 638 if (lfs_ss_getmagic(osb, sp) != SS_MAGIC) 639 pwarn("pseg at 0x%jx: " 640 "wrong magic number\n", 641 (uintmax_t)daddr); 642 else if (lfs_ss_getident(osb, sp) != lfs_sb_getident(osb)) 643 pwarn("pseg at 0x%jx: " 644 "expected ident %jx, got %jx\n", 645 (uintmax_t)daddr, 646 (uintmax_t)lfs_ss_getident(osb, sp), 647 (uintmax_t)lfs_sb_getident(osb)); 648 else if (lfs_ss_getserial(osb, sp) >= serial) 649 pwarn("pseg at 0x%jx: " 650 "serial %d < %d\n", 651 (uintmax_t)daddr, 652 (int)lfs_ss_getserial(osb, sp), (int)serial); 653 else 654 pwarn("pseg at 0x%jx: " 655 "summary checksum wrong\n", 656 (uintmax_t)daddr); 657 } 658 break; 659 } 660 if (debug && lfs_ss_getserial(osb, sp) != serial) 661 pwarn("warning, serial=%d ss_serial=%d\n", 662 (int)serial, (int)lfs_ss_getserial(osb, sp)); 663 ++serial; 664 bc = check_summary(osb, sp, daddr, debug, devvp, NULL); 665 if (bc == 0) { 666 brelse(bp, 0); 667 break; 668 } 669 if (debug) 670 pwarn("summary good: 0x%x/%d\n", (uintmax_t)daddr, 671 (int)lfs_ss_getserial(osb, sp)); 672 assert (bc > 0); 673 odaddr = daddr; 674 daddr += lfs_btofsb(osb, lfs_sb_getsumsize(osb) + bc); 675 if (lfs_dtosn(osb, odaddr) != lfs_dtosn(osb, daddr) || 676 lfs_dtosn(osb, daddr) != lfs_dtosn(osb, daddr + 677 lfs_btofsb(osb, lfs_sb_getsumsize(osb) + lfs_sb_getbsize(osb)) - 1)) { 678 daddr = lfs_ss_getnext(osb, sp); 679 } 680 681 /* 682 * Check for the beginning and ending of a sequence of 683 * dirops. Writes from the cleaner never involve new 684 * information, and are always checkpoints; so don't try 685 * to roll forward through them. Likewise, psegs written 686 * by a previous roll-forward attempt are not interesting. 687 */ 688 if (lfs_ss_getflags(osb, sp) & (SS_CLEAN | SS_RFW)) 689 hitclean = 1; 690 if (hitclean == 0 && (lfs_ss_getflags(osb, sp) & SS_CONT) == 0) 691 nodirop_daddr = daddr; 692 693 brelse(bp, 0); 694 } 695 696 if (goal == 0) 697 return nodirop_daddr; 698 else 699 return daddr; 700 } 701 702 /* Use try_verify to check whether the newer superblock is valid. */ 703 struct lfs * 704 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug) 705 { 706 daddr_t daddr; 707 struct lfs *osb, *nsb; 708 709 /* 710 * Verify the checkpoint of the newer superblock, 711 * if the timestamp/serial number of the two superblocks is 712 * different. 713 */ 714 715 osb = NULL; 716 if (debug) 717 pwarn("sb0 %ju, sb1 %ju", 718 (uintmax_t) lfs_sb_getserial(sb0), 719 (uintmax_t) lfs_sb_getserial(sb1)); 720 721 if ((lfs_sb_getversion(sb0) == 1 && 722 lfs_sb_getotstamp(sb0) != lfs_sb_getotstamp(sb1)) || 723 (lfs_sb_getversion(sb0) > 1 && 724 lfs_sb_getserial(sb0) != lfs_sb_getserial(sb1))) { 725 if (lfs_sb_getversion(sb0) == 1) { 726 if (lfs_sb_getotstamp(sb0) > lfs_sb_getotstamp(sb1)) { 727 osb = sb1; 728 nsb = sb0; 729 } else { 730 osb = sb0; 731 nsb = sb1; 732 } 733 } else { 734 if (lfs_sb_getserial(sb0) > lfs_sb_getserial(sb1)) { 735 osb = sb1; 736 nsb = sb0; 737 } else { 738 osb = sb0; 739 nsb = sb1; 740 } 741 } 742 if (debug) { 743 printf("Attempting to verify newer checkpoint..."); 744 fflush(stdout); 745 } 746 daddr = try_verify(osb, devvp, lfs_sb_getoffset(nsb), debug); 747 748 if (debug) 749 printf("done.\n"); 750 if (daddr == lfs_sb_getoffset(nsb)) { 751 pwarn("** Newer checkpoint verified; recovered %jd seconds of data\n", 752 (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb))); 753 sbdirty(); 754 } else { 755 pwarn("** Newer checkpoint invalid; lost %jd seconds of data\n", (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb))); 756 } 757 return (daddr == lfs_sb_getoffset(nsb) ? nsb : osb); 758 } 759 /* Nothing to check */ 760 return osb; 761 } 762 763 /* Verify a partial-segment summary; return the number of bytes on disk. */ 764 int 765 check_summary(struct lfs *fs, SEGSUM *sp, daddr_t pseg_addr, int debug, 766 struct uvnode *devvp, void (func(daddr_t, FINFO *))) 767 { 768 FINFO *fp; 769 int bc; /* Bytes in partial segment */ 770 int nblocks; 771 daddr_t daddr; 772 IINFO *iibase, *iip; 773 struct ubuf *bp; 774 int i, j, k, datac, len; 775 lfs_checkword *datap; 776 u_int32_t ccksum; 777 778 /* We've already checked the sumsum, just do the data bounds and sum */ 779 780 /* Count the blocks. */ 781 nblocks = howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)); 782 bc = nblocks << (lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) : lfs_sb_getbshift(fs)); 783 assert(bc >= 0); 784 785 fp = SEGSUM_FINFOBASE(fs, sp); 786 for (i = 0; i < lfs_ss_getnfinfo(fs, sp); i++) { 787 nblocks += lfs_fi_getnblocks(fs, fp); 788 bc += lfs_fi_getlastlength(fs, fp) + ((lfs_fi_getnblocks(fs, fp) - 1) 789 << lfs_sb_getbshift(fs)); 790 assert(bc >= 0); 791 fp = NEXT_FINFO(fs, fp); 792 if (((char *)fp) - (char *)sp > lfs_sb_getsumsize(fs)) 793 return 0; 794 } 795 datap = emalloc(nblocks * sizeof(*datap)); 796 datac = 0; 797 798 iibase = SEGSUM_IINFOSTART(fs, sp); 799 800 iip = iibase; 801 daddr = pseg_addr + lfs_btofsb(fs, lfs_sb_getsumsize(fs)); 802 fp = SEGSUM_FINFOBASE(fs, sp); 803 for (i = 0, j = 0; 804 i < lfs_ss_getnfinfo(fs, sp) || j < howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)); i++) { 805 if (i >= lfs_ss_getnfinfo(fs, sp) && lfs_ii_getblock(fs, iip) != daddr) { 806 pwarn("Not enough inode blocks in pseg at 0x%jx: " 807 "found %d, wanted %d\n", 808 pseg_addr, j, howmany(lfs_ss_getninos(fs, sp), 809 LFS_INOPB(fs))); 810 if (debug) 811 pwarn("iip=0x%jx, daddr=0x%jx\n", 812 (uintmax_t)lfs_ii_getblock(fs, iip), 813 (intmax_t)daddr); 814 break; 815 } 816 while (j < howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)) && lfs_ii_getblock(fs, iip) == daddr) { 817 bread(devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs), 818 0, &bp); 819 datap[datac++] = ((lfs_checkword *)bp->b_data)[0]; 820 brelse(bp, 0); 821 822 ++j; 823 daddr += lfs_btofsb(fs, lfs_sb_getibsize(fs)); 824 iip = NEXTLOWER_IINFO(fs, iip); 825 } 826 if (i < lfs_ss_getnfinfo(fs, sp)) { 827 if (func) 828 func(daddr, fp); 829 for (k = 0; k < lfs_fi_getnblocks(fs, fp); k++) { 830 len = (k == lfs_fi_getnblocks(fs, fp) - 1 ? 831 lfs_fi_getlastlength(fs, fp) 832 : lfs_sb_getbsize(fs)); 833 bread(devvp, LFS_FSBTODB(fs, daddr), len, 834 0, &bp); 835 datap[datac++] = ((lfs_checkword *)bp->b_data)[0]; 836 brelse(bp, 0); 837 daddr += lfs_btofsb(fs, len); 838 } 839 fp = NEXT_FINFO(fs, fp); 840 } 841 } 842 843 if (datac != nblocks) { 844 pwarn("Partial segment at 0x%jx expected %d blocks counted %d\n", 845 (intmax_t)pseg_addr, nblocks, datac); 846 } 847 ccksum = cksum(datap, nblocks * sizeof(datap[0])); 848 /* Check the data checksum */ 849 if (ccksum != lfs_ss_getdatasum(fs, sp)) { 850 pwarn("Partial segment at 0x%jx data checksum" 851 " mismatch: given 0x%x, computed 0x%x\n", 852 (uintmax_t)pseg_addr, lfs_ss_getdatasum(fs, sp), ccksum); 853 free(datap); 854 return 0; 855 } 856 free(datap); 857 assert(bc >= 0); 858 return bc; 859 } 860 861 /* print message and exit */ 862 void 863 my_vpanic(int fatal, const char *fmt, va_list ap) 864 { 865 (void) vprintf(fmt, ap); 866 exit(8); 867 } 868 869 void 870 call_panic(const char *fmt, ...) 871 { 872 va_list ap; 873 874 va_start(ap, fmt); 875 panic_func(1, fmt, ap); 876 va_end(ap); 877 } 878 879 /* Allocate a new inode. */ 880 struct uvnode * 881 lfs_valloc(struct lfs *fs, ino_t ino) 882 { 883 struct ubuf *bp, *cbp; 884 IFILE *ifp; 885 ino_t new_ino; 886 int error; 887 CLEANERINFO *cip; 888 889 /* Get the head of the freelist. */ 890 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino); 891 892 /* 893 * Remove the inode from the free list and write the new start 894 * of the free list into the superblock. 895 */ 896 LFS_IENTRY(ifp, fs, new_ino, bp); 897 if (lfs_if_getdaddr(fs, ifp) != LFS_UNUSED_DADDR) 898 panic("lfs_valloc: inuse inode %d on the free list", new_ino); 899 LFS_PUT_HEADFREE(fs, cip, cbp, lfs_if_getnextfree(fs, ifp)); 900 901 brelse(bp, 0); 902 903 /* Extend IFILE so that the next lfs_valloc will succeed. */ 904 if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) { 905 if ((error = extend_ifile(fs)) != 0) { 906 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino); 907 return NULL; 908 } 909 } 910 911 /* Set superblock modified bit and increment file count. */ 912 sbdirty(); 913 lfs_sb_addnfiles(fs, 1); 914 915 return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0); 916 } 917 918 #ifdef IN_FSCK_LFS 919 void reset_maxino(ino_t); 920 #endif 921 922 /* 923 * Add a new block to the Ifile, to accommodate future file creations. 924 */ 925 int 926 extend_ifile(struct lfs *fs) 927 { 928 struct uvnode *vp; 929 struct inode *ip; 930 IFILE64 *ifp64; 931 IFILE32 *ifp32; 932 IFILE_V1 *ifp_v1; 933 struct ubuf *bp, *cbp; 934 daddr_t i, blkno, max; 935 ino_t oldlast; 936 CLEANERINFO *cip; 937 938 vp = fs->lfs_ivnode; 939 ip = VTOI(vp); 940 blkno = lfs_lblkno(fs, lfs_dino_getsize(fs, ip->i_din)); 941 942 lfs_balloc(vp, lfs_dino_getsize(fs, ip->i_din), lfs_sb_getbsize(fs), &bp); 943 lfs_dino_setsize(fs, ip->i_din, 944 lfs_dino_getsize(fs, ip->i_din) + lfs_sb_getbsize(fs)); 945 ip->i_state |= IN_MODIFIED; 946 947 i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) * 948 lfs_sb_getifpb(fs); 949 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast); 950 LFS_PUT_HEADFREE(fs, cip, cbp, i); 951 max = i + lfs_sb_getifpb(fs); 952 lfs_sb_subbfree(fs, lfs_btofsb(fs, lfs_sb_getbsize(fs))); 953 954 if (fs->lfs_is64) { 955 for (ifp64 = (IFILE64 *)bp->b_data; i < max; ++ifp64) { 956 ifp64->if_version = 1; 957 ifp64->if_daddr = LFS_UNUSED_DADDR; 958 ifp64->if_nextfree = ++i; 959 } 960 ifp64--; 961 ifp64->if_nextfree = oldlast; 962 } else if (lfs_sb_getversion(fs) > 1) { 963 for (ifp32 = (IFILE32 *)bp->b_data; i < max; ++ifp32) { 964 ifp32->if_version = 1; 965 ifp32->if_daddr = LFS_UNUSED_DADDR; 966 ifp32->if_nextfree = ++i; 967 } 968 ifp32--; 969 ifp32->if_nextfree = oldlast; 970 } else { 971 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) { 972 ifp_v1->if_version = 1; 973 ifp_v1->if_daddr = LFS_UNUSED_DADDR; 974 ifp_v1->if_nextfree = ++i; 975 } 976 ifp_v1--; 977 ifp_v1->if_nextfree = oldlast; 978 } 979 LFS_PUT_TAILFREE(fs, cip, cbp, max - 1); 980 981 LFS_BWRITE_LOG(bp); 982 983 #ifdef IN_FSCK_LFS 984 reset_maxino(((lfs_dino_getsize(fs, ip->i_din) >> lfs_sb_getbshift(fs)) 985 - lfs_sb_getsegtabsz(fs) 986 - lfs_sb_getcleansz(fs)) * lfs_sb_getifpb(fs)); 987 #endif 988 return 0; 989 } 990 991 /* 992 * Allocate a block, and to inode and filesystem block accounting for it 993 * and for any indirect blocks the may need to be created in order for 994 * this block to be created. 995 * 996 * Blocks which have never been accounted for (i.e., which "do not exist") 997 * have disk address 0, which is translated by ulfs_bmap to the special value 998 * UNASSIGNED == -1, as in the historical ULFS. 999 * 1000 * Blocks which have been accounted for but which have not yet been written 1001 * to disk are given the new special disk address UNWRITTEN == -2, so that 1002 * they can be differentiated from completely new blocks. 1003 */ 1004 int 1005 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp) 1006 { 1007 int offset; 1008 daddr_t daddr, idaddr; 1009 struct ubuf *ibp, *bp; 1010 struct inode *ip; 1011 struct lfs *fs; 1012 struct indir indirs[ULFS_NIADDR+2], *idp; 1013 daddr_t lbn, lastblock; 1014 int bcount; 1015 int error, frags, i, nsize, osize, num; 1016 1017 ip = VTOI(vp); 1018 fs = ip->i_lfs; 1019 offset = lfs_blkoff(fs, startoffset); 1020 lbn = lfs_lblkno(fs, startoffset); 1021 1022 /* 1023 * Three cases: it's a block beyond the end of file, it's a block in 1024 * the file that may or may not have been assigned a disk address or 1025 * we're writing an entire block. 1026 * 1027 * Note, if the daddr is UNWRITTEN, the block already exists in 1028 * the cache (it was read or written earlier). If so, make sure 1029 * we don't count it as a new block or zero out its contents. If 1030 * it did not, make sure we allocate any necessary indirect 1031 * blocks. 1032 * 1033 * If we are writing a block beyond the end of the file, we need to 1034 * check if the old last block was a fragment. If it was, we need 1035 * to rewrite it. 1036 */ 1037 1038 if (bpp) 1039 *bpp = NULL; 1040 1041 /* Check for block beyond end of file and fragment extension needed. */ 1042 lastblock = lfs_lblkno(fs, lfs_dino_getsize(fs, ip->i_din)); 1043 if (lastblock < ULFS_NDADDR && lastblock < lbn) { 1044 osize = lfs_blksize(fs, ip, lastblock); 1045 if (osize < lfs_sb_getbsize(fs) && osize > 0) { 1046 if ((error = lfs_fragextend(vp, osize, lfs_sb_getbsize(fs), 1047 lastblock, 1048 (bpp ? &bp : NULL)))) 1049 return (error); 1050 lfs_dino_setsize(fs, ip->i_din, (lastblock + 1) * lfs_sb_getbsize(fs)); 1051 ip->i_state |= IN_CHANGE | IN_UPDATE; 1052 if (bpp) 1053 (void) VOP_BWRITE(bp); 1054 } 1055 } 1056 1057 /* 1058 * If the block we are writing is a direct block, it's the last 1059 * block in the file, and offset + iosize is less than a full 1060 * block, we can write one or more fragments. There are two cases: 1061 * the block is brand new and we should allocate it the correct 1062 * size or it already exists and contains some fragments and 1063 * may need to extend it. 1064 */ 1065 if (lbn < ULFS_NDADDR && lfs_lblkno(fs, lfs_dino_getsize(fs, ip->i_din)) <= lbn) { 1066 osize = lfs_blksize(fs, ip, lbn); 1067 nsize = lfs_fragroundup(fs, offset + iosize); 1068 if (lfs_lblktosize(fs, lbn) >= lfs_dino_getsize(fs, ip->i_din)) { 1069 /* Brand new block or fragment */ 1070 frags = lfs_numfrags(fs, nsize); 1071 if (bpp) { 1072 *bpp = bp = getblk(vp, lbn, nsize); 1073 bp->b_blkno = UNWRITTEN; 1074 } 1075 ip->i_lfs_effnblks += frags; 1076 lfs_sb_subbfree(fs, frags); 1077 lfs_dino_setdb(fs, ip->i_din, lbn, UNWRITTEN); 1078 } else { 1079 if (nsize <= osize) { 1080 /* No need to extend */ 1081 if (bpp && (error = bread(vp, lbn, osize, 1082 0, &bp))) 1083 return error; 1084 } else { 1085 /* Extend existing block */ 1086 if ((error = 1087 lfs_fragextend(vp, osize, nsize, lbn, 1088 (bpp ? &bp : NULL)))) 1089 return error; 1090 } 1091 if (bpp) 1092 *bpp = bp; 1093 } 1094 return 0; 1095 } 1096 1097 error = ulfs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num); 1098 if (error) 1099 return (error); 1100 1101 /* 1102 * Do byte accounting all at once, so we can gracefully fail *before* 1103 * we start assigning blocks. 1104 */ 1105 frags = LFS_FSBTODB(fs, 1); /* frags = VFSTOULFS(vp->v_mount)->um_seqinc; */ 1106 bcount = 0; 1107 if (daddr == UNASSIGNED) { 1108 bcount = frags; 1109 } 1110 for (i = 1; i < num; ++i) { 1111 if (!indirs[i].in_exists) { 1112 bcount += frags; 1113 } 1114 } 1115 lfs_sb_subbfree(fs, bcount); 1116 ip->i_lfs_effnblks += bcount; 1117 1118 if (daddr == UNASSIGNED) { 1119 if (num > 0 && lfs_dino_getib(fs, ip->i_din, indirs[0].in_off) == 0) { 1120 lfs_dino_setib(fs, ip->i_din, indirs[0].in_off, 1121 UNWRITTEN); 1122 } 1123 1124 /* 1125 * Create new indirect blocks if necessary 1126 */ 1127 if (num > 1) { 1128 idaddr = lfs_dino_getib(fs, ip->i_din, indirs[0].in_off); 1129 for (i = 1; i < num; ++i) { 1130 ibp = getblk(vp, indirs[i].in_lbn, 1131 lfs_sb_getbsize(fs)); 1132 if (!indirs[i].in_exists) { 1133 memset(ibp->b_data, 0, ibp->b_bufsize); 1134 ibp->b_blkno = UNWRITTEN; 1135 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) { 1136 ibp->b_blkno = LFS_FSBTODB(fs, idaddr); 1137 ibp->b_flags |= B_READ; 1138 VOP_STRATEGY(ibp); 1139 } 1140 /* 1141 * This block exists, but the next one may not. 1142 * If that is the case mark it UNWRITTEN to 1143 * keep the accounting straight. 1144 */ 1145 if (lfs_iblock_get(fs, ibp->b_data, 1146 indirs[i].in_off) == 0) 1147 lfs_iblock_set(fs, ibp->b_data, 1148 indirs[i].in_off, UNWRITTEN); 1149 idaddr = lfs_iblock_get(fs, ibp->b_data, 1150 indirs[i].in_off); 1151 if ((error = VOP_BWRITE(ibp))) 1152 return error; 1153 } 1154 } 1155 } 1156 1157 1158 /* 1159 * Get the existing block from the cache, if requested. 1160 */ 1161 if (bpp) 1162 *bpp = bp = getblk(vp, lbn, lfs_blksize(fs, ip, lbn)); 1163 1164 /* 1165 * The block we are writing may be a brand new block 1166 * in which case we need to do accounting. 1167 * 1168 * We can tell a truly new block because ulfs_bmaparray will say 1169 * it is UNASSIGNED. Once we allocate it we will assign it the 1170 * disk address UNWRITTEN. 1171 */ 1172 if (daddr == UNASSIGNED) { 1173 if (bpp) { 1174 /* Note the new address */ 1175 bp->b_blkno = UNWRITTEN; 1176 } 1177 1178 switch (num) { 1179 case 0: 1180 lfs_dino_setdb(fs, ip->i_din, lbn, UNWRITTEN); 1181 break; 1182 case 1: 1183 lfs_dino_setib(fs, ip->i_din, indirs[0].in_off, 1184 UNWRITTEN); 1185 break; 1186 default: 1187 idp = &indirs[num - 1]; 1188 if (bread(vp, idp->in_lbn, lfs_sb_getbsize(fs), 0, &ibp)) 1189 panic("lfs_balloc: bread bno %lld", 1190 (long long)idp->in_lbn); 1191 lfs_iblock_set(fs, ibp->b_data, idp->in_off, 1192 UNWRITTEN); 1193 VOP_BWRITE(ibp); 1194 } 1195 } else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) { 1196 /* 1197 * Not a brand new block, also not in the cache; 1198 * read it in from disk. 1199 */ 1200 if (iosize == lfs_sb_getbsize(fs)) 1201 /* Optimization: I/O is unnecessary. */ 1202 bp->b_blkno = daddr; 1203 else { 1204 /* 1205 * We need to read the block to preserve the 1206 * existing bytes. 1207 */ 1208 bp->b_blkno = daddr; 1209 bp->b_flags |= B_READ; 1210 VOP_STRATEGY(bp); 1211 return 0; 1212 } 1213 } 1214 1215 return (0); 1216 } 1217 1218 int 1219 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn, 1220 struct ubuf **bpp) 1221 { 1222 struct inode *ip; 1223 struct lfs *fs; 1224 int frags; 1225 int error; 1226 1227 ip = VTOI(vp); 1228 fs = ip->i_lfs; 1229 frags = (long)lfs_numfrags(fs, nsize - osize); 1230 error = 0; 1231 1232 /* 1233 * If we are not asked to actually return the block, all we need 1234 * to do is allocate space for it. UBC will handle dirtying the 1235 * appropriate things and making sure it all goes to disk. 1236 * Don't bother to read in that case. 1237 */ 1238 if (bpp && (error = bread(vp, lbn, osize, 0, bpp))) { 1239 brelse(*bpp, 0); 1240 goto out; 1241 } 1242 1243 lfs_sb_subbfree(fs, frags); 1244 ip->i_lfs_effnblks += frags; 1245 ip->i_state |= IN_CHANGE | IN_UPDATE; 1246 1247 if (bpp) { 1248 (*bpp)->b_data = erealloc((*bpp)->b_data, nsize); 1249 (void)memset((*bpp)->b_data + osize, 0, nsize - osize); 1250 } 1251 1252 out: 1253 return (error); 1254 } 1255