Home | History | Annotate | Line # | Download | only in newfs_lfs
make_lfs.c revision 1.31
      1 /*	$NetBSD: make_lfs.c,v 1.31 2015/05/31 15:44:31 hannken 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.31 2015/05/31 15:44:31 hannken 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 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 	if (ULFS_NDADDR < nblocks) {
    241 		/* Count up how many indirect blocks we need, recursively */
    242 		/* XXX We are only called with nblocks > 1 for Ifile */
    243 		bb = nblocks - ULFS_NDADDR;
    244 		while (bb > 0) {
    245 			bb = howmany(bb, LFS_NINDIR(fs));
    246 			ifibc += bb;
    247 			--bb;
    248 		}
    249 		dip->di_blocks += lfs_blkstofrags(fs, ifibc);
    250 	}
    251 
    252 	/* Assign the block addresses for the ifile */
    253 	for (i = 0; i < MIN(nblocks,ULFS_NDADDR); i++) {
    254 		dip->di_db[i] = 0x0;
    255 	}
    256 	if (nblocks > ULFS_NDADDR) {
    257 		dip->di_ib[0] = 0x0;
    258 		bb = howmany(nblocks - ULFS_NDADDR, LFS_NINDIR(fs)) - 1;
    259 		factor = LFS_NINDIR(fs);
    260 		base = -ULFS_NDADDR - factor;
    261 		lvl = 1;
    262 		while (bb > 0) {
    263 			dip->di_ib[lvl] = 0x0;
    264 			bb = howmany(bb, LFS_NINDIR(fs));
    265 			--bb;
    266 			factor *= LFS_NINDIR(fs);
    267 			base -= factor;
    268 			++lvl;
    269 		}
    270 	}
    271 }
    272 
    273 /*
    274  * Construct a set of directory entries in "bufp".  We assume that all the
    275  * entries in protodir fir in the first DIRBLKSIZ.
    276  */
    277 static void
    278 make_dir(void *bufp, struct lfs_direct *protodir, int entries)
    279 {
    280 	char *cp;
    281 	int i, spcleft;
    282 
    283 	spcleft = LFS_DIRBLKSIZ;
    284 	for (cp = bufp, i = 0; i < entries - 1; i++) {
    285 		protodir[i].d_reclen = LFS_DIRSIZ(LFS_NEWDIRFMT, &protodir[i], 0);
    286 		memmove(cp, &protodir[i], protodir[i].d_reclen);
    287 		cp += protodir[i].d_reclen;
    288 		if ((spcleft -= protodir[i].d_reclen) < 0)
    289 			fatal("%s: %s", special, "directory too big");
    290 	}
    291 	protodir[i].d_reclen = spcleft;
    292 	memmove(cp, &protodir[i], LFS_DIRSIZ(LFS_NEWDIRFMT, &protodir[i], 0));
    293 }
    294 
    295 int
    296 make_lfs(int devfd, uint secsize, struct dkwedge_info *dkw, int minfree,
    297 	 int block_size, int frag_size, int seg_size, int minfreeseg,
    298 	 int resvseg, int version, daddr_t start, int ibsize, int interleave,
    299 	 u_int32_t roll_id)
    300 {
    301 	struct ulfs1_dinode *dip;	/* Pointer to a disk inode */
    302 	CLEANERINFO *cip;	/* Segment cleaner information table */
    303 	IFILE *ip;		/* Pointer to array of ifile structures */
    304 	IFILE_V1 *ip_v1 = NULL;
    305 	struct lfs *fs;		/* Superblock */
    306 	SEGUSE *segp;		/* Segment usage table */
    307 	daddr_t	sb_addr;	/* Address of superblocks */
    308 	daddr_t	seg_addr;	/* Address of current segment */
    309 	int bsize;		/* Block size */
    310 	int fsize;		/* Fragment size */
    311 	int db_per_blk;		/* Disk blocks per file block */
    312 	int i, j;
    313 	int sb_interval;	/* number of segs between super blocks */
    314 	int ssize;		/* Segment size */
    315 	double fssize;
    316 	int warned_segtoobig=0;
    317 	int label_fsb, sb_fsb;
    318 	int curw, ww;
    319 	char tbuf[BUFSIZ];
    320 	struct ubuf *bp;
    321 	struct uvnode *vp, *save_devvp;
    322 	int bb, ubb, dmeta, labelskew;
    323 	u_int64_t tsepb, tnseg;
    324 
    325 	/*
    326 	 * Initialize buffer cache.  Use a ballpark guess of the length of
    327 	 * the segment table for the number of hash chains.
    328 	 */
    329 	tnseg = dkw->dkw_size / ((seg_size ? seg_size : DFL_LFSSEG) / secsize);
    330 	tsepb = (block_size ? block_size : DFL_LFSBLOCK) / sizeof(SEGSUM);
    331 	if (tnseg == 0)
    332 		fatal("zero size partition");
    333 	bufinit(tnseg / tsepb);
    334 
    335 	/* Initialize LFS subsystem with blank superblock and ifile. */
    336 	fs = lfs_init(devfd, start, (ulfs_daddr_t)0, 1, 1/* XXX debug*/);
    337 	save_devvp = fs->lfs_devvp;
    338 	vp = fs->lfs_ivnode;
    339 	*fs = lfs_default;
    340 	fs->lfs_ivnode = vp;
    341 	fs->lfs_devvp = save_devvp;
    342 
    343 
    344 	/* Set version first of all since it is used to compute other fields */
    345 	fs->lfs_version = version;
    346 
    347 	/* If partition is not an LFS partition, warn that that is the case */
    348 	if (strcmp(dkw->dkw_ptype, DKW_PTYPE_LFS) != 0) {
    349 		fatal("partition label indicated fs type \"%s\", "
    350 		    "expected \"%s\"", dkw->dkw_ptype, DKW_PTYPE_LFS);
    351 	}
    352 
    353 	if (!(bsize = block_size)) {
    354 		bsize = DFL_LFSBLOCK;
    355 		if (dkw->dkw_size <= SMALL_FSSIZE)
    356 			bsize = SMALL_LFSBLOCK;
    357 	}
    358 	if (!(fsize = frag_size)) {
    359 		fsize = DFL_LFSFRAG;
    360 		if (dkw->dkw_size <= SMALL_FSSIZE)
    361 			fsize = SMALL_LFSFRAG;
    362 	}
    363 	if (!(ssize = seg_size)) {
    364 		ssize = DFL_LFSSEG;
    365 		if (dkw->dkw_size <= SMALL_FSSIZE)
    366 			ssize = SMALL_LFSSEG;
    367 	}
    368 	if (version > 1) {
    369 		if (ibsize == 0)
    370 			ibsize = fsize;
    371 		if (ibsize <= 0 || ibsize % fsize)
    372 			fatal("illegal inode block size: %d\n", ibsize);
    373 	} else if (ibsize && ibsize != bsize)
    374 		fatal("cannot specify inode block size when version == 1\n");
    375 
    376 	/* Sanity check: fsize<=bsize<ssize */
    377 	if (fsize > bsize) {
    378 		/* Only complain if fsize was explicitly set */
    379 		if(frag_size)
    380 			fatal("fragment size must be <= block size %d", bsize);
    381 		fsize = bsize;
    382 	}
    383 	if (bsize >= ssize) {
    384 		/* Only fatal if ssize was explicitly set */
    385 		if(seg_size)
    386 			fatal("block size must be < segment size");
    387 		warnx("%s: disklabel segment size (%d) too small, using default (%d)",
    388 		      progname, ssize, DFL_LFSSEG);
    389 		ssize = DFL_LFSSEG;
    390 	}
    391 	if (start < 0 || start >= dkw->dkw_size)
    392 		fatal("filesystem offset %ld out of range", (long)start);
    393 	if (version == 1) {
    394 		if (start)
    395 			warnx("filesystem offset ignored for version 1 filesystem");
    396 		start = LFS_LABELPAD / secsize;
    397 	}
    398 
    399     tryagain:
    400 	/* Modify parts of superblock overridden by command line arguments */
    401 	if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) {
    402 		fs->lfs_bshift = lfs_log2(bsize);
    403 		if (1 << fs->lfs_bshift != bsize)
    404 			fatal("%d: block size not a power of 2", bsize);
    405 		fs->lfs_bsize = bsize;
    406 		fs->lfs_fsize = fsize;
    407 		fs->lfs_bmask = bsize - 1;
    408 		fs->lfs_ffmask = fsize - 1;
    409 		fs->lfs_ffshift = lfs_log2(fsize);
    410 		if (1 << fs->lfs_ffshift != fsize)
    411 			fatal("%d: frag size not a power of 2", fsize);
    412 		fs->lfs_frag = lfs_numfrags(fs, bsize);
    413 		fs->lfs_fbmask = fs->lfs_frag - 1;
    414 		fs->lfs_fbshift = lfs_log2(fs->lfs_frag);
    415 		fs->lfs_ifpb = bsize / sizeof(IFILE);
    416 		/* XXX ondisk32 */
    417 		fs->lfs_nindir = bsize / sizeof(int32_t);
    418 	}
    419 
    420 	if (fs->lfs_version == 1) {
    421 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
    422 		fs->lfs_segshift = lfs_log2(ssize);
    423 		if (1 << fs->lfs_segshift != ssize)
    424 			fatal("%d: segment size not power of 2", ssize);
    425 		fs->lfs_segmask = ssize - 1;
    426 		fs->lfs_ifpb = fs->lfs_bsize / sizeof(IFILE_V1);
    427 		fs->lfs_ibsize = fs->lfs_bsize;
    428 		fs->lfs_sepb = bsize / sizeof(SEGUSE_V1);
    429 		fs->lfs_ssize = ssize >> fs->lfs_bshift;
    430 	} else {
    431 		if (ssize % fsize) {
    432 			fprintf(stderr,
    433 				"Segment size %d is not a multiple of frag size; ",
    434 				 ssize);
    435 			ssize = roundup(ssize, fsize);
    436 			fprintf(stderr, "trying size %d.\n", ssize);
    437 			goto tryagain;
    438 		}
    439 		fs->lfs_sumsize = fsize;
    440 		fs->lfs_segshift = 0;
    441 		fs->lfs_segmask = 0;
    442 		fs->lfs_sepb = bsize / sizeof(SEGUSE);
    443 		fs->lfs_ssize = ssize;
    444 		fs->lfs_ibsize = ibsize;
    445 	}
    446 	fs->lfs_inopb = fs->lfs_ibsize / sizeof(struct ulfs1_dinode);
    447 	fs->lfs_minfree = minfree;
    448 
    449 	if (version > 1) {
    450 		fs->lfs_inopf = secsize/LFS_DINODE1_SIZE;
    451 		fs->lfs_interleave = interleave;
    452 		if (roll_id == 0)
    453 			roll_id = arc4random();
    454 		fs->lfs_ident = roll_id;
    455 	}
    456 
    457 	/*
    458 	 * Fill in parts of superblock that can be computed from file system
    459 	 * size, disk geometry and current time.
    460 	 */
    461 	db_per_blk = bsize/secsize;
    462 	fs->lfs_blktodb = lfs_log2(db_per_blk);
    463 	fs->lfs_fsbtodb = lfs_log2(fsize / secsize);
    464 	if (version == 1) {
    465 		fs->lfs_sushift = lfs_log2(fs->lfs_sepb);
    466 		fs->lfs_fsbtodb = 0;
    467 		fs->lfs_size = dkw->dkw_size >> fs->lfs_blktodb;
    468 	}
    469 	label_fsb = lfs_btofsb(fs, roundup(LFS_LABELPAD, fsize));
    470 	sb_fsb = lfs_btofsb(fs, roundup(LFS_SBPAD, fsize));
    471 	fs->lfs_fsbpseg = LFS_DBTOFSB(fs, ssize / secsize);
    472 	fs->lfs_size = dkw->dkw_size >> fs->lfs_fsbtodb;
    473 	fs->lfs_dsize = LFS_DBTOFSB(fs, dkw->dkw_size) -
    474 		MAX(label_fsb, LFS_DBTOFSB(fs, start));
    475 	fs->lfs_nseg = fs->lfs_dsize / lfs_segtod(fs, 1);
    476 
    477 	fs->lfs_nclean = fs->lfs_nseg - 1;
    478 	fs->lfs_maxfilesize = maxfilesize(fs->lfs_bshift);
    479 
    480 	if (minfreeseg == 0)
    481 		fs->lfs_minfreeseg = fs->lfs_nseg / DFL_MIN_FREE_SEGS;
    482 	else
    483 		fs->lfs_minfreeseg = minfreeseg;
    484 	if (fs->lfs_minfreeseg < MIN_FREE_SEGS)
    485 		fs->lfs_minfreeseg = MIN_FREE_SEGS;
    486 
    487 	if (resvseg == 0)
    488 		fs->lfs_resvseg = fs->lfs_minfreeseg / 2 + 1;
    489 	else
    490 		fs->lfs_resvseg = resvseg;
    491 	if (fs->lfs_resvseg < MIN_RESV_SEGS)
    492 		fs->lfs_resvseg = MIN_RESV_SEGS;
    493 
    494 	if(fs->lfs_nseg < (4 * fs->lfs_minfreeseg)
    495 	   || fs->lfs_nseg < LFS_MIN_SBINTERVAL + 1)
    496 	{
    497 		if(seg_size == 0 && ssize > (bsize<<1)) {
    498 			if(!warned_segtoobig) {
    499 				fprintf(stderr,"Segment size %d is too large; "
    500 					"trying smaller sizes.\n", ssize);
    501 				if (ssize == (bsize << 16)) {
    502 					fprintf(stderr, "(Did you perhaps "
    503 						"accidentally leave \"16\" "
    504 						"in the disklabel's sgs "
    505 						"field?)\n");
    506 				}
    507 			}
    508 			++warned_segtoobig;
    509 			ssize >>= 1;
    510 			goto tryagain;
    511 		}
    512 		fatal("Could not allocate enough segments with segment "
    513 			"size %d and block size %d;\nplease decrease the "
    514 			"segment size.\n", ssize, fs->lfs_bsize);
    515 	}
    516 	if(warned_segtoobig)
    517 		fprintf(stderr,"Using segment size %d, block size %d, frag size %d.\n", ssize, bsize, fsize);
    518 
    519 	/*
    520 	 * Now that we've determined what we're going to do, announce it
    521 	 * to the user.
    522 	 */
    523         printf("Creating a version %d LFS", fs->lfs_version);
    524         if (fs->lfs_version > 1)
    525                 printf(" with roll-forward ident 0x%x", fs->lfs_ident);
    526         printf("\n");
    527         fssize = (double)fs->lfs_nseg;
    528         fssize *= (double)ssize;
    529         fssize /= 1048576.0;
    530         printf("%.1fMB in %d segments of size %d\n", fssize,
    531                fs->lfs_nseg, ssize);
    532 
    533 	/*
    534 	 * The number of free blocks is set from the number of segments
    535 	 * times the segment size - lfs_minfreesegs (that we never write
    536 	 * because we need to make sure the cleaner can run).  Then
    537 	 * we'll subtract off the room for the superblocks ifile entries
    538 	 * and segment usage table, and half a block per segment that can't
    539 	 * be written due to fragmentation.
    540 	 */
    541 	fs->lfs_dsize = (fs->lfs_nseg - fs->lfs_minfreeseg) *
    542 		lfs_segtod(fs, 1);
    543 	fs->lfs_bfree = fs->lfs_dsize;
    544 	fs->lfs_bfree -= LFS_DBTOFSB(fs, ((fs->lfs_nseg / 2) <<
    545 		fs->lfs_blktodb));
    546 
    547 	fs->lfs_segtabsz = SEGTABSIZE_SU(fs);
    548 	fs->lfs_cleansz = CLEANSIZE_SU(fs);
    549 	if ((fs->lfs_tstamp = time(NULL)) == -1)
    550 		fatal("time: %s", strerror(errno));
    551 	if (version == 1)
    552 		fs->lfs_otstamp = fs->lfs_tstamp;
    553 
    554 	if ((sb_interval = fs->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
    555 		sb_interval = LFS_MIN_SBINTERVAL;
    556 
    557 	/*
    558 	 * Figure out where the superblocks are going to live.
    559 	 *
    560 	 * Make segment 0 start at either zero, or LFS_LABELPAD, or
    561 	 * >= LFS_SBPAD+LFS_LABELPAD, in order to prevent segment 0
    562 	 * from having half a superblock in it.
    563 	 */
    564 	if (LFS_FSBTODB(fs, LFS_DBTOFSB(fs, start)) != start)
    565 		fatal("Segment 0 offset is not multiple of frag size\n");
    566 	if (start != 0 && dbtob(start) != LFS_LABELPAD &&
    567 	    dbtob(start) < LFS_SBPAD + LFS_LABELPAD) {
    568 		fatal("Using flags \"-O %" PRId64 "\" would result in the "
    569 		      "first segment containing only\npart of a superblock.  "
    570 		      "Please choose an offset of 0, %d, or %d or more,\n",
    571 		      start, btodb(LFS_LABELPAD),
    572 		      btodb(LFS_LABELPAD + LFS_SBPAD));
    573 	}
    574 	fs->lfs_sboffs[0] = label_fsb;
    575 	if (version == 1)
    576 		fs->lfs_s0addr = fs->lfs_sboffs[0];
    577 	else
    578 		fs->lfs_s0addr = LFS_DBTOFSB(fs, start);
    579         fs->lfs_dsize -= sb_fsb;
    580 	for (i = 1; i < LFS_MAXNUMSB; i++) {
    581 		sb_addr = ((i * sb_interval) * lfs_segtod(fs, 1))
    582 		    + fs->lfs_sboffs[0];
    583 		/* Segment 0 eats the label, except for version 1 */
    584 		if (fs->lfs_version > 1 && fs->lfs_s0addr < label_fsb)
    585 			sb_addr -= label_fsb - start;
    586 		if (sb_addr + sizeof(struct dlfs)
    587 		    >= LFS_DBTOFSB(fs, dkw->dkw_size))
    588 			break;
    589 		fs->lfs_sboffs[i] = sb_addr;
    590 		fs->lfs_dsize -= sb_fsb;
    591 	}
    592 
    593 	/* We need >= 2 superblocks */
    594 	if(fs->lfs_sboffs[1] == 0x0) {
    595 		fatal("Could not assign a disk address for the second "
    596 		      "superblock.\nPlease decrease the segment size.\n");
    597 	}
    598 
    599 	fs->lfs_lastseg = lfs_sntod(fs, fs->lfs_nseg - 2);
    600 	fs->lfs_curseg = lfs_sntod(fs, fs->lfs_nseg - 1);
    601 	fs->lfs_offset = lfs_sntod(fs, fs->lfs_nseg);
    602 	fs->lfs_nextseg = lfs_sntod(fs, 0);
    603 
    604 	/*
    605 	 * Initialize the Ifile inode.  Do this before we do anything
    606 	 * with the Ifile or segment tables.
    607 	 */
    608 	dip = VTOI(fs->lfs_ivnode)->i_din.ffs1_din = (struct ulfs1_dinode *)
    609 		malloc(sizeof(*dip));
    610 	if (dip == NULL)
    611 		err(1, NULL);
    612 	memset(dip, 0, sizeof(*dip));
    613 	dip->di_mode  = LFS_IFREG | 0600;
    614 	dip->di_flags = SF_IMMUTABLE;
    615 	make_dinode(LFS_IFILE_INUM, dip,
    616 		lfs_blkstofrags(fs, fs->lfs_cleansz + fs->lfs_segtabsz + 1), fs);
    617 	dip->di_size = (fs->lfs_cleansz + fs->lfs_segtabsz + 1) << fs->lfs_bshift;
    618 	for (i = 0; i < ULFS_NDADDR && i < (dip->di_size >> fs->lfs_bshift); i++)
    619 		VTOI(fs->lfs_ivnode)->i_lfs_fragsize[i] = fs->lfs_bsize;
    620 
    621 	/*
    622 	 * Set up in-superblock segment usage cache
    623 	 */
    624  	fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
    625 	if (fs->lfs_suflags == NULL)
    626 		err(1, NULL);
    627 	fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
    628 	if (fs->lfs_suflags[0] == NULL)
    629 		err(1, NULL);
    630 	fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
    631 	if (fs->lfs_suflags[1] == NULL)
    632 		err(1, NULL);
    633 
    634 	/*
    635 	 * Initialize the cleanerinfo block
    636 	 */
    637 	LFS_CLEANERINFO(cip, fs, bp);
    638 	cip->clean = fs->lfs_nseg;
    639 	cip->dirty = 0;
    640 	if (version > 1) {
    641 		cip->free_head = HIGHEST_USED_INO + 1;
    642 		cip->free_tail = fs->lfs_ifpb - 1;
    643 	}
    644 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    645 
    646 	/*
    647 	 * Run through segment table and initialize that
    648 	 */
    649 	for (i = j = 0; i < fs->lfs_nseg; i++) {
    650 		LFS_SEGENTRY(segp, fs, i, bp);
    651 
    652 		if (i == 0 &&
    653 		    fs->lfs_s0addr < lfs_btofsb(fs, LFS_LABELPAD + LFS_SBPAD)) {
    654 			segp->su_flags = SEGUSE_SUPERBLOCK;
    655 			fs->lfs_bfree -= sb_fsb;
    656 			++j;
    657 		}
    658 		if (i > 0) {
    659 			if ((i % sb_interval) == 0 && j < LFS_MAXNUMSB) {
    660 				segp->su_flags = SEGUSE_SUPERBLOCK;
    661 				fs->lfs_bfree -= sb_fsb;
    662 				++j;
    663 			} else
    664 				segp->su_flags = 0;
    665 		}
    666 		segp->su_lastmod = 0;
    667 		segp->su_nbytes = 0;
    668 		segp->su_ninos = 0;
    669 		segp->su_nsums = 0;
    670 
    671 		LFS_WRITESEGENTRY(segp, fs, i, bp);
    672 	}
    673 
    674 	/* Initialize root directory */
    675 	vp = lfs_raw_vget(fs, ULFS_ROOTINO, devfd, 0x0);
    676 	dip = VTOI(vp)->i_din.ffs1_din;
    677 	make_dinode(ULFS_ROOTINO, dip, howmany(LFS_DIRBLKSIZ,fs->lfs_fsize), fs);
    678 	dip->di_mode = LFS_IFDIR | UMASK;
    679 	VTOI(vp)->i_lfs_osize = dip->di_size = LFS_DIRBLKSIZ;
    680 #ifdef MAKE_LF_DIR
    681 	VTOI(vp)->i_nlink = dip->di_nlink = 3;
    682 #else
    683 	VTOI(vp)->i_nlink = dip->di_nlink = 2;
    684 #endif
    685         VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
    686 		lfs_btofsb(fs, roundup(LFS_DIRBLKSIZ,fs->lfs_fsize));
    687 	for (i = 0; i < ULFS_NDADDR && i < howmany(LFS_DIRBLKSIZ, fs->lfs_bsize); i++)
    688 		VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize;
    689 	if (LFS_DIRBLKSIZ < fs->lfs_bsize)
    690 		VTOI(vp)->i_lfs_fragsize[i - 1] =
    691 			roundup(LFS_DIRBLKSIZ,fs->lfs_fsize);
    692 	bread(vp, 0, fs->lfs_fsize, 0, &bp);
    693 	make_dir(bp->b_data, lfs_root_dir,
    694 		 sizeof(lfs_root_dir) / sizeof(struct lfs_direct));
    695 	VOP_BWRITE(bp);
    696 
    697 #ifdef MAKE_LF_DIR
    698 	/* Initialize lost+found directory */
    699 	vp = lfs_raw_vget(fs, LOSTFOUNDINO, devfd, 0x0);
    700 	dip = VTOI(vp)->i_din.ffs1_din;
    701 	make_dinode(LOSTFOUNDINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs);
    702 	dip->di_mode = IFDIR | UMASK;
    703 	VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ;
    704         VTOI(vp)->i_nlink = dip->di_nlink = 2;
    705         VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
    706 		lfs_btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize));
    707 	for (i = 0; i < ULFS_NDADDR && i < howmany(DIRBLKSIZ, fs->lfs_bsize); i++)
    708 		VTOI(vp)->i_lfs_fragsize[i] = fs->lfs_bsize;
    709 	if (DIRBLKSIZ < fs->lfs_bsize)
    710 		VTOI(vp)->i_lfs_fragsize[i - 1] =
    711 			roundup(DIRBLKSIZ,fs->lfs_fsize);
    712 	bread(vp, 0, fs->lfs_fsize, 0, &bp);
    713 	make_dir(bp->b_data, lfs_lf_dir,
    714 		 sizeof(lfs_lf_dir) / sizeof(struct lfs_direct));
    715 	VOP_BWRITE(bp);
    716 #endif /* MAKE_LF_DIR */
    717 
    718 	/* Set their IFILE entry version numbers to 1 */
    719 	LFS_IENTRY(ip, fs, 1, bp);
    720 	if (version == 1) {
    721 		ip_v1 = (IFILE_V1 *)ip;
    722 		for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
    723 			ip_v1->if_version = 1;
    724 			ip_v1->if_daddr = 0x0;
    725 			ip_v1->if_nextfree = 0;
    726 			++ip_v1;
    727 		}
    728 	} else {
    729 		for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
    730 			ip->if_version = 1;
    731 			ip->if_daddr = 0x0;
    732 			ip->if_nextfree = 0;
    733 			++ip;
    734 		}
    735 	}
    736 	/* Link remaining IFILE entries in free list */
    737 	if (version == 1) {
    738 		for (;
    739 		     i < fs->lfs_ifpb; ++ip_v1) {
    740 			ip_v1->if_version = 1;
    741 			ip_v1->if_daddr = LFS_UNUSED_DADDR;
    742 			ip_v1->if_nextfree = ++i;
    743 		}
    744 		--ip_v1;
    745 		ip_v1->if_nextfree = LFS_UNUSED_INUM;
    746 	} else {
    747 		for (;
    748 		     i < fs->lfs_ifpb; ++ip) {
    749 			ip->if_version = 1;
    750 			ip->if_daddr = LFS_UNUSED_DADDR;
    751 			ip->if_nextfree = ++i;
    752 		}
    753 		--ip;
    754 		ip->if_nextfree = LFS_UNUSED_INUM;
    755 	}
    756 	VOP_BWRITE(bp);
    757 
    758 	/* Write it all to disk. */
    759 	if (!Nflag)
    760 		lfs_segwrite(fs, SEGM_CKP);
    761 
    762 	/*
    763 	 * Now that we've written everything, look to see what's available
    764 	 * for writing.
    765 	 */
    766 	fs->lfs_avail = 0;
    767 	bb = ubb = dmeta = 0;
    768 	for (i = 0; i < fs->lfs_nseg; i++) {
    769 		LFS_SEGENTRY(segp, fs, i, bp);
    770                 if (segp->su_flags & SEGUSE_DIRTY) {
    771                         bb += lfs_btofsb(fs, segp->su_nbytes +
    772                             segp->su_nsums * fs->lfs_sumsize);
    773                         ubb += lfs_btofsb(fs, segp->su_nbytes +
    774                             segp->su_nsums * fs->lfs_sumsize +
    775                             segp->su_ninos * fs->lfs_ibsize);
    776                         dmeta += lfs_btofsb(fs,
    777                             fs->lfs_sumsize * segp->su_nsums);
    778                         dmeta += lfs_btofsb(fs,
    779                             fs->lfs_ibsize * segp->su_ninos);
    780 		} else {
    781                         fs->lfs_avail += lfs_segtod(fs, 1);
    782                         if (segp->su_flags & SEGUSE_SUPERBLOCK)
    783                                 fs->lfs_avail -= lfs_btofsb(fs, LFS_SBPAD);
    784                         if (i == 0 && fs->lfs_version > 1 &&
    785                             fs->lfs_s0addr < lfs_btofsb(fs, LFS_LABELPAD))
    786                                 fs->lfs_avail -= lfs_btofsb(fs, LFS_LABELPAD) -
    787                                     fs->lfs_s0addr;
    788                 }
    789 		brelse(bp, 0);
    790         }
    791         /* Also may be available bytes in current seg */
    792         i = lfs_dtosn(fs, fs->lfs_offset);
    793         fs->lfs_avail += lfs_sntod(fs, i + 1) - fs->lfs_offset;
    794         /* But do not count minfreesegs */
    795         fs->lfs_avail -= lfs_segtod(fs, (fs->lfs_minfreeseg - (fs->lfs_minfreeseg / 2)));
    796 
    797         labelskew = 0;
    798         if (fs->lfs_version > 1 && fs->lfs_s0addr < lfs_btofsb(fs, LFS_LABELPAD))
    799                 labelskew = lfs_btofsb(fs, LFS_LABELPAD);
    800         fs->lfs_bfree = fs->lfs_dsize - labelskew - (ubb + bb) / 2;
    801 
    802 	/* Put that in the Ifile version too, and write it */
    803 	LFS_CLEANERINFO(cip, fs, bp);
    804 	cip->bfree = fs->lfs_bfree;
    805 	cip->avail = fs->lfs_avail;
    806 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
    807 	if (!Nflag)
    808 		lfs_segwrite(fs, SEGM_CKP);
    809 
    810 	/*
    811 	 * Finally write out superblocks.
    812 	 */
    813 	printf("super-block backups (for fsck -b #) at:\n");
    814 	curw = 0;
    815 	for (i = 0; i < LFS_MAXNUMSB; i++) {
    816 		seg_addr = fs->lfs_sboffs[i];
    817 		if (seg_addr == 0)
    818 			break;
    819 
    820 		if (i != 0)
    821 			curw += printf(", ");
    822 		ww = snprintf(tbuf, sizeof(tbuf), "%lld",
    823 		    (long long)LFS_FSBTODB(fs, seg_addr));
    824 		curw += ww;
    825 		if (curw >= 78) {
    826 			printf("\n%s", tbuf);
    827 			curw = ww;
    828 		} else
    829 			printf("%s", tbuf);
    830 		fflush(stdout);
    831 
    832 		/* Leave the time stamp on the alt sb, zero the rest */
    833 		if (i == 2) {
    834 			fs->lfs_tstamp = 0;
    835 			fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
    836 		}
    837 		if (!Nflag)
    838 			lfs_writesuper(fs, seg_addr);
    839 	}
    840 	printf(".\n");
    841 
    842 	return 0;
    843 }
    844 
    845 /*
    846  * Compatibility with fsck_lfs, since the "generic" LFS userland code uses it.
    847  */
    848 void
    849 pwarn(const char *fmt, ...)
    850 {
    851         va_list ap;
    852 
    853         va_start(ap, fmt);
    854         vfprintf(stderr, fmt, ap);
    855         va_end(ap);
    856 }
    857