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