xref: /original-bsd/sbin/newfs/mkfs.c (revision 4ec22e22)
1 /*
2  * Copyright (c) 1980, 1989 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[] = "@(#)mkfs.c	6.14 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 #ifndef STANDALONE
13 #include <stdio.h>
14 #include <a.out.h>
15 #endif
16 
17 #include <sys/param.h>
18 #include <sys/time.h>
19 #include <sys/wait.h>
20 #include <sys/resource.h>
21 #include <ufs/dinode.h>
22 #include <ufs/fs.h>
23 #include <ufs/dir.h>
24 #include <sys/disklabel.h>
25 #include <machine/endian.h>
26 
27 /*
28  * make file system for cylinder-group style file systems
29  */
30 
31 /*
32  * The size of a cylinder group is calculated by CGSIZE. The maximum size
33  * is limited by the fact that cylinder groups are at most one block.
34  * Its size is derived from the size of the maps maintained in the
35  * cylinder group and the (struct cg) size.
36  */
37 #define CGSIZE(fs) \
38     /* base cg */	(sizeof(struct cg) + \
39     /* blktot size */	(fs)->fs_cpg * sizeof(long) + \
40     /* blks size */	(fs)->fs_cpg * (fs)->fs_nrpos * sizeof(short) + \
41     /* inode map */	howmany((fs)->fs_ipg, NBBY) + \
42     /* block map */	howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY))
43 
44 /*
45  * We limit the size of the inode map to be no more than a
46  * third of the cylinder group space, since we must leave at
47  * least an equal amount of space for the block map.
48  *
49  * N.B.: MAXIPG must be a multiple of INOPB(fs).
50  */
51 #define MAXIPG(fs)	roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
52 
53 #define UMASK		0755
54 #define MAXINOPB	(MAXBSIZE / sizeof(struct dinode))
55 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
56 
57 /*
58  * variables set up by front end.
59  */
60 extern int	mfs;		/* run as the memory based filesystem */
61 extern int	Nflag;		/* run mkfs without writing file system */
62 extern int	fssize;		/* file system size */
63 extern int	ntracks;	/* # tracks/cylinder */
64 extern int	nsectors;	/* # sectors/track */
65 extern int	nphyssectors;	/* # sectors/track including spares */
66 extern int	secpercyl;	/* sectors per cylinder */
67 extern int	sectorsize;	/* bytes/sector */
68 extern int	rpm;		/* revolutions/minute of drive */
69 extern int	interleave;	/* hardware sector interleave */
70 extern int	trackskew;	/* sector 0 skew, per track */
71 extern int	headswitch;	/* head switch time, usec */
72 extern int	trackseek;	/* track-to-track seek, usec */
73 extern int	fsize;		/* fragment size */
74 extern int	bsize;		/* block size */
75 extern int	cpg;		/* cylinders/cylinder group */
76 extern int	cpgflg;		/* cylinders/cylinder group flag was given */
77 extern int	minfree;	/* free space threshold */
78 extern int	opt;		/* optimization preference (space or time) */
79 extern int	density;	/* number of bytes per inode */
80 extern int	maxcontig;	/* max contiguous blocks to allocate */
81 extern int	rotdelay;	/* rotational delay between blocks */
82 extern int	maxbpg;		/* maximum blocks per file in a cyl group */
83 extern int	nrpos;		/* # of distinguished rotational positions */
84 extern int	bbsize;		/* boot block size */
85 extern int	sbsize;		/* superblock size */
86 extern u_long	memleft;	/* virtual memory available */
87 extern caddr_t	membase;	/* start address of memory based filesystem */
88 extern caddr_t	malloc(), calloc();
89 
90 union {
91 	struct fs fs;
92 	char pad[SBSIZE];
93 } fsun;
94 #define	sblock	fsun.fs
95 struct	csum *fscs;
96 
97 union {
98 	struct cg cg;
99 	char pad[MAXBSIZE];
100 } cgun;
101 #define	acg	cgun.cg
102 
103 struct dinode zino[MAXBSIZE / sizeof(struct dinode)];
104 
105 int	fsi, fso;
106 time_t	utime;
107 daddr_t	alloc();
108 
109 mkfs(pp, fsys, fi, fo)
110 	struct partition *pp;
111 	char *fsys;
112 	int fi, fo;
113 {
114 	register long i, mincpc, mincpg, inospercg;
115 	long cylno, rpos, blk, j, warn = 0;
116 	long used, mincpgcnt, bpcg;
117 	long mapcramped, inodecramped;
118 	long postblsize, rotblsize, totalsbsize;
119 	int ppid, status, started();
120 
121 #ifndef STANDALONE
122 	time(&utime);
123 #endif
124 	if (mfs) {
125 		ppid = getpid();
126 		(void) signal(SIGUSR1, started);
127 		if (i = fork()) {
128 			if (i == -1) {
129 				perror("mfs");
130 				exit(10);
131 			}
132 			if (waitpid(i, &status, 0) != -1 && WIFEXITED(status))
133 				exit(WEXITSTATUS(status));
134 			exit(11);
135 			/* NOTREACHED */
136 		}
137 		(void)malloc(0);
138 		if (fssize * sectorsize > memleft)
139 			fssize = (memleft - 16384) / sectorsize;
140 		if ((membase = malloc(fssize * sectorsize)) == 0)
141 			exit(12);
142 	}
143 	fsi = fi;
144 	fso = fo;
145 	/*
146 	 * Validate the given file system size.
147 	 * Verify that its last block can actually be accessed.
148 	 */
149 	if (fssize <= 0)
150 		printf("preposterous size %d\n", fssize), exit(13);
151 	wtfs(fssize - 1, sectorsize, (char *)&sblock);
152 	/*
153 	 * collect and verify the sector and track info
154 	 */
155 	sblock.fs_nsect = nsectors;
156 	sblock.fs_ntrak = ntracks;
157 	if (sblock.fs_ntrak <= 0)
158 		printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
159 	if (sblock.fs_nsect <= 0)
160 		printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
161 	/*
162 	 * collect and verify the block and fragment sizes
163 	 */
164 	sblock.fs_bsize = bsize;
165 	sblock.fs_fsize = fsize;
166 	if (!POWEROF2(sblock.fs_bsize)) {
167 		printf("block size must be a power of 2, not %d\n",
168 		    sblock.fs_bsize);
169 		exit(16);
170 	}
171 	if (!POWEROF2(sblock.fs_fsize)) {
172 		printf("fragment size must be a power of 2, not %d\n",
173 		    sblock.fs_fsize);
174 		exit(17);
175 	}
176 	if (sblock.fs_fsize < sectorsize) {
177 		printf("fragment size %d is too small, minimum is %d\n",
178 		    sblock.fs_fsize, sectorsize);
179 		exit(18);
180 	}
181 	if (sblock.fs_bsize < MINBSIZE) {
182 		printf("block size %d is too small, minimum is %d\n",
183 		    sblock.fs_bsize, MINBSIZE);
184 		exit(19);
185 	}
186 	if (sblock.fs_bsize < sblock.fs_fsize) {
187 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
188 		    sblock.fs_bsize, sblock.fs_fsize);
189 		exit(20);
190 	}
191 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
192 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
193 	/*
194 	 * Planning now for future expansion.
195 	 */
196 #	if (BYTE_ORDER == BIG_ENDIAN)
197 		sblock.fs_qbmask.val[0] = 0;
198 		sblock.fs_qbmask.val[1] = ~sblock.fs_bmask;
199 		sblock.fs_qfmask.val[0] = 0;
200 		sblock.fs_qfmask.val[1] = ~sblock.fs_fmask;
201 #	endif /* BIG_ENDIAN */
202 #	if (BYTE_ORDER == LITTLE_ENDIAN)
203 		sblock.fs_qbmask.val[0] = ~sblock.fs_bmask;
204 		sblock.fs_qbmask.val[1] = 0;
205 		sblock.fs_qfmask.val[0] = ~sblock.fs_fmask;
206 		sblock.fs_qfmask.val[1] = 0;
207 #	endif /* LITTLE_ENDIAN */
208 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
209 		sblock.fs_bshift++;
210 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
211 		sblock.fs_fshift++;
212 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
213 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
214 		sblock.fs_fragshift++;
215 	if (sblock.fs_frag > MAXFRAG) {
216 		printf("fragment size %d is too small, minimum with block size %d is %d\n",
217 		    sblock.fs_fsize, sblock.fs_bsize,
218 		    sblock.fs_bsize / MAXFRAG);
219 		exit(21);
220 	}
221 	sblock.fs_nrpos = nrpos;
222 	sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
223 	sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode);
224 	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
225 	for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
226 		sblock.fs_fsbtodb++;
227 	sblock.fs_sblkno =
228 	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
229 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
230 	    roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
231 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
232 	sblock.fs_cgoffset = roundup(
233 	    howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
234 	for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
235 		sblock.fs_cgmask <<= 1;
236 	if (!POWEROF2(sblock.fs_ntrak))
237 		sblock.fs_cgmask <<= 1;
238 	/*
239 	 * Validate specified/determined secpercyl
240 	 * and calculate minimum cylinders per group.
241 	 */
242 	sblock.fs_spc = secpercyl;
243 	for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
244 	     sblock.fs_cpc > 1 && (i & 1) == 0;
245 	     sblock.fs_cpc >>= 1, i >>= 1)
246 		/* void */;
247 	mincpc = sblock.fs_cpc;
248 	bpcg = sblock.fs_spc * sectorsize;
249 	inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock));
250 	if (inospercg > MAXIPG(&sblock))
251 		inospercg = MAXIPG(&sblock);
252 	used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
253 	mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
254 	    sblock.fs_spc);
255 	mincpg = roundup(mincpgcnt, mincpc);
256 	/*
257 	 * Insure that cylinder group with mincpg has enough space
258 	 * for block maps
259 	 */
260 	sblock.fs_cpg = mincpg;
261 	sblock.fs_ipg = inospercg;
262 	mapcramped = 0;
263 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
264 		mapcramped = 1;
265 		if (sblock.fs_bsize < MAXBSIZE) {
266 			sblock.fs_bsize <<= 1;
267 			if ((i & 1) == 0) {
268 				i >>= 1;
269 			} else {
270 				sblock.fs_cpc <<= 1;
271 				mincpc <<= 1;
272 				mincpg = roundup(mincpgcnt, mincpc);
273 				sblock.fs_cpg = mincpg;
274 			}
275 			sblock.fs_frag <<= 1;
276 			sblock.fs_fragshift += 1;
277 			if (sblock.fs_frag <= MAXFRAG)
278 				continue;
279 		}
280 		if (sblock.fs_fsize == sblock.fs_bsize) {
281 			printf("There is no block size that");
282 			printf(" can support this disk\n");
283 			exit(22);
284 		}
285 		sblock.fs_frag >>= 1;
286 		sblock.fs_fragshift -= 1;
287 		sblock.fs_fsize <<= 1;
288 		sblock.fs_nspf <<= 1;
289 	}
290 	/*
291 	 * Insure that cylinder group with mincpg has enough space for inodes
292 	 */
293 	inodecramped = 0;
294 	used *= sectorsize;
295 	inospercg = roundup((mincpg * bpcg - used) / density, INOPB(&sblock));
296 	sblock.fs_ipg = inospercg;
297 	while (inospercg > MAXIPG(&sblock)) {
298 		inodecramped = 1;
299 		if (mincpc == 1 || sblock.fs_frag == 1 ||
300 		    sblock.fs_bsize == MINBSIZE)
301 			break;
302 		printf("With a block size of %d %s %d\n", sblock.fs_bsize,
303 		    "minimum bytes per inode is",
304 		    (mincpg * bpcg - used) / MAXIPG(&sblock) + 1);
305 		sblock.fs_bsize >>= 1;
306 		sblock.fs_frag >>= 1;
307 		sblock.fs_fragshift -= 1;
308 		mincpc >>= 1;
309 		sblock.fs_cpg = roundup(mincpgcnt, mincpc);
310 		if (CGSIZE(&sblock) > sblock.fs_bsize) {
311 			sblock.fs_bsize <<= 1;
312 			break;
313 		}
314 		mincpg = sblock.fs_cpg;
315 		inospercg =
316 		    roundup((mincpg * bpcg - used) / density, INOPB(&sblock));
317 		sblock.fs_ipg = inospercg;
318 	}
319 	if (inodecramped) {
320 		if (inospercg > MAXIPG(&sblock)) {
321 			printf("Minimum bytes per inode is %d\n",
322 			    (mincpg * bpcg - used) / MAXIPG(&sblock) + 1);
323 		} else if (!mapcramped) {
324 			printf("With %d bytes per inode, ", density);
325 			printf("minimum cylinders per group is %d\n", mincpg);
326 		}
327 	}
328 	if (mapcramped) {
329 		printf("With %d sectors per cylinder, ", sblock.fs_spc);
330 		printf("minimum cylinders per group is %d\n", mincpg);
331 	}
332 	if (inodecramped || mapcramped) {
333 		if (sblock.fs_bsize != bsize)
334 			printf("%s to be changed from %d to %d\n",
335 			    "This requires the block size",
336 			    bsize, sblock.fs_bsize);
337 		if (sblock.fs_fsize != fsize)
338 			printf("\t%s to be changed from %d to %d\n",
339 			    "and the fragment size",
340 			    fsize, sblock.fs_fsize);
341 		exit(23);
342 	}
343 	/*
344 	 * Calculate the number of cylinders per group
345 	 */
346 	sblock.fs_cpg = cpg;
347 	if (sblock.fs_cpg % mincpc != 0) {
348 		printf("%s groups must have a multiple of %d cylinders\n",
349 			cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
350 		sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
351 		if (!cpgflg)
352 			cpg = sblock.fs_cpg;
353 	}
354 	/*
355 	 * Must insure there is enough space for inodes
356 	 */
357 	sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
358 		INOPB(&sblock));
359 	while (sblock.fs_ipg > MAXIPG(&sblock)) {
360 		inodecramped = 1;
361 		sblock.fs_cpg -= mincpc;
362 		sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
363 			INOPB(&sblock));
364 	}
365 	/*
366 	 * Must insure there is enough space to hold block map
367 	 */
368 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
369 		mapcramped = 1;
370 		sblock.fs_cpg -= mincpc;
371 		sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
372 			INOPB(&sblock));
373 	}
374 	sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
375 	if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
376 		printf("panic (fs_cpg * fs_spc) % NSPF != 0");
377 		exit(24);
378 	}
379 	if (sblock.fs_cpg < mincpg) {
380 		printf("cylinder groups must have at least %d cylinders\n",
381 			mincpg);
382 		exit(25);
383 	} else if (sblock.fs_cpg != cpg) {
384 		if (!cpgflg)
385 			printf("Warning: ");
386 		else if (!mapcramped && !inodecramped)
387 			exit(26);
388 		if (mapcramped && inodecramped)
389 			printf("Block size and bytes per inode restrict");
390 		else if (mapcramped)
391 			printf("Block size restricts");
392 		else
393 			printf("Bytes per inode restrict");
394 		printf(" cylinders per group to %d.\n", sblock.fs_cpg);
395 		if (cpgflg)
396 			exit(27);
397 	}
398 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
399 	/*
400 	 * Now have size for file system and nsect and ntrak.
401 	 * Determine number of cylinders and blocks in the file system.
402 	 */
403 	sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
404 	sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
405 	if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
406 		sblock.fs_ncyl++;
407 		warn = 1;
408 	}
409 	if (sblock.fs_ncyl < 1) {
410 		printf("file systems must have at least one cylinder\n");
411 		exit(28);
412 	}
413 	/*
414 	 * Determine feasability/values of rotational layout tables.
415 	 *
416 	 * The size of the rotational layout tables is limited by the
417 	 * size of the superblock, SBSIZE. The amount of space available
418 	 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
419 	 * The size of these tables is inversely proportional to the block
420 	 * size of the file system. The size increases if sectors per track
421 	 * are not powers of two, because more cylinders must be described
422 	 * by the tables before the rotational pattern repeats (fs_cpc).
423 	 */
424 	sblock.fs_interleave = interleave;
425 	sblock.fs_trackskew = trackskew;
426 	sblock.fs_npsect = nphyssectors;
427 	sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
428 	sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
429 	if (sblock.fs_ntrak == 1) {
430 		sblock.fs_cpc = 0;
431 		goto next;
432 	}
433 	postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(short);
434 	rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
435 	totalsbsize = sizeof(struct fs) + rotblsize;
436 	if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
437 		/* use old static table space */
438 		sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
439 		    (char *)(&sblock.fs_link);
440 		sblock.fs_rotbloff = &sblock.fs_space[0] -
441 		    (u_char *)(&sblock.fs_link);
442 	} else {
443 		/* use dynamic table space */
444 		sblock.fs_postbloff = &sblock.fs_space[0] -
445 		    (u_char *)(&sblock.fs_link);
446 		sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
447 		totalsbsize += postblsize;
448 	}
449 	if (totalsbsize > SBSIZE ||
450 	    sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
451 		printf("%s %s %d %s %d.%s",
452 		    "Warning: insufficient space in super block for\n",
453 		    "rotational layout tables with nsect", sblock.fs_nsect,
454 		    "and ntrak", sblock.fs_ntrak,
455 		    "\nFile system performance may be impaired.\n");
456 		sblock.fs_cpc = 0;
457 		goto next;
458 	}
459 	sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
460 	/*
461 	 * calculate the available blocks for each rotational position
462 	 */
463 	for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
464 		for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
465 			fs_postbl(&sblock, cylno)[rpos] = -1;
466 	for (i = (rotblsize - 1) * sblock.fs_frag;
467 	     i >= 0; i -= sblock.fs_frag) {
468 		cylno = cbtocylno(&sblock, i);
469 		rpos = cbtorpos(&sblock, i);
470 		blk = fragstoblks(&sblock, i);
471 		if (fs_postbl(&sblock, cylno)[rpos] == -1)
472 			fs_rotbl(&sblock)[blk] = 0;
473 		else
474 			fs_rotbl(&sblock)[blk] =
475 			    fs_postbl(&sblock, cylno)[rpos] - blk;
476 		fs_postbl(&sblock, cylno)[rpos] = blk;
477 	}
478 next:
479 	/*
480 	 * Compute/validate number of cylinder groups.
481 	 */
482 	sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
483 	if (sblock.fs_ncyl % sblock.fs_cpg)
484 		sblock.fs_ncg++;
485 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
486 	i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
487 	if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
488 		printf("inode blocks/cyl group (%d) >= data blocks (%d)\n",
489 		    cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
490 		    sblock.fs_fpg / sblock.fs_frag);
491 		printf("number of cylinders per cylinder group (%d) %s.\n",
492 		    sblock.fs_cpg, "must be increased");
493 		exit(29);
494 	}
495 	j = sblock.fs_ncg - 1;
496 	if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
497 	    cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
498 		if (j == 0) {
499 			printf("Filesystem must have at least %d sectors\n",
500 			    NSPF(&sblock) *
501 			    (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
502 			exit(30);
503 		}
504 		printf("Warning: inode blocks/cyl group (%d) >= data blocks (%d) in last\n",
505 		    (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
506 		    i / sblock.fs_frag);
507 		printf("    cylinder group. This implies %d sector(s) cannot be allocated.\n",
508 		    i * NSPF(&sblock));
509 		sblock.fs_ncg--;
510 		sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
511 		sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
512 		    NSPF(&sblock);
513 		warn = 0;
514 	}
515 	if (warn && !mfs) {
516 		printf("Warning: %d sector(s) in last cylinder unallocated\n",
517 		    sblock.fs_spc -
518 		    (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
519 		    * sblock.fs_spc));
520 	}
521 	/*
522 	 * fill in remaining fields of the super block
523 	 */
524 	sblock.fs_csaddr = cgdmin(&sblock, 0);
525 	sblock.fs_cssize =
526 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
527 	i = sblock.fs_bsize / sizeof(struct csum);
528 	sblock.fs_csmask = ~(i - 1);
529 	for (sblock.fs_csshift = 0; i > 1; i >>= 1)
530 		sblock.fs_csshift++;
531 	fscs = (struct csum *)calloc(1, sblock.fs_cssize);
532 	sblock.fs_magic = FS_MAGIC;
533 	sblock.fs_rotdelay = rotdelay;
534 	sblock.fs_minfree = minfree;
535 	sblock.fs_maxcontig = maxcontig;
536 	sblock.fs_headswitch = headswitch;
537 	sblock.fs_trkseek = trackseek;
538 	sblock.fs_maxbpg = maxbpg;
539 	sblock.fs_rps = rpm / 60;
540 	sblock.fs_optim = opt;
541 	sblock.fs_cgrotor = 0;
542 	sblock.fs_cstotal.cs_ndir = 0;
543 	sblock.fs_cstotal.cs_nbfree = 0;
544 	sblock.fs_cstotal.cs_nifree = 0;
545 	sblock.fs_cstotal.cs_nffree = 0;
546 	sblock.fs_fmod = 0;
547 	sblock.fs_ronly = 0;
548 	/*
549 	 * Dump out summary information about file system.
550 	 */
551 	if (!mfs) {
552 		printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
553 		    fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
554 		    "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
555 		printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
556 		    (float)sblock.fs_size * sblock.fs_fsize * 1e-6,
557 		    sblock.fs_ncg, sblock.fs_cpg,
558 		    (float)sblock.fs_fpg * sblock.fs_fsize * 1e-6,
559 		    sblock.fs_ipg);
560 	}
561 	/*
562 	 * Now build the cylinders group blocks and
563 	 * then print out indices of cylinder groups.
564 	 */
565 	if (!mfs)
566 		printf("super-block backups (for fsck -b #) at:");
567 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
568 		initcg(cylno);
569 		if (mfs)
570 			continue;
571 		if (cylno % 9 == 0)
572 			printf("\n");
573 		printf(" %d,", fsbtodb(&sblock, cgsblock(&sblock, cylno)));
574 	}
575 	if (!mfs)
576 		printf("\n");
577 	if (Nflag && !mfs)
578 		exit(0);
579 	/*
580 	 * Now construct the initial file system,
581 	 * then write out the super-block.
582 	 */
583 	fsinit();
584 	sblock.fs_time = utime;
585 	wtfs(SBOFF / sectorsize, sbsize, (char *)&sblock);
586 	for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
587 		wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
588 			sblock.fs_cssize - i < sblock.fs_bsize ?
589 			    sblock.fs_cssize - i : sblock.fs_bsize,
590 			((char *)fscs) + i);
591 	/*
592 	 * Write out the duplicate super blocks
593 	 */
594 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
595 		wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
596 		    sbsize, (char *)&sblock);
597 	/*
598 	 * Update information about this partion in pack
599 	 * label, to that it may be updated on disk.
600 	 */
601 	pp->p_fstype = FS_BSDFFS;
602 	pp->p_fsize = sblock.fs_fsize;
603 	pp->p_frag = sblock.fs_frag;
604 	pp->p_cpg = sblock.fs_cpg;
605 	/*
606 	 * Notify parent process of success.
607 	 */
608 	if (mfs)
609 		kill(ppid, SIGUSR1);
610 }
611 
612 /*
613  * Initialize a cylinder group.
614  */
615 initcg(cylno)
616 	int cylno;
617 {
618 	daddr_t cbase, d, dlower, dupper, dmax;
619 	long i, j, s;
620 	register struct csum *cs;
621 
622 	/*
623 	 * Determine block bounds for cylinder group.
624 	 * Allow space for super block summary information in first
625 	 * cylinder group.
626 	 */
627 	cbase = cgbase(&sblock, cylno);
628 	dmax = cbase + sblock.fs_fpg;
629 	if (dmax > sblock.fs_size)
630 		dmax = sblock.fs_size;
631 	dlower = cgsblock(&sblock, cylno) - cbase;
632 	dupper = cgdmin(&sblock, cylno) - cbase;
633 	if (cylno == 0)
634 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
635 	cs = fscs + cylno;
636 	acg.cg_time = utime;
637 	acg.cg_magic = CG_MAGIC;
638 	acg.cg_cgx = cylno;
639 	if (cylno == sblock.fs_ncg - 1)
640 		acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
641 	else
642 		acg.cg_ncyl = sblock.fs_cpg;
643 	acg.cg_niblk = sblock.fs_ipg;
644 	acg.cg_ndblk = dmax - cbase;
645 	acg.cg_cs.cs_ndir = 0;
646 	acg.cg_cs.cs_nffree = 0;
647 	acg.cg_cs.cs_nbfree = 0;
648 	acg.cg_cs.cs_nifree = 0;
649 	acg.cg_rotor = 0;
650 	acg.cg_frotor = 0;
651 	acg.cg_irotor = 0;
652 	acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_link);
653 	acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(long);
654 	acg.cg_iusedoff = acg.cg_boff +
655 		sblock.fs_cpg * sblock.fs_nrpos * sizeof(short);
656 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
657 	acg.cg_nextfreeoff = acg.cg_freeoff +
658 		howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
659 	for (i = 0; i < sblock.fs_frag; i++) {
660 		acg.cg_frsum[i] = 0;
661 	}
662 	bzero((caddr_t)cg_inosused(&acg), acg.cg_freeoff - acg.cg_iusedoff);
663 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
664 	if (cylno == 0)
665 		for (i = 0; i < ROOTINO; i++) {
666 			setbit(cg_inosused(&acg), i);
667 			acg.cg_cs.cs_nifree--;
668 		}
669 	for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
670 		wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
671 		    sblock.fs_bsize, (char *)zino);
672 	bzero((caddr_t)cg_blktot(&acg), acg.cg_boff - acg.cg_btotoff);
673 	bzero((caddr_t)cg_blks(&sblock, &acg, 0),
674 	    acg.cg_iusedoff - acg.cg_boff);
675 	bzero((caddr_t)cg_blksfree(&acg), acg.cg_nextfreeoff - acg.cg_freeoff);
676 	if (cylno > 0) {
677 		/*
678 		 * In cylno 0, beginning space is reserved
679 		 * for boot and super blocks.
680 		 */
681 		for (d = 0; d < dlower; d += sblock.fs_frag) {
682 			setblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
683 			acg.cg_cs.cs_nbfree++;
684 			cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
685 			cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
686 			    [cbtorpos(&sblock, d)]++;
687 		}
688 		sblock.fs_dsize += dlower;
689 	}
690 	sblock.fs_dsize += acg.cg_ndblk - dupper;
691 	if (i = dupper % sblock.fs_frag) {
692 		acg.cg_frsum[sblock.fs_frag - i]++;
693 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
694 			setbit(cg_blksfree(&acg), dupper);
695 			acg.cg_cs.cs_nffree++;
696 		}
697 	}
698 	for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
699 		setblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag);
700 		acg.cg_cs.cs_nbfree++;
701 		cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
702 		cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
703 		    [cbtorpos(&sblock, d)]++;
704 		d += sblock.fs_frag;
705 	}
706 	if (d < dmax - cbase) {
707 		acg.cg_frsum[dmax - cbase - d]++;
708 		for (; d < dmax - cbase; d++) {
709 			setbit(cg_blksfree(&acg), d);
710 			acg.cg_cs.cs_nffree++;
711 		}
712 	}
713 	sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
714 	sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
715 	sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
716 	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
717 	*cs = acg.cg_cs;
718 	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
719 		sblock.fs_bsize, (char *)&acg);
720 }
721 
722 /*
723  * initialize the file system
724  */
725 struct dinode node;
726 
727 #ifdef LOSTDIR
728 #define PREDEFDIR 3
729 #else
730 #define PREDEFDIR 2
731 #endif
732 
733 struct direct root_dir[] = {
734 	{ ROOTINO, sizeof(struct direct), 1, "." },
735 	{ ROOTINO, sizeof(struct direct), 2, ".." },
736 #ifdef LOSTDIR
737 	{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
738 #endif
739 };
740 #ifdef LOSTDIR
741 struct direct lost_found_dir[] = {
742 	{ LOSTFOUNDINO, sizeof(struct direct), 1, "." },
743 	{ ROOTINO, sizeof(struct direct), 2, ".." },
744 	{ 0, DIRBLKSIZ, 0, 0 },
745 };
746 #endif
747 char buf[MAXBSIZE];
748 
749 fsinit()
750 {
751 	int i;
752 
753 	/*
754 	 * initialize the node
755 	 */
756 	node.di_atime = utime;
757 	node.di_mtime = utime;
758 	node.di_ctime = utime;
759 #ifdef LOSTDIR
760 	/*
761 	 * create the lost+found directory
762 	 */
763 	(void)makedir(lost_found_dir, 2);
764 	for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
765 		bcopy(&lost_found_dir[2], &buf[i], DIRSIZ(&lost_found_dir[2]));
766 	node.di_mode = IFDIR | UMASK;
767 	node.di_nlink = 2;
768 	node.di_size = sblock.fs_bsize;
769 	node.di_db[0] = alloc(node.di_size, node.di_mode);
770 	node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
771 	wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
772 	iput(&node, LOSTFOUNDINO);
773 #endif
774 	/*
775 	 * create the root directory
776 	 */
777 	if (mfs)
778 		node.di_mode = IFDIR | 01777;
779 	else
780 		node.di_mode = IFDIR | UMASK;
781 	node.di_nlink = PREDEFDIR;
782 	node.di_size = makedir(root_dir, PREDEFDIR);
783 	node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
784 	node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
785 	wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
786 	iput(&node, ROOTINO);
787 }
788 
789 /*
790  * construct a set of directory entries in "buf".
791  * return size of directory.
792  */
793 makedir(protodir, entries)
794 	register struct direct *protodir;
795 	int entries;
796 {
797 	char *cp;
798 	int i, spcleft;
799 
800 	spcleft = DIRBLKSIZ;
801 	for (cp = buf, i = 0; i < entries - 1; i++) {
802 		protodir[i].d_reclen = DIRSIZ(&protodir[i]);
803 		bcopy(&protodir[i], cp, protodir[i].d_reclen);
804 		cp += protodir[i].d_reclen;
805 		spcleft -= protodir[i].d_reclen;
806 	}
807 	protodir[i].d_reclen = spcleft;
808 	bcopy(&protodir[i], cp, DIRSIZ(&protodir[i]));
809 	return (DIRBLKSIZ);
810 }
811 
812 /*
813  * allocate a block or frag
814  */
815 daddr_t
816 alloc(size, mode)
817 	int size;
818 	int mode;
819 {
820 	int i, frag;
821 	daddr_t d;
822 
823 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
824 	    (char *)&acg);
825 	if (acg.cg_magic != CG_MAGIC) {
826 		printf("cg 0: bad magic number\n");
827 		return (0);
828 	}
829 	if (acg.cg_cs.cs_nbfree == 0) {
830 		printf("first cylinder group ran out of space\n");
831 		return (0);
832 	}
833 	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
834 		if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
835 			goto goth;
836 	printf("internal error: can't find block in cyl 0\n");
837 	return (0);
838 goth:
839 	clrblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag);
840 	acg.cg_cs.cs_nbfree--;
841 	sblock.fs_cstotal.cs_nbfree--;
842 	fscs[0].cs_nbfree--;
843 	if (mode & IFDIR) {
844 		acg.cg_cs.cs_ndir++;
845 		sblock.fs_cstotal.cs_ndir++;
846 		fscs[0].cs_ndir++;
847 	}
848 	cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
849 	cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
850 	if (size != sblock.fs_bsize) {
851 		frag = howmany(size, sblock.fs_fsize);
852 		fscs[0].cs_nffree += sblock.fs_frag - frag;
853 		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
854 		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
855 		acg.cg_frsum[sblock.fs_frag - frag]++;
856 		for (i = frag; i < sblock.fs_frag; i++)
857 			setbit(cg_blksfree(&acg), d + i);
858 	}
859 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
860 	    (char *)&acg);
861 	return (d);
862 }
863 
864 /*
865  * Allocate an inode on the disk
866  */
867 iput(ip, ino)
868 	register struct dinode *ip;
869 	register ino_t ino;
870 {
871 	struct dinode buf[MAXINOPB];
872 	daddr_t d;
873 	int c;
874 
875 	c = itog(&sblock, ino);
876 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
877 	    (char *)&acg);
878 	if (acg.cg_magic != CG_MAGIC) {
879 		printf("cg 0: bad magic number\n");
880 		exit(31);
881 	}
882 	acg.cg_cs.cs_nifree--;
883 	setbit(cg_inosused(&acg), ino);
884 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
885 	    (char *)&acg);
886 	sblock.fs_cstotal.cs_nifree--;
887 	fscs[0].cs_nifree--;
888 	if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
889 		printf("fsinit: inode value out of range (%d).\n", ino);
890 		exit(32);
891 	}
892 	d = fsbtodb(&sblock, itod(&sblock, ino));
893 	rdfs(d, sblock.fs_bsize, buf);
894 	buf[itoo(&sblock, ino)] = *ip;
895 	wtfs(d, sblock.fs_bsize, buf);
896 }
897 
898 /*
899  * Notify parent process that the filesystem has created itself successfully.
900  */
901 started()
902 {
903 
904 	exit(0);
905 }
906 
907 /*
908  * Replace libc function with one suited to our needs.
909  */
910 caddr_t
911 malloc(size)
912 	register u_long size;
913 {
914 	u_long base, i;
915 	static u_long pgsz;
916 	struct rlimit rlp;
917 
918 	if (pgsz == 0) {
919 		base = sbrk(0);
920 		pgsz = getpagesize() - 1;
921 		i = (base + pgsz) &~ pgsz;
922 		base = sbrk(i - base);
923 		if (getrlimit(RLIMIT_DATA, &rlp) < 0)
924 			perror("getrlimit");
925 		rlp.rlim_cur = rlp.rlim_max;
926 		if (setrlimit(RLIMIT_DATA, &rlp) < 0)
927 			perror("setrlimit");
928 		memleft = rlp.rlim_max - base;
929 	}
930 	size = (size + pgsz) &~ pgsz;
931 	if (size > memleft)
932 		size = memleft;
933 	memleft -= size;
934 	if (size == 0)
935 		return (0);
936 	return ((caddr_t)sbrk(size));
937 }
938 
939 /*
940  * Replace libc function with one suited to our needs.
941  */
942 caddr_t
943 realloc(ptr, size)
944 	char *ptr;
945 	u_long size;
946 {
947 
948 	/* always fail for now */
949 	return ((caddr_t)0);
950 }
951 
952 /*
953  * Replace libc function with one suited to our needs.
954  */
955 char *
956 calloc(size, numelm)
957 	u_long size, numelm;
958 {
959 	caddr_t base;
960 
961 	size *= numelm;
962 	base = malloc(size);
963 	bzero(base, size);
964 	return (base);
965 }
966 
967 /*
968  * Replace libc function with one suited to our needs.
969  */
970 free(ptr)
971 	char *ptr;
972 {
973 
974 	/* do not worry about it for now */
975 }
976 
977 /*
978  * read a block from the file system
979  */
980 rdfs(bno, size, bf)
981 	daddr_t bno;
982 	int size;
983 	char *bf;
984 {
985 	int n;
986 
987 	if (mfs) {
988 		bcopy(membase + bno * sectorsize, bf, size);
989 		return;
990 	}
991 	if (lseek(fsi, bno * sectorsize, 0) < 0) {
992 		printf("seek error: %ld\n", bno);
993 		perror("rdfs");
994 		exit(33);
995 	}
996 	n = read(fsi, bf, size);
997 	if(n != size) {
998 		printf("read error: %ld\n", bno);
999 		perror("rdfs");
1000 		exit(34);
1001 	}
1002 }
1003 
1004 /*
1005  * write a block to the file system
1006  */
1007 wtfs(bno, size, bf)
1008 	daddr_t bno;
1009 	int size;
1010 	char *bf;
1011 {
1012 	int n;
1013 
1014 	if (mfs) {
1015 		bcopy(bf, membase + bno * sectorsize, size);
1016 		return;
1017 	}
1018 	if (Nflag)
1019 		return;
1020 	if (lseek(fso, bno * sectorsize, 0) < 0) {
1021 		printf("seek error: %ld\n", bno);
1022 		perror("wtfs");
1023 		exit(35);
1024 	}
1025 	n = write(fso, bf, size);
1026 	if(n != size) {
1027 		printf("write error: %ld\n", bno);
1028 		perror("wtfs");
1029 		exit(36);
1030 	}
1031 }
1032 
1033 /*
1034  * check if a block is available
1035  */
1036 isblock(fs, cp, h)
1037 	struct fs *fs;
1038 	unsigned char *cp;
1039 	int h;
1040 {
1041 	unsigned char mask;
1042 
1043 	switch (fs->fs_frag) {
1044 	case 8:
1045 		return (cp[h] == 0xff);
1046 	case 4:
1047 		mask = 0x0f << ((h & 0x1) << 2);
1048 		return ((cp[h >> 1] & mask) == mask);
1049 	case 2:
1050 		mask = 0x03 << ((h & 0x3) << 1);
1051 		return ((cp[h >> 2] & mask) == mask);
1052 	case 1:
1053 		mask = 0x01 << (h & 0x7);
1054 		return ((cp[h >> 3] & mask) == mask);
1055 	default:
1056 #ifdef STANDALONE
1057 		printf("isblock bad fs_frag %d\n", fs->fs_frag);
1058 #else
1059 		fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1060 #endif
1061 		return (0);
1062 	}
1063 }
1064 
1065 /*
1066  * take a block out of the map
1067  */
1068 clrblock(fs, cp, h)
1069 	struct fs *fs;
1070 	unsigned char *cp;
1071 	int h;
1072 {
1073 	switch ((fs)->fs_frag) {
1074 	case 8:
1075 		cp[h] = 0;
1076 		return;
1077 	case 4:
1078 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1079 		return;
1080 	case 2:
1081 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1082 		return;
1083 	case 1:
1084 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
1085 		return;
1086 	default:
1087 #ifdef STANDALONE
1088 		printf("clrblock bad fs_frag %d\n", fs->fs_frag);
1089 #else
1090 		fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1091 #endif
1092 		return;
1093 	}
1094 }
1095 
1096 /*
1097  * put a block into the map
1098  */
1099 setblock(fs, cp, h)
1100 	struct fs *fs;
1101 	unsigned char *cp;
1102 	int h;
1103 {
1104 	switch (fs->fs_frag) {
1105 	case 8:
1106 		cp[h] = 0xff;
1107 		return;
1108 	case 4:
1109 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1110 		return;
1111 	case 2:
1112 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1113 		return;
1114 	case 1:
1115 		cp[h >> 3] |= (0x01 << (h & 0x7));
1116 		return;
1117 	default:
1118 #ifdef STANDALONE
1119 		printf("setblock bad fs_frag %d\n", fs->fs_frag);
1120 #else
1121 		fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1122 #endif
1123 		return;
1124 	}
1125 }
1126