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