xref: /openbsd-src/sbin/newfs/mkfs.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: mkfs.c,v 1.76 2011/06/05 15:20:37 chl Exp $	*/
2 /*	$NetBSD: mkfs.c,v 1.25 1995/06/18 21:35:38 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Marshall
9  * Kirk McKusick and Network Associates Laboratories, the Security
10  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
11  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
12  * research program.
13  *
14  * Copyright (c) 1980, 1989, 1993
15  *	The Regents of the University of California.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41 
42 #include <sys/param.h>
43 #include <machine/vmparam.h>
44 #include <sys/time.h>
45 #include <sys/disklabel.h>
46 #include <sys/ioctl.h>
47 #include <sys/mman.h>
48 #include <sys/sysctl.h>
49 
50 #include <ufs/ufs/dinode.h>
51 #include <ufs/ufs/dir.h>
52 #include <ufs/ffs/fs.h>
53 
54 #include <err.h>
55 #include <string.h>
56 #include <stdlib.h>
57 #include <stdint.h>
58 #include <unistd.h>
59 
60 #ifndef STANDALONE
61 #include <a.out.h>
62 #include <stdio.h>
63 #include <errno.h>
64 #endif
65 
66 /*
67  * Default directory umask.
68  */
69 #define UMASK		0755
70 
71 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
72 
73 /*
74  * 'Standard' bad FFS magic.
75  */
76 #define FS_BAD_MAGIC	0x19960408
77 
78 /*
79  * The minimum number of cylinder groups that should be created.
80  */
81 #define MINCYLGRPS	4
82 
83 /*
84  * variables set up by front end.
85  */
86 extern int	mfs;		/* run as the memory based filesystem */
87 extern int	Nflag;		/* run mkfs without writing file system */
88 extern int	Oflag;		/* format as an 4.3BSD file system */
89 extern daddr64_t fssize;	/* file system size */
90 extern long long	sectorsize;	/* bytes/sector */
91 extern int	fsize;		/* fragment size */
92 extern int	bsize;		/* block size */
93 extern int	maxfrgspercg;	/* maximum fragments per cylinder group */
94 extern int	minfree;	/* free space threshold */
95 extern int	opt;		/* optimization preference (space or time) */
96 extern int	density;	/* number of bytes per inode */
97 extern int	maxbpg;		/* maximum blocks per file in a cyl group */
98 extern int	avgfilesize;	/* expected average file size */
99 extern int	avgfilesperdir;	/* expected number of files per directory */
100 extern int	quiet;		/* quiet flag */
101 extern caddr_t	membase;	/* start address of memory based filesystem */
102 
103 union fs_u {
104 	struct fs fs;
105 	char pad[SBSIZE];
106 } *fsun;
107 #define sblock	fsun->fs
108 
109 struct	csum *fscs;
110 
111 union cg_u {
112 	struct cg cg;
113 	char pad[MAXBSIZE];
114 } *cgun;
115 #define acg	cgun->cg
116 
117 union dinode {
118 	struct ufs1_dinode dp1;
119 	struct ufs2_dinode dp2;
120 };
121 
122 int	fsi, fso;
123 
124 static caddr_t iobuf;
125 static long iobufsize;
126 
127 daddr64_t	alloc(int, int);
128 static int	charsperline(void);
129 static int	ilog2(int);
130 void		initcg(int, time_t);
131 void		wtfs(daddr64_t, int, void *);
132 int		fsinit1(time_t, mode_t, uid_t, gid_t);
133 int		fsinit2(time_t);
134 int		makedir(struct direct *, int);
135 void		iput(union dinode *, ino_t);
136 void		setblock(struct fs *, unsigned char *, int);
137 void		clrblock(struct fs *, unsigned char *, int);
138 int		isblock(struct fs *, unsigned char *, int);
139 void		rdfs(daddr64_t, int, void *);
140 void		mkfs(struct partition *, char *, int, int,
141 		    mode_t, uid_t, gid_t);
142 static		void checksz(void);
143 
144 #ifndef STANDALONE
145 volatile sig_atomic_t cur_cylno;
146 volatile const char *cur_fsys;
147 
148 void
149 siginfo(int sig)
150 {
151 	int save_errno = errno;
152 	char buf[128];
153 
154 	snprintf(buf, sizeof(buf), "%s: initializing cg %ld/%d\n",
155 	    cur_fsys, (long)cur_cylno, sblock.fs_ncg);
156 	write(STDERR_FILENO, buf, strlen(buf));
157 	errno = save_errno;
158 }
159 #endif
160 
161 void
162 mkfs(struct partition *pp, char *fsys, int fi, int fo, mode_t mfsmode,
163     uid_t mfsuid, gid_t mfsgid)
164 {
165 	time_t utime;
166 	quad_t sizepb;
167 	int i, j, width, origdensity, fragsperinode, minfpg, optimalfpg;
168 	int lastminfpg, mincylgrps;
169 	long cylno, csfrags;
170 	char tmpbuf[100];	/* XXX this will break in about 2,500 years */
171 
172 	if ((fsun = calloc(1, sizeof (union fs_u))) == NULL ||
173 	    (cgun = calloc(1, sizeof (union cg_u))) == NULL)
174 		err(1, "calloc");
175 
176 #ifndef STANDALONE
177 	time(&utime);
178 #endif
179 	if (mfs) {
180 		quad_t sz = (quad_t)fssize * DEV_BSIZE;
181 		if (sz > SIZE_T_MAX) {
182 			errno = ENOMEM;
183 			err(12, "mmap");
184 		}
185 		membase = mmap(NULL, sz, PROT_READ|PROT_WRITE,
186 		    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
187 		if (membase == MAP_FAILED)
188 			err(12, "mmap");
189 		madvise(membase, sz, MADV_RANDOM);
190 	}
191 	fsi = fi;
192 	fso = fo;
193 	/*
194 	 * Validate the given file system size.
195 	 * Verify that its last block can actually be accessed.
196 	 */
197 	if (Oflag <= 1 && fssize > INT_MAX)
198 		errx(13, "preposterous size %lld, max is %d", fssize, INT_MAX);
199 	if (Oflag == 2 && fssize > MAXDISKSIZE)
200 		errx(13, "preposterous size %lld, max is %lld", fssize,
201 		    MAXDISKSIZE);
202 
203 	wtfs(fssize - (sectorsize / DEV_BSIZE), sectorsize, (char *)&sblock);
204 
205 	sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
206 	sblock.fs_avgfilesize = avgfilesize;
207 	sblock.fs_avgfpdir = avgfilesperdir;
208 
209 	/*
210 	 * Collect and verify the block and fragment sizes.
211 	 */
212 	if (!POWEROF2(bsize)) {
213 		errx(16, "block size must be a power of 2, not %d", bsize);
214 	}
215 	if (!POWEROF2(fsize)) {
216 		errx(17, "fragment size must be a power of 2, not %d",
217 		     fsize);
218 	}
219 	if (fsize < sectorsize) {
220 		errx(18, "fragment size %d is too small, minimum is %lld",
221 		     fsize, sectorsize);
222 	}
223 	if (bsize < MINBSIZE) {
224 		errx(19, "block size %d is too small, minimum is %d",
225 		     bsize, MINBSIZE);
226 	}
227 	if (bsize > MAXBSIZE) {
228 		errx(19, "block size %d is too large, maximum is %d",
229 		     bsize, MAXBSIZE);
230 	}
231 	if (bsize < fsize) {
232 		errx(20, "block size (%d) cannot be smaller than fragment size (%d)",
233 		     bsize, fsize);
234 	}
235 	sblock.fs_bsize = bsize;
236 	sblock.fs_fsize = fsize;
237 
238 	/*
239 	 * Calculate the superblock bitmasks and shifts.
240 	 */
241 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
242 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
243 	sblock.fs_qbmask = ~sblock.fs_bmask;
244 	sblock.fs_qfmask = ~sblock.fs_fmask;
245 	sblock.fs_bshift = ilog2(sblock.fs_bsize);
246 	sblock.fs_fshift = ilog2(sblock.fs_fsize);
247 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
248 	if (sblock.fs_frag > MAXFRAG) {
249 		errx(21, "fragment size %d is too small, minimum with block "
250 		    "size %d is %d", sblock.fs_fsize, sblock.fs_bsize,
251 		    sblock.fs_bsize / MAXFRAG);
252 	}
253 	sblock.fs_fragshift = ilog2(sblock.fs_frag);
254 	sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / DEV_BSIZE);
255 	sblock.fs_size = dbtofsb(&sblock, fssize);
256 	sblock.fs_nspf = sblock.fs_fsize / DEV_BSIZE;
257 	sblock.fs_maxcontig = 1;
258 	sblock.fs_nrpos = 1;
259 	sblock.fs_cpg = 1;
260 
261 	/*
262 	 * Before the file system is fully initialized, mark it as invalid.
263 	 */
264 	sblock.fs_magic = FS_BAD_MAGIC;
265 
266 	/*
267 	 * Set the remaining superblock fields.  Note that for FFS1, media
268 	 * geometry fields are set to fake values.  This is for compatibility
269 	 * with really ancient kernels that might still inspect these values.
270 	 */
271 	if (Oflag <= 1) {
272 		sblock.fs_sblockloc = SBLOCK_UFS1;
273 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
274 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
275 		if (Oflag == 0) {
276 			sblock.fs_maxsymlinklen = 0;
277 			sblock.fs_inodefmt = FS_42INODEFMT;
278 		} else {
279 			sblock.fs_maxsymlinklen = MAXSYMLINKLEN_UFS1;
280 			sblock.fs_inodefmt = FS_44INODEFMT;
281 		}
282 		sblock.fs_cgoffset = 0;
283 		sblock.fs_cgmask = 0xffffffff;
284 		sblock.fs_ffs1_size = sblock.fs_size;
285 		sblock.fs_rotdelay = 0;
286 		sblock.fs_rps = 60;
287 		sblock.fs_interleave = 1;
288 		sblock.fs_trackskew = 0;
289 		sblock.fs_cpc = 0;
290 	} else {
291 		sblock.fs_inodefmt = FS_44INODEFMT;
292 		sblock.fs_sblockloc = SBLOCK_UFS2;
293 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
294 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
295 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN_UFS2;
296 	}
297 	sblock.fs_sblkno =
298 	    roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
299 		sblock.fs_frag);
300 	sblock.fs_cblkno = (int32_t)(sblock.fs_sblkno +
301 	    roundup(howmany(SBSIZE, sblock.fs_fsize), sblock.fs_frag));
302 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
303 	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
304 	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
305 		sizepb *= NINDIR(&sblock);
306 		sblock.fs_maxfilesize += sizepb;
307 	}
308 #ifdef notyet
309 	/*
310 	 * It is impossible to create a snapshot in case fs_maxfilesize is
311 	 * smaller than fssize.
312 	 */
313 	if (sblock.fs_maxfilesize < (u_quad_t)fssize)
314 		warnx("WARNING: You will be unable to create snapshots on this "
315 		    "file system. Correct by using a larger blocksize.");
316 #endif
317 	/*
318 	 * Calculate the number of blocks to put into each cylinder group. The
319 	 * first goal is to have at least enough data blocks in each cylinder
320 	 * group to meet the density requirement. Once this goal is achieved
321 	 * we try to expand to have at least mincylgrps cylinder groups. Once
322 	 * this goal is achieved, we pack as many blocks into each cylinder
323 	 * group map as will fit.
324 	 *
325 	 * We start by calculating the smallest number of blocks that we can
326 	 * put into each cylinder group. If this is too big, we reduce the
327 	 * density until it fits.
328 	 */
329 	origdensity = density;
330 	for (;;) {
331 		fragsperinode = MAX(numfrags(&sblock, density), 1);
332 
333 		minfpg = fragsperinode * INOPB(&sblock);
334 		if (minfpg > sblock.fs_size)
335 			minfpg = sblock.fs_size;
336 
337 		sblock.fs_ipg = INOPB(&sblock);
338 		sblock.fs_fpg = roundup(sblock.fs_iblkno +
339 		    sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
340 		if (sblock.fs_fpg < minfpg)
341 			sblock.fs_fpg = minfpg;
342 
343 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
344 		    INOPB(&sblock));
345 		sblock.fs_fpg = roundup(sblock.fs_iblkno +
346 		    sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
347 		if (sblock.fs_fpg < minfpg)
348 			sblock.fs_fpg = minfpg;
349 
350 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
351 		    INOPB(&sblock));
352 
353 		if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
354 			break;
355 
356 		density -= sblock.fs_fsize;
357 	}
358 	if (density != origdensity)
359 		warnx("density reduced from %d to %d bytes per inode",
360 		    origdensity, density);
361 
362 	/*
363 	 * Use a lower value for mincylgrps if the user specified a large
364 	 * number of blocks per cylinder group.  This is needed for, e.g. the
365 	 * install media which needs to pack 2 files very tightly.
366 	 */
367 	mincylgrps = MINCYLGRPS;
368 	if (maxfrgspercg != INT_MAX) {
369 		i = sblock.fs_size / maxfrgspercg;
370 		if (i < MINCYLGRPS)
371 			mincylgrps = i <= 0 ? 1 : i;
372 	}
373 
374 	/*
375 	 * Start packing more blocks into the cylinder group until it cannot
376 	 * grow any larger, the number of cylinder groups drops below
377 	 * mincylgrps, or we reach the requested size.
378 	 */
379 	for (;;) {
380 		sblock.fs_fpg += sblock.fs_frag;
381 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
382 		    INOPB(&sblock));
383 
384 		if (sblock.fs_fpg > maxfrgspercg ||
385 		    sblock.fs_size / sblock.fs_fpg < mincylgrps ||
386 		    CGSIZE(&sblock) > (unsigned long)sblock.fs_bsize)
387 			break;
388 	}
389 	sblock.fs_fpg -= sblock.fs_frag;
390 	sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
391 	    INOPB(&sblock));
392 	if (sblock.fs_fpg > maxfrgspercg)
393 		warnx("can't honour -c: minimum is %d", sblock.fs_fpg);
394 
395 	/*
396 	 * Check to be sure that the last cylinder group has enough blocks to
397 	 * be viable. If it is too small, reduce the number of blocks per
398 	 * cylinder group which will have the effect of moving more blocks into
399 	 * 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 			errx(28, "file system size %jd < minimum size of %d "
408 			    "sectors", (intmax_t)sblock.fs_size, lastminfpg);
409 
410 		if (sblock.fs_size % sblock.fs_fpg >= lastminfpg ||
411 		    sblock.fs_size % sblock.fs_fpg == 0)
412 			break;
413 
414 		sblock.fs_fpg -= sblock.fs_frag;
415 		sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
416 		    INOPB(&sblock));
417 	}
418 
419 	if (optimalfpg != sblock.fs_fpg)
420 		warnx("reduced number of fragments per cylinder group from %d"
421 		    " to %d to enlarge last cylinder group", optimalfpg,
422 		    sblock.fs_fpg);
423 
424 	/*
425 	 * Back to filling superblock fields.
426 	 */
427 	if (Oflag <= 1) {
428 		sblock.fs_spc = sblock.fs_fpg * sblock.fs_nspf;
429 		sblock.fs_nsect = sblock.fs_spc;
430 		sblock.fs_npsect = sblock.fs_spc;
431 		sblock.fs_ncyl = sblock.fs_ncg;
432 	}
433 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
434 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
435 	sblock.fs_csaddr = cgdmin(&sblock, 0);
436 	sblock.fs_cssize =
437 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
438 
439 	fscs = (struct csum *)calloc(1, sblock.fs_cssize);
440 	if (fscs == NULL)
441 		errx(31, "calloc failed");
442 
443 	sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
444 	if (sblock.fs_sbsize > SBLOCKSIZE)
445 		sblock.fs_sbsize = SBLOCKSIZE;
446 
447 	sblock.fs_minfree = minfree;
448 	sblock.fs_maxbpg = maxbpg;
449 	sblock.fs_optim = opt;
450 	sblock.fs_cgrotor = 0;
451 	sblock.fs_pendingblocks = 0;
452 	sblock.fs_pendinginodes = 0;
453 	sblock.fs_fmod = 0;
454 	sblock.fs_ronly = 0;
455 	sblock.fs_state = 0;
456 	sblock.fs_clean = 1;
457 	sblock.fs_id[0] = (u_int32_t)utime;
458 	sblock.fs_id[1] = (u_int32_t)arc4random();
459 	sblock.fs_fsmnt[0] = '\0';
460 
461 	csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
462 	sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
463 	    sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
464 
465 	sblock.fs_cstotal.cs_nbfree = fragstoblks(&sblock, sblock.fs_dsize) -
466 	    howmany(csfrags, sblock.fs_frag);
467 	sblock.fs_cstotal.cs_nffree = fragnum(&sblock, sblock.fs_size) +
468 	    (fragnum(&sblock, csfrags) > 0 ?
469 	    sblock.fs_frag - fragnum(&sblock, csfrags) : 0);
470 	sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO;
471 	sblock.fs_cstotal.cs_ndir = 0;
472 
473 	sblock.fs_dsize -= csfrags;
474 	sblock.fs_time = utime;
475 
476 	if (Oflag <= 1) {
477 		sblock.fs_ffs1_time = sblock.fs_time;
478 		sblock.fs_ffs1_dsize = sblock.fs_dsize;
479 		sblock.fs_ffs1_csaddr = sblock.fs_csaddr;
480 		sblock.fs_ffs1_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
481 		sblock.fs_ffs1_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
482 		sblock.fs_ffs1_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
483 		sblock.fs_ffs1_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
484 	}
485 
486 	/*
487 	 * Dump out summary information about file system.
488 	 */
489 	if (!mfs) {
490 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
491 		printf("%s: %.1fMB in %jd sectors of %lld bytes\n", fsys,
492 		    (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
493 		    (intmax_t)fsbtodb(&sblock, sblock.fs_size), sectorsize);
494 		printf("%d cylinder groups of %.2fMB, %d blocks, %d"
495 		    " inodes each\n", sblock.fs_ncg,
496 		    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
497 		    sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
498 #undef B2MBFACTOR
499 		checksz();
500 	}
501 
502 	/*
503 	 * Wipe out old FFS1 superblock if necessary.
504 	 */
505 	if (Oflag >= 2) {
506 		union fs_u *fsun1;
507 		struct fs *fs1;
508 
509 		fsun1 = calloc(1, sizeof(union fs_u));
510 		if (fsun1 == NULL)
511 			err(39, "calloc");
512 		fs1 = &fsun1->fs;
513 		rdfs(SBLOCK_UFS1 / DEV_BSIZE, SBSIZE, (char *)fs1);
514 		if (fs1->fs_magic == FS_UFS1_MAGIC) {
515 			fs1->fs_magic = FS_BAD_MAGIC;
516 			wtfs(SBLOCK_UFS1 / DEV_BSIZE, SBSIZE, (char *)fs1);
517 		}
518 		free(fsun1);
519 	}
520 
521 	wtfs((int)sblock.fs_sblockloc / DEV_BSIZE, SBSIZE, (char *)&sblock);
522 	sblock.fs_magic = (Oflag <= 1) ? FS_UFS1_MAGIC : FS_UFS2_MAGIC;
523 
524 	/*
525 	 * Now build the cylinders group blocks and
526 	 * then print out indices of cylinder groups.
527 	 */
528 	if (!quiet)
529 		printf("super-block backups (for fsck -b #) at:\n");
530 #ifndef STANDALONE
531 	else if (!mfs && isatty(STDIN_FILENO)) {
532 		signal(SIGINFO, siginfo);
533 		cur_fsys = fsys;
534 	}
535 #endif
536 	i = 0;
537 	width = charsperline();
538 	/*
539 	* Allocate space for superblock, cylinder group map, and two sets of
540 	* inode blocks.
541 	*/
542 	if (sblock.fs_bsize < SBLOCKSIZE)
543 		iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
544 	else
545 		iobufsize = 4 * sblock.fs_bsize;
546 	if ((iobuf = malloc(iobufsize)) == 0)
547 		errx(38, "cannot allocate I/O buffer");
548 	bzero(iobuf, iobufsize);
549 	/*
550 	 * Make a copy of the superblock into the buffer that we will be
551 	 * writing out in each cylinder group.
552 	 */
553 	bcopy((char *)&sblock, iobuf, SBLOCKSIZE);
554 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
555 		cur_cylno = (sig_atomic_t)cylno;
556 		initcg(cylno, utime);
557 		if (quiet)
558 			continue;
559 		j = snprintf(tmpbuf, sizeof tmpbuf, " %lld,",
560 		    fsbtodb(&sblock, cgsblock(&sblock, cylno)));
561 		if (j >= sizeof tmpbuf)
562 			j = sizeof tmpbuf - 1;
563 		if (j == -1 || i+j >= width) {
564 			printf("\n");
565 			i = 0;
566 		}
567 		i += j;
568 		printf("%s", tmpbuf);
569 		fflush(stdout);
570 	}
571 	if (!quiet)
572 		printf("\n");
573 	if (Nflag && !mfs)
574 		exit(0);
575 	/*
576 	 * Now construct the initial file system, then write out the superblock.
577 	 */
578 	if (Oflag <= 1) {
579 		if (fsinit1(utime, mfsmode, mfsuid, mfsgid))
580 			errx(32, "fsinit1 failed");
581 		sblock.fs_ffs1_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
582 		sblock.fs_ffs1_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
583 		sblock.fs_ffs1_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
584 		sblock.fs_ffs1_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
585 	} else {
586 		if (fsinit2(utime))
587 			errx(32, "fsinit2 failed");
588 	}
589 
590 	wtfs((int)sblock.fs_sblockloc / DEV_BSIZE, SBSIZE, (char *)&sblock);
591 
592 	for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
593 		wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
594 		    sblock.fs_cssize - i < sblock.fs_bsize ?
595 		    sblock.fs_cssize - i : sblock.fs_bsize,
596 		    ((char *)fscs) + i);
597 
598 	/*
599 	 * Update information about this partition in pack label, to that it may
600 	 * be updated on disk.
601 	 */
602 	pp->p_fstype = FS_BSDFFS;
603 	pp->p_fragblock =
604 	    DISKLABELV1_FFS_FRAGBLOCK(sblock.fs_fsize, sblock.fs_frag);
605 	pp->p_cpg = sblock.fs_cpg;
606 }
607 
608 /*
609  * Initialize a cylinder group.
610  */
611 void
612 initcg(int cylno, time_t utime)
613 {
614 	int i, j, d, dlower, dupper, blkno, start;
615 	daddr64_t cbase, dmax;
616 	struct ufs1_dinode *dp1;
617 	struct ufs2_dinode *dp2;
618 	struct csum *cs;
619 
620 	/*
621 	 * Determine block bounds for cylinder group.  Allow space for
622 	 * super block summary information in first cylinder group.
623 	 */
624 	cbase = cgbase(&sblock, cylno);
625 	dmax = cbase + sblock.fs_fpg;
626 	if (dmax > sblock.fs_size)
627 		dmax = sblock.fs_size;
628 	if (fsbtodb(&sblock, cgsblock(&sblock, cylno)) + iobufsize / sectorsize
629 	    > fssize)
630 		errx(40, "inode table does not fit in cylinder group");
631 
632 	dlower = cgsblock(&sblock, cylno) - cbase;
633 	dupper = cgdmin(&sblock, cylno) - cbase;
634 	if (cylno == 0)
635 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
636 	cs = &fscs[cylno];
637 	memset(&acg, 0, sblock.fs_cgsize);
638 	acg.cg_ffs2_time = utime;
639 	acg.cg_magic = CG_MAGIC;
640 	acg.cg_cgx = cylno;
641 	acg.cg_ffs2_niblk = sblock.fs_ipg;
642 	acg.cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
643 	acg.cg_ndblk = dmax - cbase;
644 
645 	start = sizeof(struct cg);
646 	if (Oflag <= 1) {
647 		/* Hack to maintain compatibility with old fsck. */
648 		if (cylno == sblock.fs_ncg - 1)
649 			acg.cg_ncyl = 0;
650 		else
651 			acg.cg_ncyl = sblock.fs_cpg;
652 		acg.cg_time = acg.cg_ffs2_time;
653 		acg.cg_ffs2_time = 0;
654 		acg.cg_niblk = acg.cg_ffs2_niblk;
655 		acg.cg_ffs2_niblk = 0;
656 		acg.cg_initediblk = 0;
657 		acg.cg_btotoff = start;
658 		acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
659 		acg.cg_iusedoff = acg.cg_boff +
660 		    sblock.fs_cpg * sizeof(u_int16_t);
661 	} else {
662 		acg.cg_iusedoff = start;
663 	}
664 
665 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
666 	acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT);
667 	if (acg.cg_nextfreeoff > sblock.fs_cgsize)
668 		errx(37, "panic: cylinder group too big: %d > %d",
669 		    acg.cg_nextfreeoff, sblock.fs_cgsize);
670 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
671 	if (cylno == 0) {
672 		for (i = 0; i < ROOTINO; i++) {
673 			setbit(cg_inosused(&acg), i);
674 			acg.cg_cs.cs_nifree--;
675 		}
676 	}
677 	if (cylno > 0) {
678 		/*
679 		 * In cylno 0, space is reserved for boot and super blocks.
680 		 */
681 		for (d = 0; d < dlower; d += sblock.fs_frag) {
682 			blkno = d / sblock.fs_frag;
683 			setblock(&sblock, cg_blksfree(&acg), blkno);
684 			acg.cg_cs.cs_nbfree++;
685 			if (Oflag <= 1) {
686 				cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
687 				cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
688 				    [cbtorpos(&sblock, d)]++;
689 			}
690 		}
691 	}
692 	if ((i = dupper % sblock.fs_frag)) {
693 		acg.cg_frsum[sblock.fs_frag - i]++;
694 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
695 			setbit(cg_blksfree(&acg), dupper);
696 			acg.cg_cs.cs_nffree++;
697 		}
698 	}
699 	for (d = dupper;
700 	    d + sblock.fs_frag <= acg.cg_ndblk;
701 	    d += sblock.fs_frag) {
702 		blkno = d / sblock.fs_frag;
703 		setblock(&sblock, cg_blksfree(&acg), blkno);
704 		acg.cg_cs.cs_nbfree++;
705 		if (Oflag <= 1) {
706 			cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
707 			cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
708 			    [cbtorpos(&sblock, d)]++;
709 		}
710 	}
711 	if (d < acg.cg_ndblk) {
712 		acg.cg_frsum[acg.cg_ndblk - d]++;
713 		for (; d < acg.cg_ndblk; d++) {
714 			setbit(cg_blksfree(&acg), d);
715 			acg.cg_cs.cs_nffree++;
716 		}
717 	}
718 	*cs = acg.cg_cs;
719 
720 	/*
721 	 * Write out the duplicate superblock, the cylinder group map
722 	 * and two blocks worth of inodes in a single write.
723 	 */
724 	start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
725 	bcopy((char *)&acg, &iobuf[start], sblock.fs_cgsize);
726 	start += sblock.fs_bsize;
727 	dp1 = (struct ufs1_dinode *)(&iobuf[start]);
728 	dp2 = (struct ufs2_dinode *)(&iobuf[start]);
729 	for (i = MIN(sblock.fs_ipg, 2 * INOPB(&sblock)); i != 0; i--) {
730 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
731 			dp1->di_gen = (u_int32_t)arc4random();
732 			dp1++;
733 		} else {
734 			dp2->di_gen = (u_int32_t)arc4random();
735 			dp2++;
736 		}
737 	}
738 	wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
739 
740 	if (Oflag <= 1) {
741 		/* Initialize inodes for FFS1. */
742 		for (i = 2 * sblock.fs_frag;
743 		    i < sblock.fs_ipg / INOPF(&sblock);
744 		    i += sblock.fs_frag) {
745 			dp1 = (struct ufs1_dinode *)(&iobuf[start]);
746 			for (j = 0; j < INOPB(&sblock); j++) {
747 				dp1->di_gen = (u_int32_t)arc4random();
748 				dp1++;
749 			}
750 			wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
751 			    sblock.fs_bsize, &iobuf[start]);
752 		}
753 	}
754 }
755 
756 #define PREDEFDIR 2
757 
758 struct direct root_dir[] = {
759 	{ ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
760 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
761 };
762 struct odirect {
763 	u_int32_t d_ino;
764 	u_int16_t d_reclen;
765 	u_int16_t d_namlen;
766 	u_char	d_name[MAXNAMLEN + 1];
767 } oroot_dir[] = {
768 	{ ROOTINO, sizeof(struct direct), 1, "." },
769 	{ ROOTINO, sizeof(struct direct), 2, ".." },
770 };
771 
772 int
773 fsinit1(time_t utime, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
774 {
775 	union dinode node;
776 
777 	/*
778 	 * Initialize the node
779 	 */
780 	memset(&node, 0, sizeof(node));
781 	node.dp1.di_atime = utime;
782 	node.dp1.di_mtime = utime;
783 	node.dp1.di_ctime = utime;
784 
785 	/*
786 	 * Create the root directory.
787 	 */
788 	if (mfs) {
789 		node.dp1.di_mode = IFDIR | mfsmode;
790 		node.dp1.di_uid = mfsuid;
791 		node.dp1.di_gid = mfsgid;
792 	} else {
793 		node.dp1.di_mode = IFDIR | UMASK;
794 		node.dp1.di_uid = geteuid();
795 		node.dp1.di_gid = getegid();
796 	}
797 	node.dp1.di_nlink = PREDEFDIR;
798 	if (Oflag == 0)
799 		node.dp1.di_size = makedir((struct direct *)oroot_dir,
800 		    PREDEFDIR);
801 	else
802 		node.dp1.di_size = makedir(root_dir, PREDEFDIR);
803 	node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
804 	if (node.dp1.di_db[0] == 0)
805 		return (1);
806 
807 	node.dp1.di_blocks = btodb(fragroundup(&sblock, node.dp1.di_size));
808 
809 	wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, iobuf);
810 	iput(&node, ROOTINO);
811 
812 #ifdef notyet
813 	/*
814 	* Create the .snap directory.
815 	*/
816 	node.dp1.di_mode |= 020;
817 	node.dp1.di_gid = gid;
818 	node.dp1.di_nlink = SNAPLINKCNT;
819 	node.dp1.di_size = makedir(snap_dir, SNAPLINKCNT);
820 
821 	node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
822 	if (node.dp1.di_db[0] == 0)
823 		return (1);
824 
825 	node.dp1.di_blocks = btodb(fragroundup(&sblock, node.dp1.di_size));
826 
827 	wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, iobuf);
828 	iput(&node, ROOTINO + 1);
829 #endif
830 	return (0);
831 }
832 
833 int
834 fsinit2(time_t utime)
835 {
836 	union dinode node;
837 
838 	/*
839 	 * Initialize the node.
840 	 */
841 	memset(&node, 0, sizeof(node));
842 	node.dp2.di_atime = utime;
843 	node.dp2.di_mtime = utime;
844 	node.dp2.di_ctime = utime;
845 
846 	/*
847 	 * Create the root directory.
848 	 */
849 	node.dp2.di_mode = IFDIR | UMASK;
850 	node.dp2.di_uid = geteuid();
851 	node.dp2.di_gid = getegid();
852 	node.dp2.di_nlink = PREDEFDIR;
853 	node.dp2.di_size = makedir(root_dir, PREDEFDIR);
854 
855 	node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
856 	if (node.dp2.di_db[0] == 0)
857 		return (1);
858 
859 	node.dp2.di_blocks = btodb(fragroundup(&sblock, node.dp2.di_size));
860 
861 	wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, iobuf);
862 	iput(&node, ROOTINO);
863 
864 #ifdef notyet
865 	/*
866 	 * Create the .snap directory.
867 	 */
868 	node.dp2.di_mode |= 020;
869 	node.dp2.di_gid = gid;
870 	node.dp2.di_nlink = SNAPLINKCNT;
871 	node.dp2.di_size = makedir(snap_dir, SNAPLINKCNT);
872 
873 	node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
874 	if (node.dp2.di_db[0] == 0)
875 		return (1);
876 
877 	node.dp2.di_blocks = btodb(fragroundup(&sblock, node.dp2.di_size));
878 
879 	wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, iobuf);
880 	iput(&node, ROOTINO + 1);
881 #endif
882 	return (0);
883 }
884 
885 /*
886  * construct a set of directory entries in "buf".
887  * return size of directory.
888  */
889 int
890 makedir(struct direct *protodir, int entries)
891 {
892 	char *cp;
893 	int i, spcleft;
894 
895 	spcleft = DIRBLKSIZ;
896 	for (cp = iobuf, i = 0; i < entries - 1; i++) {
897 		protodir[i].d_reclen = DIRSIZ(0, &protodir[i]);
898 		memcpy(cp, &protodir[i], protodir[i].d_reclen);
899 		cp += protodir[i].d_reclen;
900 		spcleft -= protodir[i].d_reclen;
901 	}
902 	protodir[i].d_reclen = spcleft;
903 	memcpy(cp, &protodir[i], DIRSIZ(0, &protodir[i]));
904 	return (DIRBLKSIZ);
905 }
906 
907 /*
908  * allocate a block or frag
909  */
910 daddr64_t
911 alloc(int size, int mode)
912 {
913 	int i, frag;
914 	daddr64_t d, blkno;
915 
916 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
917 	    (char *)&acg);
918 	if (acg.cg_magic != CG_MAGIC) {
919 		warnx("cg 0: bad magic number");
920 		return (0);
921 	}
922 	if (acg.cg_cs.cs_nbfree == 0) {
923 		warnx("first cylinder group ran out of space");
924 		return (0);
925 	}
926 	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
927 		if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
928 			goto goth;
929 	warnx("internal error: can't find block in cyl 0");
930 	return (0);
931 goth:
932 	blkno = fragstoblks(&sblock, d);
933 	clrblock(&sblock, cg_blksfree(&acg), blkno);
934 	acg.cg_cs.cs_nbfree--;
935 	sblock.fs_cstotal.cs_nbfree--;
936 	fscs[0].cs_nbfree--;
937 	if (mode & IFDIR) {
938 		acg.cg_cs.cs_ndir++;
939 		sblock.fs_cstotal.cs_ndir++;
940 		fscs[0].cs_ndir++;
941 	}
942 	if (Oflag <= 1) {
943 		cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
944 		cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
945 		    [cbtorpos(&sblock, d)]--;
946 	}
947 	if (size != sblock.fs_bsize) {
948 		frag = howmany(size, sblock.fs_fsize);
949 		fscs[0].cs_nffree += sblock.fs_frag - frag;
950 		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
951 		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
952 		acg.cg_frsum[sblock.fs_frag - frag]++;
953 		for (i = frag; i < sblock.fs_frag; i++)
954 			setbit(cg_blksfree(&acg), d + i);
955 	}
956 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
957 	    (char *)&acg);
958 	return (d);
959 }
960 
961 /*
962  * Allocate an inode on the disk
963  */
964 void
965 iput(union dinode *ip, ino_t ino)
966 {
967 	daddr64_t d;
968 
969 	if (Oflag <= 1)
970 		ip->dp1.di_gen = (u_int32_t)arc4random();
971 	else
972 		ip->dp2.di_gen = (u_int32_t)arc4random();
973 
974 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
975 	    (char *)&acg);
976 	if (acg.cg_magic != CG_MAGIC)
977 		errx(41, "cg 0: bad magic number");
978 
979 	acg.cg_cs.cs_nifree--;
980 	setbit(cg_inosused(&acg), ino);
981 
982 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
983 	    (char *)&acg);
984 
985 	sblock.fs_cstotal.cs_nifree--;
986 	fscs[0].cs_nifree--;
987 	if (ino >= sblock.fs_ipg * sblock.fs_ncg)
988 		errx(32, "fsinit: inode value %d out of range", ino);
989 	d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
990 	rdfs(d, sblock.fs_bsize, iobuf);
991 
992 	if (Oflag <= 1)
993 		((struct ufs1_dinode *)iobuf)[ino_to_fsbo(&sblock, ino)] =
994 		    ip->dp1;
995 	else
996 		((struct ufs2_dinode *)iobuf)[ino_to_fsbo(&sblock, ino)] =
997 		    ip->dp2;
998 
999 	wtfs(d, sblock.fs_bsize, iobuf);
1000 }
1001 
1002 /*
1003  * read a block from the file system
1004  */
1005 void
1006 rdfs(daddr64_t bno, int size, void *bf)
1007 {
1008 	int n;
1009 
1010 	if (mfs) {
1011 		memcpy(bf, membase + bno * DEV_BSIZE, size);
1012 		return;
1013 	}
1014 	n = pread(fsi, bf, size, (off_t)bno * DEV_BSIZE);
1015 	if (n != size) {
1016 		err(34, "rdfs: read error on block %lld", bno);
1017 	}
1018 }
1019 
1020 /*
1021  * write a block to the file system
1022  */
1023 void
1024 wtfs(daddr64_t bno, int size, void *bf)
1025 {
1026 	int n;
1027 
1028 	if (mfs) {
1029 		memcpy(membase + bno * DEV_BSIZE, bf, size);
1030 		return;
1031 	}
1032 	if (Nflag)
1033 		return;
1034 	n = pwrite(fso, bf, size, (off_t)bno * DEV_BSIZE);
1035 	if (n != size) {
1036 		err(36, "wtfs: write error on block %lld", bno);
1037 	}
1038 }
1039 
1040 /*
1041  * check if a block is available
1042  */
1043 int
1044 isblock(struct fs *fs, unsigned char *cp, int h)
1045 {
1046 	unsigned char mask;
1047 
1048 	switch (fs->fs_frag) {
1049 	case 8:
1050 		return (cp[h] == 0xff);
1051 	case 4:
1052 		mask = 0x0f << ((h & 0x1) << 2);
1053 		return ((cp[h >> 1] & mask) == mask);
1054 	case 2:
1055 		mask = 0x03 << ((h & 0x3) << 1);
1056 		return ((cp[h >> 2] & mask) == mask);
1057 	case 1:
1058 		mask = 0x01 << (h & 0x7);
1059 		return ((cp[h >> 3] & mask) == mask);
1060 	default:
1061 #ifdef STANDALONE
1062 		printf("isblock bad fs_frag %d\n", fs->fs_frag);
1063 #else
1064 		warnx("isblock bad fs_frag %d", fs->fs_frag);
1065 #endif
1066 		return (0);
1067 	}
1068 }
1069 
1070 /*
1071  * take a block out of the map
1072  */
1073 void
1074 clrblock(struct fs *fs, unsigned char *cp, int h)
1075 {
1076 	switch ((fs)->fs_frag) {
1077 	case 8:
1078 		cp[h] = 0;
1079 		return;
1080 	case 4:
1081 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1082 		return;
1083 	case 2:
1084 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1085 		return;
1086 	case 1:
1087 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
1088 		return;
1089 	default:
1090 #ifdef STANDALONE
1091 		printf("clrblock bad fs_frag %d\n", fs->fs_frag);
1092 #else
1093 		warnx("clrblock bad fs_frag %d", fs->fs_frag);
1094 #endif
1095 		return;
1096 	}
1097 }
1098 
1099 /*
1100  * put a block into the map
1101  */
1102 void
1103 setblock(struct fs *fs, unsigned char *cp, int h)
1104 {
1105 	switch (fs->fs_frag) {
1106 	case 8:
1107 		cp[h] = 0xff;
1108 		return;
1109 	case 4:
1110 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1111 		return;
1112 	case 2:
1113 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1114 		return;
1115 	case 1:
1116 		cp[h >> 3] |= (0x01 << (h & 0x7));
1117 		return;
1118 	default:
1119 #ifdef STANDALONE
1120 		printf("setblock bad fs_frag %d\n", fs->fs_frag);
1121 #else
1122 		warnx("setblock bad fs_frag %d", fs->fs_frag);
1123 #endif
1124 		return;
1125 	}
1126 }
1127 
1128 /*
1129  * Determine the number of characters in a
1130  * single line.
1131  */
1132 static int
1133 charsperline(void)
1134 {
1135 	int columns;
1136 	char *cp;
1137 	struct winsize ws;
1138 
1139 	columns = 0;
1140 	if (ioctl(0, TIOCGWINSZ, &ws) != -1)
1141 		columns = ws.ws_col;
1142 	if (columns == 0 && (cp = getenv("COLUMNS")))
1143 		columns = atoi(cp);
1144 	if (columns == 0)
1145 		columns = 80;   /* last resort */
1146 	return columns;
1147 }
1148 
1149 static int
1150 ilog2(int val)
1151 {
1152 	int n;
1153 
1154 	for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
1155 		if (1 << n == val)
1156 			return (n);
1157 
1158 	errx(1, "ilog2: %d is not a power of 2\n", val);
1159 }
1160 
1161 struct inoinfo {
1162         struct  inoinfo *i_nexthash;    /* next entry in hash chain */
1163         struct  inoinfo *i_child, *i_sibling, *i_parentp;
1164         size_t  i_isize;                /* size of inode */
1165         ino_t   i_number;               /* inode number of this entry */
1166         ino_t   i_parent;               /* inode number of parent */
1167 
1168         ino_t   i_dotdot;               /* inode number of `..' */
1169         u_int   i_numblks;              /* size of block array in bytes */
1170         daddr64_t       i_blks[1];              /* actually longer */
1171 };
1172 
1173 static void
1174 checksz(void)
1175 {
1176 	unsigned long long allocate, maxino, maxfsblock, ndir, bound;
1177 	int mib[2];
1178 	size_t len;
1179 
1180 	mib[0] = CTL_HW;
1181 	mib[1] = HW_PHYSMEM64;
1182 	len = sizeof(bound);
1183 
1184 	if (sysctl(mib, 2, &bound, &len, NULL, 0) != 0)
1185 		err(1, "can't get physmem");
1186 	bound = MIN(MAXDSIZ, bound);
1187 
1188 	allocate = 0;
1189 	maxino = sblock.fs_ncg * (unsigned long long)sblock.fs_ipg;
1190 	maxfsblock = sblock.fs_size;
1191 	ndir = maxino / avgfilesperdir;
1192 
1193 	allocate += roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
1194 	allocate += (maxino + 1) * 3;
1195 	allocate += sblock.fs_ncg * sizeof(long);
1196 	allocate += (MAX(ndir, 128) + 10) * sizeof(struct inoinfo);
1197 	allocate += MAX(ndir, 128) * sizeof(struct inoinfo);
1198 
1199 	if (allocate > bound)
1200 		warnx("warning: fsck_ffs will need %lluMB; "
1201 		    "min(MAXDSIZ,physmem) is %lluMB",
1202 		    allocate / (1024ULL * 1024ULL),
1203 		    bound / (1024ULL * 1024ULL));
1204 }
1205