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