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