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