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