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