1 /* $NetBSD: make_lfs.c,v 1.28 2013/06/18 18:18:58 christos 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /*- 32 * Copyright (c) 1991, 1993 33 * The Regents of the University of California. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 */ 59 60 #include <sys/cdefs.h> 61 #ifndef lint 62 #if 0 63 static char sccsid[] = "@(#)lfs.c 8.5 (Berkeley) 5/24/95"; 64 #else 65 __RCSID("$NetBSD: make_lfs.c,v 1.28 2013/06/18 18:18:58 christos Exp $"); 66 #endif 67 #endif /* not lint */ 68 69 #include <sys/param.h> 70 #include <sys/disk.h> 71 #include <sys/time.h> 72 #include <sys/mount.h> 73 #include <sys/stat.h> 74 75 /* Override certain things to make <ufs/lfs/lfs.h> work */ 76 # undef simple_lock 77 # define simple_lock(x) 78 # undef simple_unlock 79 # define simple_unlock(x) 80 # define vnode uvnode 81 # define buf ubuf 82 # define panic call_panic 83 #include <ufs/lfs/lfs.h> 84 #include <ufs/lfs/lfs_inode.h> 85 86 #include <err.h> 87 #include <errno.h> 88 #include <stdio.h> 89 #include <stdlib.h> 90 #include <string.h> 91 #include <time.h> 92 #include <unistd.h> 93 94 #include "config.h" 95 #include "extern.h" 96 97 #include "bufcache.h" 98 #include "vnode.h" 99 #include "lfs_user.h" 100 #include "segwrite.h" 101 102 extern int Nflag; /* Don't write anything */ 103 ulfs_daddr_t ifibc; /* How many indirect blocks */ 104 105 #ifdef MAKE_LF_DIR 106 # define HIGHEST_USED_INO LOSTFOUNDINO 107 #else 108 # define HIGHEST_USED_INO ULFS_ROOTINO 109 #endif 110 111 static struct lfs lfs_default = { 112 .lfs_dlfs = { /* lfs_dlfs */ 113 /* dlfs_magic */ LFS_MAGIC, 114 /* dlfs_version */ LFS_VERSION, 115 /* dlfs_size */ 0, 116 /* dlfs_ssize */ DFL_LFSSEG, 117 /* dlfs_dsize */ 0, 118 /* dlfs_bsize */ DFL_LFSBLOCK, 119 /* dlfs_fsize */ DFL_LFSFRAG, 120 /* dlfs_frag */ DFL_LFSBLOCK/DFL_LFSFRAG, 121 /* dlfs_freehd */ HIGHEST_USED_INO + 1, 122 /* dlfs_bfree */ 0, 123 /* dlfs_nfiles */ 0, 124 /* dlfs_avail */ 0, 125 /* dlfs_uinodes */ 0, 126 /* dlfs_idaddr */ 0, 127 /* dlfs_ifile */ LFS_IFILE_INUM, 128 /* dlfs_lastseg */ 0, 129 /* dlfs_nextseg */ 0, 130 /* dlfs_curseg */ 0, 131 /* dlfs_offset */ 0, 132 /* dlfs_lastpseg */ 0, 133 /* dlfs_inopf */ 0, 134 /* dlfs_minfree */ MINFREE, 135 /* dlfs_maxfilesize */ 0, 136 /* dlfs_fsbpseg */ 0, 137 /* dlfs_inopb */ DFL_LFSBLOCK/sizeof(struct ulfs1_dinode), 138 /* dlfs_ifpb */ DFL_LFSBLOCK/sizeof(IFILE), 139 /* dlfs_sepb */ DFL_LFSBLOCK/sizeof(SEGUSE), 140 /* XXX ondisk32 */ 141 /* dlfs_nindir */ DFL_LFSBLOCK/sizeof(int32_t), 142 /* dlfs_nseg */ 0, 143 /* dlfs_nspf */ 0, 144 /* dlfs_cleansz */ 0, 145 /* dlfs_segtabsz */ 0, 146 /* dlfs_segmask */ DFL_LFSSEG_MASK, 147 /* dlfs_segshift */ DFL_LFSSEG_SHIFT, 148 /* dlfs_bshift */ DFL_LFSBLOCK_SHIFT, 149 /* dlfs_ffshift */ DFL_LFS_FFSHIFT, 150 /* dlfs_fbshift */ DFL_LFS_FBSHIFT, 151 /* dlfs_bmask */ DFL_LFSBLOCK_MASK, 152 /* dlfs_ffmask */ DFL_LFS_FFMASK, 153 /* dlfs_fbmask */ DFL_LFS_FBMASK, 154 /* dlfs_blktodb */ 0, 155 /* dlfs_sushift */ 0, 156 /* dlfs_maxsymlinklen */ ULFS1_MAXSYMLINKLEN, 157 /* dlfs_sboffs */ { 0 }, 158 /* dlfs_nclean */ 0, 159 /* dlfs_fsmnt */ { 0 }, 160 /* dlfs_pflags */ LFS_PF_CLEAN, 161 /* dlfs_dmeta */ 0, 162 /* dlfs_minfreeseg */ 0, 163 /* dlfs_sumsize */ 0, 164 /* dlfs_serial */ 0, 165 /* dlfs_ibsize */ DFL_LFSFRAG, 166 /* dlfs_start */ 0, 167 /* dlfs_tstamp */ 0, 168 /* dlfs_inodefmt */ LFS_44INODEFMT, 169 /* dlfs_interleave */ 0, 170 /* dlfs_ident */ 0, 171 /* dlfs_fsbtodb */ 0, 172 /* dlfs_resvseg */ 0, 173 174 /* dlfs_pad */ { 0 }, 175 /* dlfs_cksum */ 0 176 }, 177 }; 178 179 #define UMASK 0755 180 181 struct lfs_direct lfs_root_dir[] = { 182 { ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 1, "."}, 183 { ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 2, ".."}, 184 /* { LFS_IFILE_INUM, sizeof(struct lfs_direct), LFS_DT_REG, 5, "ifile"}, */ 185 #ifdef MAKE_LF_DIR 186 { LOSTFOUNDINO, sizeof(struct lfs_direct), LFS_DT_DIR, 10, "lost+found"}, 187 #endif 188 }; 189 190 #ifdef MAKE_LF_DIR 191 struct lfs_direct lfs_lf_dir[] = { 192 { LOSTFOUNDINO, sizeof(struct lfs_direct), LFS_DT_DIR, 1, "." }, 193 { ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 2, ".." }, 194 }; 195 #endif 196 197 void pwarn(const char *, ...); 198 static void make_dinode(ino_t, struct ulfs1_dinode *, int, struct lfs *); 199 static void make_dir( void *, struct lfs_direct *, int); 200 static uint64_t maxfilesize(int); 201 202 /* 203 * calculate the maximum file size allowed with the specified block shift. 204 */ 205 static uint64_t 206 maxfilesize(int bshift) 207 { 208 uint64_t nptr; /* number of block pointers per block */ 209 uint64_t maxblock; 210 211 nptr = (1 << bshift) / sizeof(uint32_t); 212 maxblock = ULFS_NDADDR + nptr + nptr * nptr + nptr * nptr * nptr; 213 214 return maxblock << bshift; 215 } 216 217 /* 218 * Create the root directory for this file system and the lost+found 219 * directory. 220 */ 221 static void 222 make_dinode(ino_t ino, struct ulfs1_dinode *dip, int nfrags, struct lfs *fs) 223 { 224 int fsb_per_blk, i; 225 int nblocks, bb, base, factor, lvl; 226 227 nblocks = howmany(nfrags, fs->lfs_frag); 228 if (nblocks >= ULFS_NDADDR) 229 nfrags = roundup(nfrags, fs->lfs_frag); 230 231 dip->di_nlink = 1; 232 dip->di_blocks = nfrags; 233 234 dip->di_size = (nfrags << fs->lfs_ffshift); 235 dip->di_atime = dip->di_mtime = dip->di_ctime = fs->lfs_tstamp; 236 dip->di_atimensec = dip->di_mtimensec = dip->di_ctimensec = 0; 237 dip->di_inumber = ino; 238 dip->di_gen = 1; 239 240 fsb_per_blk = lfs_blkstofrags(fs, 1); 241 242 if (ULFS_NDADDR < nblocks) { 243 /* Count up how many indirect blocks we need, recursively */ 244 /* XXX We are only called with nblocks > 1 for Ifile */ 245 bb = nblocks - ULFS_NDADDR; 246 while (bb > 0) { 247 bb = howmany(bb, LFS_NINDIR(fs)); 248 ifibc += bb; 249 --bb; 250 } 251 dip->di_blocks += lfs_blkstofrags(fs, ifibc); 252 } 253 254 /* Assign the block addresses for the ifile */ 255 for (i = 0; i < MIN(nblocks,ULFS_NDADDR); i++) { 256 dip->di_db[i] = 0x0; 257 } 258 if (nblocks > ULFS_NDADDR) { 259 dip->di_ib[0] = 0x0; 260 bb = howmany(nblocks - ULFS_NDADDR, LFS_NINDIR(fs)) - 1; 261 factor = LFS_NINDIR(fs); 262 base = -ULFS_NDADDR - factor; 263 lvl = 1; 264 while (bb > 0) { 265 dip->di_ib[lvl] = 0x0; 266 bb = howmany(bb, LFS_NINDIR(fs)); 267 --bb; 268 factor *= LFS_NINDIR(fs); 269 base -= factor; 270 ++lvl; 271 } 272 } 273 } 274 275 /* 276 * Construct a set of directory entries in "bufp". We assume that all the 277 * entries in protodir fir in the first DIRBLKSIZ. 278 */ 279 static void 280 make_dir(void *bufp, struct lfs_direct *protodir, int entries) 281 { 282 char *cp; 283 int i, spcleft; 284 285 spcleft = LFS_DIRBLKSIZ; 286 for (cp = bufp, i = 0; i < entries - 1; i++) { 287 protodir[i].d_reclen = LFS_DIRSIZ(LFS_NEWDIRFMT, &protodir[i], 0); 288 memmove(cp, &protodir[i], protodir[i].d_reclen); 289 cp += protodir[i].d_reclen; 290 if ((spcleft -= protodir[i].d_reclen) < 0) 291 fatal("%s: %s", special, "directory too big"); 292 } 293 protodir[i].d_reclen = spcleft; 294 memmove(cp, &protodir[i], LFS_DIRSIZ(LFS_NEWDIRFMT, &protodir[i], 0)); 295 } 296 297 int 298 make_lfs(int devfd, uint secsize, struct dkwedge_info *dkw, int minfree, 299 int block_size, int frag_size, int seg_size, int minfreeseg, 300 int resvseg, int version, daddr_t start, int ibsize, int interleave, 301 u_int32_t roll_id) 302 { 303 struct ulfs1_dinode *dip; /* Pointer to a disk inode */ 304 CLEANERINFO *cip; /* Segment cleaner information table */ 305 IFILE *ip; /* Pointer to array of ifile structures */ 306 IFILE_V1 *ip_v1 = NULL; 307 struct lfs *fs; /* Superblock */ 308 SEGUSE *segp; /* Segment usage table */ 309 daddr_t sb_addr; /* Address of superblocks */ 310 daddr_t seg_addr; /* Address of current segment */ 311 int bsize; /* Block size */ 312 int fsize; /* Fragment size */ 313 int db_per_blk; /* Disk blocks per file block */ 314 int i, j; 315 int sb_interval; /* number of segs between super blocks */ 316 int ssize; /* Segment size */ 317 double fssize; 318 int warned_segtoobig=0; 319 int label_fsb, sb_fsb; 320 int curw, ww; 321 char tbuf[BUFSIZ]; 322 struct ubuf *bp; 323 struct uvnode *vp, *save_devvp; 324 int bb, ubb, dmeta, labelskew; 325 u_int64_t tsepb, tnseg; 326 327 /* 328 * Initialize buffer cache. Use a ballpark guess of the length of 329 * the segment table for the number of hash chains. 330 */ 331 tnseg = dkw->dkw_size / ((seg_size ? seg_size : DFL_LFSSEG) / secsize); 332 tsepb = (block_size ? block_size : DFL_LFSBLOCK) / sizeof(SEGSUM); 333 if (tnseg == 0) 334 fatal("zero size partition"); 335 bufinit(tnseg / tsepb); 336 337 /* Initialize LFS subsystem with blank superblock and ifile. */ 338 fs = lfs_init(devfd, start, (ulfs_daddr_t)0, 1, 1/* XXX debug*/); 339 save_devvp = fs->lfs_devvp; 340 vp = fs->lfs_ivnode; 341 *fs = lfs_default; 342 fs->lfs_ivnode = vp; 343 fs->lfs_devvp = save_devvp; 344 345 346 /* Set version first of all since it is used to compute other fields */ 347 fs->lfs_version = version; 348 349 /* If partition is not an LFS partition, warn that that is the case */ 350 if (strcmp(dkw->dkw_ptype, DKW_PTYPE_LFS) != 0) { 351 fatal("partition label indicated fs type \"%s\", " 352 "expected \"%s\"", dkw->dkw_ptype, DKW_PTYPE_LFS); 353 } 354 355 if (!(bsize = block_size)) { 356 bsize = DFL_LFSBLOCK; 357 if (dkw->dkw_size <= SMALL_FSSIZE) 358 bsize = SMALL_LFSBLOCK; 359 } 360 if (!(fsize = frag_size)) { 361 fsize = DFL_LFSFRAG; 362 if (dkw->dkw_size <= SMALL_FSSIZE) 363 fsize = SMALL_LFSFRAG; 364 } 365 if (!(ssize = seg_size)) { 366 ssize = DFL_LFSSEG; 367 if (dkw->dkw_size <= SMALL_FSSIZE) 368 ssize = SMALL_LFSSEG; 369 } 370 if (version > 1) { 371 if (ibsize == 0) 372 ibsize = fsize; 373 if (ibsize <= 0 || ibsize % fsize) 374 fatal("illegal inode block size: %d\n", ibsize); 375 } else if (ibsize && ibsize != bsize) 376 fatal("cannot specify inode block size when version == 1\n"); 377 378 /* Sanity check: fsize<=bsize<ssize */ 379 if (fsize > bsize) { 380 /* Only complain if fsize was explicitly set */ 381 if(frag_size) 382 fatal("fragment size must be <= block size %d", bsize); 383 fsize = bsize; 384 } 385 if (bsize >= ssize) { 386 /* Only fatal if ssize was explicitly set */ 387 if(seg_size) 388 fatal("block size must be < segment size"); 389 warnx("%s: disklabel segment size (%d) too small, using default (%d)", 390 progname, ssize, DFL_LFSSEG); 391 ssize = DFL_LFSSEG; 392 } 393 if (start < 0 || start >= dkw->dkw_size) 394 fatal("filesystem offset %ld out of range", (long)start); 395 if (version == 1) { 396 if (start) 397 warnx("filesystem offset ignored for version 1 filesystem"); 398 start = LFS_LABELPAD / secsize; 399 } 400 401 tryagain: 402 /* Modify parts of superblock overridden by command line arguments */ 403 if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) { 404 fs->lfs_bshift = lfs_log2(bsize); 405 if (1 << fs->lfs_bshift != bsize) 406 fatal("%d: block size not a power of 2", bsize); 407 fs->lfs_bsize = bsize; 408 fs->lfs_fsize = fsize; 409 fs->lfs_bmask = bsize - 1; 410 fs->lfs_ffmask = fsize - 1; 411 fs->lfs_ffshift = lfs_log2(fsize); 412 if (1 << fs->lfs_ffshift != fsize) 413 fatal("%d: frag size not a power of 2", fsize); 414 fs->lfs_frag = lfs_numfrags(fs, bsize); 415 fs->lfs_fbmask = fs->lfs_frag - 1; 416 fs->lfs_fbshift = lfs_log2(fs->lfs_frag); 417 fs->lfs_ifpb = bsize / sizeof(IFILE); 418 /* XXX ondisk32 */ 419 fs->lfs_nindir = bsize / sizeof(int32_t); 420 } 421 422 if (fs->lfs_version == 1) { 423 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE; 424 fs->lfs_segshift = lfs_log2(ssize); 425 if (1 << fs->lfs_segshift != ssize) 426 fatal("%d: segment size not power of 2", ssize); 427 fs->lfs_segmask = ssize - 1; 428 fs->lfs_ifpb = fs->lfs_bsize / sizeof(IFILE_V1); 429 fs->lfs_ibsize = fs->lfs_bsize; 430 fs->lfs_sepb = bsize / sizeof(SEGUSE_V1); 431 fs->lfs_ssize = ssize >> fs->lfs_bshift; 432 } else { 433 if (ssize % fsize) { 434 fprintf(stderr, 435 "Segment size %d is not a multiple of frag size; ", 436 ssize); 437 ssize = roundup(ssize, fsize); 438 fprintf(stderr, "trying size %d.\n", ssize); 439 goto tryagain; 440 } 441 fs->lfs_sumsize = fsize; 442 fs->lfs_segshift = 0; 443 fs->lfs_segmask = 0; 444 fs->lfs_sepb = bsize / sizeof(SEGUSE); 445 fs->lfs_ssize = ssize; 446 fs->lfs_ibsize = ibsize; 447 } 448 fs->lfs_inopb = fs->lfs_ibsize / sizeof(struct ulfs1_dinode); 449 fs->lfs_minfree = minfree; 450 451 if (version > 1) { 452 fs->lfs_inopf = secsize/LFS_DINODE1_SIZE; 453 fs->lfs_interleave = interleave; 454 if (roll_id == 0) 455 roll_id = arc4random(); 456 fs->lfs_ident = roll_id; 457 } 458 459 /* 460 * Fill in parts of superblock that can be computed from file system 461 * size, disk geometry and current time. 462 */ 463 db_per_blk = bsize/secsize; 464 fs->lfs_blktodb = lfs_log2(db_per_blk); 465 fs->lfs_fsbtodb = lfs_log2(fsize / secsize); 466 if (version == 1) { 467 fs->lfs_sushift = lfs_log2(fs->lfs_sepb); 468 fs->lfs_fsbtodb = 0; 469 fs->lfs_size = dkw->dkw_size >> fs->lfs_blktodb; 470 } 471 label_fsb = lfs_btofsb(fs, roundup(LFS_LABELPAD, fsize)); 472 sb_fsb = lfs_btofsb(fs, roundup(LFS_SBPAD, fsize)); 473 fs->lfs_fsbpseg = LFS_DBTOFSB(fs, ssize / secsize); 474 fs->lfs_size = dkw->dkw_size >> fs->lfs_fsbtodb; 475 fs->lfs_dsize = LFS_DBTOFSB(fs, dkw->dkw_size) - 476 MAX(label_fsb, LFS_DBTOFSB(fs, start)); 477 fs->lfs_nseg = fs->lfs_dsize / lfs_segtod(fs, 1); 478 479 fs->lfs_nclean = fs->lfs_nseg - 1; 480 fs->lfs_maxfilesize = maxfilesize(fs->lfs_bshift); 481 482 if (minfreeseg == 0) 483 fs->lfs_minfreeseg = fs->lfs_nseg / DFL_MIN_FREE_SEGS; 484 else 485 fs->lfs_minfreeseg = minfreeseg; 486 if (fs->lfs_minfreeseg < MIN_FREE_SEGS) 487 fs->lfs_minfreeseg = MIN_FREE_SEGS; 488 489 if (resvseg == 0) 490 fs->lfs_resvseg = fs->lfs_minfreeseg / 2 + 1; 491 else 492 fs->lfs_resvseg = resvseg; 493 if (fs->lfs_resvseg < MIN_RESV_SEGS) 494 fs->lfs_resvseg = MIN_RESV_SEGS; 495 496 if(fs->lfs_nseg < (4 * fs->lfs_minfreeseg) 497 || fs->lfs_nseg < LFS_MIN_SBINTERVAL + 1) 498 { 499 if(seg_size == 0 && ssize > (bsize<<1)) { 500 if(!warned_segtoobig) { 501 fprintf(stderr,"Segment size %d is too large; " 502 "trying smaller sizes.\n", ssize); 503 if (ssize == (bsize << 16)) { 504 fprintf(stderr, "(Did you perhaps " 505 "accidentally leave \"16\" " 506 "in the disklabel's sgs " 507 "field?)\n"); 508 } 509 } 510 ++warned_segtoobig; 511 ssize >>= 1; 512 goto tryagain; 513 } 514 fatal("Could not allocate enough segments with segment " 515 "size %d and block size %d;\nplease decrease the " 516 "segment size.\n", ssize, fs->lfs_bsize); 517 } 518 if(warned_segtoobig) 519 fprintf(stderr,"Using segment size %d, block size %d, frag size %d.\n", ssize, bsize, fsize); 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 lfs_segtod(fs, 1); 545 fs->lfs_bfree = fs->lfs_dsize; 546 fs->lfs_bfree -= LFS_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 (LFS_FSBTODB(fs, LFS_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 = LFS_DBTOFSB(fs, start); 581 fs->lfs_dsize -= sb_fsb; 582 for (i = 1; i < LFS_MAXNUMSB; i++) { 583 sb_addr = ((i * sb_interval) * lfs_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 >= LFS_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 = lfs_sntod(fs, fs->lfs_nseg - 2); 602 fs->lfs_curseg = lfs_sntod(fs, fs->lfs_nseg - 1); 603 fs->lfs_offset = lfs_sntod(fs, fs->lfs_nseg); 604 fs->lfs_nextseg = lfs_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 ulfs1_dinode *) 611 malloc(sizeof(*dip)); 612 if (dip == NULL) 613 err(1, NULL); 614 memset(dip, 0, sizeof(*dip)); 615 dip->di_mode = LFS_IFREG | 0600; 616 dip->di_flags = SF_IMMUTABLE; 617 make_dinode(LFS_IFILE_INUM, dip, 618 lfs_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 < ULFS_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 < lfs_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, ULFS_ROOTINO, devfd, 0x0); 678 dip = VTOI(vp)->i_din.ffs1_din; 679 make_dinode(ULFS_ROOTINO, dip, howmany(LFS_DIRBLKSIZ,fs->lfs_fsize), fs); 680 dip->di_mode = LFS_IFDIR | UMASK; 681 VTOI(vp)->i_lfs_osize = dip->di_size = LFS_DIRBLKSIZ; 682 #ifdef MAKE_LF_DIR 683 VTOI(vp)->i_nlink = dip->di_nlink = 3; 684 #else 685 VTOI(vp)->i_nlink = dip->di_nlink = 2; 686 #endif 687 VTOI(vp)->i_lfs_effnblks = dip->di_blocks = 688 lfs_btofsb(fs, roundup(LFS_DIRBLKSIZ,fs->lfs_fsize)); 689 for (i = 0; i < ULFS_NDADDR && i < howmany(LFS_DIRBLKSIZ, fs->lfs_bsize); i++) 690 VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize; 691 if (LFS_DIRBLKSIZ < fs->lfs_bsize) 692 VTOI(vp)->i_lfs_fragsize[i - 1] = 693 roundup(LFS_DIRBLKSIZ,fs->lfs_fsize); 694 bread(vp, 0, fs->lfs_fsize, NOCRED, 0, &bp); 695 make_dir(bp->b_data, lfs_root_dir, 696 sizeof(lfs_root_dir) / sizeof(struct lfs_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_nlink = dip->di_nlink = 2; 707 VTOI(vp)->i_lfs_effnblks = dip->di_blocks = 708 lfs_btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize)); 709 for (i = 0; i < ULFS_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, 0, &bp); 715 make_dir(bp->b_data, lfs_lf_dir, 716 sizeof(lfs_lf_dir) / sizeof(struct lfs_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 += lfs_btofsb(fs, segp->su_nbytes + 774 segp->su_nsums * fs->lfs_sumsize); 775 ubb += lfs_btofsb(fs, segp->su_nbytes + 776 segp->su_nsums * fs->lfs_sumsize + 777 segp->su_ninos * fs->lfs_ibsize); 778 dmeta += lfs_btofsb(fs, 779 fs->lfs_sumsize * segp->su_nsums); 780 dmeta += lfs_btofsb(fs, 781 fs->lfs_ibsize * segp->su_ninos); 782 } else { 783 fs->lfs_avail += lfs_segtod(fs, 1); 784 if (segp->su_flags & SEGUSE_SUPERBLOCK) 785 fs->lfs_avail -= lfs_btofsb(fs, LFS_SBPAD); 786 if (i == 0 && fs->lfs_version > 1 && 787 fs->lfs_start < lfs_btofsb(fs, LFS_LABELPAD)) 788 fs->lfs_avail -= lfs_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 = lfs_dtosn(fs, fs->lfs_offset); 795 fs->lfs_avail += lfs_sntod(fs, i + 1) - fs->lfs_offset; 796 /* But do not count minfreesegs */ 797 fs->lfs_avail -= lfs_segtod(fs, (fs->lfs_minfreeseg - (fs->lfs_minfreeseg / 2))); 798 799 labelskew = 0; 800 if (fs->lfs_version > 1 && fs->lfs_start < lfs_btofsb(fs, LFS_LABELPAD)) 801 labelskew = lfs_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)LFS_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