GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/fsck_ffs/setup.c Lines: 0 368 0.0 %
Date: 2016-12-06 Branches: 0 348 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: setup.c,v 1.60 2015/11/24 21:42:54 deraadt Exp $	*/
2
/*	$NetBSD: setup.c,v 1.27 1996/09/27 22:45:19 christos Exp $	*/
3
4
/*
5
 * Copyright (c) 1980, 1986, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include <sys/param.h>	/* MAXBSIZE DEV_BSIZE roundup */
34
#define DKTYPENAMES
35
#include <sys/time.h>
36
#include <ufs/ufs/dinode.h>
37
#include <ufs/ffs/fs.h>
38
#include <sys/stat.h>
39
#include <sys/ioctl.h>
40
#include <sys/dkio.h>
41
#include <sys/disklabel.h>
42
43
#include <errno.h>
44
#include <fcntl.h>
45
#include <stdio.h>
46
#include <stdlib.h>
47
#include <unistd.h>
48
#include <string.h>
49
#include <util.h>
50
#include <limits.h>
51
#include <ctype.h>
52
#include <err.h>
53
54
#include "fsck.h"
55
#include "extern.h"
56
#include "fsutil.h"
57
58
#define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
59
60
#define altsblock (*asblk.b_un.b_fs)
61
#define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
62
63
void badsb(int, char *);
64
int calcsb(char *, int, struct fs *, struct disklabel *);
65
static struct disklabel *getdisklabel(char *, int);
66
static int readsb(int);
67
static int cmpsb(struct fs *, struct fs *);
68
static char rdevname[PATH_MAX];
69
70
long numdirs, listmax, inplast;
71
72
/*
73
 * Possible locations for the superblock.
74
 */
