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