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