xref: /csrg-svn/sbin/newlfs/lfs.c (revision 52088)
151149Sbostic /*-
251149Sbostic  * Copyright (c) 1991 The Regents of the University of California.
351149Sbostic  * All rights reserved.
451149Sbostic  *
551149Sbostic  * %sccs.include.redist.c%
651149Sbostic  */
751149Sbostic 
851149Sbostic #ifndef lint
9*52088Sbostic static char sccsid[] = "@(#)lfs.c	5.5 (Berkeley) 12/31/91";
1051149Sbostic #endif /* not lint */
1151149Sbostic 
1251149Sbostic #include <sys/param.h>
1351864Sbostic #include <sys/uio.h>
1451149Sbostic #include <sys/disklabel.h>
1551864Sbostic #include <sys/time.h>
1651864Sbostic #include <sys/resource.h>
1751864Sbostic #include <sys/proc.h>
1851864Sbostic #include <sys/vnode.h>
1951866Sbostic 
2051866Sbostic #include <ufs/ufs/dir.h>
2151866Sbostic #include <ufs/ufs/quota.h>
2251866Sbostic #include <ufs/ufs/dinode.h>
2351866Sbostic #include <ufs/ufs/ufsmount.h>
2451866Sbostic #include <ufs/lfs/lfs.h>
2551866Sbostic 
2651149Sbostic #include <unistd.h>
2751149Sbostic #include <errno.h>
2851149Sbostic #include <stdlib.h>
2951149Sbostic #include <string.h>
3051149Sbostic #include "config.h"
3151149Sbostic #include "extern.h"
3251149Sbostic 
3351866Sbostic static struct lfs lfs_default =  {
3451149Sbostic 	/* lfs_magic */		LFS_MAGIC,
3551149Sbostic 	/* lfs_version */	LFS_VERSION,
3651149Sbostic 	/* lfs_size */		0,
3751866Sbostic 	/* lfs_ssize */		DFL_LFSSEG/DFL_LFSBLOCK,
3851149Sbostic 	/* lfs_dsize */		0,
3951149Sbostic 	/* lfs_bsize */		DFL_LFSBLOCK,
4051864Sbostic 	/* lfs_fsize */		DFL_LFSBLOCK,
4151864Sbostic 	/* lfs_frag */		1,
4251149Sbostic 	/* lfs_free */		LFS_FIRST_INUM,
4351864Sbostic 	/* lfs_bfree */		0,
4451864Sbostic 	/* lfs_nfiles */	0,
4551149Sbostic 	/* lfs_idaddr */	0,
4651149Sbostic 	/* lfs_ifile */		LFS_IFILE_INUM,
4751149Sbostic 	/* lfs_lastseg */	0,
4851864Sbostic 	/* lfs_nextseg */	0,
4951866Sbostic 	/* lfs_curseg */	0,
5051866Sbostic 	/* lfs_offset */	0,
5151149Sbostic 	/* lfs_tstamp */	0,
5251149Sbostic 	/* lfs_minfree */	MINFREE,
5351866Sbostic 	/* lfs_dbpseg */	DFL_LFSSEG/DEV_BSIZE,
5451149Sbostic 	/* lfs_inopb */		DFL_LFSBLOCK/sizeof(struct dinode),
5551149Sbostic 	/* lfs_ifpb */		DFL_LFSBLOCK/sizeof(IFILE),
5651866Sbostic 	/* lfs_sepb */		DFL_LFSBLOCK/sizeof(SEGUSE),
5751149Sbostic 	/* lfs_nindir */	DFL_LFSBLOCK/sizeof(daddr_t),
5851149Sbostic 	/* lfs_nseg */		0,
5951149Sbostic 	/* lfs_nspf */		0,
6051866Sbostic 	/* lfs_cleansz */	0,
6151149Sbostic 	/* lfs_segtabsz */	0,
6251149Sbostic 	/* lfs_segmask */	DFL_LFSSEG_MASK,
6351149Sbostic 	/* lfs_segshift */	DFL_LFSSEG_SHIFT,
6451149Sbostic 	/* lfs_bmask */		DFL_LFSBLOCK_MASK,
6551149Sbostic 	/* lfs_bshift */	DFL_LFSBLOCK_SHIFT,
6651149Sbostic 	/* lfs_ffmask */	0,
6751149Sbostic 	/* lfs_ffshift */	0,
6851149Sbostic 	/* lfs_fbmask */	0,
6951149Sbostic 	/* lfs_fbshift */	0,
7051149Sbostic 	/* lfs_fsbtodb */	0,
7151149Sbostic 	/* lfs_sboffs */	{ 0 },
7251864Sbostic 	/* lfs_ivnode */	NULL,
7351864Sbostic 	/* lfs_segtab */	NULL,
7451864Sbostic 	/* lfs_seglist */	NULL,
7551864Sbostic 	/* lfs_iocount */	0,
7651149Sbostic 	/* lfs_fmod */		0,
7751149Sbostic 	/* lfs_clean */		0,
7851149Sbostic 	/* lfs_ronly */		0,
7951149Sbostic 	/* lfs_flags */		0,
8051149Sbostic 	/* lfs_fsmnt */		{ 0 },
8151864Sbostic 	/* lfs_pad */		{ 0 },
8251149Sbostic 	/* lfs_cksum */		0
8351149Sbostic };
8451149Sbostic 
8551864Sbostic 
8651864Sbostic struct direct lfs_root_dir[] = {
8751864Sbostic 	{ ROOTINO, sizeof(struct direct), 1, "."},
8851864Sbostic 	{ ROOTINO, sizeof(struct direct), 2, ".."},
8951866Sbostic 	{ LFS_IFILE_INUM, sizeof(struct direct), 5, "ifile"},
9051864Sbostic 	{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found"},
9151864Sbostic };
9251864Sbostic 
9351864Sbostic struct direct lfs_lf_dir[] = {
9451864Sbostic         { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
9551864Sbostic         { ROOTINO, sizeof(struct direct), 2, ".." },
9651864Sbostic };
9751864Sbostic 
9851864Sbostic static daddr_t make_dinode
9951866Sbostic 	__P((ino_t, struct dinode *, int, daddr_t, struct lfs *));
10051864Sbostic static void make_dir __P(( void *, struct direct *, int));
10151149Sbostic static void put __P((int, off_t, void *, size_t));
10251149Sbostic 
10351149Sbostic int
10451149Sbostic make_lfs(fd, lp, partp, minfree, block_size, seg_size)
10551149Sbostic 	int fd;
10651149Sbostic 	struct disklabel *lp;
10751149Sbostic 	struct partition *partp;
10851149Sbostic 	int minfree;
10951149Sbostic 	int block_size;
11051149Sbostic 	int seg_size;
11151149Sbostic {
11251149Sbostic 	struct dinode *dip;	/* Pointer to a disk inode */
11351864Sbostic 	struct dinode *dpagep;	/* Pointer to page of disk inodes */
11451866Sbostic 	CLEANERINFO *cleaninfo;	/* Segment cleaner information table */
11551149Sbostic 	FINFO file_info;	/* File info structure in summary blocks */
11651149Sbostic 	IFILE *ifile;		/* Pointer to array of ifile structures */
11751149Sbostic 	IFILE *ip;		/* Pointer to array of ifile structures */
11851866Sbostic 	struct lfs *lfsp;	/* Superblock */
11951864Sbostic 	SEGUSE *segp;		/* Segment usage table */
12051864Sbostic 	SEGUSE *segtable;	/* Segment usage table */
12151149Sbostic 	SEGSUM summary;		/* Segment summary structure */
12251866Sbostic 	SEGSUM *sp;		/* Segment summary pointer */
12351149Sbostic 	daddr_t	last_sb_addr;	/* Address of superblocks */
12451864Sbostic 	daddr_t last_addr;	/* Previous segment address */
12551149Sbostic 	daddr_t	sb_addr;	/* Address of superblocks */
12651864Sbostic 	daddr_t	seg_addr;	/* Address of current segment */
12751866Sbostic 	void *ipagep;		/* Pointer to the page we use to write stuff */
12851149Sbostic 	void *sump;		/* Used to copy stuff into segment buffer */
12951149Sbostic 	u_long *block_array;	/* Array of logical block nos to put in sum */
13051866Sbostic 	u_long blocks_used;	/* Number of blocks in first segment */
13151866Sbostic 	u_long *dp;		/* Used to computed checksum on data */
13251866Sbostic 	u_long *datasump;	/* Used to computed checksum on data */
13351149Sbostic 	int block_array_size;	/* How many entries in block array */
13451149Sbostic 	int bsize;		/* Block size */
13551149Sbostic 	int db_per_fb;		/* Disk blocks per file block */
13651864Sbostic 	int i, j;
13751866Sbostic 	int off;		/* Offset at which to write */
13851149Sbostic 	int sb_to_sum;		/* offset between superblock and summary */
13951149Sbostic 	int sb_interval;	/* number of segs between super blocks */
14051149Sbostic 	int seg_seek;		/* Seek offset for a segment */
14151149Sbostic 	int ssize;		/* Segment size */
14251149Sbostic 	int sum_size;		/* Size of the summary block */
14351149Sbostic 	int wbytes;		/* Number of bytes returned by write */
14451149Sbostic 
14551149Sbostic 	lfsp = &lfs_default;
14651149Sbostic 
14751149Sbostic 	if (!(bsize = block_size))
14851149Sbostic 		bsize = DFL_LFSBLOCK;
14951149Sbostic 	if (!(ssize = seg_size))
15051149Sbostic 		ssize = DFL_LFSSEG;
15151149Sbostic 
15251149Sbostic 	/* Modify parts of superblock overridden by command line arguments */
15351149Sbostic 	if (bsize != DFL_LFSBLOCK) {
15451149Sbostic 		lfsp->lfs_bshift = log2(bsize);
15551149Sbostic 		if (1 << lfsp->lfs_bshift != bsize)
15651149Sbostic 			fatal("%d: block size not a power of 2", bsize);
15751149Sbostic 		lfsp->lfs_bsize = bsize;
15851864Sbostic 		lfsp->lfs_fsize = bsize;
15951149Sbostic 		lfsp->lfs_bmask = bsize - 1;
16051149Sbostic 		lfsp->lfs_inopb = bsize / sizeof(struct dinode);
16151149Sbostic /* MIS -- should I round to power of 2 */
16251149Sbostic 		lfsp->lfs_ifpb = bsize / sizeof(IFILE);
16351866Sbostic 		lfsp->lfs_sepb = bsize / sizeof(SEGUSE);
16451149Sbostic 		lfsp->lfs_nindir = bsize / sizeof(daddr_t);
16551149Sbostic 	}
16651149Sbostic 
16751149Sbostic 	if (ssize != DFL_LFSSEG) {
16851149Sbostic 		lfsp->lfs_segshift = log2(ssize);
16951149Sbostic 		if (1 << lfsp->lfs_segshift != ssize)
17051149Sbostic 			fatal("%d: segment size not power of 2", ssize);
17151149Sbostic 		lfsp->lfs_ssize = ssize;
17251149Sbostic 		lfsp->lfs_segmask = ssize - 1;
17351149Sbostic 	}
17451149Sbostic 	lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift;
17551149Sbostic 
17651149Sbostic 	if (minfree)
17751149Sbostic 		lfsp->lfs_minfree = minfree;
17851149Sbostic 
17951149Sbostic 	/*
18051149Sbostic 	 * Fill in parts of superblock that can be computed from file system
18151149Sbostic 	 * size, disk geometry and current time.
18251149Sbostic 	 */
18351149Sbostic 	db_per_fb = bsize/lp->d_secsize;
18451149Sbostic 	lfsp->lfs_fsbtodb = log2(db_per_fb);
18551149Sbostic 	lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb;
18651149Sbostic 	lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift);
18751149Sbostic 	lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize;
18851864Sbostic 
18951864Sbostic 	/*
19051864Sbostic 	 * The number of free blocks is set from the total data size (lfs_dsize)
19151864Sbostic 	 * minus one block for each segment (for the segment summary).  Then
19251866Sbostic 	 * we'll subtract off the room for the superblocks, ifile entries and
19351866Sbostic 	 * segment usage table.
19451864Sbostic 	 */
19551864Sbostic 	lfsp->lfs_bfree = lfsp->lfs_dsize - lfsp->lfs_nseg;
19651149Sbostic 	lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp);
19751866Sbostic 	lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp);
19851149Sbostic 	if ((lfsp->lfs_tstamp = time(NULL)) == -1)
19951149Sbostic 		fatal("time: %s", strerror(errno));
20051149Sbostic 	if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
20151149Sbostic 		sb_interval = LFS_MIN_SBINTERVAL;
20251149Sbostic 
20351149Sbostic 	/*
20451149Sbostic 	 * Now, lay out the file system.  We need to figure out where
20551149Sbostic 	 * the superblocks go, initialize the checkpoint information
20651149Sbostic 	 * for the first two superblocks, initialize the segment usage
20751149Sbostic 	 * information, put the segusage information in the ifile, create
20851149Sbostic 	 * the first block of IFILE structures, and link all the IFILE
20951149Sbostic 	 * structures into a free list.
21051149Sbostic 	 */
21151149Sbostic 
21251149Sbostic 	/* Figure out where the superblocks are going to live */
21351149Sbostic 	lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize;
21451149Sbostic 	for (i = 1; i < LFS_MAXNUMSB; i++) {
21551149Sbostic 		sb_addr = ((i * sb_interval) <<
21651149Sbostic 		    (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb))
21751149Sbostic 		    + lfsp->lfs_sboffs[0];
21851149Sbostic 		if (sb_addr > partp->p_size)
21951149Sbostic 			break;
22051149Sbostic 		lfsp->lfs_sboffs[i] = sb_addr;
22151149Sbostic 	}
22251149Sbostic 	last_sb_addr = lfsp->lfs_sboffs[i - 1];
22351149Sbostic 	lfsp->lfs_lastseg = lfsp->lfs_sboffs[0];
22451864Sbostic 	lfsp->lfs_nextseg =
22551864Sbostic 	    lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0];
22651866Sbostic 	lfsp->lfs_curseg = lfsp->lfs_lastseg;
22751149Sbostic 
22851149Sbostic 	/*
22951149Sbostic 	 * Initialize the segment usage table.  The first segment will
23051866Sbostic 	 * contain the superblock, the cleanerinfo (cleansz), the segusage
23151866Sbostic 	 * table * (segtabsz), 1 block's worth of IFILE entries, the root
23251866Sbostic 	 * directory, the lost+found directory and one block's worth of
23351866Sbostic 	 * inodes (containing the ifile, root, and l+f inodes).
23451149Sbostic 	 */
23551866Sbostic 	if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift)))
23651866Sbostic 		fatal("%s", strerror(errno));
23751930Sbostic 	cleaninfo->clean = lfsp->lfs_nseg - 1;
23851930Sbostic 	cleaninfo->dirty = 1;
23951930Sbostic 
24051149Sbostic 	if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift)))
24151149Sbostic 		fatal("%s", strerror(errno));
24251149Sbostic 	segp = segtable;
24351866Sbostic 	blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4;
244*52088Sbostic 	segp->su_nbytes = blocks_used << lfsp->lfs_bshift;
24551149Sbostic 	segp->su_lastmod = lfsp->lfs_tstamp;
24651930Sbostic 	segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY;
24751930Sbostic 	lfsp->lfs_bfree -= lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4;
24851149Sbostic 
24951866Sbostic 	/*
25051866Sbostic 	 * Now figure out the address of the ifile inode. The inode block
25151866Sbostic 	 * appears immediately after the segment summary.
25251866Sbostic 	 */
25351866Sbostic 	lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) /
25451866Sbostic 	    lp->d_secsize;
25551149Sbostic 
25651149Sbostic 	for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) {
25751864Sbostic 		if ((i % sb_interval) == 0) {
25851930Sbostic 			segp->su_flags = SEGUSE_SUPERBLOCK;
25951864Sbostic 			lfsp->lfs_bfree -= (LFS_SBPAD >> lfsp->lfs_bshift);
260*52088Sbostic 		} else
26151930Sbostic 			segp->su_flags = 0;
26251149Sbostic 		segp->su_lastmod = 0;
263*52088Sbostic 		segp->su_nbytes = 0;
26451149Sbostic 	}
26551149Sbostic 
26651149Sbostic 	/*
26751149Sbostic 	 * Ready to start writing segments.  The first segment is different
26851149Sbostic 	 * because it contains the segment usage table and the ifile inode
26951864Sbostic 	 * as well as a superblock.  For the rest of the segments, set the
27051864Sbostic 	 * time stamp to be 0 so that the first segment is the most recent.
27151864Sbostic 	 * For each segment that is supposed to contain a copy of the super
27251864Sbostic 	 * block, initialize its first few blocks and its segment summary
27351864Sbostic 	 * to indicate this.
27451149Sbostic 	 */
27551864Sbostic 	lfsp->lfs_nfiles = LFS_FIRST_INUM - 1;
27651149Sbostic 	lfsp->lfs_cksum =
27751866Sbostic 	    cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum));
27851149Sbostic 
27951864Sbostic 	/* Now create a block of disk inodes */
28051864Sbostic 	if (!(dpagep = malloc(lfsp->lfs_bsize)))
28151864Sbostic 		fatal("%s", strerror(errno));
28251864Sbostic 	dip = (struct dinode *)dpagep;
28351864Sbostic 	bzero(dip, lfsp->lfs_bsize);
28451864Sbostic 
28551866Sbostic 	/* Create a block of IFILE structures. */
28651866Sbostic 	if (!(ipagep = malloc(lfsp->lfs_bsize)))
28751149Sbostic 		fatal("%s", strerror(errno));
28851866Sbostic 	ifile = (IFILE *)ipagep;
28951864Sbostic 
29051866Sbostic 	/*
29151866Sbostic 	 * Initialize IFILE.  It is the next block following the
29251866Sbostic 	 * block of inodes (whose address has been calculated in
29351866Sbostic 	 * lfsp->lfs_idaddr;
29451866Sbostic 	 */
29551866Sbostic 	sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize;
29651866Sbostic 	sb_addr = make_dinode(LFS_IFILE_INUM, dip,
29751866Sbostic 	    lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp);
29851864Sbostic 	dip->di_mode = IFREG|IREAD|IWRITE;
29951864Sbostic 	ip = &ifile[LFS_IFILE_INUM];
30051864Sbostic 	ip->if_version = 1;
30151864Sbostic 	ip->if_daddr = lfsp->lfs_idaddr;
30251864Sbostic 	ip->if_st_atime = lfsp->lfs_tstamp;
30351864Sbostic 
30451864Sbostic 	/* Initialize the ROOT Directory */
30551864Sbostic 	sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp);
30651864Sbostic 	dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC;
30751864Sbostic 	dip->di_size = DIRBLKSIZ;
30851864Sbostic 	dip->di_nlink = 3;
30951864Sbostic 	ip = &ifile[ROOTINO];
31051864Sbostic 	ip->if_version = 1;
31151864Sbostic 	ip->if_daddr = lfsp->lfs_idaddr;
31251864Sbostic 	ip->if_st_atime = lfsp->lfs_tstamp;
31351864Sbostic 
31451864Sbostic 	/* Initialize the lost+found Directory */
31551864Sbostic 	sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp);
31651864Sbostic 	dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC;
31751864Sbostic 	dip->di_size = DIRBLKSIZ;
31851864Sbostic 	dip->di_nlink = 2;
31951864Sbostic 	ip = &ifile[LOSTFOUNDINO];
32051864Sbostic 	ip->if_version = 1;
32151864Sbostic 	ip->if_daddr = lfsp->lfs_idaddr;
32251864Sbostic 	ip->if_st_atime = lfsp->lfs_tstamp;
32351864Sbostic 
32451864Sbostic 	/* Make all the other dinodes invalid */
32551864Sbostic 	for (i = INOPB(lfsp)-3, dip++; i; i--, dip++)
32651864Sbostic 		dip->di_inum = LFS_UNUSED_INUM;
32751864Sbostic 
32851864Sbostic 
32951864Sbostic 	/* Link remaining IFILE entries in free list */
33051864Sbostic 	for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM;
33151864Sbostic 	    i < lfsp->lfs_ifpb; ++ip) {
33251149Sbostic 		ip->if_version = 1;
33351149Sbostic 		ip->if_daddr = LFS_UNUSED_DADDR;
33451149Sbostic 		ip->if_nextfree = ++i;
33551149Sbostic 	}
33651149Sbostic 	ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM;
33751149Sbostic 
33851866Sbostic 	/* Now, write the segment */
33951149Sbostic 
34051866Sbostic 	/* Compute a checksum across all the data you're writing */
34151866Sbostic 	dp = datasump = malloc (blocks_used * sizeof(u_long));
34251866Sbostic 	*dp++ = ((u_long *)dip)[0];		/* inode block */
34351866Sbostic 	for (i = 0; i < lfsp->lfs_cleansz; i++)
34451866Sbostic 		*dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) /
34551866Sbostic 		    sizeof(u_long)];		/* Cleaner info */
34651866Sbostic 	for (i = 0; i < lfsp->lfs_segtabsz; i++)
34751866Sbostic 		*dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) /
34851866Sbostic 		    sizeof(u_long)];		/* Segusage table */
34951866Sbostic 	*dp++ = ((u_long *)ifile)[0];		/* Ifile */
35051866Sbostic 
35151866Sbostic 	/* Still need the root and l+f bytes; get them later */
35251866Sbostic 
35351866Sbostic 	/* Write out the inode block */
35451866Sbostic 	off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE;
35551866Sbostic 	put(fd, off, dpagep, lfsp->lfs_bsize);
35651866Sbostic 	free(dpagep);
35751866Sbostic 	off += lfsp->lfs_bsize;
35851866Sbostic 
35951866Sbostic 	/* Write out the ifile */
36051866Sbostic 
36151866Sbostic 	put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift);
36251866Sbostic 	off += (lfsp->lfs_cleansz << lfsp->lfs_bshift);
36351866Sbostic 	(void)free(cleaninfo);
36451866Sbostic 
36551866Sbostic 	put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift);
36651866Sbostic 	off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift);
36751866Sbostic 	(void)free(segtable);
36851866Sbostic 
36951866Sbostic 	put(fd, off, ifile, lfsp->lfs_bsize);
37051866Sbostic 	off += lfsp->lfs_bsize;
37151866Sbostic 
37251866Sbostic 	/*
37351866Sbostic 	 * use ipagep for space for writing out other stuff.  It used to
37451866Sbostic 	 * contain the ifile, but we're done with it.
37551866Sbostic 	 */
37651866Sbostic 
37751864Sbostic 	/* Write out the root and lost and found directories */
37851866Sbostic 	bzero(ipagep, lfsp->lfs_bsize);
37951866Sbostic 	make_dir(ipagep, lfs_root_dir,
38051864Sbostic 	    sizeof(lfs_root_dir) / sizeof(struct direct));
38151866Sbostic 	*dp++ = ((u_long *)ipagep)[0];
38251866Sbostic 	put(fd, off, ipagep, lfsp->lfs_bsize);
38351866Sbostic 	off += lfsp->lfs_bsize;
38451149Sbostic 
38551866Sbostic 	bzero(ipagep, lfsp->lfs_bsize);
38651866Sbostic 	make_dir(ipagep, lfs_lf_dir,
38751864Sbostic 		sizeof(lfs_lf_dir) / sizeof(struct direct));
38851866Sbostic 	*dp++ = ((u_long *)ipagep)[0];
38951866Sbostic 	put(fd, off, ipagep, lfsp->lfs_bsize);
39051149Sbostic 
39151866Sbostic 	/* Write Supberblock */
39251866Sbostic 	lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize;
39351866Sbostic 	put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs));
39451149Sbostic 
39551866Sbostic 	/*
39651866Sbostic 	 * Finally, calculate all the fields for the summary structure
39751866Sbostic 	 * and write it.
39851866Sbostic 	 */
39951866Sbostic 
40051864Sbostic 	summary.ss_next = lfsp->lfs_nextseg;
40151149Sbostic 	summary.ss_create = lfsp->lfs_tstamp;
40251866Sbostic 	summary.ss_nfinfo = 3;
40351866Sbostic 	summary.ss_ninos = 3;
40451866Sbostic 	summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used);
40551149Sbostic 
40651864Sbostic 	/*
40751864Sbostic 	 * Make sure that we don't overflow a summary block. We have to
40851866Sbostic 	 * record: FINFO structures for ifile, root, and l+f.  The number
40951866Sbostic 	 * of blocks recorded for the ifile is determined by the size of
41051866Sbostic 	 * the cleaner info and the segments usage table.  There is room
41151866Sbostic 	 * for one block included in sizeof(FINFO) so we don't need to add
41251866Sbostic 	 * any extra space for the ROOT and L+F, and one block of the ifile
41351866Sbostic 	 * is already counted.  Finally, we leave room for 1 inode block
41451866Sbostic 	 * address.
41551864Sbostic 	 */
41651866Sbostic 	sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t);
41751866Sbostic 	    (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long);
41851149Sbostic #define	SUMERR \
41951149Sbostic "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes."
42051149Sbostic 	if (sum_size > LFS_SUMMARY_SIZE)
42151149Sbostic 		fatal(SUMERR, sum_size);
42251149Sbostic 
42351866Sbostic 		block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1;
42451149Sbostic 
42551149Sbostic 	if (!(block_array = malloc(block_array_size *sizeof(int))))
42651149Sbostic 		fatal("%s: %s", special, strerror(errno));
42751149Sbostic 
42851149Sbostic 	/* fill in the array */
42951866Sbostic 	for (i = 0; i < block_array_size; i++)
43051149Sbostic 		block_array[i] = i;
43151149Sbostic 
43251149Sbostic 	/* copy into segment */
43351866Sbostic 	sump = ipagep;
43451149Sbostic 	bcopy(&summary, sump, sizeof(SEGSUM));
43551149Sbostic 	sump += sizeof(SEGSUM);
43651149Sbostic 
43751149Sbostic 	/* Now, add the ifile */
43851866Sbostic 	file_info.fi_nblocks = block_array_size;
43951149Sbostic 	file_info.fi_version = 1;
44051149Sbostic 	file_info.fi_ino = LFS_IFILE_INUM;
44151149Sbostic 
44251149Sbostic 	bcopy(&file_info, sump, sizeof(FINFO) - sizeof(u_long));
44351149Sbostic 	sump += sizeof(FINFO) - sizeof(u_long);
44451149Sbostic 	bcopy(block_array, sump, sizeof(u_long) * file_info.fi_nblocks);
44551864Sbostic 	sump += sizeof(u_long) * file_info.fi_nblocks;
44651149Sbostic 
44751864Sbostic 	/* Now, add the root directory */
44851864Sbostic 	file_info.fi_nblocks = 1;
44951864Sbostic 	file_info.fi_version = 1;
45051864Sbostic 	file_info.fi_ino = ROOTINO;
45151864Sbostic 	file_info.fi_blocks[0] = 0;
45251864Sbostic 	bcopy(&file_info, sump, sizeof(FINFO));
45351864Sbostic 	sump += sizeof(FINFO);
45451864Sbostic 
45551864Sbostic 	/* Now, add the lost and found */
45651864Sbostic 	file_info.fi_ino = LOSTFOUNDINO;
45751864Sbostic 	bcopy(&file_info, sump, sizeof(FINFO));
45851864Sbostic 
45951866Sbostic 	((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] =
46051866Sbostic 	    lfsp->lfs_idaddr;
46151866Sbostic 	((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum),
46251866Sbostic 	    LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum));
46351866Sbostic 	put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE);
46451864Sbostic 
46551866Sbostic 	sp = (SEGSUM *)ipagep;
46651866Sbostic 	sp->ss_create = 0;
46751866Sbostic 	sp->ss_nfinfo = 0;
46851866Sbostic 	sp->ss_ninos = 0;
46951866Sbostic 	sp->ss_datasum = 0;
47051149Sbostic 
47151149Sbostic 	/* Now, write rest of segments containing superblocks */
47251149Sbostic 	lfsp->lfs_tstamp = 0;
47351864Sbostic 	lfsp->lfs_cksum =
47451866Sbostic 	    cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum));
47551864Sbostic 	for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1;
47651864Sbostic 	    i < lfsp->lfs_nseg; i++) {
47751149Sbostic 
47851864Sbostic 		seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb;
47951866Sbostic 		sp->ss_next = last_addr;
48051866Sbostic 		last_addr = seg_addr;
48151866Sbostic 		seg_seek = seg_addr * lp->d_secsize;
48251866Sbostic 
48351864Sbostic 		if (seg_addr == lfsp->lfs_sboffs[j]) {
48451864Sbostic 			if (j < (LFS_MAXNUMSB - 2))
48551864Sbostic 				j++;
48651866Sbostic 			put(fd, seg_seek, lfsp, sizeof(struct lfs));
48751866Sbostic 			seg_seek += LFS_SBPAD;
48851866Sbostic 		}
48951149Sbostic 
49051149Sbostic 		/* Summary */
49151866Sbostic 		sp->ss_sumsum = cksum(&sp->ss_datasum,
49251866Sbostic 		    LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum));
49351866Sbostic 		put(fd, seg_seek, sp, LFS_SUMMARY_SIZE);
49451149Sbostic 	}
49551866Sbostic 	free(ipagep);
49651864Sbostic 	close(fd);
49751149Sbostic 	return (0);
49851149Sbostic }
49951149Sbostic 
50051149Sbostic static void
50151149Sbostic put(fd, off, p, len)
50251149Sbostic 	int fd;
50351149Sbostic 	off_t off;
50451149Sbostic 	void *p;
50551149Sbostic 	size_t len;
50651149Sbostic {
50751149Sbostic 	int wbytes;
50851149Sbostic 
50951149Sbostic 	if (lseek(fd, off, SEEK_SET) < 0)
51051149Sbostic 		fatal("%s: %s", special, strerror(errno));
51151149Sbostic 	if ((wbytes = write(fd, p, len)) < 0)
51251149Sbostic 		fatal("%s: %s", special, strerror(errno));
51351149Sbostic 	if (wbytes != len)
51451149Sbostic 		fatal("%s: short write (%d, not %d)", special, wbytes, len);
51551149Sbostic }
51651864Sbostic 
51751864Sbostic /*
51851864Sbostic  * Create the root directory for this file system and the lost+found
51951864Sbostic  * directory.
52051864Sbostic  */
52151864Sbostic 
52251864Sbostic 	u_long	d_ino;			/* inode number of entry */
52351864Sbostic 	u_short	d_reclen;		/* length of this record */
52451864Sbostic 	u_short	d_namlen;		/* length of string in d_name */
52551864Sbostic 	char	d_name[MAXNAMLEN + 1];	/* name with length <= MAXNAMLEN */
52651864Sbostic lfsinit()
52751864Sbostic {
52851864Sbostic }
52951864Sbostic 
53051864Sbostic static daddr_t
53151864Sbostic make_dinode(ino, dip, nblocks, saddr, lfsp)
53251864Sbostic 	ino_t ino;				/* inode we're creating */
53351864Sbostic 	struct dinode *dip;			/* disk inode */
53451864Sbostic 	int nblocks;				/* number of blocks in file */
53551864Sbostic 	daddr_t saddr;				/* starting block address */
53651866Sbostic 	struct lfs *lfsp;			/* superblock */
53751864Sbostic {
53851864Sbostic 	int db_per_fb, i;
53951864Sbostic 
54051864Sbostic 	dip->di_nlink = 1;
54151864Sbostic 	dip->di_blocks = nblocks;
54251864Sbostic 
54351864Sbostic 	/* If we ever need something longer than 32 bits, this changes */
54451864Sbostic 	dip->di_size = (dip->di_blocks << lfsp->lfs_bshift);
54551864Sbostic 	dip->di_atime = dip->di_mtime = dip->di_ctime = lfsp->lfs_tstamp;
54651864Sbostic 	dip->di_inum = ino;
54751864Sbostic 
54851864Sbostic #define	SEGERR \
54951864Sbostic "File requires more than the number of direct blocks; increase block or segment size."
55051864Sbostic 	if (NDADDR < nblocks)
55151864Sbostic 		fatal("%s", SEGERR);
55251864Sbostic 
55351864Sbostic 	/* Assign the block addresses for the ifile */
55451864Sbostic 	db_per_fb = 1 << lfsp->lfs_fsbtodb;
55551864Sbostic 	for (i = 0; i < dip->di_blocks; i++, saddr += db_per_fb)
55651864Sbostic 		dip->di_db[i] = saddr;
55751864Sbostic 
55851864Sbostic 	return (saddr);
55951864Sbostic }
56051864Sbostic 
56151864Sbostic 
56251864Sbostic /*
56351864Sbostic  * Construct a set of directory entries in "bufp".  We assume that all the
56451864Sbostic  * entries in protodir fir in the first DIRBLKSIZ.
56551864Sbostic  */
56651864Sbostic static void
56751864Sbostic make_dir(bufp, protodir, entries)
56851864Sbostic 	void *bufp;
56951864Sbostic 	register struct direct *protodir;
57051864Sbostic 	int entries;
57151864Sbostic {
57251864Sbostic 	char *cp;
57351864Sbostic 	int i, spcleft;
57451864Sbostic 
57551864Sbostic 	spcleft = DIRBLKSIZ;
57651864Sbostic 	for (cp = bufp, i = 0; i < entries - 1; i++) {
57751864Sbostic 		protodir[i].d_reclen = DIRSIZ(&protodir[i]);
57851864Sbostic 		bcopy(&protodir[i], cp, protodir[i].d_reclen);
57951864Sbostic 		cp += protodir[i].d_reclen;
58051864Sbostic 		if ((spcleft -= protodir[i].d_reclen) < 0)
58151864Sbostic 			fatal("%s: %s", special, "directory too big");
58251864Sbostic 	}
58351864Sbostic 	protodir[i].d_reclen = spcleft;
58451864Sbostic 	bcopy(&protodir[i], cp, DIRSIZ(&protodir[i]));
58551864Sbostic }
586