75
static const int sbtry[] = SBLOCKSEARCH;
76
/* locations the 1st alternate sb can be at */
77
static const int altsbtry[] = { 32, 64, 128, 144, 160, 192, 256 };
78
79
int
80
setup(char *dev)
81
{
82
	long cg, size, asked, i, j;
83
	size_t bmapsize;
84
	struct disklabel *lp;
85
	off_t sizepb;
86
	struct stat statb;
87
	struct fs proto;
88
	int doskipclean;
89
	int32_t maxsymlinklen, nindir, inopb;
90
	u_int64_t maxfilesize;
91
	char *realdev;
92
93
	havesb = 0;
94
	fswritefd = fsreadfd = -1;
95
	doskipclean = skipclean;
96
	if ((fsreadfd = opendev(dev, O_RDONLY, 0, &realdev)) < 0) {
97
		printf("Can't open %s: %s\n", dev, strerror(errno));
98
		return (0);
99
	}
100
	if (strncmp(dev, realdev, PATH_MAX) != 0) {
101
		blockcheck(unrawname(realdev));
102
		strlcpy(rdevname, realdev, sizeof(rdevname));
103
		setcdevname(rdevname, dev, preen);
104
105
		if (!hotroot())
106
			if (pledge("stdio rpath wpath getpw disklabel cpath", NULL) == -1)
107
				err(1, "pledge");
108
	}
109
	if (fstat(fsreadfd, &statb) < 0) {
110
		printf("Can't stat %s: %s\n", realdev, strerror(errno));
111
		close(fsreadfd);
112
		return (0);
113
	}
114
	if (!S_ISCHR(statb.st_mode)) {
115
		pfatal("%s is not a character device", realdev);
116
		if (reply("CONTINUE") == 0) {
117
			close(fsreadfd);
118
			return (0);
119
		}
120
	}
121
	if (preen == 0) {
122
		printf("** %s", realdev);
123
		if (strncmp(dev, realdev, PATH_MAX) != 0)
124
			printf(" (%s)", dev);
125
	}
126
	if (nflag || (fswritefd = opendev(dev, O_WRONLY, 0, NULL)) < 0) {
127
		fswritefd = -1;
128
		if (preen)
129
			pfatal("NO WRITE ACCESS");
130
		printf(" (NO WRITE)");
131
	}
132
	if (preen == 0)
133
		printf("\n");
134
	fsmodified = 0;
135
	lfdir = 0;
136
	initbarea(&sblk);
137
	initbarea(&asblk);
138
	sblk.b_un.b_buf = malloc(SBSIZE);
139
	asblk.b_un.b_buf = malloc(SBSIZE);
140
	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
141
		errexit("cannot allocate space for superblock\n");
142
	if ((lp = getdisklabel(NULL, fsreadfd)) != NULL)
143
		secsize = lp->d_secsize;
144
	else
145
		secsize = DEV_BSIZE;
146
147
	if (!hotroot()) {
148
#ifndef SMALL
149
		if (pledge("stdio getpw rpath cpath wpath", NULL) == -1)
150
			err(1, "pledge");
151
#else
152
		if (pledge("stdio rpath cpath wpath", NULL) == -1)
153
			err(1, "pledge");
154
#endif
155
	}
156
157
	/*
158
	 * Read in the superblock, looking for alternates if necessary
159
	 */
160
	if (readsb(1) == 0) {
161
		if (bflag || preen || calcsb(realdev, fsreadfd, &proto, lp) == 0)
162
			return(0);
163
		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
164
			return (0);
165
		for (i = 0; i < sizeof(altsbtry) / sizeof(altsbtry[0]); i++) {
166
			bflag = altsbtry[i];
167
			/* proto partially setup by calcsb */
168
			if (readsb(0) != 0 &&
169
			    proto.fs_fsize == sblock.fs_fsize &&
170
			    proto.fs_bsize == sblock.fs_bsize)
171
				goto found;
172
		}
173
		for (cg = 0; cg < proto.fs_ncg; cg++) {
174
			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
175
			if (readsb(0) != 0)
176
				break;
177
		}
178
		if (cg >= proto.fs_ncg) {
179
			printf("%s %s\n%s %s\n%s %s\n",
180
			    "SEARCH FOR ALTERNATE SUPER-BLOCK",
181
			    "FAILED. YOU MUST USE THE",
182
			    "-b OPTION TO FSCK_FFS TO SPECIFY THE",
183
			    "LOCATION OF AN ALTERNATE",
184
			    "SUPER-BLOCK TO SUPPLY NEEDED",
185
			    "INFORMATION; SEE fsck_ffs(8).");
186
			return(0);
187
		}
188
found:
189
		doskipclean = 0;
190
		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
191
	}
192
	if (debug)
193
		printf("clean = %d\n", sblock.fs_clean);
194
	if (sblock.fs_clean & FS_ISCLEAN) {
195
		if (doskipclean) {
196
			pwarn("%sile system is clean; not checking\n",
197
			    preen ? "f" : "** F");
198
			return (-1);
199
		}
200
		if (!preen)
201
			pwarn("** File system is already clean\n");
202
	}
203
	maxfsblock = sblock.fs_size;
204
	maxino = sblock.fs_ncg * sblock.fs_ipg;
205
	sizepb = sblock.fs_bsize;
206
	maxfilesize = sblock.fs_bsize * NDADDR - 1;
207
	for (i = 0; i < NIADDR; i++) {
208
		sizepb *= NINDIR(&sblock);
209
		maxfilesize += sizepb;
210
	}
211
	/*
212
	 * Check and potentially fix certain fields in the super block.
213
	 */
214
	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
215
		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
216
		if (reply("SET TO DEFAULT") == 1) {
217
			sblock.fs_optim = FS_OPTTIME;
218
			sbdirty();
219
		}
220
	}
221
	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
222
		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
223
		    sblock.fs_minfree);
224
		if (reply("SET TO DEFAULT") == 1) {
225
			sblock.fs_minfree = 10;
226
			sbdirty();
227
		}
228
	}
229
	if (sblock.fs_npsect < sblock.fs_nsect ||
230
	    sblock.fs_npsect > sblock.fs_nsect*2) {
231
		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
232
		    sblock.fs_npsect);
233
		sblock.fs_npsect = sblock.fs_nsect;
234
		if (preen)
235
			printf(" (FIXED)\n");
