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