Home | History | Annotate | Line # | Download | only in newfs_lfs
make_lfs.c revision 1.34
      1 /*	$NetBSD: make_lfs.c,v 1.34 2015/07/28 05:09:34 dholland 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 (at) 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.34 2015/07/28 05:09:34 dholland 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_accessors.h>
     85 #include <ufs/lfs/lfs_inode.h>
     86 
     87 #include <err.h>
     88 #include <errno.h>
     89 #include <stdio.h>
     90 #include <stdlib.h>
     91 #include <string.h>
     92 #include <time.h>
     93 #include <unistd.h>
     94 
     95 #include "config.h"
     96 #include "extern.h"
     97 
     98 #include "bufcache.h"
     99 #include "vnode.h"
    100 #include "lfs_user.h"
    101 #include "segwrite.h"
    102 
    103 extern int Nflag; /* Don't write anything */
    104 ulfs_daddr_t ifibc; /* How many indirect blocks */
    105 
    106 #ifdef MAKE_LF_DIR
    107 # define HIGHEST_USED_INO LOSTFOUNDINO
    108 #else
    109 # define HIGHEST_USED_INO ULFS_ROOTINO
    110 #endif
    111 
    112 static struct lfs lfs_default =  {
    113 	.lfs_dlfs = { /* lfs_dlfs */
    114 		/* dlfs_magic */	LFS_MAGIC,
    115 		/* dlfs_version */	LFS_VERSION,
    116 		/* dlfs_size */		0,
    117 		/* dlfs_ssize */	DFL_LFSSEG,
    118 		/* dlfs_dsize */	0,
    119 		/* dlfs_bsize */	DFL_LFSBLOCK,
    120 		/* dlfs_fsize */	DFL_LFSFRAG,
    121 		/* dlfs_frag */		DFL_LFSBLOCK/DFL_LFSFRAG,
    122 		/* dlfs_freehd */	HIGHEST_USED_INO + 1,
    123 		/* dlfs_bfree */	0,
    124 		/* dlfs_nfiles */	0,
    125 		/* dlfs_avail */	0,
    126 		/* dlfs_uinodes */	0,
    127 		/* dlfs_idaddr */	0,
    128 		/* dlfs_ifile */	LFS_IFILE_INUM,
    129 		/* dlfs_lastseg */	0,
    130 		/* dlfs_nextseg */	0,
    131 		/* dlfs_curseg */	0,
    132 		/* dlfs_offset */	0,
    133 		/* dlfs_lastpseg */	0,
    134 		/* dlfs_inopf */	0,
    135 		/* dlfs_minfree */	MINFREE,
    136 		/* dlfs_maxfilesize */	0,
    137 		/* dlfs_fsbpseg */	0,
    138 		/* dlfs_inopb */	DFL_LFSBLOCK/sizeof(struct ulfs1_dinode),
    139 		/* dlfs_ifpb */		DFL_LFSBLOCK/sizeof(IFILE),
    140 		/* dlfs_sepb */		DFL_LFSBLOCK/sizeof(SEGUSE),
    141 		/* XXX ondisk32 */
    142 		/* dlfs_nindir */	DFL_LFSBLOCK/sizeof(int32_t),
    143 		/* dlfs_nseg */		0,
    144 		/* dlfs_nspf */		0,
    145 		/* dlfs_cleansz */	0,
    146 		/* dlfs_segtabsz */	0,
    147 		/* dlfs_segmask */	DFL_LFSSEG_MASK,
    148 		/* dlfs_segshift */	DFL_LFSSEG_SHIFT,
    149 		/* dlfs_bshift */	DFL_LFSBLOCK_SHIFT,
    150 		/* dlfs_ffshift */	DFL_LFS_FFSHIFT,
    151 		/* dlfs_fbshift */	DFL_LFS_FBSHIFT,
    152 		/* dlfs_bmask */	DFL_LFSBLOCK_MASK,
    153 		/* dlfs_ffmask */	DFL_LFS_FFMASK,
    154 		/* dlfs_fbmask */	DFL_LFS_FBMASK,
    155 		/* dlfs_blktodb */	0,
    156 		/* dlfs_sushift */	0,
    157 		/* dlfs_maxsymlinklen */	ULFS1_MAXSYMLINKLEN,
    158 		/* dlfs_sboffs */	{ 0 },
    159 		/* dlfs_nclean */       0,
    160 		/* dlfs_fsmnt */        { 0 },
    161 		/* dlfs_pflags */       LFS_PF_CLEAN,
    162 		/* dlfs_dmeta */	0,
    163 		/* dlfs_minfreeseg */   0,
    164 		/* dlfs_sumsize */	0,
    165 		/* dlfs_serial */	0,
    166 		/* dlfs_ibsize */	DFL_LFSFRAG,
    167 		/* dlfs_start */	0,
    168 		/* dlfs_tstamp */       0,
    169 		/* dlfs_inodefmt */     LFS_44INODEFMT,
    170 		/* dlfs_interleave */   0,
    171 		/* dlfs_ident */        0,
    172 		/* dlfs_fsbtodb */      0,
    173 		/* dlfs_resvseg */      0,
    174 
    175 		/* dlfs_pad */ 		{ 0 },
    176 		/* dlfs_cksum */	0
    177 	},
    178 };
    179 
    180 #define	UMASK	0755
    181 
    182 struct lfs_direct lfs_root_dir[] = {
    183 	{ ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 1, "."},
    184 	{ ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 2, ".."},
    185 	/* { LFS_IFILE_INUM, sizeof(struct lfs_direct), LFS_DT_REG, 5, "ifile"}, */
    186 #ifdef MAKE_LF_DIR
    187 	{ LOSTFOUNDINO, sizeof(struct lfs_direct), LFS_DT_DIR, 10, "lost+found"},
    188 #endif
    189 };
    190 
    191 #ifdef MAKE_LF_DIR
    192 struct lfs_direct lfs_lf_dir[] = {
    193         { LOSTFOUNDINO, sizeof(struct lfs_direct), LFS_DT_DIR, 1, "." },
    194         { ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 2, ".." },
    195 };
    196 #endif
    197 
    198 void pwarn(const char *, ...);
    199 static void make_dinode(ino_t, struct ulfs1_dinode *, int, struct lfs *);
    200 static void make_dir( void *, struct lfs_direct *, int);
    201 static uint64_t maxfilesize(int);
    202 
    203 /*
    204  * calculate the maximum file size allowed with the specified block shift.
    205  */
    206 static uint64_t
    207 maxfilesize(int bshift)
    208 {
    209 	uint64_t nptr; /* number of block pointers per block */
    210 	uint64_t maxblock;
    211 
    212 	nptr = (1 << bshift) / sizeof(uint32_t);
    213 	maxblock = ULFS_NDADDR + nptr + nptr * nptr + nptr * nptr * nptr;
    214 
    215 	return maxblock << bshift;
    216 }
    217 
    218 /*
    219  * Create the root directory for this file system and the lost+found
    220  * directory.
    221  */
    222 static void
    223 make_dinode(ino_t ino, struct ulfs1_dinode *dip, int nfrags, struct lfs *fs)
    224 {
    225 	int i;
    226 	int nblocks, bb, base, factor, lvl;
    227 
    228 	nblocks = howmany(nfrags, lfs_sb_getfrag(fs));
    229 	if (nblocks >= ULFS_NDADDR)
    230 		nfrags = roundup(nfrags, lfs_sb_getfrag(fs));
    231 
    232 	dip->di_nlink = 1;
    233 	dip->di_blocks = nfrags;
    234 
    235 	dip->di_size = (nfrags << lfs_sb_getffshift(fs));
    236 	dip->di_atime = dip->di_mtime = dip->di_ctime = lfs_sb_gettstamp(fs);
    237 	dip->di_atimensec = dip->di_mtimensec = dip->di_ctimensec = 0;
    238 	dip->di_inumber = ino;
    239 	dip->di_gen = 1;
    240 
    241 	if (ULFS_NDADDR < nblocks) {
    242 		/* Count up how many indirect blocks we need, recursively */
    243 		/* XXX We are only called with nblocks > 1 for Ifile */
    244 		bb = nblocks - ULFS_NDADDR;
    245 		while (bb > 0) {
    246 			bb = howmany(bb, LFS_NINDIR(fs));
    247 			ifibc += bb;
    248 			--bb;
    249 		}
    250 		dip->di_blocks += lfs_blkstofrags(fs, ifibc);
    251 	}
    252 
    253 	/* Assign the block addresses for the ifile */
    254 	for (i = 0; i < MIN(nblocks,ULFS_NDADDR); i++) {
    255 		dip->di_db[i] = 0x0;
    256 	}
    257 	if (nblocks > ULFS_NDADDR) {
    258 		dip->di_ib[0] = 0x0;
    259 		bb = howmany(nblocks - ULFS_NDADDR, LFS_NINDIR(fs)) - 1;
    260 		factor = LFS_NINDIR(fs);
    261 		base = -ULFS_NDADDR - factor;
    262 		lvl = 1;
    263 		while (bb > 0) {
    264 			dip->di_ib[lvl] = 0x0;
    265 			bb = howmany(bb, LFS_NINDIR(fs));
    266 			--bb;
    267 			factor *= LFS_NINDIR(fs);
    268 			base -= factor;
    269 			++lvl;
    270 		}
    271 	}
    272 }
    273 
    274 /*
    275  * Construct a set of directory entries in "bufp".  We assume that all the
    276  * entries in protodir fir in the first DIRBLKSIZ.
    277  */
    278 static void
    279 make_dir(void *bufp, struct lfs_direct *protodir, int entries)
    280 {
    281 	char *cp;
    282 	int i, spcleft;
    283 
    284 	spcleft = LFS_DIRBLKSIZ;
    285 	for (cp = bufp, i = 0; i < entries - 1; i++) {
    286 		protodir[i].d_reclen = LFS_DIRSIZ(LFS_NEWDIRFMT, &protodir[i], 0);
    287 		memmove(cp, &protodir[i], protodir[i].d_reclen);
    288 		cp += protodir[i].d_reclen;
    289 		if ((spcleft -= protodir[i].d_reclen) < 0)
    290 			fatal("%s: %s", special, "directory too big");
    291 	}
    292 	protodir[i].d_reclen = spcleft;
    293 	memmove(cp, &protodir[i], LFS_DIRSIZ(LFS_NEWDIRFMT, &protodir[i], 0));
    294 }
    295 
    296 int
    297 make_lfs(int devfd, uint secsize, struct dkwedge_info *dkw, int minfree,
    298 	 int block_size, int frag_size, int seg_size, int minfreeseg,
    299 	 int resvseg, int version, daddr_t start, int ibsize, int interleave,
    300 	 u_int32_t roll_id)
    301 {
    302 	struct ulfs1_dinode *dip;	/* Pointer to a disk inode */
    303 	CLEANERINFO *cip;	/* Segment cleaner information table */
    304 	IFILE *ip;		/* Pointer to array of ifile structures */
    305 	IFILE_V1 *ip_v1 = NULL;
    306 	struct lfs *fs;		/* Superblock */
    307 	SEGUSE *segp;		/* Segment usage table */
    308 	daddr_t	sb_addr;	/* Address of superblocks */
    309 	daddr_t	seg_addr;	/* Address of current segment */
    310 	int bsize;		/* Block size */
    311 	int fsize;		/* Fragment size */
    312 	int db_per_blk;		/* Disk blocks per file block */
    313 	int i, j;
    314 	int sb_interval;	/* number of segs between super blocks */
    315 	int ssize;		/* Segment size */
    316 	double fssize;
    317 	int warned_segtoobig=0;
    318 	int label_fsb, sb_fsb;
    319 	int curw, ww;
    320 	char tbuf[BUFSIZ];
    321 	struct ubuf *bp;
    322 	struct uvnode *vp, *save_devvp;
    323 	int bb, ubb, dmeta, labelskew;
    324 	u_int64_t tsepb, tnseg;
    325 	time_t stamp;
    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 		lfs_sb_setbshift(fs, lfs_log2(bsize));
    405 		if (1 << lfs_sb_getbshift(fs) != bsize)
    406 			fatal("%d: block size not a power of 2", bsize);
    407 		lfs_sb_setbsize(fs, bsize);
    408 		lfs_sb_setfsize(fs, fsize);
    409 		lfs_sb_setbmask(fs, bsize - 1);
    410 		lfs_sb_setffmask(fs, fsize - 1);
    411 		lfs_sb_setffshift(fs, lfs_log2(fsize));
    412 		if (1 << lfs_sb_getffshift(fs) != fsize)
    413 			fatal("%d: frag size not a power of 2", fsize);
    414 		lfs_sb_setfrag(fs, lfs_numfrags(fs, bsize));
    415 		lfs_sb_setfbmask(fs, lfs_sb_getfrag(fs) - 1);
    416 		lfs_sb_setfbshift(fs, lfs_log2(lfs_sb_getfrag(fs)));
    417 		lfs_sb_setifpb(fs, bsize / sizeof(IFILE));
    418 		/* XXX ondisk32 */
    419 		lfs_sb_setnindir(fs, bsize / sizeof(int32_t));
    420 	}
    421 
    422 	if (fs->lfs_version == 1) {
    423 		lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
    424 		lfs_sb_setsegshift(fs, lfs_log2(ssize));
    425 		if (1 << lfs_sb_getsegshift(fs) != ssize)
    426 			fatal("%d: segment size not power of 2", ssize);
    427 		lfs_sb_setsegmask(fs, ssize - 1);
    428 		lfs_sb_setifpb(fs, lfs_sb_getbsize(fs) / sizeof(IFILE_V1));
    429 		lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
    430 		lfs_sb_setsepb(fs, bsize / sizeof(SEGUSE_V1));
    431 		lfs_sb_setssize(fs, ssize >> lfs_sb_getbshift(fs));
    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 		lfs_sb_setsumsize(fs, fsize);
    442 		lfs_sb_setsegshift(fs, 0);
    443 		lfs_sb_setsegmask(fs, 0);
    444 		lfs_sb_setsepb(fs, bsize / sizeof(SEGUSE));
    445 		lfs_sb_setssize(fs, ssize);
    446 		lfs_sb_setibsize(fs, ibsize);
    447 	}
    448 	lfs_sb_setinopb(fs, lfs_sb_getibsize(fs) / sizeof(struct ulfs1_dinode));
    449 	lfs_sb_setminfree(fs, minfree);
    450 
    451 	if (version > 1) {
    452 		lfs_sb_setinopf(fs, secsize/LFS_DINODE1_SIZE);
    453 		lfs_sb_setinterleave(fs, interleave);
    454 		if (roll_id == 0)
    455 			roll_id = arc4random();
    456 		lfs_sb_setident(fs, 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 	 * XXX: this seems to set dlfs_size wrong for version 1... as in,
    464 	 * sets it and then overwrites it a few lines later.
    465 	 */
    466 	db_per_blk = bsize/secsize;
    467 	lfs_sb_setblktodb(fs, lfs_log2(db_per_blk));
    468 	lfs_sb_setfsbtodb(fs, lfs_log2(fsize / secsize));
    469 	if (version == 1) {
    470 		lfs_sb_setsushift(fs, lfs_log2(lfs_sb_getsepb(fs)));
    471 		lfs_sb_setfsbtodb(fs, 0);
    472 		lfs_sb_setsize(fs, dkw->dkw_size >> lfs_sb_getblktodb(fs));
    473 	}
    474 	label_fsb = lfs_btofsb(fs, roundup(LFS_LABELPAD, fsize));
    475 	sb_fsb = lfs_btofsb(fs, roundup(LFS_SBPAD, fsize));
    476 	lfs_sb_setfsbpseg(fs, LFS_DBTOFSB(fs, ssize / secsize));
    477 	lfs_sb_setsize(fs, dkw->dkw_size >> lfs_sb_getfsbtodb(fs));
    478 	lfs_sb_setdsize(fs, LFS_DBTOFSB(fs, dkw->dkw_size) -
    479 		MAX(label_fsb, LFS_DBTOFSB(fs, start)));
    480 	lfs_sb_setnseg(fs, lfs_sb_getdsize(fs) / lfs_segtod(fs, 1));
    481 
    482 	lfs_sb_setnclean(fs, lfs_sb_getnseg(fs) - 1);
    483 	lfs_sb_setmaxfilesize(fs, maxfilesize(lfs_sb_getbshift(fs)));
    484 
    485 	if (minfreeseg == 0)
    486 		lfs_sb_setminfreeseg(fs, lfs_sb_getnseg(fs) / DFL_MIN_FREE_SEGS);
    487 	else
    488 		lfs_sb_setminfreeseg(fs, minfreeseg);
    489 	if (lfs_sb_getminfreeseg(fs) < MIN_FREE_SEGS)
    490 		lfs_sb_setminfreeseg(fs, MIN_FREE_SEGS);
    491 
    492 	if (resvseg == 0)
    493 		lfs_sb_setresvseg(fs, lfs_sb_getminfreeseg(fs) / 2 + 1);
    494 	else
    495 		lfs_sb_setresvseg(fs, resvseg);
    496 	if (lfs_sb_getresvseg(fs) < MIN_RESV_SEGS)
    497 		lfs_sb_setresvseg(fs, MIN_RESV_SEGS);
    498 
    499 	if(lfs_sb_getnseg(fs) < (4 * lfs_sb_getminfreeseg(fs))
    500 	   || lfs_sb_getnseg(fs) < LFS_MIN_SBINTERVAL + 1)
    501 	{
    502 		if(seg_size == 0 && ssize > (bsize<<1)) {
    503 			if(!warned_segtoobig) {
    504 				fprintf(stderr,"Segment size %d is too large; "
    505 					"trying smaller sizes.\n", ssize);
    506 				if (ssize == (bsize << 16)) {
    507 					fprintf(stderr, "(Did you perhaps "
    508 						"accidentally leave \"16\" "
    509 						"in the disklabel's sgs "
    510 						"field?)\n");
    511 				}
    512 			}
    513 			++warned_segtoobig;
    514 			ssize >>= 1;
    515 			goto tryagain;
    516 		}
    517 		fatal("Could not allocate enough segments with segment "
    518 			"size %d and block size %d;\nplease decrease the "
    519 			"segment size.\n", ssize, lfs_sb_getbsize(fs));
    520 	}
    521 	if(warned_segtoobig)
    522 		fprintf(stderr,"Using segment size %d, block size %d, frag size %d.\n", ssize, bsize, fsize);
    523 
    524 	/*
    525 	 * Now that we've determined what we're going to do, announce it
    526 	 * to the user.
    527 	 */
    528         printf("Creating a version %d LFS", fs->lfs_version);
    529         if (fs->lfs_version > 1)
    530                 printf(" with roll-forward ident 0x%x", lfs_sb_getident(fs));
    531         printf("\n");
    532         fssize = (double)lfs_sb_getnseg(fs);
    533         fssize *= (double)ssize;
    534         fssize /= 1048576.0;
    535         printf("%.1fMB in %d segments of size %d\n", fssize,
    536                lfs_sb_getnseg(fs), ssize);
    537 
    538 	/*
    539 	 * The number of free blocks is set from the number of segments
    540 	 * times the segment size - lfs_minfreesegs (that we never write
    541 	 * because we need to make sure the cleaner can run).  Then
    542 	 * we'll subtract off the room for the superblocks ifile entries
    543 	 * and segment usage table, and half a block per segment that can't
    544 	 * be written due to fragmentation.
    545 	 */
    546 	lfs_sb_setdsize(fs, (lfs_sb_getnseg(fs) - lfs_sb_getminfreeseg(fs)) *
    547 		lfs_segtod(fs, 1));
    548 	lfs_sb_setbfree(fs, lfs_sb_getdsize(fs));
    549 	lfs_sb_subbfree(fs, LFS_DBTOFSB(fs, ((lfs_sb_getnseg(fs) / 2) <<
    550 		lfs_sb_getblktodb(fs))));
    551 
    552 	lfs_sb_setsegtabsz(fs, SEGTABSIZE_SU(fs));
    553 	lfs_sb_setcleansz(fs, CLEANSIZE_SU(fs));
    554 	if (time(&stamp) == -1)
    555 		fatal("time: %s", strerror(errno));
    556 	lfs_sb_settstamp(fs, stamp);
    557 	if (version == 1)
    558 		lfs_sb_setotstamp(fs, stamp);
    559 
    560 	if ((sb_interval = lfs_sb_getnseg(fs) / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
    561 		sb_interval = LFS_MIN_SBINTERVAL;
    562 
    563 	/*
    564 	 * Figure out where the superblocks are going to live.
    565 	 *
    566 	 * Make segment 0 start at either zero, or LFS_LABELPAD, or
    567 	 * >= LFS_SBPAD+LFS_LABELPAD, in order to prevent segment 0
    568 	 * from having half a superblock in it.
    569 	 */
    570 	if (LFS_FSBTODB(fs, LFS_DBTOFSB(fs, start)) != start)
    571 		fatal("Segment 0 offset is not multiple of frag size\n");
    572 	if (start != 0 && dbtob(start) != LFS_LABELPAD &&
    573 	    dbtob(start) < LFS_SBPAD + LFS_LABELPAD) {
    574 		fatal("Using flags \"-O %" PRId64 "\" would result in the "
    575 		      "first segment containing only\npart of a superblock.  "
    576 		      "Please choose an offset of 0, %d, or %d or more,\n",
    577 		      start, btodb(LFS_LABELPAD),
    578 		      btodb(LFS_LABELPAD + LFS_SBPAD));
    579 	}
    580 	lfs_sb_setsboff(fs, 0, label_fsb);
    581 	if (version == 1)
    582 		lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
    583 	else
    584 		lfs_sb_sets0addr(fs, LFS_DBTOFSB(fs, start));
    585         lfs_sb_setdsize(fs, lfs_sb_getdsize(fs) - sb_fsb);
    586 	for (i = 1; i < LFS_MAXNUMSB; i++) {
    587 		sb_addr = ((i * sb_interval) * lfs_segtod(fs, 1))
    588 		    + lfs_sb_getsboff(fs, 0);
    589 		/* Segment 0 eats the label, except for version 1 */
    590 		if (fs->lfs_version > 1 && lfs_sb_gets0addr(fs) < label_fsb)
    591 			sb_addr -= label_fsb - start;
    592 		if (sb_addr + sizeof(struct dlfs)
    593 		    >= LFS_DBTOFSB(fs, dkw->dkw_size))
    594 			break;
    595 		lfs_sb_setsboff(fs, i, sb_addr);
    596 		lfs_sb_setdsize(fs, lfs_sb_getdsize(fs) - sb_fsb);
    597 	}
    598 
    599 	/* We need >= 2 superblocks */
    600 	if (lfs_sb_getsboff(fs, 1) == 0x0) {
    601 		fatal("Could not assign a disk address for the second "
    602 		      "superblock.\nPlease decrease the segment size.\n");
    603 	}
    604 
    605 	lfs_sb_setlastseg(fs, lfs_sntod(fs, lfs_sb_getnseg(fs) - 2));
    606 	lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_sb_getnseg(fs) - 1));
    607 	lfs_sb_setoffset(fs, lfs_sntod(fs, lfs_sb_getnseg(fs)));
    608 	lfs_sb_setnextseg(fs, lfs_sntod(fs, 0));
    609 
    610 	/*
    611 	 * Initialize the Ifile inode.  Do this before we do anything
    612 	 * with the Ifile or segment tables.
    613 	 */
    614 	dip = VTOI(fs->lfs_ivnode)->i_din.ffs1_din = (struct ulfs1_dinode *)
    615 		malloc(sizeof(*dip));
    616 	if (dip == NULL)
    617 		err(1, NULL);
    618 	memset(dip, 0, sizeof(*dip));
    619 	dip->di_mode  = LFS_IFREG | 0600;
    620 	dip->di_flags = SF_IMMUTABLE;
    621 	make_dinode(LFS_IFILE_INUM, dip,
    622 		lfs_blkstofrags(fs, lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs) + 1), fs);
    623 	dip->di_size = (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs) + 1) << lfs_sb_getbshift(fs);
    624 	for (i = 0; i < ULFS_NDADDR && i < (dip->di_size >> lfs_sb_getbshift(fs)); i++)
    625 		VTOI(fs->lfs_ivnode)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
    626 
    627 	/*
    628 	 * Set up in-superblock segment usage cache
    629 	 */
    630  	fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
    631 	if (fs->lfs_suflags == NULL)
    632 		err(1, NULL);
    633 	fs->lfs_suflags[0] = (u_int32_t *) malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
    634 	if (fs->lfs_suflags[0] == NULL)
    635 		err(1, NULL);
    636 	fs->lfs_suflags[1] = (u_int32_t *) malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
    637 	if (fs->lfs_suflags[1] == NULL)
    638 		err(1, NULL);
    639 
    640 	/*
    641 	 * Initialize the cleanerinfo block
    642 	 */
    643 	LFS_CLEANERINFO(cip, fs, bp);
    644 	cip->clean = lfs_sb_getnseg(fs);
    645 	cip->dirty = 0;
    646 	if (version > 1) {
    647 		cip->free_head = HIGHEST_USED_INO + 1;
    648 		cip->free_tail = lfs_sb_getifpb(fs) - 1;
    649 	}
    650 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    651 
    652 	/*
    653 	 * Run through segment table and initialize that
    654 	 */
    655 	for (i = j = 0; i < lfs_sb_getnseg(fs); i++) {
    656 		LFS_SEGENTRY(segp, fs, i, bp);
    657 
    658 		if (i == 0 &&
    659 		    lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD + LFS_SBPAD)) {
    660 			segp->su_flags = SEGUSE_SUPERBLOCK;
    661 			lfs_sb_subbfree(fs, sb_fsb);
    662 			++j;
    663 		}
    664 		if (i > 0) {
    665 			if ((i % sb_interval) == 0 && j < LFS_MAXNUMSB) {
    666 				segp->su_flags = SEGUSE_SUPERBLOCK;
    667 				lfs_sb_subbfree(fs, sb_fsb);
    668 				++j;
    669 			} else
    670 				segp->su_flags = 0;
    671 		}
    672 		segp->su_lastmod = 0;
    673 		segp->su_nbytes = 0;
    674 		segp->su_ninos = 0;
    675 		segp->su_nsums = 0;
    676 
    677 		LFS_WRITESEGENTRY(segp, fs, i, bp);
    678 	}
    679 
    680 	/* Initialize root directory */
    681 	vp = lfs_raw_vget(fs, ULFS_ROOTINO, devfd, 0x0);
    682 	dip = VTOI(vp)->i_din.ffs1_din;
    683 	make_dinode(ULFS_ROOTINO, dip, howmany(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs)), fs);
    684 	dip->di_mode = LFS_IFDIR | UMASK;
    685 	VTOI(vp)->i_lfs_osize = dip->di_size = LFS_DIRBLKSIZ;
    686 #ifdef MAKE_LF_DIR
    687 	VTOI(vp)->i_nlink = dip->di_nlink = 3;
    688 #else
    689 	VTOI(vp)->i_nlink = dip->di_nlink = 2;
    690 #endif
    691         VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
    692 		lfs_btofsb(fs, roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs)));
    693 	for (i = 0; i < ULFS_NDADDR && i < howmany(LFS_DIRBLKSIZ, lfs_sb_getbsize(fs)); i++)
    694 		VTOI(vp)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
    695 	if (LFS_DIRBLKSIZ < lfs_sb_getbsize(fs))
    696 		VTOI(vp)->i_lfs_fragsize[i - 1] =
    697 			roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs));
    698 	bread(vp, 0, lfs_sb_getfsize(fs), 0, &bp);
    699 	make_dir(bp->b_data, lfs_root_dir,
    700 		 sizeof(lfs_root_dir) / sizeof(struct lfs_direct));
    701 	VOP_BWRITE(bp);
    702 
    703 #ifdef MAKE_LF_DIR
    704 	/* Initialize lost+found directory */
    705 	vp = lfs_raw_vget(fs, LOSTFOUNDINO, devfd, 0x0);
    706 	dip = VTOI(vp)->i_din.ffs1_din;
    707 	make_dinode(LOSTFOUNDINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs);
    708 	dip->di_mode = IFDIR | UMASK;
    709 	VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ;
    710         VTOI(vp)->i_nlink = dip->di_nlink = 2;
    711         VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
    712 		lfs_btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize));
    713 	for (i = 0; i < ULFS_NDADDR && i < howmany(DIRBLKSIZ, lfs_sb_getbsize(fs)); i++)
    714 		VTOI(vp)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
    715 	if (DIRBLKSIZ < lfs_sb_getbsize(fs))
    716 		VTOI(vp)->i_lfs_fragsize[i - 1] =
    717 			roundup(DIRBLKSIZ,fs->lfs_fsize);
    718 	bread(vp, 0, fs->lfs_fsize, 0, &bp);
    719 	make_dir(bp->b_data, lfs_lf_dir,
    720 		 sizeof(lfs_lf_dir) / sizeof(struct lfs_direct));
    721 	VOP_BWRITE(bp);
    722 #endif /* MAKE_LF_DIR */
    723 
    724 	/* Set their IFILE entry version numbers to 1 */
    725 	LFS_IENTRY(ip, fs, 1, bp);
    726 	if (version == 1) {
    727 		ip_v1 = (IFILE_V1 *)ip;
    728 		for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
    729 			ip_v1->if_version = 1;
    730 			ip_v1->if_daddr = 0x0;
    731 			ip_v1->if_nextfree = 0;
    732 			++ip_v1;
    733 		}
    734 	} else {
    735 		for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
    736 			ip->if_version = 1;
    737 			ip->if_daddr = 0x0;
    738 			ip->if_nextfree = 0;
    739 			++ip;
    740 		}
    741 	}
    742 	/* Link remaining IFILE entries in free list */
    743 	if (version == 1) {
    744 		for (;
    745 		     i < lfs_sb_getifpb(fs); ++ip_v1) {
    746 			ip_v1->if_version = 1;
    747 			ip_v1->if_daddr = LFS_UNUSED_DADDR;
    748 			ip_v1->if_nextfree = ++i;
    749 		}
    750 		--ip_v1;
    751 		ip_v1->if_nextfree = LFS_UNUSED_INUM;
    752 	} else {
    753 		for (;
    754 		     i < lfs_sb_getifpb(fs); ++ip) {
    755 			ip->if_version = 1;
    756 			ip->if_daddr = LFS_UNUSED_DADDR;
    757 			ip->if_nextfree = ++i;
    758 		}
    759 		--ip;
    760 		ip->if_nextfree = LFS_UNUSED_INUM;
    761 	}
    762 	VOP_BWRITE(bp);
    763 
    764 	/* Write it all to disk. */
    765 	if (!Nflag)
    766 		lfs_segwrite(fs, SEGM_CKP);
    767 
    768 	/*
    769 	 * Now that we've written everything, look to see what's available
    770 	 * for writing.
    771 	 */
    772 	lfs_sb_setavail(fs, 0);
    773 	bb = ubb = dmeta = 0;
    774 	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
    775 		LFS_SEGENTRY(segp, fs, i, bp);
    776                 if (segp->su_flags & SEGUSE_DIRTY) {
    777                         bb += lfs_btofsb(fs, segp->su_nbytes +
    778                             segp->su_nsums * lfs_sb_getsumsize(fs));
    779                         ubb += lfs_btofsb(fs, segp->su_nbytes +
    780                             segp->su_nsums * lfs_sb_getsumsize(fs) +
    781                             segp->su_ninos * lfs_sb_getibsize(fs));
    782                         dmeta += lfs_btofsb(fs,
    783                             lfs_sb_getsumsize(fs) * segp->su_nsums);
    784                         dmeta += lfs_btofsb(fs,
    785                             lfs_sb_getibsize(fs) * segp->su_ninos);
    786 		} else {
    787                         lfs_sb_addavail(fs, lfs_segtod(fs, 1));
    788                         if (segp->su_flags & SEGUSE_SUPERBLOCK)
    789                                 lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_SBPAD));
    790                         if (i == 0 && fs->lfs_version > 1 &&
    791                             lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
    792                                 lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_LABELPAD) -
    793                                     lfs_sb_gets0addr(fs));
    794                 }
    795 		brelse(bp, 0);
    796         }
    797         /* Also may be available bytes in current seg */
    798         i = lfs_dtosn(fs, lfs_sb_getoffset(fs));
    799         lfs_sb_addavail(fs, lfs_sntod(fs, i + 1) - lfs_sb_getoffset(fs));
    800         /* But do not count minfreesegs */
    801         lfs_sb_subavail(fs, lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) - (lfs_sb_getminfreeseg(fs) / 2))));
    802 
    803         labelskew = 0;
    804         if (fs->lfs_version > 1 && lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
    805                 labelskew = lfs_btofsb(fs, LFS_LABELPAD);
    806         lfs_sb_setbfree(fs, lfs_sb_getdsize(fs) - labelskew - (ubb + bb) / 2);
    807 
    808 	/* Put that in the Ifile version too, and write it */
    809 	LFS_CLEANERINFO(cip, fs, bp);
    810 	cip->bfree = lfs_sb_getbfree(fs);
    811 	cip->avail = lfs_sb_getavail(fs);
    812 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    813 	if (!Nflag)
    814 		lfs_segwrite(fs, SEGM_CKP);
    815 
    816 	/*
    817 	 * Finally write out superblocks.
    818 	 */
    819 	printf("super-block backups (for fsck -b #) at:\n");
    820 	curw = 0;
    821 	for (i = 0; i < LFS_MAXNUMSB; i++) {
    822 		seg_addr = lfs_sb_getsboff(fs, i);
    823 		if (seg_addr == 0)
    824 			break;
    825 
    826 		if (i != 0)
    827 			curw += printf(", ");
    828 		ww = snprintf(tbuf, sizeof(tbuf), "%lld",
    829 		    (long long)LFS_FSBTODB(fs, seg_addr));
    830 		curw += ww;
    831 		if (curw >= 78) {
    832 			printf("\n%s", tbuf);
    833 			curw = ww;
    834 		} else
    835 			printf("%s", tbuf);
    836 		fflush(stdout);
    837 
    838 		/* Leave the time stamp on the alt sb, zero the rest */
    839 		if (i == 2) {
    840 			lfs_sb_settstamp(fs, 0);
    841 			lfs_sb_setcksum(fs, lfs_sb_cksum(&(fs->lfs_dlfs)));
    842 		}
    843 		if (!Nflag)
    844 			lfs_writesuper(fs, seg_addr);
    845 	}
    846 	printf(".\n");
    847 
    848 	return 0;
    849 }
    850 
    851 /*
    852  * Compatibility with fsck_lfs, since the "generic" LFS userland code uses it.
    853  */
    854 void
    855 pwarn(const char *fmt, ...)
    856 {
    857         va_list ap;
    858 
    859         va_start(ap, fmt);
    860         vfprintf(stderr, fmt, ap);
    861         va_end(ap);
    862 }
    863