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