236
		if (preen || reply("SET TO DEFAULT") == 1) {
237
			sbdirty();
238
			dirty(&asblk);
239
		}
240
	}
241
	if (sblock.fs_bmask != ~(sblock.fs_bsize - 1)) {
242
		pwarn("INCORRECT BMASK=%x IN SUPERBLOCK",
243
		    sblock.fs_bmask);
244
		sblock.fs_bmask = ~(sblock.fs_bsize - 1);
245
		if (preen)
246
			printf(" (FIXED)\n");
247
		if (preen || reply("FIX") == 1) {
248
			sbdirty();
249
			dirty(&asblk);
250
		}
251
	}
252
	if (sblock.fs_fmask != ~(sblock.fs_fsize - 1)) {
253
		pwarn("INCORRECT FMASK=%x IN SUPERBLOCK",
254
		    sblock.fs_fmask);
255
		sblock.fs_fmask = ~(sblock.fs_fsize - 1);
256
		if (preen)
257
			printf(" (FIXED)\n");
258
		if (preen || reply("FIX") == 1) {
259
			sbdirty();
260
			dirty(&asblk);
261
		}
262
	}
263
	if (1 << sblock.fs_bshift != sblock.fs_bsize) {
264
		pwarn("INCORRECT BSHIFT=%d IN SUPERBLOCK", sblock.fs_bshift);
265
		sblock.fs_bshift = ffs(sblock.fs_bsize) - 1;
266
		if (preen)
267
			printf(" (FIXED)\n");
268
		if (preen || reply("FIX") == 1) {
269
			sbdirty();
270
			dirty(&asblk);
271
		}
272
	}
273
	if (1 << sblock.fs_fshift != sblock.fs_fsize) {
274
		pwarn("INCORRECT FSHIFT=%d IN SUPERBLOCK", sblock.fs_fshift);
275
		sblock.fs_fshift = ffs(sblock.fs_fsize) - 1;
276
		if (preen)
277
			printf(" (FIXED)\n");
278
		if (preen || reply("FIX") == 1) {
279
			sbdirty();
280
			dirty(&asblk);
281
		}
282
	}
283
	if (sblock.fs_inodefmt < FS_44INODEFMT) {
284
		pwarn("Format of filesystem is too old.\n");
285
		pwarn("Must update to modern format using a version of fsck\n");
286
		pfatal("from before release 5.0 with the command ``fsck -c 2''\n");
287
		exit(8);
288
	}
289
	if (sblock.fs_maxfilesize != maxfilesize) {
290
		pwarn("INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK",
291
		    (unsigned long long)sblock.fs_maxfilesize);
292
		sblock.fs_maxfilesize = maxfilesize;
293
		if (preen)
294
			printf(" (FIXED)\n");
295
		if (preen || reply("FIX") == 1) {
296
			sbdirty();
297
			dirty(&asblk);
298
		}
299
	}
300
	maxsymlinklen = sblock.fs_magic == FS_UFS1_MAGIC ?
301
	    MAXSYMLINKLEN_UFS1 : MAXSYMLINKLEN_UFS2;
302
	if (sblock.fs_maxsymlinklen != maxsymlinklen) {
303
		pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
304
		    sblock.fs_maxsymlinklen);
305
		sblock.fs_maxsymlinklen = maxsymlinklen;
306
		if (preen)
307
			printf(" (FIXED)\n");
308
		if (preen || reply("FIX") == 1) {
309
			sbdirty();
310
			dirty(&asblk);
311
		}
312
	}
313
	if (sblock.fs_qbmask != ~sblock.fs_bmask) {
314
		pwarn("INCORRECT QBMASK=%lx IN SUPERBLOCK",
315
		    (unsigned long)sblock.fs_qbmask);
316
		sblock.fs_qbmask = ~sblock.fs_bmask;
317
		if (preen)
318
			printf(" (FIXED)\n");
319
		if (preen || reply("FIX") == 1) {
320
			sbdirty();
321
			dirty(&asblk);
322
		}
323
	}
