mkfs.c revision 1.35 1 /* $NetBSD: mkfs.c,v 1.35 1999/03/16 21:52:34 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.35 1999/03/16 21:52:34 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 node.di_uid = geteuid();
900 node.di_gid = getegid();
901 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
902 iput(&node, LOSTFOUNDINO);
903 #endif
904 /*
905 * create the root directory
906 */
907 if (mfs)
908 node.di_mode = IFDIR | 01777;
909 else
910 node.di_mode = IFDIR | UMASK;
911 node.di_nlink = PREDEFDIR;
912 if (Oflag)
913 node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
914 else
915 node.di_size = makedir(root_dir, PREDEFDIR);
916 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
917 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
918 node.di_uid = geteuid();
919 node.di_gid = getegid();
920 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
921 iput(&node, ROOTINO);
922 }
923
924 /*
925 * construct a set of directory entries in "buf".
926 * return size of directory.
927 */
928 int
929 makedir(protodir, entries)
930 struct direct *protodir;
931 int entries;
932 {
933 char *cp;
934 int i, spcleft;
935
936 spcleft = DIRBLKSIZ;
937 for (cp = buf, i = 0; i < entries - 1; i++) {
938 protodir[i].d_reclen = DIRSIZ(Oflag, &protodir[i], 0);
939 copy_dir(&protodir[i], (struct direct*)cp);
940 cp += protodir[i].d_reclen;
941 spcleft -= protodir[i].d_reclen;
942 }
943 protodir[i].d_reclen = spcleft;
944 copy_dir(&protodir[i], (struct direct*)cp);
945 return (DIRBLKSIZ);
946 }
947
948 /*
949 * allocate a block or frag
950 */
951 daddr_t
952 alloc(size, mode)
953 int size;
954 int mode;
955 {
956 int i, frag;
957 daddr_t d, blkno;
958
959 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
960 /* fs -> host byte order */
961 if (needswap)
962 swap_cg(&acg, &acg);
963 if (acg.cg_magic != CG_MAGIC) {
964 printf("cg 0: bad magic number\n");
965 return (0);
966 }
967 if (acg.cg_cs.cs_nbfree == 0) {
968 printf("first cylinder group ran out of space\n");
969 return (0);
970 }
971 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
972 if (isblock(&sblock, cg_blksfree(&acg, 0), d / sblock.fs_frag))
973 goto goth;
974 printf("internal error: can't find block in cyl 0\n");
975 return (0);
976 goth:
977 blkno = fragstoblks(&sblock, d);
978 clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
979 if (sblock.fs_contigsumsize > 0)
980 clrbit(cg_clustersfree(&acg, 0), blkno);
981 acg.cg_cs.cs_nbfree--;
982 sblock.fs_cstotal.cs_nbfree--;
983 fscs[0].cs_nbfree--;
984 if (mode & IFDIR) {
985 acg.cg_cs.cs_ndir++;
986 sblock.fs_cstotal.cs_ndir++;
987 fscs[0].cs_ndir++;
988 }
989 cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]--;
990 cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)[cbtorpos(&sblock, d)]--;
991 if (size != sblock.fs_bsize) {
992 frag = howmany(size, sblock.fs_fsize);
993 fscs[0].cs_nffree += sblock.fs_frag - frag;
994 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
995 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
996 acg.cg_frsum[sblock.fs_frag - frag]++;
997 for (i = frag; i < sblock.fs_frag; i++)
998 setbit(cg_blksfree(&acg, 0), d + i);
999 }
1000 /* host -> fs byte order */
1001 if (needswap)
1002 swap_cg(&acg, &acg);
1003 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1004 (char *)&acg);
1005 return (d);
1006 }
1007
1008 /*
1009 * Calculate number of inodes per group.
1010 */
1011 int32_t
1012 calcipg(cpg, bpcg, usedbp)
1013 int32_t cpg;
1014 int32_t bpcg;
1015 off_t *usedbp;
1016 {
1017 int i;
1018 int32_t ipg, new_ipg, ncg, ncyl;
1019 off_t usedb;
1020 #if __GNUC__ /* XXX work around gcc 2.7.2 initialization bug */
1021 (void)&usedb;
1022 #endif
1023
1024 /*
1025 * Prepare to scale by fssize / (number of sectors in cylinder groups).
1026 * Note that fssize is still in sectors, not filesystem blocks.
1027 */
1028 ncyl = howmany(fssize, secpercyl);
1029 ncg = howmany(ncyl, cpg);
1030 /*
1031 * Iterate a few times to allow for ipg depending on itself.
1032 */
1033 ipg = 0;
1034 for (i = 0; i < 10; i++) {
1035 usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
1036 * NSPF(&sblock) * (off_t)sectorsize;
1037 new_ipg = (cpg * (quad_t)bpcg - usedb) / density * fssize
1038 / ncg / secpercyl / cpg;
1039 new_ipg = roundup(new_ipg, INOPB(&sblock));
1040 if (new_ipg == ipg)
1041 break;
1042 ipg = new_ipg;
1043 }
1044 *usedbp = usedb;
1045 return (ipg);
1046 }
1047
1048 /*
1049 * Allocate an inode on the disk
1050 */
1051 static void
1052 iput(ip, ino)
1053 struct dinode *ip;
1054 ino_t ino;
1055 {
1056 struct dinode buf[MAXINOPB];
1057 daddr_t d;
1058 int c, i;
1059
1060 c = ino_to_cg(&sblock, ino);
1061 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1062 /* fs -> host byte order */
1063 if (needswap)
1064 swap_cg(&acg, &acg);
1065 if (acg.cg_magic != CG_MAGIC) {
1066 printf("cg 0: bad magic number\n");
1067 exit(31);
1068 }
1069 acg.cg_cs.cs_nifree--;
1070 setbit(cg_inosused(&acg, 0), ino);
1071 /* host -> fs byte order */
1072 if (needswap)
1073 swap_cg(&acg, &acg);
1074 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
1075 (char *)&acg);
1076 sblock.fs_cstotal.cs_nifree--;
1077 fscs[0].cs_nifree--;
1078 if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
1079 printf("fsinit: inode value out of range (%d).\n", ino);
1080 exit(32);
1081 }
1082 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1083 rdfs(d, sblock.fs_bsize, buf);
1084 if (needswap) {
1085 ffs_dinode_swap(ip, &buf[ino_to_fsbo(&sblock, ino)]);
1086 /* ffs_dinode_swap() doesn't swap blocks addrs */
1087 for (i=0; i<NDADDR + NIADDR; i++)
1088 (&buf[ino_to_fsbo(&sblock, ino)])->di_db[i] =
1089 bswap32(ip->di_db[i]);
1090 } else
1091 buf[ino_to_fsbo(&sblock, ino)] = *ip;
1092 wtfs(d, sblock.fs_bsize, buf);
1093 }
1094
1095 /*
1096 * Replace libc function with one suited to our needs.
1097 */
1098 void *
1099 malloc(size)
1100 size_t size;
1101 {
1102 char *base, *i;
1103 static u_long pgsz;
1104 struct rlimit rlp;
1105
1106 if (pgsz == 0) {
1107 base = sbrk(0);
1108 pgsz = getpagesize() - 1;
1109 i = (char *)((u_long)(base + pgsz) &~ pgsz);
1110 base = sbrk(i - base);
1111 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1112 perror("getrlimit");
1113 rlp.rlim_cur = rlp.rlim_max;
1114 if (setrlimit(RLIMIT_DATA, &rlp) < 0)
1115 perror("setrlimit");
1116 memleft = rlp.rlim_max - (u_long)base;
1117 }
1118 size = (size + pgsz) &~ pgsz;
1119 if (size > memleft)
1120 size = memleft;
1121 memleft -= size;
1122 if (size == 0)
1123 return (0);
1124 return ((caddr_t)sbrk(size));
1125 }
1126
1127 /*
1128 * Replace libc function with one suited to our needs.
1129 */
1130 void *
1131 realloc(ptr, size)
1132 void *ptr;
1133 size_t size;
1134 {
1135 void *p;
1136
1137 if ((p = malloc(size)) == NULL)
1138 return (NULL);
1139 memmove(p, ptr, size);
1140 free(ptr);
1141 return (p);
1142 }
1143
1144 /*
1145 * Replace libc function with one suited to our needs.
1146 */
1147 void *
1148 calloc(size, numelm)
1149 size_t size, numelm;
1150 {
1151 void *base;
1152
1153 size *= numelm;
1154 base = malloc(size);
1155 memset(base, 0, size);
1156 return (base);
1157 }
1158
1159 /*
1160 * Replace libc function with one suited to our needs.
1161 */
1162 void
1163 free(ptr)
1164 void *ptr;
1165 {
1166
1167 /* do not worry about it for now */
1168 }
1169
1170 /*
1171 * read a block from the file system
1172 */
1173 void
1174 rdfs(bno, size, bf)
1175 daddr_t bno;
1176 int size;
1177 void *bf;
1178 {
1179 int n;
1180 off_t offset;
1181
1182 if (mfs) {
1183 memmove(bf, membase + bno * sectorsize, size);
1184 return;
1185 }
1186 offset = bno;
1187 offset *= sectorsize;
1188 if (lseek(fsi, offset, SEEK_SET) < 0) {
1189 printf("seek error: %d\n", bno);
1190 perror("rdfs");
1191 exit(33);
1192 }
1193 n = read(fsi, bf, size);
1194 if (n != size) {
1195 printf("read error: %d\n", bno);
1196 perror("rdfs");
1197 exit(34);
1198 }
1199 }
1200
1201 /*
1202 * write a block to the file system
1203 */
1204 void
1205 wtfs(bno, size, bf)
1206 daddr_t bno;
1207 int size;
1208 void *bf;
1209 {
1210 int n;
1211 off_t offset;
1212
1213 if (mfs) {
1214 memmove(membase + bno * sectorsize, bf, size);
1215 return;
1216 }
1217 if (Nflag)
1218 return;
1219 offset = bno;
1220 offset *= sectorsize;
1221 if (lseek(fso, offset, SEEK_SET) < 0) {
1222 printf("seek error: %d\n", bno);
1223 perror("wtfs");
1224 exit(35);
1225 }
1226 n = write(fso, bf, size);
1227 if (n != size) {
1228 printf("write error: %d\n", bno);
1229 perror("wtfs");
1230 exit(36);
1231 }
1232 }
1233
1234 /*
1235 * check if a block is available
1236 */
1237 int
1238 isblock(fs, cp, h)
1239 struct fs *fs;
1240 unsigned char *cp;
1241 int h;
1242 {
1243 unsigned char mask;
1244
1245 switch (fs->fs_frag) {
1246 case 8:
1247 return (cp[h] == 0xff);
1248 case 4:
1249 mask = 0x0f << ((h & 0x1) << 2);
1250 return ((cp[h >> 1] & mask) == mask);
1251 case 2:
1252 mask = 0x03 << ((h & 0x3) << 1);
1253 return ((cp[h >> 2] & mask) == mask);
1254 case 1:
1255 mask = 0x01 << (h & 0x7);
1256 return ((cp[h >> 3] & mask) == mask);
1257 default:
1258 #ifdef STANDALONE
1259 printf("isblock bad fs_frag %d\n", fs->fs_frag);
1260 #else
1261 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1262 #endif
1263 return (0);
1264 }
1265 }
1266
1267 /*
1268 * take a block out of the map
1269 */
1270 void
1271 clrblock(fs, cp, h)
1272 struct fs *fs;
1273 unsigned char *cp;
1274 int h;
1275 {
1276 switch ((fs)->fs_frag) {
1277 case 8:
1278 cp[h] = 0;
1279 return;
1280 case 4:
1281 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1282 return;
1283 case 2:
1284 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1285 return;
1286 case 1:
1287 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1288 return;
1289 default:
1290 #ifdef STANDALONE
1291 printf("clrblock bad fs_frag %d\n", fs->fs_frag);
1292 #else
1293 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1294 #endif
1295 return;
1296 }
1297 }
1298
1299 /*
1300 * put a block into the map
1301 */
1302 void
1303 setblock(fs, cp, h)
1304 struct fs *fs;
1305 unsigned char *cp;
1306 int h;
1307 {
1308 switch (fs->fs_frag) {
1309 case 8:
1310 cp[h] = 0xff;
1311 return;
1312 case 4:
1313 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1314 return;
1315 case 2:
1316 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1317 return;
1318 case 1:
1319 cp[h >> 3] |= (0x01 << (h & 0x7));
1320 return;
1321 default:
1322 #ifdef STANDALONE
1323 printf("setblock bad fs_frag %d\n", fs->fs_frag);
1324 #else
1325 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1326 #endif
1327 return;
1328 }
1329 }
1330
1331 /* swap byte order of cylinder group */
1332 static void
1333 swap_cg(o, n)
1334 struct cg *o, *n;
1335 {
1336 int i, btotsize, fbsize;
1337 u_int32_t *n32, *o32;
1338 u_int16_t *n16, *o16;
1339
1340 n->cg_firstfield = bswap32(o->cg_firstfield);
1341 n->cg_magic = bswap32(o->cg_magic);
1342 n->cg_time = bswap32(o->cg_time);
1343 n->cg_cgx = bswap32(o->cg_cgx);
1344 n->cg_ncyl = bswap16(o->cg_ncyl);
1345 n->cg_niblk = bswap16(o->cg_niblk);
1346 n->cg_ndblk = bswap32(o->cg_ndblk);
1347 n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
1348 n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
1349 n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
1350 n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
1351 n->cg_rotor = bswap32(o->cg_rotor);
1352 n->cg_frotor = bswap32(o->cg_frotor);
1353 n->cg_irotor = bswap32(o->cg_irotor);
1354 n->cg_btotoff = bswap32(o->cg_btotoff);
1355 n->cg_boff = bswap32(o->cg_boff);
1356 n->cg_iusedoff = bswap32(o->cg_iusedoff);
1357 n->cg_freeoff = bswap32(o->cg_freeoff);
1358 n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
1359 n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
1360 n->cg_clusteroff = bswap32(o->cg_clusteroff);
1361 n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
1362 for (i=0; i < MAXFRAG; i++)
1363 n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
1364
1365 /* alays new format */
1366 if (n->cg_magic == CG_MAGIC) {
1367 btotsize = n->cg_boff - n->cg_btotoff;
1368 fbsize = n->cg_iusedoff - n->cg_boff;
1369 n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
1370 o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
1371 n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
1372 o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
1373 } else {
1374 btotsize = bswap32(n->cg_boff) - bswap32(n->cg_btotoff);
1375 fbsize = bswap32(n->cg_iusedoff) - bswap32(n->cg_boff);
1376 n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_btotoff));
1377 o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_btotoff));
1378 n16 = (u_int16_t*)((u_int8_t*)n + bswap32(n->cg_boff));
1379 o16 = (u_int16_t*)((u_int8_t*)o + bswap32(n->cg_boff));
1380 }
1381 for (i=0; i < btotsize / sizeof(u_int32_t); i++)
1382 n32[i] = bswap32(o32[i]);
1383
1384 for (i=0; i < fbsize/sizeof(u_int16_t); i++)
1385 n16[i] = bswap16(o16[i]);
1386
1387 if (n->cg_magic == CG_MAGIC) {
1388 n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
1389 o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
1390 } else {
1391 n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_clustersumoff));
1392 o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_clustersumoff));
1393 }
1394 for (i = 0; i < sblock.fs_contigsumsize + 1; i++)
1395 n32[i] = bswap32(o32[i]);
1396 }
1397
1398 /* copy a direntry to a buffer, in fs byte order */
1399 static void
1400 copy_dir(dir, dbuf)
1401 struct direct *dir;
1402 struct direct *dbuf;
1403 {
1404 memcpy(dbuf, dir, DIRSIZ(Oflag, dir, 0));
1405 if (needswap) {
1406 dbuf->d_ino = bswap32(dir->d_ino);
1407 dbuf->d_reclen = bswap16(dir->d_reclen);
1408 if (Oflag)
1409 ((struct odirect*)dbuf)->d_namlen =
1410 bswap16(((struct odirect*)dir)->d_namlen);
1411 }
1412 }
1413