GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/newfs/mkfs.c Lines: 0 496 0.0 %
Date: 2016-12-06 Branches: 0 297 0.0 %

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