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