mkfs.c revision 1.14 1 /* $NetBSD: mkfs.c,v 1.14 2003/04/02 10:39:49 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * This software was developed for the FreeBSD Project by Marshall
8 * Kirk McKusick and Network Associates Laboratories, the Security
9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * research program
12 *
13 * Copyright (c) 1980, 1989, 1993
14 * The Regents of the University of California. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 */
44
45 #include <sys/cdefs.h>
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
49 #else
50 __RCSID("$NetBSD: mkfs.c,v 1.14 2003/04/02 10:39:49 fvdl Exp $");
51 #endif
52 #endif /* not lint */
53
54 #include <sys/param.h>
55 #include <sys/time.h>
56 #include <sys/resource.h>
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <errno.h>
63
64 #include "makefs.h"
65
66 #include <ufs/ufs/dinode.h>
67 #include <ufs/ufs/ufs_bswap.h>
68 #include <ufs/ffs/fs.h>
69
70 #include "ffs/ufs_inode.h"
71 #include "ffs/ffs_extern.h"
72 #include "ffs/newfs_extern.h"
73
74 static void initcg(int, time_t, const fsinfo_t *);
75 static int ilog2(int);
76
77 static int count_digits(int);
78
79 /*
80 * make file system for cylinder-group style file systems
81 */
82 #define UMASK 0755
83 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
84
85 union {
86 struct fs fs;
87 char pad[SBLOCKSIZE];
88 } fsun;
89 #define sblock fsun.fs
90 struct csum *fscs;
91
92 union {
93 struct cg cg;
94 char pad[MAXBSIZE];
95 } cgun;
96 #define acg cgun.cg
97
98 char *iobuf;
99 int iobufsize;
100
101 char writebuf[MAXBSIZE];
102
103 static int Oflag; /* format as an 4.3BSD file system */
104 static int64_t fssize; /* file system size */
105 static int sectorsize; /* bytes/sector */
106 static int fsize; /* fragment size */
107 static int bsize; /* block size */
108 static int maxbsize; /* maximum clustering */
109 static int maxblkspercg;
110 static int minfree; /* free space threshold */
111 static int opt; /* optimization preference (space or time) */
112 static int density; /* number of bytes per inode */
113 static int maxcontig; /* max contiguous blocks to allocate */
114 static int maxbpg; /* maximum blocks per file in a cyl group */
115 static int bbsize; /* boot block size */
116 static int sbsize; /* superblock size */
117 static int avgfilesize; /* expected average file size */
118 static int avgfpdir; /* expected number of files per directory */
119
120 struct fs *
121 ffs_mkfs(const char *fsys, const fsinfo_t *fsopts)
122 {
123 int fragsperinode, optimalfpg, origdensity, minfpg, lastminfpg;
124 int32_t cylno, i, csfrags;
125 long long sizepb;
126 void *space;
127 int size, blks;
128 int nprintcols, printcolwidth;
129
130 Oflag = fsopts->version;
131 fssize = fsopts->size / fsopts->sectorsize;
132 sectorsize = fsopts->sectorsize;
133 fsize = fsopts->fsize;
134 bsize = fsopts->bsize;
135 maxbsize = fsopts->maxbsize;
136 maxblkspercg = fsopts->maxblkspercg;
137 minfree = fsopts->minfree;
138 opt = fsopts->optimization;
139 density = fsopts->density;
140 maxcontig = fsopts->maxcontig;
141 maxbpg = fsopts->maxbpg;
142 avgfilesize = fsopts->avgfilesize;
143 avgfpdir = fsopts->avgfpdir;
144 bbsize = BBSIZE;
145 sbsize = SBLOCKSIZE;
146
147 if (Oflag == 0) {
148 sblock.fs_old_inodefmt = FS_42INODEFMT;
149 sblock.fs_maxsymlinklen = 0;
150 sblock.fs_old_flags = 0;
151 } else {
152 sblock.fs_old_inodefmt = FS_44INODEFMT;
153 sblock.fs_maxsymlinklen = (Oflag == 1 ? MAXSYMLINKLEN_UFS1 :
154 MAXSYMLINKLEN_UFS2);
155 sblock.fs_old_flags = FS_FLAGS_UPDATED;
156 sblock.fs_flags = 0;
157 }
158 /*
159 * Validate the given file system size.
160 * Verify that its last block can actually be accessed.
161 * Convert to file system fragment sized units.
162 */
163 if (fssize <= 0) {
164 printf("preposterous size %lld\n", (long long)fssize);
165 exit(13);
166 }
167 ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts);
168
169 /*
170 * collect and verify the filesystem density info
171 */
172 sblock.fs_avgfilesize = avgfilesize;
173 sblock.fs_avgfpdir = avgfpdir;
174 if (sblock.fs_avgfilesize <= 0)
175 printf("illegal expected average file size %d\n",
176 sblock.fs_avgfilesize), exit(14);
177 if (sblock.fs_avgfpdir <= 0)
178 printf("illegal expected number of files per directory %d\n",
179 sblock.fs_avgfpdir), exit(15);
180 /*
181 * collect and verify the block and fragment sizes
182 */
183 sblock.fs_bsize = bsize;
184 sblock.fs_fsize = fsize;
185 if (!POWEROF2(sblock.fs_bsize)) {
186 printf("block size must be a power of 2, not %d\n",
187 sblock.fs_bsize);
188 exit(16);
189 }
190 if (!POWEROF2(sblock.fs_fsize)) {
191 printf("fragment size must be a power of 2, not %d\n",
192 sblock.fs_fsize);
193 exit(17);
194 }
195 if (sblock.fs_fsize < sectorsize) {
196 printf("fragment size %d is too small, minimum is %d\n",
197 sblock.fs_fsize, sectorsize);
198 exit(18);
199 }
200 if (sblock.fs_bsize < MINBSIZE) {
201 printf("block size %d is too small, minimum is %d\n",
202 sblock.fs_bsize, MINBSIZE);
203 exit(19);
204 }
205 if (sblock.fs_bsize > MAXBSIZE) {
206 printf("block size %d is too large, maximum is %d\n",
207 sblock.fs_bsize, MAXBSIZE);
208 exit(19);
209 }
210 if (sblock.fs_bsize < sblock.fs_fsize) {
211 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
212 sblock.fs_bsize, sblock.fs_fsize);
213 exit(20);
214 }
215
216 if (maxbsize < bsize || !POWEROF2(maxbsize)) {
217 sblock.fs_maxbsize = sblock.fs_bsize;
218 printf("Extent size set to %d\n", sblock.fs_maxbsize);
219 } else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
220 sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
221 printf("Extent size reduced to %d\n", sblock.fs_maxbsize);
222 } else {
223 sblock.fs_maxbsize = maxbsize;
224 }
225 sblock.fs_maxcontig = maxcontig;
226 if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
227 sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
228 printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
229 }
230
231 if (sblock.fs_maxcontig > 1)
232 sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
233
234 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
235 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
236 sblock.fs_qbmask = ~sblock.fs_bmask;
237 sblock.fs_qfmask = ~sblock.fs_fmask;
238 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
239 sblock.fs_bshift++;
240 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
241 sblock.fs_fshift++;
242 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
243 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
244 sblock.fs_fragshift++;
245 if (sblock.fs_frag > MAXFRAG) {
246 printf("fragment size %d is too small, "
247 "minimum with block size %d is %d\n",
248 sblock.fs_fsize, sblock.fs_bsize,
249 sblock.fs_bsize / MAXFRAG);
250 exit(21);
251 }
252 sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
253 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
254
255 if (Oflag <= 1) {
256 sblock.fs_magic = FS_UFS1_MAGIC;
257 sblock.fs_sblockloc = SBLOCK_UFS1;
258 sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
259 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
260 sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) *
261 sizeof (int32_t));
262 sblock.fs_old_inodefmt = FS_44INODEFMT;
263 sblock.fs_old_cgoffset = 0;
264 sblock.fs_old_cgmask = 0xffffffff;
265 sblock.fs_old_size = sblock.fs_size;
266 sblock.fs_old_rotdelay = 0;
267 sblock.fs_old_rps = 60;
268 sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
269 sblock.fs_old_cpg = 1;
270 sblock.fs_old_interleave = 1;
271 sblock.fs_old_trackskew = 0;
272 sblock.fs_old_cpc = 0;
273 sblock.fs_old_postblformat = 1;
274 sblock.fs_old_nrpos = 1;
275 } else {
276 sblock.fs_magic = FS_UFS2_MAGIC;
277 #if 0 /* XXX makefs is used for small filesystems. */
278 sblock.fs_sblockloc = SBLOCK_UFS2;
279 #else
280 sblock.fs_sblockloc = SBLOCK_UFS1;
281 #endif
282 sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
283 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
284 sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) *
285 sizeof (int64_t));
286 }
287
288 sblock.fs_sblkno =
289 roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
290 sblock.fs_frag);
291 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
292 roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
293 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
294 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
295 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
296 sizepb *= NINDIR(&sblock);
297 sblock.fs_maxfilesize += sizepb;
298 }
299
300 /*
301 * Calculate the number of blocks to put into each cylinder group.
302 *
303 * This algorithm selects the number of blocks per cylinder
304 * group. The first goal is to have at least enough data blocks
305 * in each cylinder group to meet the density requirement. Once
306 * this goal is achieved we try to expand to have at least
307 * 1 cylinder group. Once this goal is achieved, we pack as
308 * many blocks into each cylinder group map as will fit.
309 *
310 * We start by calculating the smallest number of blocks that we
311 * can put into each cylinder group. If this is too big, we reduce
312 * the density until it fits.
313 */
314 origdensity = density;
315 for (;;) {
316 fragsperinode = MAX(numfrags(&sblock, density), 1);
317 minfpg = fragsperinode * INOPB(&sblock);
318 if (minfpg > sblock.fs_size)
319 minfpg = sblock.fs_size;
320 sblock.fs_ipg = INOPB(&sblock);
321 sblock.fs_fpg = roundup(sblock.fs_iblkno +
322 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
323 if (sblock.fs_fpg < minfpg)
324 sblock.fs_fpg = minfpg;
325 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
326 INOPB(&sblock));
327 sblock.fs_fpg = roundup(sblock.fs_iblkno +
328 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
329 if (sblock.fs_fpg < minfpg)
330 sblock.fs_fpg = minfpg;
331 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
332 INOPB(&sblock));
333 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
334 break;
335 density -= sblock.fs_fsize;
336 }
337 if (density != origdensity)
338 printf("density reduced from %d to %d\n", origdensity, density);
339
340 if (maxblkspercg <= 0 || maxblkspercg >= fssize)
341 maxblkspercg = fssize - 1;
342 /*
343 * Start packing more blocks into the cylinder group until
344 * it cannot grow any larger, the number of cylinder groups
345 * drops below 1, or we reach the size requested.
346 */
347 for ( ; sblock.fs_fpg < maxblkspercg; sblock.fs_fpg += sblock.fs_frag) {
348 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
349 INOPB(&sblock));
350 if (sblock.fs_size / sblock.fs_fpg < 1)
351 break;
352 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
353 continue;
354 if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize)
355 break;
356 sblock.fs_fpg -= sblock.fs_frag;
357 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
358 INOPB(&sblock));
359 break;
360 }
361 /*
362 * Check to be sure that the last cylinder group has enough blocks
363 * to be viable. If it is too small, reduce the number of blocks
364 * per cylinder group which will have the effect of moving more
365 * blocks into the last cylinder group.
366 */
367 optimalfpg = sblock.fs_fpg;
368 for (;;) {
369 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
370 lastminfpg = roundup(sblock.fs_iblkno +
371 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
372 if (sblock.fs_size < lastminfpg) {
373 printf("Filesystem size %lld < minimum size of %d\n",
374 (long long)sblock.fs_size, lastminfpg);
375 exit(28);
376 }
377 if (sblock.fs_size % sblock.fs_fpg >= lastminfpg ||
378 sblock.fs_size % sblock.fs_fpg == 0)
379 break;
380 sblock.fs_fpg -= sblock.fs_frag;
381 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
382 INOPB(&sblock));
383 }
384 if (optimalfpg != sblock.fs_fpg)
385 printf("Reduced frags per cylinder group from %d to %d %s\n",
386 optimalfpg, sblock.fs_fpg, "to enlarge last cyl group");
387 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
388 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
389 if (Oflag <= 1) {
390 sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
391 sblock.fs_old_nsect = sblock.fs_old_spc;
392 sblock.fs_old_npsect = sblock.fs_old_spc;
393 sblock.fs_old_ncyl = sblock.fs_ncg;
394 }
395
396 /*
397 * fill in remaining fields of the super block
398 */
399 sblock.fs_csaddr = cgdmin(&sblock, 0);
400 sblock.fs_cssize =
401 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
402
403 /*
404 * Setup memory for temporary in-core cylgroup summaries.
405 * Cribbed from ffs_mountfs().
406 */
407 size = sblock.fs_cssize;
408 blks = howmany(size, sblock.fs_fsize);
409 if (sblock.fs_contigsumsize > 0)
410 size += sblock.fs_ncg * sizeof(int32_t);
411 if ((space = (char *)calloc(1, size)) == NULL)
412 err(1, "memory allocation error for cg summaries");
413 sblock.fs_csp = space;
414 space = (char *)space + sblock.fs_cssize;
415 if (sblock.fs_contigsumsize > 0) {
416 int32_t *lp;
417
418 sblock.fs_maxcluster = lp = space;
419 for (i = 0; i < sblock.fs_ncg; i++)
420 *lp++ = sblock.fs_contigsumsize;
421 }
422
423 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
424 if (sblock.fs_sbsize > SBLOCKSIZE)
425 sblock.fs_sbsize = SBLOCKSIZE;
426 sblock.fs_minfree = minfree;
427 sblock.fs_maxcontig = maxcontig;
428 sblock.fs_maxbpg = maxbpg;
429 sblock.fs_optim = opt;
430 sblock.fs_cgrotor = 0;
431 sblock.fs_pendingblocks = 0;
432 sblock.fs_pendinginodes = 0;
433 sblock.fs_cstotal.cs_ndir = 0;
434 sblock.fs_cstotal.cs_nbfree = 0;
435 sblock.fs_cstotal.cs_nifree = 0;
436 sblock.fs_cstotal.cs_nffree = 0;
437 sblock.fs_fmod = 0;
438 sblock.fs_ronly = 0;
439 sblock.fs_state = 0;
440 sblock.fs_clean = FS_ISCLEAN;
441 sblock.fs_ronly = 0;
442 sblock.fs_id[0] = start_time.tv_sec;
443 sblock.fs_id[1] = random();
444 sblock.fs_fsmnt[0] = '\0';
445 csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
446 sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
447 sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
448 sblock.fs_cstotal.cs_nbfree =
449 fragstoblks(&sblock, sblock.fs_dsize) -
450 howmany(csfrags, sblock.fs_frag);
451 sblock.fs_cstotal.cs_nffree =
452 fragnum(&sblock, sblock.fs_size) +
453 (fragnum(&sblock, csfrags) > 0 ?
454 sblock.fs_frag - fragnum(&sblock, csfrags) : 0);
455 sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO;
456 sblock.fs_cstotal.cs_ndir = 0;
457 sblock.fs_dsize -= csfrags;
458 sblock.fs_time = start_time.tv_sec;
459 if (Oflag <= 1) {
460 sblock.fs_old_time = start_time.tv_sec;
461 sblock.fs_old_dsize = sblock.fs_dsize;
462 sblock.fs_old_csaddr = sblock.fs_csaddr;
463 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
464 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
465 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
466 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
467 }
468 /*
469 * Dump out summary information about file system.
470 */
471 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
472 printf("%s: %.1fMB (%lld sectors) block size %d, "
473 "fragment size %d\n",
474 fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
475 (long long)fsbtodb(&sblock, sblock.fs_size),
476 sblock.fs_bsize, sblock.fs_fsize);
477 printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
478 "%d inodes.\n",
479 sblock.fs_ncg,
480 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
481 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
482 #undef B2MBFACTOR
483 /*
484 * Now determine how wide each column will be, and calculate how
485 * many columns will fit in a 76 char line. 76 is the width of the
486 * subwindows in sysinst.
487 */
488 printcolwidth = count_digits(
489 fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
490 nprintcols = 76 / (printcolwidth + 2);
491
492 /*
493 * allocate space for superblock, cylinder group map, and
494 * two sets of inode blocks.
495 */
496 if (sblock.fs_bsize < SBLOCKSIZE)
497 iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
498 else
499 iobufsize = 4 * sblock.fs_bsize;
500 if ((iobuf = malloc(iobufsize)) == 0) {
501 printf("Cannot allocate I/O buffer\n");
502 exit(38);
503 }
504 memset(iobuf, 0, iobufsize);
505 /*
506 * Make a copy of the superblock into the buffer that we will be
507 * writing out in each cylinder group.
508 */
509 memcpy(writebuf, &sblock, sbsize);
510 if (fsopts->needswap)
511 ffs_sb_swap(&sblock, (struct fs*)writebuf);
512 memcpy(iobuf, writebuf, SBLOCKSIZE);
513
514 printf("super-block backups (for fsck -b #) at:");
515 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
516 initcg(cylno, start_time.tv_sec, fsopts);
517 if (cylno % nprintcols == 0)
518 printf("\n");
519 printf(" %*lld,", printcolwidth,
520 (long long)fsbtodb(&sblock, cgsblock(&sblock, cylno)));
521 fflush(stdout);
522 }
523 printf("\n");
524
525 /*
526 * Now construct the initial file system,
527 * then write out the super-block.
528 */
529 sblock.fs_time = start_time.tv_sec;
530 if (Oflag <= 1) {
531 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
532 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
533 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
534 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
535 }
536 if (fsopts->needswap)
537 sblock.fs_flags |= FS_SWAPPED;
538 ffs_write_superblock(&sblock, fsopts);
539 return (&sblock);
540 }
541
542 /*
543 * Write out the superblock and its duplicates,
544 * and the cylinder group summaries
545 */
546 void
547 ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts)
548 {
549 int cylno, size, blks, i, saveflag;
550 void *space;
551 char *wrbuf;
552
553 saveflag = fs->fs_flags & FS_INTERNAL;
554 fs->fs_flags &= ~FS_INTERNAL;
555
556 memcpy(writebuf, &sblock, sbsize);
557 if (fsopts->needswap)
558 ffs_sb_swap(fs, (struct fs*)writebuf);
559 ffs_wtfs(fs->fs_sblockloc / sectorsize, sbsize, writebuf, fsopts);
560
561 /* Write out the duplicate super blocks */
562 for (cylno = 0; cylno < fs->fs_ncg; cylno++)
563 ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)),
564 sbsize, writebuf, fsopts);
565
566 /* Write out the cylinder group summaries */
567 size = fs->fs_cssize;
568 blks = howmany(size, fs->fs_fsize);
569 space = (void *)fs->fs_csp;
570 if ((wrbuf = malloc(size)) == NULL)
571 err(1, "ffs_write_superblock: malloc %d", size);
572 for (i = 0; i < blks; i+= fs->fs_frag) {
573 size = fs->fs_bsize;
574 if (i + fs->fs_frag > blks)
575 size = (blks - i) * fs->fs_fsize;
576 if (fsopts->needswap)
577 ffs_csum_swap((struct csum *)space,
578 (struct csum *)wrbuf, size);
579 else
580 memcpy(wrbuf, space, (u_int)size);
581 ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts);
582 space = (char *)space + size;
583 }
584 free(wrbuf);
585 fs->fs_flags |= saveflag;
586 }
587
588 /*
589 * Initialize a cylinder group.
590 */
591 static void
592 initcg(int cylno, time_t utime, const fsinfo_t *fsopts)
593 {
594 daddr_t cbase, dmax;
595 int32_t i, j, d, dlower, dupper, blkno;
596 struct ufs1_dinode *dp1;
597 struct ufs2_dinode *dp2;
598 int start;
599
600 /*
601 * Determine block bounds for cylinder group.
602 * Allow space for super block summary information in first
603 * cylinder group.
604 */
605 cbase = cgbase(&sblock, cylno);
606 dmax = cbase + sblock.fs_fpg;
607 if (dmax > sblock.fs_size)
608 dmax = sblock.fs_size;
609 dlower = cgsblock(&sblock, cylno) - cbase;
610 dupper = cgdmin(&sblock, cylno) - cbase;
611 if (cylno == 0)
612 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
613 memset(&acg, 0, sblock.fs_cgsize);
614 acg.cg_time = utime;
615 acg.cg_magic = CG_MAGIC;
616 acg.cg_cgx = cylno;
617 acg.cg_niblk = sblock.fs_ipg;
618 acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
619 sblock.fs_ipg : 2 * INOPB(&sblock);
620 acg.cg_ndblk = dmax - cbase;
621 if (sblock.fs_contigsumsize > 0)
622 acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
623 start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
624 if (Oflag == 2) {
625 acg.cg_iusedoff = start;
626 } else {
627 if (cylno == sblock.fs_ncg - 1)
628 acg.cg_old_ncyl = howmany(acg.cg_ndblk,
629 sblock.fs_fpg / sblock.fs_old_cpg);
630 else
631 acg.cg_old_ncyl = sblock.fs_old_cpg;
632 acg.cg_old_time = acg.cg_time;
633 acg.cg_time = 0;
634 acg.cg_old_niblk = acg.cg_niblk;
635 acg.cg_niblk = 0;
636 acg.cg_initediblk = 0;
637 acg.cg_old_btotoff = start;
638 acg.cg_old_boff = acg.cg_old_btotoff +
639 sblock.fs_old_cpg * sizeof(int32_t);
640 acg.cg_iusedoff = acg.cg_old_boff +
641 sblock.fs_old_cpg * sizeof(u_int16_t);
642 }
643 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
644 if (sblock.fs_contigsumsize <= 0) {
645 acg.cg_nextfreeoff = acg.cg_freeoff +
646 howmany(sblock.fs_fpg, CHAR_BIT);
647 } else {
648 acg.cg_clustersumoff = acg.cg_freeoff +
649 howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
650 acg.cg_clustersumoff =
651 roundup(acg.cg_clustersumoff, sizeof(int32_t));
652 acg.cg_clusteroff = acg.cg_clustersumoff +
653 (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
654 acg.cg_nextfreeoff = acg.cg_clusteroff +
655 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
656 }
657 if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
658 printf("Panic: cylinder group too big\n");
659 exit(37);
660 }
661 acg.cg_cs.cs_nifree += sblock.fs_ipg;
662 if (cylno == 0)
663 for (i = 0; i < ROOTINO; i++) {
664 setbit(cg_inosused(&acg, 0), i);
665 acg.cg_cs.cs_nifree--;
666 }
667 if (cylno > 0) {
668 /*
669 * In cylno 0, beginning space is reserved
670 * for boot and super blocks.
671 */
672 for (d = 0, blkno = 0; d < dlower;) {
673 ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
674 if (sblock.fs_contigsumsize > 0)
675 setbit(cg_clustersfree(&acg, 0), blkno);
676 acg.cg_cs.cs_nbfree++;
677 d += sblock.fs_frag;
678 blkno++;
679 }
680 }
681 if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
682 acg.cg_frsum[sblock.fs_frag - i]++;
683 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
684 setbit(cg_blksfree(&acg, 0), dupper);
685 acg.cg_cs.cs_nffree++;
686 }
687 }
688 for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
689 d + sblock.fs_frag <= acg.cg_ndblk; ) {
690 ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
691 if (sblock.fs_contigsumsize > 0)
692 setbit(cg_clustersfree(&acg, 0), blkno);
693 acg.cg_cs.cs_nbfree++;
694 d += sblock.fs_frag;
695 blkno++;
696 }
697 if (d < acg.cg_ndblk) {
698 acg.cg_frsum[acg.cg_ndblk - d]++;
699 for (; d < acg.cg_ndblk; d++) {
700 setbit(cg_blksfree(&acg, 0), d);
701 acg.cg_cs.cs_nffree++;
702 }
703 }
704 if (sblock.fs_contigsumsize > 0) {
705 int32_t *sump = cg_clustersum(&acg, 0);
706 u_char *mapp = cg_clustersfree(&acg, 0);
707 int map = *mapp++;
708 int bit = 1;
709 int run = 0;
710
711 for (i = 0; i < acg.cg_nclusterblks; i++) {
712 if ((map & bit) != 0) {
713 run++;
714 } else if (run != 0) {
715 if (run > sblock.fs_contigsumsize)
716 run = sblock.fs_contigsumsize;
717 sump[run]++;
718 run = 0;
719 }
720 if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
721 bit <<= 1;
722 } else {
723 map = *mapp++;
724 bit = 1;
725 }
726 }
727 if (run != 0) {
728 if (run > sblock.fs_contigsumsize)
729 run = sblock.fs_contigsumsize;
730 sump[run]++;
731 }
732 }
733 sblock.fs_cs(&sblock, cylno) = acg.cg_cs;
734 /*
735 * Write out the duplicate super block, the cylinder group map
736 * and two blocks worth of inodes in a single write.
737 */
738 start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
739 memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
740 if (fsopts->needswap)
741 ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
742 start += sblock.fs_bsize;
743 dp1 = (struct ufs1_dinode *)(&iobuf[start]);
744 dp2 = (struct ufs2_dinode *)(&iobuf[start]);
745 for (i = 0; i < acg.cg_initediblk; i++) {
746 if (sblock.fs_magic == FS_UFS1_MAGIC) {
747 /* No need to swap, it'll stay random */
748 dp1->di_gen = random();
749 dp1++;
750 } else {
751 dp2->di_gen = random();
752 dp2++;
753 }
754 }
755 ffs_wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf,
756 fsopts);
757 /*
758 * For the old file system, we have to initialize all the inodes.
759 */
760 if (Oflag <= 1) {
761 for (i = 2 * sblock.fs_frag;
762 i < sblock.fs_ipg / INOPF(&sblock);
763 i += sblock.fs_frag) {
764 dp1 = (struct ufs1_dinode *)(&iobuf[start]);
765 for (j = 0; j < INOPB(&sblock); j++) {
766 dp1->di_gen = random();
767 dp1++;
768 }
769 ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
770 sblock.fs_bsize, &iobuf[start], fsopts);
771 }
772 }
773 }
774
775 /*
776 * read a block from the file system
777 */
778 void
779 ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
780 {
781 int n;
782 off_t offset;
783
784 offset = bno;
785 offset *= fsopts->sectorsize;
786 if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
787 err(1, "ffs_rdfs: seek error for sector %lld: %s\n",
788 (long long)bno, strerror(errno));
789 n = read(fsopts->fd, bf, size);
790 if (n == -1) {
791 abort();
792 err(1, "ffs_rdfs: read error bno %lld size %d", (long long)bno,
793 size);
794 }
795 else if (n != size)
796 errx(1, "ffs_rdfs: read error for sector %lld: %s\n",
797 (long long)bno, strerror(errno));
798 }
799
800 /*
801 * write a block to the file system
802 */
803 void
804 ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
805 {
806 int n;
807 off_t offset;
808
809 offset = bno;
810 offset *= fsopts->sectorsize;
811 if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
812 err(1, "wtfs: seek error for sector %lld: %s\n",
813 (long long)bno, strerror(errno));
814 n = write(fsopts->fd, bf, size);
815 if (n == -1)
816 err(1, "wtfs: write error for sector %lld: %s\n",
817 (long long)bno, strerror(errno));
818 else if (n != size)
819 errx(1, "wtfs: write error for sector %lld: %s\n",
820 (long long)bno, strerror(errno));
821 }
822
823
824 /* Determine how many digits are needed to print a given integer */
825 static int
826 count_digits(int num)
827 {
828 int ndig;
829
830 for(ndig = 1; num > 9; num /=10, ndig++);
831
832 return (ndig);
833 }
834
835 static int
836 ilog2(int val)
837 {
838 u_int n;
839
840 for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
841 if (1 << n == val)
842 return (n);
843 errx(1, "ilog2: %d is not a power of 2\n", val);
844 }
845