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