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