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