324
	if (sblock.fs_qfmask != ~sblock.fs_fmask) {
325
		pwarn("INCORRECT QFMASK=%lx IN SUPERBLOCK",
326
		    (unsigned long)sblock.fs_qfmask);
327
		sblock.fs_qfmask = ~sblock.fs_fmask;
328
		if (preen)
329
			printf(" (FIXED)\n");
330
		if (preen || reply("FIX") == 1) {
331
			sbdirty();
332
			dirty(&asblk);
333
		}
334
	}
335
	if (sblock.fs_cgsize != fragroundup(&sblock, CGSIZE(&sblock))) {
336
		pwarn("INCONSISTENT CGSIZE=%d\n", sblock.fs_cgsize);
337
		sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
338
		if (preen)
339
			printf(" (FIXED)\n");
340
		if (preen || reply("FIX") == 1) {
341
			sbdirty();
342
			dirty(&asblk);
343
		}
344
	}
345
	if (sblock.fs_magic == FS_UFS2_MAGIC)
346
		inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
347
	else
348
		inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
349
	if (INOPB(&sblock) != inopb) {
350
		pwarn("INCONSISTENT INOPB=%d\n", INOPB(&sblock));
351
		sblock.fs_inopb = inopb;
352
		if (preen)
353
			printf(" (FIXED)\n");
354
		if (preen || reply("FIX") == 1) {
355
			sbdirty();
356
			dirty(&asblk);
357
		}
358
	}
359
	if (sblock.fs_magic == FS_UFS2_MAGIC)
360
		nindir = sblock.fs_bsize / sizeof(int64_t);
361
	else
362
		nindir = sblock.fs_bsize / sizeof(int32_t);
363
	if (NINDIR(&sblock) != nindir) {
364
		pwarn("INCONSISTENT NINDIR=%d\n", NINDIR(&sblock));
365
		sblock.fs_nindir = nindir;
366
		if (preen)
367
			printf(" (FIXED)\n");
368
		if (preen || reply("FIX") == 1) {
369
			sbdirty();
370
			dirty(&asblk);
371
		}
372
	}
373
	if (asblk.b_dirty && !bflag) {
374
		memcpy(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
375
		flush(fswritefd, &asblk);
376
	}
377
	/*
378
	 * read in the summary info.
379
	 */
380
	asked = 0;
381
	sblock.fs_csp = calloc(1, sblock.fs_cssize);
382
	if (sblock.fs_csp == NULL) {
383
		printf("cannot alloc %u bytes for cylinder group summary area\n",
384
		    (unsigned)sblock.fs_cssize);
385
		goto badsblabel;
386
	}
387
	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
388
		size = sblock.fs_cssize - i < sblock.fs_bsize ?
389
		    sblock.fs_cssize - i : sblock.fs_bsize;
390
		if (bread(fsreadfd, (char *)sblock.fs_csp + i,
391
		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
392
		    size) != 0 && !asked) {
393
			pfatal("BAD SUMMARY INFORMATION");
394
			if (reply("CONTINUE") == 0) {
395
				ckfini(0);
396
				errexit("%s", "");
397
			}
398
			asked++;
399
		}
400
	}
401
	/*
402
	 * allocate and initialize the necessary maps
403
	 */
404
	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
405
	blockmap = calloc(bmapsize, sizeof(char));
406
	if (blockmap == NULL) {
407
		printf("cannot alloc %zu bytes for blockmap\n", bmapsize);
408
		goto badsblabel;
409
	}
410
	inostathead = calloc((unsigned)(sblock.fs_ncg),
411
	    sizeof(struct inostatlist));
412
	if (inostathead == NULL) {
413
		printf("cannot alloc %zu bytes for inostathead\n",
414
		    (unsigned)sblock.fs_ncg * sizeof(struct inostatlist));
415
		goto badsblabel;
416
	}
417
	numdirs = MAXIMUM(sblock.fs_cstotal.cs_ndir, 128);
418
	inplast = 0;
419
	listmax = numdirs + 10;
420
	inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *));
