mkfs.c revision 1.20 1 /* $NetBSD: mkfs.c,v 1.20 1995/03/21 01:28:05 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.20 1995/03/21 01:28:05 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_ronly = 0;
573 /*
574 * Dump out summary information about file system.
575 */
576 if (!mfs) {
577 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
578 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
579 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
580 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
581 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
582 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
583 sblock.fs_ncg, sblock.fs_cpg,
584 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
585 sblock.fs_ipg);
586 #undef B2MBFACTOR
587 }
588 /*
589 * Now build the cylinders group blocks and
590 * then print out indices of cylinder groups.
591 */
592 if (!mfs)
593 printf("super-block backups (for fsck -b #) at:");
594 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
595 initcg(cylno, utime);
596 if (mfs)
597 continue;
598 if (cylno % 8 == 0)
599 printf("\n");
600 printf(" %d,", fsbtodb(&sblock, cgsblock(&sblock, cylno)));
601 fflush(stderr);
602 }
603 if (!mfs)
604 printf("\n");
605 if (Nflag && !mfs)
606 exit(0);
607 /*
608 * Now construct the initial file system,
609 * then write out the super-block.
610 */
611 fsinit(utime);
612 sblock.fs_time = utime;
613 wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock);
614 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
615 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
616 sblock.fs_cssize - i < sblock.fs_bsize ?
617 sblock.fs_cssize - i : sblock.fs_bsize,
618 ((char *)fscs) + i);
619 /*
620 * Write out the duplicate super blocks
621 */
622 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
623 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
624 sbsize, (char *)&sblock);
625 /*
626 * Update information about this partion in pack
627 * label, to that it may be updated on disk.
628 */
629 pp->p_fstype = FS_BSDFFS;
630 pp->p_fsize = sblock.fs_fsize;
631 pp->p_frag = sblock.fs_frag;
632 pp->p_cpg = sblock.fs_cpg;
633 /*
634 * Notify parent process of success.
635 * Dissociate from session and tty.
636 */
637 if (mfs) {
638 kill(ppid, SIGUSR1);
639 (void) setsid();
640 (void) close(0);
641 (void) close(1);
642 (void) close(2);
643 (void) chdir("/");
644 }
645 }
646
647 /*
648 * Initialize a cylinder group.
649 */
650 initcg(cylno, utime)
651 int cylno;
652 time_t utime;
653 {
654 daddr_t cbase, d, dlower, dupper, dmax, blkno;
655 long i, j, s;
656 register struct csum *cs;
657
658 /*
659 * Determine block bounds for cylinder group.
660 * Allow space for super block summary information in first
661 * cylinder group.
662 */
663 cbase = cgbase(&sblock, cylno);
664 dmax = cbase + sblock.fs_fpg;
665 if (dmax > sblock.fs_size)
666 dmax = sblock.fs_size;
667 dlower = cgsblock(&sblock, cylno) - cbase;
668 dupper = cgdmin(&sblock, cylno) - cbase;
669 if (cylno == 0)
670 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
671 cs = fscs + cylno;
672 memset(&acg, 0, sblock.fs_cgsize);
673 acg.cg_time = utime;
674 acg.cg_magic = CG_MAGIC;
675 acg.cg_cgx = cylno;
676 if (cylno == sblock.fs_ncg - 1)
677 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
678 else
679 acg.cg_ncyl = sblock.fs_cpg;
680 acg.cg_niblk = sblock.fs_ipg;
681 acg.cg_ndblk = dmax - cbase;
682 if (sblock.fs_contigsumsize > 0)
683 acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
684 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
685 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
686 acg.cg_iusedoff = acg.cg_boff +
687 sblock.fs_cpg * sblock.fs_nrpos * sizeof(int16_t);
688 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
689 if (sblock.fs_contigsumsize <= 0) {
690 acg.cg_nextfreeoff = acg.cg_freeoff +
691 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
692 } else {
693 acg.cg_clustersumoff = acg.cg_freeoff + howmany
694 (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
695 sizeof(int32_t);
696 acg.cg_clustersumoff =
697 roundup(acg.cg_clustersumoff, sizeof(int32_t));
698 acg.cg_clusteroff = acg.cg_clustersumoff +
699 (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
700 acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
701 (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
702 }
703 if (acg.cg_nextfreeoff - (long)(&acg.cg_firstfield) > sblock.fs_cgsize) {
704 printf("Panic: cylinder group too big\n");
705 exit(37);
706 }
707 acg.cg_cs.cs_nifree += sblock.fs_ipg;
708 if (cylno == 0)
709 for (i = 0; i < ROOTINO; i++) {
710 setbit(cg_inosused(&acg), i);
711 acg.cg_cs.cs_nifree--;
712 }
713 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
714 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
715 sblock.fs_bsize, (char *)zino);
716 if (cylno > 0) {
717 /*
718 * In cylno 0, beginning space is reserved
719 * for boot and super blocks.
720 */
721 for (d = 0; d < dlower; d += sblock.fs_frag) {
722 blkno = d / sblock.fs_frag;
723 setblock(&sblock, cg_blksfree(&acg), blkno);
724 if (sblock.fs_contigsumsize > 0)
725 setbit(cg_clustersfree(&acg), blkno);
726 acg.cg_cs.cs_nbfree++;
727 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
728 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
729 [cbtorpos(&sblock, d)]++;
730 }
731 sblock.fs_dsize += dlower;
732 }
733 sblock.fs_dsize += acg.cg_ndblk - dupper;
734 if (i = dupper % sblock.fs_frag) {
735 acg.cg_frsum[sblock.fs_frag - i]++;
736 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
737 setbit(cg_blksfree(&acg), dupper);
738 acg.cg_cs.cs_nffree++;
739 }
740 }
741 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
742 blkno = d / sblock.fs_frag;
743 setblock(&sblock, cg_blksfree(&acg), blkno);
744 if (sblock.fs_contigsumsize > 0)
745 setbit(cg_clustersfree(&acg), blkno);
746 acg.cg_cs.cs_nbfree++;
747 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
748 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
749 [cbtorpos(&sblock, d)]++;
750 d += sblock.fs_frag;
751 }
752 if (d < dmax - cbase) {
753 acg.cg_frsum[dmax - cbase - d]++;
754 for (; d < dmax - cbase; d++) {
755 setbit(cg_blksfree(&acg), d);
756 acg.cg_cs.cs_nffree++;
757 }
758 }
759 if (sblock.fs_contigsumsize > 0) {
760 int32_t *sump = cg_clustersum(&acg);
761 u_char *mapp = cg_clustersfree(&acg);
762 int map = *mapp++;
763 int bit = 1;
764 int run = 0;
765
766 for (i = 0; i < acg.cg_nclusterblks; i++) {
767 if ((map & bit) != 0) {
768 run++;
769 } else if (run != 0) {
770 if (run > sblock.fs_contigsumsize)
771 run = sblock.fs_contigsumsize;
772 sump[run]++;
773 run = 0;
774 }
775 if ((i & (NBBY - 1)) != (NBBY - 1)) {
776 bit <<= 1;
777 } else {
778 map = *mapp++;
779 bit = 1;
780 }
781 }
782 if (run != 0) {
783 if (run > sblock.fs_contigsumsize)
784 run = sblock.fs_contigsumsize;
785 sump[run]++;
786 }
787 }
788 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
789 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
790 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
791 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
792 *cs = acg.cg_cs;
793 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
794 sblock.fs_bsize, (char *)&acg);
795 }
796
797 /*
798 * initialize the file system
799 */
800 struct dinode node;
801
802 #ifdef LOSTDIR
803 #define PREDEFDIR 3
804 #else
805 #define PREDEFDIR 2
806 #endif
807
808 struct direct root_dir[] = {
809 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
810 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
811 #ifdef LOSTDIR
812 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
813 #endif
814 };
815 struct odirect {
816 u_int32_t d_ino;
817 u_int16_t d_reclen;
818 u_int16_t d_namlen;
819 u_char d_name[MAXNAMLEN + 1];
820 } oroot_dir[] = {
821 { ROOTINO, sizeof(struct direct), 1, "." },
822 { ROOTINO, sizeof(struct direct), 2, ".." },
823 #ifdef LOSTDIR
824 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
825 #endif
826 };
827 #ifdef LOSTDIR
828 struct direct lost_found_dir[] = {
829 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
830 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
831 { 0, DIRBLKSIZ, 0, 0, 0 },
832 };
833 struct odirect olost_found_dir[] = {
834 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
835 { ROOTINO, sizeof(struct direct), 2, ".." },
836 { 0, DIRBLKSIZ, 0, 0 },
837 };
838 #endif
839 char buf[MAXBSIZE];
840
841 fsinit(utime)
842 time_t utime;
843 {
844 int i;
845
846 /*
847 * initialize the node
848 */
849 node.di_atime.ts_sec = utime;
850 node.di_mtime.ts_sec = utime;
851 node.di_ctime.ts_sec = utime;
852 #ifdef LOSTDIR
853 /*
854 * create the lost+found directory
855 */
856 if (Oflag) {
857 (void)makedir((struct direct *)olost_found_dir, 2);
858 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
859 memcpy(&buf[i], &olost_found_dir[2],
860 DIRSIZ(0, &olost_found_dir[2]));
861 } else {
862 (void)makedir(lost_found_dir, 2);
863 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
864 memcpy(&buf[i], &lost_found_dir[2],
865 DIRSIZ(0, &lost_found_dir[2]));
866 }
867 node.di_mode = IFDIR | UMASK;
868 node.di_nlink = 2;
869 node.di_size = sblock.fs_bsize;
870 node.di_db[0] = alloc(node.di_size, node.di_mode);
871 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
872 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
873 iput(&node, LOSTFOUNDINO);
874 #endif
875 /*
876 * create the root directory
877 */
878 if (mfs)
879 node.di_mode = IFDIR | 01777;
880 else
881 node.di_mode = IFDIR | UMASK;
882 node.di_nlink = PREDEFDIR;
883 if (Oflag)
884 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
885 else
886 node.di_size = makedir(root_dir, PREDEFDIR);
887 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
888 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
889 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
890 iput(&node, ROOTINO);
891 }
892
893 /*
894 * construct a set of directory entries in "buf".
895 * return size of directory.
896 */
897 makedir(protodir, entries)
898 register struct direct *protodir;
899 int entries;
900 {
901 char *cp;
902 int i, spcleft;
903
904 spcleft = DIRBLKSIZ;
905 for (cp = buf, i = 0; i < entries - 1; i++) {
906 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
907 memcpy(cp, &protodir[i], protodir[i].d_reclen);
908 cp += protodir[i].d_reclen;
909 spcleft -= protodir[i].d_reclen;
910 }
911 protodir[i].d_reclen = spcleft;
912 memcpy(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
913 return (DIRBLKSIZ);
914 }
915
916 /*
917 * allocate a block or frag
918 */
919 daddr_t
920 alloc(size, mode)
921 int size;
922 int mode;
923 {
924 int i, frag;
925 daddr_t d, blkno;
926
927 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
928 (char *)&acg);
929 if (acg.cg_magic != CG_MAGIC) {
930 printf("cg 0: bad magic number\n");
931 return (0);
932 }
933 if (acg.cg_cs.cs_nbfree == 0) {
934 printf("first cylinder group ran out of space\n");
935 return (0);
936 }
937 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
938 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
939 goto goth;
940 printf("internal error: can't find block in cyl 0\n");
941 return (0);
942 goth:
943 blkno = fragstoblks(&sblock, d);
944 clrblock(&sblock, cg_blksfree(&acg), blkno);
945 if (sblock.fs_contigsumsize > 0)
946 clrbit(cg_clustersfree(&acg), blkno);
947 acg.cg_cs.cs_nbfree--;
948 sblock.fs_cstotal.cs_nbfree--;
949 fscs[0].cs_nbfree--;
950 if (mode & IFDIR) {
951 acg.cg_cs.cs_ndir++;
952 sblock.fs_cstotal.cs_ndir++;
953 fscs[0].cs_ndir++;
954 }
955 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
956 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
957 if (size != sblock.fs_bsize) {
958 frag = howmany(size, sblock.fs_fsize);
959 fscs[0].cs_nffree += sblock.fs_frag - frag;
960 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
961 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
962 acg.cg_frsum[sblock.fs_frag - frag]++;
963 for (i = frag; i < sblock.fs_frag; i++)
964 setbit(cg_blksfree(&acg), d + i);
965 }
966 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
967 (char *)&acg);
968 return (d);
969 }
970
971 /*
972 * Allocate an inode on the disk
973 */
974 iput(ip, ino)
975 register struct dinode *ip;
976 register ino_t ino;
977 {
978 struct dinode buf[MAXINOPB];
979 daddr_t d;
980 int c;
981
982 c = ino_to_cg(&sblock, ino);
983 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
984 (char *)&acg);
985 if (acg.cg_magic != CG_MAGIC) {
986 printf("cg 0: bad magic number\n");
987 exit(31);
988 }
989 acg.cg_cs.cs_nifree--;
990 setbit(cg_inosused(&acg), ino);
991 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
992 (char *)&acg);
993 sblock.fs_cstotal.cs_nifree--;
994 fscs[0].cs_nifree--;
995 if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
996 printf("fsinit: inode value out of range (%d).\n", ino);
997 exit(32);
998 }
999 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1000 rdfs(d, sblock.fs_bsize, buf);
1001 buf[ino_to_fsbo(&sblock, ino)] = *ip;
1002 wtfs(d, sblock.fs_bsize, buf);
1003 }
1004
1005 /*
1006 * Notify parent process that the filesystem has created itself successfully.
1007 */
1008 void
1009 started()
1010 {
1011
1012 exit(0);
1013 }
1014
1015 /*
1016 * Replace libc function with one suited to our needs.
1017 */
1018 caddr_t
1019 malloc(size)
1020 register u_long size;
1021 {
1022 char *base, *i;
1023 static u_long pgsz;
1024 struct rlimit rlp;
1025
1026 if (pgsz == 0) {
1027 base = sbrk(0);
1028 pgsz = getpagesize() - 1;
1029 i = (char *)((u_long)(base + pgsz) &~ pgsz);
1030 base = sbrk(i - base);
1031 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1032 perror("getrlimit");
1033 rlp.rlim_cur = rlp.rlim_max;
1034 if (setrlimit(RLIMIT_DATA, &rlp) < 0)
1035 perror("setrlimit");
1036 memleft = rlp.rlim_max - (u_long)base;
1037 }
1038 size = (size + pgsz) &~ pgsz;
1039 if (size > memleft)
1040 size = memleft;
1041 memleft -= size;
1042 if (size == 0)
1043 return (0);
1044 return ((caddr_t)sbrk(size));
1045 }
1046
1047 /*
1048 * Replace libc function with one suited to our needs.
1049 */
1050 caddr_t
1051 realloc(ptr, size)
1052 char *ptr;
1053 u_long size;
1054 {
1055 void *p;
1056
1057 if ((p = malloc(size)) == NULL)
1058 return (NULL);
1059 memcpy(p, ptr, size);
1060 free(ptr);
1061 return (p);
1062 }
1063
1064 /*
1065 * Replace libc function with one suited to our needs.
1066 */
1067 char *
1068 calloc(size, numelm)
1069 u_long size, numelm;
1070 {
1071 caddr_t base;
1072
1073 size *= numelm;
1074 base = malloc(size);
1075 memset(base, 0, size);
1076 return (base);
1077 }
1078
1079 /*
1080 * Replace libc function with one suited to our needs.
1081 */
1082 free(ptr)
1083 char *ptr;
1084 {
1085
1086 /* do not worry about it for now */
1087 }
1088
1089 /*
1090 * read a block from the file system
1091 */
1092 rdfs(bno, size, bf)
1093 daddr_t bno;
1094 int size;
1095 char *bf;
1096 {
1097 int n;
1098 off_t offset;
1099
1100 if (mfs) {
1101 memcpy(bf, membase + bno * sectorsize, size);
1102 return;
1103 }
1104 offset = bno;
1105 offset *= sectorsize;
1106 if (lseek(fsi, offset, SEEK_SET) < 0) {
1107 printf("seek error: %ld\n", bno);
1108 perror("rdfs");
1109 exit(33);
1110 }
1111 n = read(fsi, bf, size);
1112 if (n != size) {
1113 printf("read error: %ld\n", bno);
1114 perror("rdfs");
1115 exit(34);
1116 }
1117 }
1118
1119 /*
1120 * write a block to the file system
1121 */
1122 wtfs(bno, size, bf)
1123 daddr_t bno;
1124 int size;
1125 char *bf;
1126 {
1127 int n;
1128 off_t offset;
1129
1130 if (mfs) {
1131 memcpy(membase + bno * sectorsize, bf, size);
1132 return;
1133 }
1134 if (Nflag)
1135 return;
1136 offset = bno;
1137 offset *= sectorsize;
1138 if (lseek(fso, offset, SEEK_SET) < 0) {
1139 printf("seek error: %ld\n", bno);
1140 perror("wtfs");
1141 exit(35);
1142 }
1143 n = write(fso, bf, size);
1144 if (n != size) {
1145 printf("write error: %ld\n", bno);
1146 perror("wtfs");
1147 exit(36);
1148 }
1149 }
1150
1151 /*
1152 * check if a block is available
1153 */
1154 isblock(fs, cp, h)
1155 struct fs *fs;
1156 unsigned char *cp;
1157 int h;
1158 {
1159 unsigned char mask;
1160
1161 switch (fs->fs_frag) {
1162 case 8:
1163 return (cp[h] == 0xff);
1164 case 4:
1165 mask = 0x0f << ((h & 0x1) << 2);
1166 return ((cp[h >> 1] & mask) == mask);
1167 case 2:
1168 mask = 0x03 << ((h & 0x3) << 1);
1169 return ((cp[h >> 2] & mask) == mask);
1170 case 1:
1171 mask = 0x01 << (h & 0x7);
1172 return ((cp[h >> 3] & mask) == mask);
1173 default:
1174 #ifdef STANDALONE
1175 printf("isblock bad fs_frag %d\n", fs->fs_frag);
1176 #else
1177 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1178 #endif
1179 return (0);
1180 }
1181 }
1182
1183 /*
1184 * take a block out of the map
1185 */
1186 clrblock(fs, cp, h)
1187 struct fs *fs;
1188 unsigned char *cp;
1189 int h;
1190 {
1191 switch ((fs)->fs_frag) {
1192 case 8:
1193 cp[h] = 0;
1194 return;
1195 case 4:
1196 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1197 return;
1198 case 2:
1199 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1200 return;
1201 case 1:
1202 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1203 return;
1204 default:
1205 #ifdef STANDALONE
1206 printf("clrblock bad fs_frag %d\n", fs->fs_frag);
1207 #else
1208 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1209 #endif
1210 return;
1211 }
1212 }
1213
1214 /*
1215 * put a block into the map
1216 */
1217 setblock(fs, cp, h)
1218 struct fs *fs;
1219 unsigned char *cp;
1220 int h;
1221 {
1222 switch (fs->fs_frag) {
1223 case 8:
1224 cp[h] = 0xff;
1225 return;
1226 case 4:
1227 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1228 return;
1229 case 2:
1230 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1231 return;
1232 case 1:
1233 cp[h >> 3] |= (0x01 << (h & 0x7));
1234 return;
1235 default:
1236 #ifdef STANDALONE
1237 printf("setblock bad fs_frag %d\n", fs->fs_frag);
1238 #else
1239 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1240 #endif
1241 return;
1242 }
1243 }
1244