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