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