421
	if (inpsort == NULL) {
422
		printf("cannot alloc %zu bytes for inpsort\n",
423
		    (unsigned)listmax * sizeof(struct inoinfo *));
424
		goto badsblabel;
425
	}
426
	inphead = calloc((unsigned)numdirs, sizeof(struct inoinfo *));
427
	if (inphead == NULL) {
428
		printf("cannot alloc %zu bytes for inphead\n",
429
		    (unsigned)numdirs * sizeof(struct inoinfo *));
430
		goto badsblabel;
431
	}
432
	bufinit();
433
	if (sblock.fs_flags & FS_DOSOFTDEP)
434
		usedsoftdep = 1;
435
	else
436
		usedsoftdep = 0;
437
	return (1);
438
439
badsblabel:
440
	ckfini(0);
441
	return (0);
442
}
443
444
/*
445
 * Read in the super block and its summary info.
446
 */
447
static int
448
readsb(int listerr)
449
{
450
	daddr_t super = 0;
451
	int i;
452
453
	if (bflag) {
454
		super = bflag;
455
456
		if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
457
			return (0);
458
459
		if (sblock.fs_magic != FS_UFS1_MAGIC &&
460
		    sblock.fs_magic != FS_UFS2_MAGIC) {
461
			badsb(listerr, "MAGIC NUMBER WRONG");
462
			return (0);
463
		}
464
	} else {
465
		for (i = 0; sbtry[i] != -1; i++) {
466
			super = sbtry[i] / DEV_BSIZE;
467
468
			if (bread(fsreadfd, (char *)&sblock, super,
469
			    (long)SBSIZE) != 0)
470
				return (0);
471
472
			if (sblock.fs_magic != FS_UFS1_MAGIC &&
473
			    sblock.fs_magic != FS_UFS2_MAGIC)
474
				continue; /* Not a superblock */
475
476
			/*
477
			 * Do not look for an FFS1 file system at SBLOCK_UFS2.
478
			 * Doing so will find the wrong super-block for file
479
			 * systems with 64k block size.
480
			 */
481
			if (sblock.fs_magic == FS_UFS1_MAGIC &&
482
			    sbtry[i] == SBLOCK_UFS2)
483
				continue;
484
485
			if (sblock.fs_magic == FS_UFS2_MAGIC &&
486
			    sblock.fs_sblockloc != sbtry[i])
487
				continue; /* Not a superblock */
488
489
			break;
490
		}
491
492
		if (sbtry[i] == -1) {
493
			badsb(listerr, "MAGIC NUMBER WRONG");
494
			return (0);
495
		}
496
	}
497
498
	sblk.b_bno = super;
499
	sblk.b_size = SBSIZE;
500
501
	/*
502
	 * run a few consistency checks of the super block
503
	 */
504
	if (sblock.fs_ncg < 1) {
505
		badsb(listerr, "NCG OUT OF RANGE");
506
		return (0);
507
	}
508
	if (sblock.fs_cpg < 1) {
509
		badsb(listerr, "CPG OUT OF RANGE");
510
		return (0);
511
	}
512
	if (sblock.fs_magic == FS_UFS1_MAGIC) {
513
		if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
514
		    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
515
			badsb(listerr, "NCYL LESS THAN NCG*CPG");
516
			return (0);
517
		}
518
	}
519
	if (sblock.fs_sbsize > SBSIZE) {
520
		badsb(listerr, "SBSIZE PREPOSTEROUSLY LARGE");
521
		return (0);
522
	}
523
524
	if (!POWEROF2(sblock.fs_bsize) || sblock.fs_bsize < MINBSIZE ||
525
	    sblock.fs_bsize > MAXBSIZE) {
526
		badsb(listerr, "ILLEGAL BLOCK SIZE IN SUPERBLOCK");
527
		return (0);
528
	}
529
530
	if (!POWEROF2(sblock.fs_fsize) || sblock.fs_fsize > sblock.fs_bsize ||
531
	    sblock.fs_fsize < sblock.fs_bsize / MAXFRAG) {
532
		badsb(listerr, "ILLEGAL FRAGMENT SIZE IN SUPERBLOCK");
533
		return (0);
534
	}
