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