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