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