xref: /original-bsd/sbin/newlfs/lfs.c (revision 860e07fc)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)lfs.c	5.21 (Berkeley) 09/03/92";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/disklabel.h>
14 #include <sys/time.h>
15 #include <sys/mount.h>
16 
17 #include <ufs/ufs/dir.h>
18 #include <ufs/ufs/quota.h>
19 #include <ufs/ufs/dinode.h>
20 #include <ufs/lfs/lfs.h>
21 
22 #include <unistd.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "config.h"
27 #include "extern.h"
28 
29 /*
30  * This table is indexed by the log base 2 of the block size.
31  * It returns the maximum file size allowed in a file system
32  * with the specified block size.  For block sizes smaller than
33  * 8K, the size is limited by tha maximum number of blocks that
34  * can be reached by triply indirect blocks:
35  *	NDADDR + INOPB(bsize) + INOPB(bsize)^2 + INOPB(bsize)^3
36  * For block size of 8K or larger, the file size is limited by the
37  * number of blocks that can be represented in the file system.  Since
38  * we use negative block numbers to represent indirect blocks, we can
39  * have a maximum of 2^31 blocks.
40  */
41 
42 u_quad_t maxtable[] = {
43 	/*    1 */ -1,
44 	/*    2 */ -1,
45 	/*    4 */ -1,
46 	/*    8 */ -1,
47 	/*   16 */ -1,
48 	/*   32 */ -1,
49 	/*   64 */ -1,
50 	/*  128 */ -1,
51 	/*  256 */ -1,
52 	/*  512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128,
53 	/* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256,
54 	/* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512,
55 	/* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024,
56 	/* 8192 */ 2 ^ 31,
57 	/* 16 K */ 2 ^ 31,
58 	/* 32 K */ 2 ^ 31
59 };
60 
61 static struct lfs lfs_default =  {
62 	/* lfs_magic */		LFS_MAGIC,
63 	/* lfs_version */	LFS_VERSION,
64 	/* lfs_size */		0,
65 	/* lfs_ssize */		DFL_LFSSEG/DFL_LFSBLOCK,
66 	/* lfs_dsize */		0,
67 	/* lfs_bsize */		DFL_LFSBLOCK,
68 	/* lfs_fsize */		DFL_LFSBLOCK,
69 	/* lfs_frag */		1,
70 	/* lfs_free */		LFS_FIRST_INUM,
71 	/* lfs_bfree */		0,
72 	/* lfs_nfiles */	0,
73 	/* lfs_avail */		0,
74 	/* lfs_uinodes */	0,
75 	/* lfs_idaddr */	0,
76 	/* lfs_ifile */		LFS_IFILE_INUM,
77 	/* lfs_lastseg */	0,
78 	/* lfs_nextseg */	0,
79 	/* lfs_curseg */	0,
80 	/* lfs_offset */	0,
81 	/* lfs_lastpseg */	0,
82 	/* lfs_tstamp */	0,
83 	/* lfs_minfree */	MINFREE,
84 	/* lfs_maxfilesize */	0,
85 	/* lfs_dbpseg */	DFL_LFSSEG/DEV_BSIZE,
86 	/* lfs_inopb */		DFL_LFSBLOCK/sizeof(struct dinode),
87 	/* lfs_ifpb */		DFL_LFSBLOCK/sizeof(IFILE),
88 	/* lfs_sepb */		DFL_LFSBLOCK/sizeof(SEGUSE),
89 	/* lfs_nindir */	DFL_LFSBLOCK/sizeof(daddr_t),
90 	/* lfs_nseg */		0,
91 	/* lfs_nspf */		0,
92 	/* lfs_cleansz */	0,
93 	/* lfs_segtabsz */	0,
94 	/* lfs_segmask */	DFL_LFSSEG_MASK,
95 	/* lfs_segshift */	DFL_LFSSEG_SHIFT,
96 	/* lfs_bmask */		DFL_LFSBLOCK_MASK,
97 	/* lfs_bshift */	DFL_LFSBLOCK_SHIFT,
98 	/* lfs_ffmask */	0,
99 	/* lfs_ffshift */	0,
100 	/* lfs_fbmask */	0,
101 	/* lfs_fbshift */	0,
102 	/* lfs_fsbtodb */	0,
103 	/* lfs_sushift */	0,
104 	/* lfs_sboffs */	{ 0 },
105 	/* lfs_ivnode */	NULL,
106 	/* lfs_seglock */	0,
107 	/* lfs_iocount */	0,
108 	/* lfs_writer */	0,
109 	/* lfs_dirops */	0,
110 	/* lfs_doifile */	0,
111 	/* lfs_nactive */	0,
112 	/* lfs_fmod */		0,
113 	/* lfs_clean */		0,
114 	/* lfs_ronly */		0,
115 	/* lfs_flags */		0,
116 	/* lfs_fsmnt */		{ 0 },
117 	/* lfs_pad */		{ 0 },
118 	/* lfs_cksum */		0
119 };
120 
121 
122 struct direct lfs_root_dir[] = {
123 	{ ROOTINO, sizeof(struct direct), DT_DIR, 1, "."},
124 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".."},
125 	{ LFS_IFILE_INUM, sizeof(struct direct), DT_REG, 5, "ifile"},
126 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found"},
127 };
128 
129 struct direct lfs_lf_dir[] = {
130         { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
131         { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
132 };
133 
134 static daddr_t make_dinode
135 	__P((ino_t, struct dinode *, int, daddr_t, struct lfs *));
136 static void make_dir __P(( void *, struct direct *, int));
137 static void put __P((int, off_t, void *, size_t));
138 
139 int
140 make_lfs(fd, lp, partp, minfree, block_size, seg_size)
141 	int fd;
142 	struct disklabel *lp;
143 	struct partition *partp;
144 	int minfree;
145 	int block_size;
146 	int seg_size;
147 {
148 	struct dinode *dip;	/* Pointer to a disk inode */
149 	struct dinode *dpagep;	/* Pointer to page of disk inodes */
150 	CLEANERINFO *cleaninfo;	/* Segment cleaner information table */
151 	FINFO file_info;	/* File info structure in summary blocks */
152 	IFILE *ifile;		/* Pointer to array of ifile structures */
153 	IFILE *ip;		/* Pointer to array of ifile structures */
154 	struct lfs *lfsp;	/* Superblock */
155 	SEGUSE *segp;		/* Segment usage table */
156 	SEGUSE *segtable;	/* Segment usage table */
157 	SEGSUM summary;		/* Segment summary structure */
158 	SEGSUM *sp;		/* Segment summary pointer */
159 	daddr_t	last_sb_addr;	/* Address of superblocks */
160 	daddr_t last_addr;	/* Previous segment address */
161 	daddr_t	sb_addr;	/* Address of superblocks */
162 	daddr_t	seg_addr;	/* Address of current segment */
163 	void *ipagep;		/* Pointer to the page we use to write stuff */
164 	void *sump;		/* Used to copy stuff into segment buffer */
165 	u_long *block_array;	/* Array of logical block nos to put in sum */
166 	u_long blocks_used;	/* Number of blocks in first segment */
167 	u_long *dp;		/* Used to computed checksum on data */
168 	u_long *datasump;	/* Used to computed checksum on data */
169 	int block_array_size;	/* How many entries in block array */
170 	int bsize;		/* Block size */
171 	int db_per_fb;		/* Disk blocks per file block */
172 	int i, j;
173 	int off;		/* Offset at which to write */
174 	int sb_interval;	/* number of segs between super blocks */
175 	int seg_seek;		/* Seek offset for a segment */
176 	int ssize;		/* Segment size */
177 	int sum_size;		/* Size of the summary block */
178 
179 	lfsp = &lfs_default;
180 
181 	if (!(bsize = block_size))
182 		bsize = DFL_LFSBLOCK;
183 	if (!(ssize = seg_size))
184 		ssize = DFL_LFSSEG;
185 
186 	/* Modify parts of superblock overridden by command line arguments */
187 	if (bsize != DFL_LFSBLOCK) {
188 		lfsp->lfs_bshift = log2(bsize);
189 		if (1 << lfsp->lfs_bshift != bsize)
190 			fatal("%d: block size not a power of 2", bsize);
191 		lfsp->lfs_bsize = bsize;
192 		lfsp->lfs_fsize = bsize;
193 		lfsp->lfs_bmask = bsize - 1;
194 		lfsp->lfs_inopb = bsize / sizeof(struct dinode);
195 /* MIS -- should I round to power of 2 */
196 		lfsp->lfs_ifpb = bsize / sizeof(IFILE);
197 		lfsp->lfs_sepb = bsize / sizeof(SEGUSE);
198 		lfsp->lfs_nindir = bsize / sizeof(daddr_t);
199 	}
200 
201 	if (ssize != DFL_LFSSEG) {
202 		lfsp->lfs_segshift = log2(ssize);
203 		if (1 << lfsp->lfs_segshift != ssize)
204 			fatal("%d: segment size not power of 2", ssize);
205 		lfsp->lfs_ssize = ssize;
206 		lfsp->lfs_segmask = ssize - 1;
207 	}
208 	lfsp->lfs_ssize = ssize >> lfsp->lfs_bshift;
209 
210 	if (minfree)
211 		lfsp->lfs_minfree = minfree;
212 
213 	/*
214 	 * Fill in parts of superblock that can be computed from file system
215 	 * size, disk geometry and current time.
216 	 */
217 	db_per_fb = bsize/lp->d_secsize;
218 	lfsp->lfs_fsbtodb = log2(db_per_fb);
219 	lfsp->lfs_sushift = log2(lfsp->lfs_sepb);
220 	lfsp->lfs_size = partp->p_size >> lfsp->lfs_fsbtodb;
221 	lfsp->lfs_dsize = lfsp->lfs_size - (LFS_LABELPAD >> lfsp->lfs_bshift);
222 	lfsp->lfs_nseg = lfsp->lfs_dsize / lfsp->lfs_ssize;
223 	lfsp->lfs_maxfilesize = maxtable[lfsp->lfs_bshift] << lfsp->lfs_bshift;
224 
225 	/*
226 	 * The number of free blocks is set from the number of segments times
227 	 * the segment size - 2 (that we never write because we need to make
228 	 * sure the cleaner can run).  Then we'll subtract off the room for the
229 	 * superblocks ifile entries and segment usage table.
230 	 */
231 	lfsp->lfs_dsize = fsbtodb(lfsp, (lfsp->lfs_nseg - 2) * lfsp->lfs_ssize);
232 	lfsp->lfs_bfree = lfsp->lfs_dsize;
233 	lfsp->lfs_segtabsz = SEGTABSIZE_SU(lfsp);
234 	lfsp->lfs_cleansz = CLEANSIZE_SU(lfsp);
235 	if ((lfsp->lfs_tstamp = time(NULL)) == -1)
236 		fatal("time: %s", strerror(errno));
237 	if ((sb_interval = lfsp->lfs_nseg / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
238 		sb_interval = LFS_MIN_SBINTERVAL;
239 
240 	/*
241 	 * Now, lay out the file system.  We need to figure out where
242 	 * the superblocks go, initialize the checkpoint information
243 	 * for the first two superblocks, initialize the segment usage
244 	 * information, put the segusage information in the ifile, create
245 	 * the first block of IFILE structures, and link all the IFILE
246 	 * structures into a free list.
247 	 */
248 
249 	/* Figure out where the superblocks are going to live */
250 	lfsp->lfs_sboffs[0] = LFS_LABELPAD/lp->d_secsize;
251 	for (i = 1; i < LFS_MAXNUMSB; i++) {
252 		sb_addr = ((i * sb_interval) <<
253 		    (lfsp->lfs_segshift - lfsp->lfs_bshift + lfsp->lfs_fsbtodb))
254 		    + lfsp->lfs_sboffs[0];
255 		if (sb_addr > partp->p_size)
256 			break;
257 		lfsp->lfs_sboffs[i] = sb_addr;
258 	}
259 	last_sb_addr = lfsp->lfs_sboffs[i - 1];
260 	lfsp->lfs_lastseg = lfsp->lfs_sboffs[0];
261 	lfsp->lfs_nextseg =
262 	    lfsp->lfs_sboffs[1] ? lfsp->lfs_sboffs[1] : lfsp->lfs_sboffs[0];
263 	lfsp->lfs_curseg = lfsp->lfs_lastseg;
264 
265 	/*
266 	 * Initialize the segment usage table.  The first segment will
267 	 * contain the superblock, the cleanerinfo (cleansz), the segusage
268 	 * table * (segtabsz), 1 block's worth of IFILE entries, the root
269 	 * directory, the lost+found directory and one block's worth of
270 	 * inodes (containing the ifile, root, and l+f inodes).
271 	 */
272 	if (!(cleaninfo = malloc(lfsp->lfs_cleansz << lfsp->lfs_bshift)))
273 		fatal("%s", strerror(errno));
274 	cleaninfo->clean = lfsp->lfs_nseg - 1;
275 	cleaninfo->dirty = 1;
276 
277 	if (!(segtable = malloc(lfsp->lfs_segtabsz << lfsp->lfs_bshift)))
278 		fatal("%s", strerror(errno));
279 	segp = segtable;
280 	blocks_used = lfsp->lfs_segtabsz + lfsp->lfs_cleansz + 4;
281 	segp->su_nbytes = ((blocks_used - 1) << lfsp->lfs_bshift) +
282 	    3 * sizeof(struct dinode) + LFS_SUMMARY_SIZE;
283 	segp->su_lastmod = lfsp->lfs_tstamp;
284 	segp->su_nsums = 1;	/* 1 summary blocks */
285 	segp->su_ninos = 1;	/* 1 inode block */
286 	segp->su_flags = SEGUSE_SUPERBLOCK | SEGUSE_DIRTY;
287 	lfsp->lfs_bfree -= LFS_SUMMARY_SIZE / lp->d_secsize;
288 	lfsp->lfs_bfree -=
289 	     fsbtodb(lfsp, lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 4);
290 
291 	/*
292 	 * Now figure out the address of the ifile inode. The inode block
293 	 * appears immediately after the segment summary.
294 	 */
295 	lfsp->lfs_idaddr = (LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE) /
296 	    lp->d_secsize;
297 
298 	for (segp = segtable + 1, i = 1; i < lfsp->lfs_nseg; i++, segp++) {
299 		if ((i % sb_interval) == 0) {
300 			segp->su_flags = SEGUSE_SUPERBLOCK;
301 			lfsp->lfs_bfree -= (LFS_SBPAD / lp->d_secsize);
302 		} else
303 			segp->su_flags = 0;
304 		segp->su_lastmod = 0;
305 		segp->su_nbytes = 0;
306 		segp->su_ninos = 0;
307 		segp->su_nsums = 0;
308 	}
309 
310 	/*
311 	 * Initialize dynamic accounting.  The blocks available for
312 	 * writing are the bfree blocks minus 1 segment summary for
313 	 * each segment since you can't write any new data without
314 	 * creating a segment summary - 2 segments that the cleaner
315 	 * needs.
316 	 */
317 	lfsp->lfs_avail = lfsp->lfs_bfree - lfsp->lfs_nseg -
318 		fsbtodb(lfsp, 2 * lfsp->lfs_ssize);
319 	lfsp->lfs_uinodes = 0;
320 	/*
321 	 * Ready to start writing segments.  The first segment is different
322 	 * because it contains the segment usage table and the ifile inode
323 	 * as well as a superblock.  For the rest of the segments, set the
324 	 * time stamp to be 0 so that the first segment is the most recent.
325 	 * For each segment that is supposed to contain a copy of the super
326 	 * block, initialize its first few blocks and its segment summary
327 	 * to indicate this.
328 	 */
329 	lfsp->lfs_nfiles = LFS_FIRST_INUM - 1;
330 	lfsp->lfs_cksum =
331 	    cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum));
332 
333 	/* Now create a block of disk inodes */
334 	if (!(dpagep = malloc(lfsp->lfs_bsize)))
335 		fatal("%s", strerror(errno));
336 	dip = (struct dinode *)dpagep;
337 	bzero(dip, lfsp->lfs_bsize);
338 
339 	/* Create a block of IFILE structures. */
340 	if (!(ipagep = malloc(lfsp->lfs_bsize)))
341 		fatal("%s", strerror(errno));
342 	ifile = (IFILE *)ipagep;
343 
344 	/*
345 	 * Initialize IFILE.  It is the next block following the
346 	 * block of inodes (whose address has been calculated in
347 	 * lfsp->lfs_idaddr;
348 	 */
349 	sb_addr = lfsp->lfs_idaddr + lfsp->lfs_bsize / lp->d_secsize;
350 	sb_addr = make_dinode(LFS_IFILE_INUM, dip,
351 	    lfsp->lfs_cleansz + lfsp->lfs_segtabsz+1, sb_addr, lfsp);
352 	dip->di_mode = IFREG|IREAD|IWRITE;
353 	ip = &ifile[LFS_IFILE_INUM];
354 	ip->if_version = 1;
355 	ip->if_daddr = lfsp->lfs_idaddr;
356 
357 	/* Initialize the ROOT Directory */
358 	sb_addr = make_dinode(ROOTINO, ++dip, 1, sb_addr, lfsp);
359 	dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC;
360 	dip->di_size = DIRBLKSIZ;
361 	dip->di_nlink = 3;
362 	ip = &ifile[ROOTINO];
363 	ip->if_version = 1;
364 	ip->if_daddr = lfsp->lfs_idaddr;
365 
366 	/* Initialize the lost+found Directory */
367 	sb_addr = make_dinode(LOSTFOUNDINO, ++dip, 1, sb_addr, lfsp);
368 	dip->di_mode = IFDIR|IREAD|IWRITE|IEXEC;
369 	dip->di_size = DIRBLKSIZ;
370 	dip->di_nlink = 2;
371 	ip = &ifile[LOSTFOUNDINO];
372 	ip->if_version = 1;
373 	ip->if_daddr = lfsp->lfs_idaddr;
374 
375 	/* Make all the other dinodes invalid */
376 	for (i = INOPB(lfsp)-3, dip++; i; i--, dip++)
377 		dip->di_inum = LFS_UNUSED_INUM;
378 
379 
380 	/* Link remaining IFILE entries in free list */
381 	for (ip = &ifile[LFS_FIRST_INUM], i = LFS_FIRST_INUM;
382 	    i < lfsp->lfs_ifpb; ++ip) {
383 		ip->if_version = 1;
384 		ip->if_daddr = LFS_UNUSED_DADDR;
385 		ip->if_nextfree = ++i;
386 	}
387 	ifile[lfsp->lfs_ifpb - 1].if_nextfree = LFS_UNUSED_INUM;
388 
389 	/* Now, write the segment */
390 
391 	/* Compute a checksum across all the data you're writing */
392 	dp = datasump = malloc (blocks_used * sizeof(u_long));
393 	*dp++ = ((u_long *)dpagep)[0];		/* inode block */
394 	for (i = 0; i < lfsp->lfs_cleansz; i++)
395 		*dp++ = ((u_long *)cleaninfo)[(i << lfsp->lfs_bshift) /
396 		    sizeof(u_long)];		/* Cleaner info */
397 	for (i = 0; i < lfsp->lfs_segtabsz; i++)
398 		*dp++ = ((u_long *)segtable)[(i << lfsp->lfs_bshift) /
399 		    sizeof(u_long)];		/* Segusage table */
400 	*dp++ = ((u_long *)ifile)[0];		/* Ifile */
401 
402 	/* Still need the root and l+f bytes; get them later */
403 
404 	/* Write out the inode block */
405 	off = LFS_LABELPAD + LFS_SBPAD + LFS_SUMMARY_SIZE;
406 	put(fd, off, dpagep, lfsp->lfs_bsize);
407 	free(dpagep);
408 	off += lfsp->lfs_bsize;
409 
410 	/* Write out the ifile */
411 
412 	put(fd, off, cleaninfo, lfsp->lfs_cleansz << lfsp->lfs_bshift);
413 	off += (lfsp->lfs_cleansz << lfsp->lfs_bshift);
414 	(void)free(cleaninfo);
415 
416 	put(fd, off, segtable, lfsp->lfs_segtabsz << lfsp->lfs_bshift);
417 	off += (lfsp->lfs_segtabsz << lfsp->lfs_bshift);
418 	(void)free(segtable);
419 
420 	put(fd, off, ifile, lfsp->lfs_bsize);
421 	off += lfsp->lfs_bsize;
422 
423 	/*
424 	 * use ipagep for space for writing out other stuff.  It used to
425 	 * contain the ifile, but we're done with it.
426 	 */
427 
428 	/* Write out the root and lost and found directories */
429 	bzero(ipagep, lfsp->lfs_bsize);
430 	make_dir(ipagep, lfs_root_dir,
431 	    sizeof(lfs_root_dir) / sizeof(struct direct));
432 	*dp++ = ((u_long *)ipagep)[0];
433 	put(fd, off, ipagep, lfsp->lfs_bsize);
434 	off += lfsp->lfs_bsize;
435 
436 	bzero(ipagep, lfsp->lfs_bsize);
437 	make_dir(ipagep, lfs_lf_dir,
438 		sizeof(lfs_lf_dir) / sizeof(struct direct));
439 	*dp++ = ((u_long *)ipagep)[0];
440 	put(fd, off, ipagep, lfsp->lfs_bsize);
441 
442 	/* Write Supberblock */
443 	lfsp->lfs_offset = (off + lfsp->lfs_bsize) / lp->d_secsize;
444 	put(fd, LFS_LABELPAD, lfsp, sizeof(struct lfs));
445 
446 	/*
447 	 * Finally, calculate all the fields for the summary structure
448 	 * and write it.
449 	 */
450 
451 	summary.ss_next = lfsp->lfs_nextseg;
452 	summary.ss_create = lfsp->lfs_tstamp;
453 	summary.ss_nfinfo = 3;
454 	summary.ss_ninos = 3;
455 	summary.ss_datasum = cksum(datasump, sizeof(u_long) * blocks_used);
456 
457 	/*
458 	 * Make sure that we don't overflow a summary block. We have to
459 	 * record: FINFO structures for ifile, root, and l+f.  The number
460 	 * of blocks recorded for the ifile is determined by the size of
461 	 * the cleaner info and the segments usage table.  There is room
462 	 * for one block included in sizeof(FINFO) so we don't need to add
463 	 * any extra space for the ROOT and L+F, and one block of the ifile
464 	 * is already counted.  Finally, we leave room for 1 inode block
465 	 * address.
466 	 */
467 	sum_size = 3*sizeof(FINFO) + sizeof(SEGSUM) + sizeof(daddr_t) +
468 	    (lfsp->lfs_cleansz + lfsp->lfs_segtabsz) * sizeof(u_long);
469 #define	SUMERR \
470 "Multiple summary blocks in segment 1 not yet implemented\nsummary is %d bytes."
471 	if (sum_size > LFS_SUMMARY_SIZE)
472 		fatal(SUMERR, sum_size);
473 
474 		block_array_size = lfsp->lfs_cleansz + lfsp->lfs_segtabsz + 1;
475 
476 	if (!(block_array = malloc(block_array_size *sizeof(int))))
477 		fatal("%s: %s", special, strerror(errno));
478 
479 	/* fill in the array */
480 	for (i = 0; i < block_array_size; i++)
481 		block_array[i] = i;
482 
483 	/* copy into segment */
484 	sump = ipagep;
485 	bcopy(&summary, sump, sizeof(SEGSUM));
486 	sump += sizeof(SEGSUM);
487 
488 	/* Now, add the ifile */
489 	file_info.fi_nblocks = block_array_size;
490 	file_info.fi_version = 1;
491 	file_info.fi_ino = LFS_IFILE_INUM;
492 
493 	bcopy(&file_info, sump, sizeof(FINFO) - sizeof(u_long));
494 	sump += sizeof(FINFO) - sizeof(u_long);
495 	bcopy(block_array, sump, sizeof(u_long) * file_info.fi_nblocks);
496 	sump += sizeof(u_long) * file_info.fi_nblocks;
497 
498 	/* Now, add the root directory */
499 	file_info.fi_nblocks = 1;
500 	file_info.fi_version = 1;
501 	file_info.fi_ino = ROOTINO;
502 	file_info.fi_blocks[0] = 0;
503 	bcopy(&file_info, sump, sizeof(FINFO));
504 	sump += sizeof(FINFO);
505 
506 	/* Now, add the lost and found */
507 	file_info.fi_ino = LOSTFOUNDINO;
508 	bcopy(&file_info, sump, sizeof(FINFO));
509 
510 	((daddr_t *)ipagep)[LFS_SUMMARY_SIZE / sizeof(daddr_t) - 1] =
511 	    lfsp->lfs_idaddr;
512 	((SEGSUM *)ipagep)->ss_sumsum = cksum(ipagep+sizeof(summary.ss_sumsum),
513 	    LFS_SUMMARY_SIZE - sizeof(summary.ss_sumsum));
514 	put(fd, LFS_LABELPAD + LFS_SBPAD, ipagep, LFS_SUMMARY_SIZE);
515 
516 	sp = (SEGSUM *)ipagep;
517 	sp->ss_create = 0;
518 	sp->ss_nfinfo = 0;
519 	sp->ss_ninos = 0;
520 	sp->ss_datasum = 0;
521 
522 	/* Now write the summary block for the next partial so it's invalid */
523 	lfsp->lfs_tstamp = 0;
524 	off += lfsp->lfs_bsize;
525 	sp->ss_sumsum =
526 	    cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum));
527 	put(fd, off, sp, LFS_SUMMARY_SIZE);
528 
529 	/* Now, write rest of segments containing superblocks */
530 	lfsp->lfs_cksum =
531 	    cksum(lfsp, sizeof(struct lfs) - sizeof(lfsp->lfs_cksum));
532 	for (seg_addr = last_addr = lfsp->lfs_sboffs[0], j = 1, i = 1;
533 	    i < lfsp->lfs_nseg; i++) {
534 
535 		seg_addr += lfsp->lfs_ssize << lfsp->lfs_fsbtodb;
536 		sp->ss_next = last_addr;
537 		last_addr = seg_addr;
538 		seg_seek = seg_addr * lp->d_secsize;
539 
540 		if (seg_addr == lfsp->lfs_sboffs[j]) {
541 			if (j < (LFS_MAXNUMSB - 2))
542 				j++;
543 			put(fd, seg_seek, lfsp, sizeof(struct lfs));
544 			seg_seek += LFS_SBPAD;
545 		}
546 
547 		/* Summary */
548 		sp->ss_sumsum = cksum(&sp->ss_datasum,
549 		    LFS_SUMMARY_SIZE - sizeof(sp->ss_sumsum));
550 		put(fd, seg_seek, sp, LFS_SUMMARY_SIZE);
551 	}
552 	free(ipagep);
553 	close(fd);
554 	return (0);
555 }
556 
557 static void
558 put(fd, off, p, len)
559 	int fd;
560 	off_t off;
561 	void *p;
562 	size_t len;
563 {
564 	int wbytes;
565 
566 	if (lseek(fd, off, SEEK_SET) < 0)
567 		fatal("%s: %s", special, strerror(errno));
568 	if ((wbytes = write(fd, p, len)) < 0)
569 		fatal("%s: %s", special, strerror(errno));
570 	if (wbytes != len)
571 		fatal("%s: short write (%d, not %d)", special, wbytes, len);
572 }
573 
574 /*
575  * Create the root directory for this file system and the lost+found
576  * directory.
577  */
578 
579 	u_long	d_ino;			/* inode number of entry */
580 	u_short	d_reclen;		/* length of this record */
581 	u_short	d_namlen;		/* length of string in d_name */
582 	char	d_name[MAXNAMLEN + 1];	/* name with length <= MAXNAMLEN */
583 void
584 lfsinit()
585 {}
586 
587 static daddr_t
588 make_dinode(ino, dip, nblocks, saddr, lfsp)
589 	ino_t ino;				/* inode we're creating */
590 	struct dinode *dip;			/* disk inode */
591 	int nblocks;				/* number of blocks in file */
592 	daddr_t saddr;				/* starting block address */
593 	struct lfs *lfsp;			/* superblock */
594 {
595 	int db_per_fb, i;
596 
597 	dip->di_nlink = 1;
598 	dip->di_blocks = nblocks << lfsp->lfs_fsbtodb;
599 
600 	dip->di_size = (nblocks << lfsp->lfs_bshift);
601 	dip->di_atime.ts_sec = dip->di_mtime.ts_sec =
602 	    dip->di_ctime.ts_sec = lfsp->lfs_tstamp;
603 	dip->di_atime.ts_nsec = dip->di_mtime.ts_nsec =
604 	    dip->di_ctime.ts_nsec = 0;
605 	dip->di_inum = ino;
606 
607 #define	SEGERR \
608 "File requires more than the number of direct blocks; increase block or segment size."
609 	if (NDADDR < nblocks)
610 		fatal("%s", SEGERR);
611 
612 	/* Assign the block addresses for the ifile */
613 	db_per_fb = 1 << lfsp->lfs_fsbtodb;
614 	for (i = 0; i < nblocks; i++, saddr += db_per_fb)
615 		dip->di_db[i] = saddr;
616 
617 	return (saddr);
618 }
619 
620 
621 /*
622  * Construct a set of directory entries in "bufp".  We assume that all the
623  * entries in protodir fir in the first DIRBLKSIZ.
624  */
625 static void
626 make_dir(bufp, protodir, entries)
627 	void *bufp;
628 	register struct direct *protodir;
629 	int entries;
630 {
631 	char *cp;
632 	int i, spcleft;
633 
634 	spcleft = DIRBLKSIZ;
635 	for (cp = bufp, i = 0; i < entries - 1; i++) {
636 		protodir[i].d_reclen = DIRSIZ(NEWDIRFMT, &protodir[i]);
637 		bcopy(&protodir[i], cp, protodir[i].d_reclen);
638 		cp += protodir[i].d_reclen;
639 		if ((spcleft -= protodir[i].d_reclen) < 0)
640 			fatal("%s: %s", special, "directory too big");
641 	}
642 	protodir[i].d_reclen = spcleft;
643 	bcopy(&protodir[i], cp, DIRSIZ(NEWDIRFMT, &protodir[i]));
644 }
645