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