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