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