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