535
536
	if (bflag)
537
		goto out;
538
	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
539
	if (asblk.b_errs)
540
		return (0);
541
	if (cmpsb(&sblock, &altsblock)) {
542
		if (debug) {
543
			long *nlp, *olp, *endlp;
544
545
			printf("superblock mismatches\n");
546
			nlp = (long *)&altsblock;
547
			olp = (long *)&sblock;
548
			endlp = olp + (sblock.fs_sbsize / sizeof *olp);
549
			for ( ; olp < endlp; olp++, nlp++) {
550
				if (*olp == *nlp)
551
					continue;
552
				printf("offset %d, original %ld, alternate %ld\n",
553
				    (int)(olp - (long *)&sblock), *olp, *nlp);
554
			}
555
		}
556
		badsb(listerr,
557
		    "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN LAST ALTERNATE");
558
		return (0);
559
	}
560
out:
561
	if (sblock.fs_magic == FS_UFS1_MAGIC) {
562
		sblock.fs_time = sblock.fs_ffs1_time;
563
		sblock.fs_size = sblock.fs_ffs1_size;
564
		sblock.fs_dsize = sblock.fs_ffs1_dsize;
565
		sblock.fs_csaddr = sblock.fs_ffs1_csaddr;
566
		sblock.fs_cstotal.cs_ndir = sblock.fs_ffs1_cstotal.cs_ndir;
567
		sblock.fs_cstotal.cs_nbfree = sblock.fs_ffs1_cstotal.cs_nbfree;
568
		sblock.fs_cstotal.cs_nifree = sblock.fs_ffs1_cstotal.cs_nifree;
569
		sblock.fs_cstotal.cs_nffree = sblock.fs_ffs1_cstotal.cs_nffree;
570
	}
571
	havesb = 1;
572
	return (1);
573
}
574
575
void
576
badsb(int listerr, char *s)
577
{
578
579
	if (!listerr)
580
		return;
581
	if (preen)
582
		printf("%s: ", cdevname());
583
	pfatal("BAD SUPER BLOCK: %s\n", s);
584
}
585
586
/*
587
 * Calculate a prototype superblock based on information in the disk label.
588
 * When done the cgsblock macro can be calculated and the fs_ncg field
589
 * can be used. Do NOT attempt to use other macros without verifying that
590
 * their needed information is available!
591
 */
592
int
593
calcsb(char *dev, int devfd, struct fs *fs, struct disklabel *lp)
594
{
595
	struct partition *pp;
596
	char *cp;
597
	int i;
598
599
	cp = strchr(dev, '\0');
600
	if ((cp == NULL || (cp[-1] < 'a' || cp[-1] >= 'a' + MAXPARTITIONS)) &&
601
	    !isdigit((unsigned char)cp[-1])) {
602
		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
603
		return (0);
604
	}
605
	cp--;
606
	if (lp == NULL)
607
		pfatal("%s: CANNOT READ DISKLABEL\n", dev);
608
	if (isdigit((unsigned char)*cp))
609
		pp = &lp->d_partitions[0];
610
	else
611
		pp = &lp->d_partitions[*cp - 'a'];
612
	if (pp->p_fstype != FS_BSDFFS) {
613
		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
614
		    dev, pp->p_fstype < FSMAXTYPES ?
615
		    fstypenames[pp->p_fstype] : "unknown");
616
		return (0);
617
	}
618
	memset(fs, 0, sizeof(struct fs));
619
	fs->fs_fsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock);
620
	fs->fs_frag = DISKLABELV1_FFS_FRAG(pp->p_fragblock);
621
	fs->fs_bsize = fs->fs_fsize * fs->fs_frag;
622
	fs->fs_cpg = pp->p_cpg;
623
	fs->fs_nspf = DL_SECTOBLK(lp, fs->fs_fsize / lp->d_secsize);
624
	/*
625
	 * fs->fs_size is in fragments, DL_GETPSIZE() is in disk sectors
626
	 * and fs_nspf is in DEV_BSIZE blocks. Shake well.
627
	 */
