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