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