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