1 /* $NetBSD: make_lfs.c,v 1.11 2007/10/08 21:42:33 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Konrad E. Schroder <perseant@hhhh.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 /*- 39 * Copyright (c) 1991, 1993 40 * The Regents of the University of California. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 */ 66 67 #include <sys/cdefs.h> 68 #ifndef lint 69 #if 0 70 static char sccsid[] = "@(#)lfs.c 8.5 (Berkeley) 5/24/95"; 71 #else 72 __RCSID("$NetBSD: make_lfs.c,v 1.11 2007/10/08 21:42:33 ad Exp $"); 73 #endif 74 #endif /* not lint */ 75 76 #include <sys/param.h> 77 #include <sys/disk.h> 78 #include <sys/time.h> 79 #include <sys/mount.h> 80 #include <sys/stat.h> 81 82 #include <ufs/ufs/dir.h> 83 #include <ufs/ufs/quota.h> 84 #include <ufs/ufs/inode.h> 85 86 /* Override certain things to make <ufs/lfs/lfs.h> work */ 87 # undef simple_lock 88 # define simple_lock(x) 89 # undef simple_unlock 90 # define simple_unlock(x) 91 # define vnode uvnode 92 # define buf ubuf 93 # define panic call_panic 94 #include <ufs/lfs/lfs.h> 95 96 #include <err.h> 97 #include <errno.h> 98 #include <stdio.h> 99 #include <stdlib.h> 100 #include <string.h> 101 #include <time.h> 102 #include <unistd.h> 103 104 #include "config.h" 105 #include "extern.h" 106 107 #include "bufcache.h" 108 #include "vnode.h" 109 #include "lfs_user.h" 110 #include "segwrite.h" 111 112 extern int Nflag; /* Don't write anything */ 113 ufs_daddr_t ifibc; /* How many indirect blocks */ 114 115 #ifdef MAKE_LF_DIR 116 # define HIGHEST_USED_INO LOSTFOUNDINO 117 #else 118 # define HIGHEST_USED_INO ROOTINO 119 #endif 120 121 static struct lfs lfs_default = { 122 .lfs_dlfs = { /* lfs_dlfs */ 123 /* dlfs_magic */ LFS_MAGIC, 124 /* dlfs_version */ LFS_VERSION, 125 /* dlfs_size */ 0, 126 /* dlfs_ssize */ DFL_LFSSEG, 127 /* dlfs_dsize */ 0, 128 /* dlfs_bsize */ DFL_LFSBLOCK, 129 /* dlfs_fsize */ DFL_LFSFRAG, 130 /* dlfs_frag */ DFL_LFSBLOCK/DFL_LFSFRAG, 131 /* dlfs_freehd */ HIGHEST_USED_INO + 1, 132 /* dlfs_bfree */ 0, 133 /* dlfs_nfiles */ 0, 134 /* dlfs_avail */ 0, 135 /* dlfs_uinodes */ 0, 136 /* dlfs_idaddr */ 0, 137 /* dlfs_ifile */ LFS_IFILE_INUM, 138 /* dlfs_lastseg */ 0, 139 /* dlfs_nextseg */ 0, 140 /* dlfs_curseg */ 0, 141 /* dlfs_offset */ 0, 142 /* dlfs_lastpseg */ 0, 143 /* dlfs_inopf */ 0, 144 /* dlfs_minfree */ MINFREE, 145 /* dlfs_maxfilesize */ 0, 146 /* dlfs_fsbpseg */ 0, 147 /* dlfs_inopb */ DFL_LFSBLOCK/sizeof(struct ufs1_dinode), 148 /* dlfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 149 /* dlfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 150 /* XXX ondisk32 */ 151 /* dlfs_nindir */ DFL_LFSBLOCK/sizeof(int32_t), 152 /* dlfs_nseg */ 0, 153 /* dlfs_nspf */ 0, 154 /* dlfs_cleansz */ 0, 155 /* dlfs_segtabsz */ 0, 156 /* dlfs_segmask */ DFL_LFSSEG_MASK, 157 /* dlfs_segshift */ DFL_LFSSEG_SHIFT, 158 /* dlfs_bshift */ DFL_LFSBLOCK_SHIFT, 159 /* dlfs_ffshift */ DFL_LFS_FFSHIFT, 160 /* dlfs_fbshift */ DFL_LFS_FBSHIFT, 161 /* dlfs_bmask */ DFL_LFSBLOCK_MASK, 162 /* dlfs_ffmask */ DFL_LFS_FFMASK, 163 /* dlfs_fbmask */ DFL_LFS_FBMASK, 164 /* dlfs_blktodb */ 0, 165 /* dlfs_sushift */ 0, 166 /* dlfs_maxsymlinklen */ MAXSYMLINKLEN_UFS1, 167 /* dlfs_sboffs */ { 0 }, 168 /* dlfs_nclean */ 0, 169 /* dlfs_fsmnt */ { 0 }, 170 /* dlfs_pflags */ LFS_PF_CLEAN, 171 /* dlfs_dmeta */ 0, 172 /* dlfs_minfreeseg */ 0, 173 /* dlfs_sumsize */ 0, 174 /* dlfs_serial */ 0, 175 /* dlfs_ibsize */ DFL_LFSFRAG, 176 /* dlfs_start */ 0, 177 /* dlfs_tstamp */ 0, 178 /* dlfs_inodefmt */ LFS_44INODEFMT, 179 /* dlfs_interleave */ 0, 180 /* dlfs_ident */ 0, 181 /* dlfs_fsbtodb */ 0, 182 /* dlfs_resvseg */ 0, 183 184 /* dlfs_pad */ { 0 }, 185 /* dlfs_cksum */ 0 186 }, 187 }; 188 189 #define UMASK 0755 190 191 struct direct lfs_root_dir[] = { 192 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "."}, 193 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."}, 194 /* { LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"}, */ 195 #ifdef MAKE_LF_DIR 196 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"}, 197 #endif 198 }; 199 200 #ifdef MAKE_LF_DIR 201 struct direct lfs_lf_dir[] = { 202 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." }, 203 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 204 }; 205 #endif 206 207 void pwarn(const char *, ...); 208 static void make_dinode(ino_t, struct ufs1_dinode *, int, struct lfs *); 209 static void make_dir( void *, struct direct *, int); 210 static uint64_t maxfilesize(int); 211 212 /* 213 * calculate the maximum file size allowed with the specified block shift. 214 */ 215 static uint64_t 216 maxfilesize(int bshift) 217 { 218 uint64_t nptr; /* number of block pointers per block */ 219 uint64_t maxblock; 220 221 nptr = (1 << bshift) / sizeof(uint32_t); 222 maxblock = NDADDR + nptr + nptr * nptr + nptr * nptr * nptr; 223 224 return maxblock << bshift; 225 } 226 227 /* 228 * Create the root directory for this file system and the lost+found 229 * directory. 230 */ 231 static void 232 make_dinode(ino_t ino, struct ufs1_dinode *dip, int nfrags, struct lfs *fs) 233 { 234 int fsb_per_blk, i; 235 int nblocks, bb, base, factor, lvl; 236 237 nblocks = howmany(nfrags, fs->lfs_frag); 238 if(nblocks >= NDADDR) 239 nfrags = roundup(nfrags, fs->lfs_frag); 240 241 dip->di_nlink = 1; 242 dip->di_blocks = fragstofsb(fs, nfrags); 243 244 dip->di_size = (nfrags << fs->lfs_ffshift); 245 dip->di_atime = dip->di_mtime = dip->di_ctime = fs->lfs_tstamp; 246 dip->di_atimensec = dip->di_mtimensec = dip->di_ctimensec = 0; 247 dip->di_inumber = ino; 248 dip->di_gen = 1; 249 250 fsb_per_blk = fragstofsb(fs, blkstofrags(fs, 1)); 251 252 if (NDADDR < nblocks) { 253 /* Count up how many indirect blocks we need, recursively */ 254 /* XXX We are only called with nblocks > 1 for Ifile */ 255 bb = nblocks - NDADDR; 256 while (bb > 0) { 257 bb = howmany(bb, NINDIR(fs)); 258 ifibc += bb; 259 --bb; 260 } 261 dip->di_blocks += fragstofsb(fs, blkstofrags(fs, ifibc)); 262 } 263 264 /* Assign the block addresses for the ifile */ 265 for (i = 0; i < MIN(nblocks,NDADDR); i++) { 266 dip->di_db[i] = 0x0; 267 } 268 if(nblocks > NDADDR) { 269 dip->di_ib[0] = 0x0; 270 bb = howmany(nblocks - NDADDR, NINDIR(fs)) - 1; 271 factor = NINDIR(fs); 272 base = -NDADDR - factor; 273 lvl = 1; 274 while (bb > 0) { 275 dip->di_ib[lvl] = 0x0; 276 bb = howmany(bb, NINDIR(fs)); 277 --bb; 278 factor *= NINDIR(fs); 279 base -= factor; 280 ++lvl; 281 } 282 } 283 } 284 285 /* 286 * Construct a set of directory entries in "bufp". We assume that all the 287 * entries in protodir fir in the first DIRBLKSIZ. 288 */ 289 static void 290 make_dir(void *bufp, struct direct *protodir, int entries) 291 { 292 char *cp; 293 int i, spcleft; 294 295 spcleft = DIRBLKSIZ; 296 for (cp = bufp, i = 0; i < entries - 1; i++) { 297 protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i], 0); 298 memmove(cp, &protodir[i], protodir[i].d_reclen); 299 cp += protodir[i].d_reclen; 300 if ((spcleft -= protodir[i].d_reclen) < 0) 301 fatal("%s: %s", special, "directory too big"); 302 } 303 protodir[i].d_reclen = spcleft; 304 memmove(cp, &protodir[i], DIRSIZ(NEWDIRFMT, &protodir[i], 0)); 305 } 306 307 int 308 make_lfs(int devfd, uint secsize, struct dkwedge_info *dkw, int minfree, 309 int block_size, int frag_size, int seg_size, int minfreeseg, 310 int resvseg, int version, daddr_t start, int ibsize, int interleave, 311 u_int32_t roll_id) 312 { 313 struct ufs1_dinode *dip; /* Pointer to a disk inode */ 314 CLEANERINFO *cip; /* Segment cleaner information table */ 315 IFILE *ip; /* Pointer to array of ifile structures */ 316 IFILE_V1 *ip_v1 = NULL; 317 struct lfs *fs; /* Superblock */ 318 SEGUSE *segp; /* Segment usage table */ 319 daddr_t sb_addr; /* Address of superblocks */ 320 daddr_t seg_addr; /* Address of current segment */ 321 int bsize; /* Block size */ 322 int fsize; /* Fragment size */ 323 int db_per_blk; /* Disk blocks per file block */ 324 int i, j; 325 int sb_interval; /* number of segs between super blocks */ 326 int ssize; /* Segment size */ 327 double fssize; 328 int warned_segtoobig=0; 329 int label_fsb, sb_fsb; 330 int curw, ww; 331 char tbuf[BUFSIZ]; 332 struct ubuf *bp; 333 struct uvnode *vp, *save_devvp; 334 int bb, ubb, dmeta, labelskew; 335 u_int64_t tsepb, tnseg; 336 337 /* 338 * Initialize buffer cache. Use a ballpark guess of the length of 339 * the segment table for the number of hash chains. 340 */ 341 tnseg = dkw->dkw_size / ((seg_size ? seg_size : DFL_LFSSEG) / secsize); 342 tsepb = (block_size ? block_size : DFL_LFSBLOCK) / sizeof(SEGSUM); 343 if (tnseg == 0) 344 fatal("zero size partition"); 345 bufinit(tnseg / tsepb); 346 347 /* Initialize LFS subsystem with blank superblock and ifile. */ 348 fs = lfs_init(devfd, start, (ufs_daddr_t)0, 1, 1/* XXX debug*/); 349 save_devvp = fs->lfs_devvp; 350 vp = fs->lfs_ivnode; 351 *fs = lfs_default; 352 fs->lfs_ivnode = vp; 353 fs->lfs_devvp = save_devvp; 354 355 356 /* Set version first of all since it is used to compute other fields */ 357 fs->lfs_version = version; 358 359 /* If partition is not an LFS partition, warn that that is the case */ 360 if (strcmp(dkw->dkw_ptype, DKW_PTYPE_LFS) != 0) { 361 fatal("partition label indicated fs type \"%s\", " 362 "expected \"%s\"", dkw->dkw_ptype, DKW_PTYPE_LFS); 363 } 364 365 if (!(bsize = block_size)) 366 bsize = DFL_LFSBLOCK; 367 if (!(fsize = frag_size)) 368 fsize = DFL_LFSFRAG; 369 if (!(ssize = seg_size)) { 370 ssize = DFL_LFSSEG; 371 } 372 if (version > 1) { 373 if (ibsize == 0) 374 ibsize = fsize; 375 if (ibsize <= 0 || ibsize % fsize) 376 fatal("illegal inode block size: %d\n", ibsize); 377 } else if (ibsize && ibsize != bsize) 378 fatal("cannot specify inode block size when version == 1\n"); 379 380 /* Sanity check: fsize<=bsize<ssize */ 381 if (fsize > bsize) { 382 /* Only complain if fsize was explicitly set */ 383 if(frag_size) 384 fatal("fragment size must be <= block size %d", bsize); 385 fsize = bsize; 386 } 387 if (bsize >= ssize) { 388 /* Only fatal if ssize was explicitly set */ 389 if(seg_size) 390 fatal("block size must be < segment size"); 391 warnx("%s: disklabel segment size (%d) too small, using default (%d)", 392 progname, ssize, DFL_LFSSEG); 393 ssize = DFL_LFSSEG; 394 } 395 if (start < 0 || start >= dkw->dkw_size) 396 fatal("filesystem offset %ld out of range", (long)start); 397 if (version == 1) { 398 if (start) 399 warnx("filesystem offset ignored for version 1 filesystem"); 400 start = LFS_LABELPAD / secsize; 401 } 402 403 tryagain: 404 /* Modify parts of superblock overridden by command line arguments */ 405 if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) { 406 fs->lfs_bshift = lfs_log2(bsize); 407 if (1 << fs->lfs_bshift != bsize) 408 fatal("%d: block size not a power of 2", bsize); 409 fs->lfs_bsize = bsize; 410 fs->lfs_fsize = fsize; 411 fs->lfs_bmask = bsize - 1; 412 fs->lfs_ffmask = fsize - 1; 413 fs->lfs_ffshift = lfs_log2(fsize); 414 if (1 << fs->lfs_ffshift != fsize) 415 fatal("%d: frag size not a power of 2", fsize); 416 fs->lfs_frag = numfrags(fs, bsize); 417 fs->lfs_fbmask = fs->lfs_frag - 1; 418 fs->lfs_fbshift = lfs_log2(fs->lfs_frag); 419 fs->lfs_ifpb = bsize / sizeof(IFILE); 420 /* XXX ondisk32 */ 421 fs->lfs_nindir = bsize / sizeof(int32_t); 422 } 423 424 if (fs->lfs_version == 1) { 425 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE; 426 fs->lfs_segshift = lfs_log2(ssize); 427 if (1 << fs->lfs_segshift != ssize) 428 fatal("%d: segment size not power of 2", ssize); 429 fs->lfs_segmask = ssize - 1; 430 fs->lfs_ifpb = fs->lfs_bsize / sizeof(IFILE_V1); 431 fs->lfs_ibsize = fs->lfs_bsize; 432 fs->lfs_sepb = bsize / sizeof(SEGUSE_V1); 433 fs->lfs_ssize = ssize >> fs->lfs_bshift; 434 } else { 435 if (ssize % fsize) { 436 fprintf(stderr, 437 "Segment size %d is not a multiple of frag size; ", 438 ssize); 439 ssize = roundup(ssize, fsize); 440 fprintf(stderr, "trying size %d.\n", ssize); 441 goto tryagain; 442 } 443 fs->lfs_sumsize = fsize; 444 fs->lfs_segshift = 0; 445 fs->lfs_segmask = 0; 446 fs->lfs_sepb = bsize / sizeof(SEGUSE); 447 fs->lfs_ssize = ssize; 448 fs->lfs_ibsize = ibsize; 449 } 450 fs->lfs_inopb = fs->lfs_ibsize / sizeof(struct ufs1_dinode); 451 fs->lfs_minfree = minfree; 452 453 if (version > 1) { 454 fs->lfs_inopf = secsize/DINODE1_SIZE; 455 fs->lfs_interleave = interleave; 456 if (roll_id == 0) 457 roll_id = arc4random(); 458 fs->lfs_ident = roll_id; 459 } 460 461 /* 462 * Fill in parts of superblock that can be computed from file system 463 * size, disk geometry and current time. 464 */ 465 db_per_blk = bsize/secsize; 466 fs->lfs_blktodb = lfs_log2(db_per_blk); 467 fs->lfs_fsbtodb = lfs_log2(fsize / secsize); 468 if (version == 1) { 469 fs->lfs_sushift = lfs_log2(fs->lfs_sepb); 470 fs->lfs_fsbtodb = 0; 471 fs->lfs_size = dkw->dkw_size >> fs->lfs_blktodb; 472 } 473 label_fsb = btofsb(fs, roundup(LFS_LABELPAD, fsize)); 474 sb_fsb = btofsb(fs, roundup(LFS_SBPAD, fsize)); 475 fs->lfs_fsbpseg = dbtofsb(fs, ssize / secsize); 476 fs->lfs_size = dkw->dkw_size >> fs->lfs_fsbtodb; 477 fs->lfs_dsize = dbtofsb(fs, dkw->dkw_size) - 478 MAX(label_fsb, dbtofsb(fs, start)); 479 fs->lfs_nseg = fs->lfs_dsize / segtod(fs, 1); 480 481 fs->lfs_nclean = fs->lfs_nseg - 1; 482 fs->lfs_maxfilesize = maxfilesize(fs->lfs_bshift); 483 484 if (minfreeseg == 0) 485 fs->lfs_minfreeseg = fs->lfs_nseg / DFL_MIN_FREE_SEGS; 486 else 487 fs->lfs_minfreeseg = minfreeseg; 488 if (fs->lfs_minfreeseg < MIN_FREE_SEGS) 489 fs->lfs_minfreeseg = MIN_FREE_SEGS; 490 491 if (resvseg == 0) 492 fs->lfs_resvseg = fs->lfs_minfreeseg / 2 + 1; 493 else 494 fs->lfs_resvseg = resvseg; 495 if (fs->lfs_resvseg < MIN_RESV_SEGS) 496 fs->lfs_resvseg = MIN_RESV_SEGS; 497 498 if(fs->lfs_nseg < fs->lfs_minfreeseg + 1 499 || fs->lfs_nseg < LFS_MIN_SBINTERVAL + 1) 500 { 501 if(seg_size == 0 && ssize > (bsize<<1)) { 502 if(!warned_segtoobig) { 503 fprintf(stderr,"Segment size %d is too large; " 504 "trying smaller sizes.\n", ssize); 505 if (ssize == (bsize << 16)) { 506 fprintf(stderr, "(Did you perhaps " 507 "accidentally leave \"16\" " 508 "in the disklabel's sgs " 509 "field?)\n"); 510 } 511 } 512 ++warned_segtoobig; 513 ssize >>= 1; 514 goto tryagain; 515 } 516 fatal("Could not allocate enough segments with segment " 517 "size %d and block size %d;\nplease decrease the " 518 "segment size.\n", ssize, fs->lfs_bsize); 519 } 520 521 /* 522 * Now that we've determined what we're going to do, announce it 523 * to the user. 524 */ 525 printf("Creating a version %d LFS", fs->lfs_version); 526 if (fs->lfs_version > 1) 527 printf(" with roll-forward ident 0x%x", fs->lfs_ident); 528 printf("\n"); 529 fssize = (double)fs->lfs_nseg; 530 fssize *= (double)ssize; 531 fssize /= 1048576.0; 532 printf("%.1fMB in %d segments of size %d\n", fssize, 533 fs->lfs_nseg, ssize); 534 535 /* 536 * The number of free blocks is set from the number of segments 537 * times the segment size - lfs_minfreesegs (that we never write 538 * because we need to make sure the cleaner can run). Then 539 * we'll subtract off the room for the superblocks ifile entries 540 * and segment usage table, and half a block per segment that can't 541 * be written due to fragmentation. 542 */ 543 fs->lfs_dsize = (fs->lfs_nseg - fs->lfs_minfreeseg) * 544 segtod(fs, 1); 545 fs->lfs_bfree = fs->lfs_dsize; 546 fs->lfs_bfree -= dbtofsb(fs, ((fs->lfs_nseg / 2) << 547 fs->lfs_blktodb)); 548 549 fs->lfs_segtabsz = SEGTABSIZE_SU(fs); 550 fs->lfs_cleansz = CLEANSIZE_SU(fs); 551 if ((fs->lfs_tstamp = time(NULL)) == -1) 552 fatal("time: %s", strerror(errno)); 553 if (version == 1) 554 fs->lfs_otstamp = fs->lfs_tstamp; 555 556 if ((sb_interval = fs->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL) 557 sb_interval = LFS_MIN_SBINTERVAL; 558 559 /* 560 * Figure out where the superblocks are going to live. 561 * 562 * Make segment 0 start at either zero, or LFS_LABELPAD, or 563 * >= LFS_SBPAD+LFS_LABELPAD, in order to prevent segment 0 564 * from having half a superblock in it. 565 */ 566 if (fsbtodb(fs, dbtofsb(fs, start)) != start) 567 fatal("Segment 0 offset is not multiple of frag size\n"); 568 if (start != 0 && dbtob(start) != LFS_LABELPAD && 569 dbtob(start) < LFS_SBPAD + LFS_LABELPAD) { 570 fatal("Using flags \"-O %" PRId64 "\" would result in the " 571 "first segment containing only\npart of a superblock. " 572 "Please choose an offset of 0, %d, or %d or more,\n", 573 start, btodb(LFS_LABELPAD), 574 btodb(LFS_LABELPAD + LFS_SBPAD)); 575 } 576 fs->lfs_sboffs[0] = label_fsb; 577 if (version == 1) 578 fs->lfs_start = fs->lfs_sboffs[0]; 579 else 580 fs->lfs_start = dbtofsb(fs, start); 581 fs->lfs_dsize -= sb_fsb; 582 for (i = 1; i < LFS_MAXNUMSB; i++) { 583 sb_addr = ((i * sb_interval) * segtod(fs, 1)) 584 + fs->lfs_sboffs[0]; 585 /* Segment 0 eats the label, except for version 1 */ 586 if (fs->lfs_version > 1 && fs->lfs_start < label_fsb) 587 sb_addr -= label_fsb - start; 588 if (sb_addr + sizeof(struct dlfs) 589 >= dbtofsb(fs, dkw->dkw_size)) 590 break; 591 fs->lfs_sboffs[i] = sb_addr; 592 fs->lfs_dsize -= sb_fsb; 593 } 594 595 /* We need >= 2 superblocks */ 596 if(fs->lfs_sboffs[1] == 0x0) { 597 fatal("Could not assign a disk address for the second " 598 "superblock.\nPlease decrease the segment size.\n"); 599 } 600 601 fs->lfs_lastseg = sntod(fs, fs->lfs_nseg - 2); 602 fs->lfs_curseg = sntod(fs, fs->lfs_nseg - 1); 603 fs->lfs_offset = sntod(fs, fs->lfs_nseg); 604 fs->lfs_nextseg = sntod(fs, 0); 605 606 /* 607 * Initialize the Ifile inode. Do this before we do anything 608 * with the Ifile or segment tables. 609 */ 610 dip = VTOI(fs->lfs_ivnode)->i_din.ffs1_din = (struct ufs1_dinode *) 611 malloc(sizeof(*dip)); 612 if (dip == NULL) 613 err(1, NULL); 614 memset(dip, 0, sizeof(*dip)); 615 dip->di_mode = IFREG|IREAD|IWRITE; 616 dip->di_flags = SF_IMMUTABLE; 617 make_dinode(LFS_IFILE_INUM, dip, 618 blkstofrags(fs, fs->lfs_cleansz + fs->lfs_segtabsz + 1), fs); 619 dip->di_size = (fs->lfs_cleansz + fs->lfs_segtabsz + 1) << fs->lfs_bshift; 620 for (i = 0; i < NDADDR && i < (dip->di_size >> fs->lfs_bshift); i++) 621 VTOI(fs->lfs_ivnode)->i_lfs_fragsize[i] = fs->lfs_bsize; 622 623 /* 624 * Set up in-superblock segment usage cache 625 */ 626 fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *)); 627 if (fs->lfs_suflags == NULL) 628 err(1, NULL); 629 fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t)); 630 if (fs->lfs_suflags[0] == NULL) 631 err(1, NULL); 632 fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t)); 633 if (fs->lfs_suflags[1] == NULL) 634 err(1, NULL); 635 636 /* 637 * Initialize the cleanerinfo block 638 */ 639 LFS_CLEANERINFO(cip, fs, bp); 640 cip->clean = fs->lfs_nseg; 641 cip->dirty = 0; 642 if (version > 1) { 643 cip->free_head = HIGHEST_USED_INO + 1; 644 cip->free_tail = fs->lfs_ifpb - 1; 645 } 646 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1); 647 648 /* 649 * Run through segment table and initialize that 650 */ 651 for (i = j = 0; i < fs->lfs_nseg; i++) { 652 LFS_SEGENTRY(segp, fs, i, bp); 653 654 if (i == 0 && 655 fs->lfs_start < btofsb(fs, LFS_LABELPAD + LFS_SBPAD)) { 656 segp->su_flags = SEGUSE_SUPERBLOCK; 657 fs->lfs_bfree -= sb_fsb; 658 ++j; 659 } 660 if (i > 0) { 661 if ((i % sb_interval) == 0 && j < LFS_MAXNUMSB) { 662 segp->su_flags = SEGUSE_SUPERBLOCK; 663 fs->lfs_bfree -= sb_fsb; 664 ++j; 665 } else 666 segp->su_flags = 0; 667 } 668 segp->su_lastmod = 0; 669 segp->su_nbytes = 0; 670 segp->su_ninos = 0; 671 segp->su_nsums = 0; 672 673 LFS_WRITESEGENTRY(segp, fs, i, bp); 674 } 675 676 /* Initialize root directory */ 677 vp = lfs_raw_vget(fs, ROOTINO, devfd, 0x0); 678 dip = VTOI(vp)->i_din.ffs1_din; 679 make_dinode(ROOTINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs); 680 dip->di_mode = IFDIR | UMASK; 681 VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ; 682 #ifdef MAKE_LF_DIR 683 VTOI(vp)->i_ffs_effnlink = dip->di_nlink = 3; 684 #else 685 VTOI(vp)->i_ffs_effnlink = dip->di_nlink = 2; 686 #endif 687 VTOI(vp)->i_lfs_effnblks = dip->di_blocks = 688 btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize)); 689 for (i = 0; i < NDADDR && i < howmany(DIRBLKSIZ, fs->lfs_bsize); i++) 690 VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize; 691 if (DIRBLKSIZ < fs->lfs_bsize) 692 VTOI(vp)->i_lfs_fragsize[i - 1] = 693 roundup(DIRBLKSIZ,fs->lfs_fsize); 694 bread(vp, 0, fs->lfs_fsize, NOCRED, &bp); 695 make_dir(bp->b_data, lfs_root_dir, 696 sizeof(lfs_root_dir) / sizeof(struct direct)); 697 VOP_BWRITE(bp); 698 699 #ifdef MAKE_LF_DIR 700 /* Initialize lost+found directory */ 701 vp = lfs_raw_vget(fs, LOSTFOUNDINO, devfd, 0x0); 702 dip = VTOI(vp)->i_din.ffs1_din; 703 make_dinode(LOSTFOUNDINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs); 704 dip->di_mode = IFDIR | UMASK; 705 VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ; 706 VTOI(vp)->i_ffs_effnlink = dip->di_nlink = 2; 707 VTOI(vp)->i_lfs_effnblks = dip->di_blocks = 708 btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize)); 709 for (i = 0; i < NDADDR && i < howmany(DIRBLKSIZ, fs->lfs_bsize); i++) 710 VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize; 711 if (DIRBLKSIZ < fs->lfs_bsize) 712 VTOI(vp)->i_lfs_fragsize[i - 1] = 713 roundup(DIRBLKSIZ,fs->lfs_fsize); 714 bread(vp, 0, fs->lfs_fsize, NOCRED, &bp); 715 make_dir(bp->b_data, lfs_lf_dir, 716 sizeof(lfs_lf_dir) / sizeof(struct direct)); 717 VOP_BWRITE(bp); 718 #endif /* MAKE_LF_DIR */ 719 720 /* Set their IFILE entry version numbers to 1 */ 721 LFS_IENTRY(ip, fs, 1, bp); 722 if (version == 1) { 723 ip_v1 = (IFILE_V1 *)ip; 724 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) { 725 ip_v1->if_version = 1; 726 ip_v1->if_daddr = 0x0; 727 ip_v1->if_nextfree = 0; 728 ++ip_v1; 729 } 730 } else { 731 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) { 732 ip->if_version = 1; 733 ip->if_daddr = 0x0; 734 ip->if_nextfree = 0; 735 ++ip; 736 } 737 } 738 /* Link remaining IFILE entries in free list */ 739 if (version == 1) { 740 for (; 741 i < fs->lfs_ifpb; ++ip_v1) { 742 ip_v1->if_version = 1; 743 ip_v1->if_daddr = LFS_UNUSED_DADDR; 744 ip_v1->if_nextfree = ++i; 745 } 746 --ip_v1; 747 ip_v1->if_nextfree = LFS_UNUSED_INUM; 748 } else { 749 for (; 750 i < fs->lfs_ifpb; ++ip) { 751 ip->if_version = 1; 752 ip->if_daddr = LFS_UNUSED_DADDR; 753 ip->if_nextfree = ++i; 754 } 755 --ip; 756 ip->if_nextfree = LFS_UNUSED_INUM; 757 } 758 VOP_BWRITE(bp); 759 760 /* Write it all to disk. */ 761 if (!Nflag) 762 lfs_segwrite(fs, SEGM_CKP); 763 764 /* 765 * Now that we've written everything, look to see what's available 766 * for writing. 767 */ 768 fs->lfs_avail = 0; 769 bb = ubb = dmeta = 0; 770 for (i = 0; i < fs->lfs_nseg; i++) { 771 LFS_SEGENTRY(segp, fs, i, bp); 772 if (segp->su_flags & SEGUSE_DIRTY) { 773 bb += btofsb(fs, segp->su_nbytes + 774 segp->su_nsums * fs->lfs_sumsize); 775 ubb += btofsb(fs, segp->su_nbytes + 776 segp->su_nsums * fs->lfs_sumsize + 777 segp->su_ninos * fs->lfs_ibsize); 778 dmeta += btofsb(fs, 779 fs->lfs_sumsize * segp->su_nsums); 780 dmeta += btofsb(fs, 781 fs->lfs_ibsize * segp->su_ninos); 782 } else { 783 fs->lfs_avail += segtod(fs, 1); 784 if (segp->su_flags & SEGUSE_SUPERBLOCK) 785 fs->lfs_avail -= btofsb(fs, LFS_SBPAD); 786 if (i == 0 && fs->lfs_version > 1 && 787 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) 788 fs->lfs_avail -= btofsb(fs, LFS_LABELPAD) - 789 fs->lfs_start; 790 } 791 brelse(bp, 0); 792 } 793 /* Also may be available bytes in current seg */ 794 i = dtosn(fs, fs->lfs_offset); 795 fs->lfs_avail += sntod(fs, i + 1) - fs->lfs_offset; 796 /* But do not count minfreesegs */ 797 fs->lfs_avail -= segtod(fs, (fs->lfs_minfreeseg - (fs->lfs_minfreeseg / 2))); 798 799 labelskew = 0; 800 if (fs->lfs_version > 1 && fs->lfs_start < btofsb(fs, LFS_LABELPAD)) 801 labelskew = btofsb(fs, LFS_LABELPAD); 802 fs->lfs_bfree = fs->lfs_dsize - labelskew - (ubb + bb) / 2; 803 804 /* Put that in the Ifile version too, and write it */ 805 LFS_CLEANERINFO(cip, fs, bp); 806 cip->bfree = fs->lfs_bfree; 807 cip->avail = fs->lfs_avail; 808 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1); 809 if (!Nflag) 810 lfs_segwrite(fs, SEGM_CKP); 811 812 /* 813 * Finally write out superblocks. 814 */ 815 printf("super-block backups (for fsck -b #) at:\n"); 816 curw = 0; 817 for (i = 0; i < LFS_MAXNUMSB; i++) { 818 seg_addr = fs->lfs_sboffs[i]; 819 if (seg_addr == 0) 820 break; 821 822 if (i != 0) 823 curw += printf(", "); 824 ww = snprintf(tbuf, sizeof(tbuf), "%lld", 825 (long long)fsbtodb(fs, seg_addr)); 826 curw += ww; 827 if (curw >= 78) { 828 printf("\n%s", tbuf); 829 curw = ww; 830 } else 831 printf("%s", tbuf); 832 fflush(stdout); 833 834 /* Leave the time stamp on the alt sb, zero the rest */ 835 if (i == 2) { 836 fs->lfs_tstamp = 0; 837 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs)); 838 } 839 if (!Nflag) 840 lfs_writesuper(fs, seg_addr); 841 } 842 printf(".\n"); 843 844 return 0; 845 } 846 847 /* 848 * Compatibility with fsck_lfs, since the "generic" LFS userland code uses it. 849 */ 850 void 851 pwarn(const char *fmt, ...) 852 { 853 va_list ap; 854 855 va_start(ap, fmt); 856 vfprintf(stderr, fmt, ap); 857 va_end(ap); 858 } 859