628
	fs->fs_size = DL_SECTOBLK(lp, DL_GETPSIZE(pp)) / fs->fs_nspf;
629
	fs->fs_ntrak = lp->d_ntracks;
630
	fs->fs_nsect = DL_SECTOBLK(lp, lp->d_nsectors);
631
	fs->fs_spc = DL_SECTOBLK(lp, lp->d_secpercyl);
632
	/* we can't use lp->d_sbsize, it is the max sb size */
633
	fs->fs_sblkno = roundup(
634
		howmany(lp->d_bbsize + SBSIZE, fs->fs_fsize),
635
		fs->fs_frag);
636
again:
637
	fs->fs_cgmask = 0xffffffff;
638
	for (i = fs->fs_ntrak; i > 1; i >>= 1)
639
		fs->fs_cgmask <<= 1;
640
	if (!POWEROF2(fs->fs_ntrak))
641
		fs->fs_cgmask <<= 1;
642
	fs->fs_cgoffset = roundup(
643
		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
644
	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
645
	fs->fs_ncg = howmany(DL_GETPSIZE(pp) / fs->fs_spc, fs->fs_cpg);
646
	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
647
		fs->fs_fsbtodb++;
648
	/*
649
	 * Mimic what mkfs is doing to get an acceptable cgsize,
650
	 * not all fields used by CGSIZE() are filled in, but it's a best
651
	 * effort anyway.
652
	 */
653
	if (CGSIZE(fs) > fs->fs_bsize && fs->fs_ntrak > 1) {
654
		fs->fs_ntrak >>= 1;
655
		fs->fs_spc >>= 1;
656
		goto again;
657
	}
658
	return (1);
659
}
660
661
static struct disklabel *
662
getdisklabel(char *s, int fd)
663
{
664
	static struct disklabel lab;
665
666
	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
667
		if (s == NULL)
668
			return (NULL);
669
		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
670
		errexit("%s: can't read disk label\n", s);
671
	}
672
	return (&lab);
673
}
674
675
/*
676
 * Compare two superblocks
677
 */
678
static int
679
cmpsb(struct fs *sb, struct fs *asb)
680
{
681
	/*
682
	 * Only compare fields which should be the same, and ignore ones
683
	 * likely to change to ensure future compatibility.
684
	 */
685
	if (asb->fs_sblkno != sb->fs_sblkno ||
686
	    asb->fs_cblkno != sb->fs_cblkno ||
687
	    asb->fs_iblkno != sb->fs_iblkno ||
688
	    asb->fs_dblkno != sb->fs_dblkno ||
689
	    asb->fs_cgoffset != sb->fs_cgoffset ||
690
	    asb->fs_cgmask != sb->fs_cgmask ||
691
	    asb->fs_ncg != sb->fs_ncg ||
692
	    asb->fs_bsize != sb->fs_bsize ||
693
	    asb->fs_fsize != sb->fs_fsize ||
694
	    asb->fs_frag != sb->fs_frag ||
695
	    asb->fs_bmask != sb->fs_bmask ||
696
	    asb->fs_fmask != sb->fs_fmask ||
697
	    asb->fs_bshift != sb->fs_bshift ||
698
	    asb->fs_fshift != sb->fs_fshift ||
699
	    asb->fs_fragshift != sb->fs_fragshift ||
700
	    asb->fs_fsbtodb != sb->fs_fsbtodb ||
701
	    asb->fs_sbsize != sb->fs_sbsize ||
702
	    asb->fs_nindir != sb->fs_nindir ||
703
	    asb->fs_inopb != sb->fs_inopb ||
704
	    asb->fs_cssize != sb->fs_cssize ||
705
	    asb->fs_cpg != sb->fs_cpg ||
706
	    asb->fs_ipg != sb->fs_ipg ||
707
	    asb->fs_fpg != sb->fs_fpg ||
708
	    asb->fs_magic != sb->fs_magic)
709
		    return (1);
710
	/* they're the same */
711
	return (0);
712
}