xref: /original-bsd/sbin/fsck/setup.c (revision 14ace923)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)setup.c	5.22 (Berkeley) 06/30/89";
9 #endif not lint
10 
11 #define DKTYPENAMES
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/vnode.h>
15 #include <ufs/inode.h>
16 #include <ufs/fs.h>
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
19 #include <sys/disklabel.h>
20 #include <sys/file.h>
21 #include <machine/endian.h>
22 #include <ctype.h>
23 #include "fsck.h"
24 
25 BUFAREA asblk;
26 #define altsblock (*asblk.b_un.b_fs)
27 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
28 
29 /*
30  * The size of a cylinder group is calculated by CGSIZE. The maximum size
31  * is limited by the fact that cylinder groups are at most one block.
32  * Its size is derived from the size of the maps maintained in the
33  * cylinder group and the (struct cg) size.
34  */
35 #define CGSIZE(fs) \
36     /* base cg */	(sizeof(struct cg) + \
37     /* blktot size */	(fs)->fs_cpg * sizeof(long) + \
38     /* blks size */	(fs)->fs_cpg * (fs)->fs_nrpos * sizeof(short) + \
39     /* inode map */	howmany((fs)->fs_ipg, NBBY) + \
40     /* block map */	howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY))
41 
42 char	*malloc(), *calloc();
43 char	*index();
44 struct	disklabel *getdisklabel();
45 
46 setup(dev)
47 	char *dev;
48 {
49 	dev_t rootdev;
50 	long cg, ncg, size, asked, i, j;
51 	struct disklabel *lp;
52 	struct stat statb;
53 	struct fs proto;
54 
55 	havesb = 0;
56 	if (stat("/", &statb) < 0)
57 		errexit("Can't stat root\n");
58 	rootdev = statb.st_dev;
59 	if (stat(dev, &statb) < 0) {
60 		perror(dev);
61 		printf("Can't stat %s\n", dev);
62 		return (0);
63 	}
64 	rawflg = 0;
65 	if ((statb.st_mode & S_IFMT) == S_IFBLK)
66 		;
67 	else if ((statb.st_mode & S_IFMT) == S_IFCHR)
68 		rawflg++;
69 	else {
70 		if (reply("file is not a block or character device; OK") == 0)
71 			return (0);
72 	}
73 	if (rootdev == statb.st_rdev)
74 		hotroot++;
75 	if ((dfile.rfdes = open(dev, O_RDONLY)) < 0) {
76 		perror(dev);
77 		printf("Can't open %s\n", dev);
78 		return (0);
79 	}
80 	if (preen == 0)
81 		printf("** %s", dev);
82 	if (nflag || (dfile.wfdes = open(dev, O_WRONLY)) < 0) {
83 		dfile.wfdes = -1;
84 		if (preen)
85 			pfatal("NO WRITE ACCESS");
86 		printf(" (NO WRITE)");
87 	}
88 	if (preen == 0)
89 		printf("\n");
90 	dfile.mod = 0;
91 	lfdir = 0;
92 	initbarea(&sblk);
93 	initbarea(&asblk);
94 	sblk.b_un.b_buf = malloc(SBSIZE);
95 	asblk.b_un.b_buf = malloc(SBSIZE);
96 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
97 		errexit("cannot allocate space for superblock\n");
98 	if (lp = getdisklabel((char *)NULL, dfile.rfdes))
99 		dev_bsize = secsize = lp->d_secsize;
100 	else
101 		dev_bsize = secsize = DEV_BSIZE;
102 	/*
103 	 * Read in the superblock, looking for alternates if necessary
104 	 */
105 	if (readsb(1) == 0) {
106 		if (bflag || preen || calcsb(dev, dfile.rfdes, &proto) == 0)
107 			return(0);
108 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
109 			return (0);
110 		for (cg = 0; cg < proto.fs_ncg; cg++) {
111 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
112 			if (readsb(0) != 0)
113 				break;
114 		}
115 		if (cg >= proto.fs_ncg) {
116 			printf("%s %s\n%s %s\n%s %s\n",
117 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
118 				"FAILED. YOU MUST USE THE",
119 				"-b OPTION TO FSCK TO SPECIFY THE",
120 				"LOCATION OF AN ALTERNATE",
121 				"SUPER-BLOCK TO SUPPLY NEEDED",
122 				"INFORMATION; SEE fsck(8).");
123 			return(0);
124 		}
125 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
126 	}
127 	fmax = sblock.fs_size;
128 	imax = sblock.fs_ncg * sblock.fs_ipg;
129 	/*
130 	 * Check and potentially fix certain fields in the super block.
131 	 */
132 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
133 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
134 		if (reply("SET TO DEFAULT") == 1) {
135 			sblock.fs_optim = FS_OPTTIME;
136 			sbdirty();
137 		}
138 	}
139 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
140 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
141 			sblock.fs_minfree);
142 		if (reply("SET TO DEFAULT") == 1) {
143 			sblock.fs_minfree = 10;
144 			sbdirty();
145 		}
146 	}
147 	if (sblock.fs_interleave < 1) {
148 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
149 			sblock.fs_interleave);
150 		sblock.fs_interleave = 1;
151 		if (preen)
152 			printf(" (FIXED)\n");
153 		if (preen || reply("SET TO DEFAULT") == 1) {
154 			sbdirty();
155 			dirty(&asblk);
156 		}
157 	}
158 	if (sblock.fs_npsect < sblock.fs_nsect) {
159 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
160 			sblock.fs_npsect);
161 		sblock.fs_npsect = sblock.fs_nsect;
162 		if (preen)
163 			printf(" (FIXED)\n");
164 		if (preen || reply("SET TO DEFAULT") == 1) {
165 			sbdirty();
166 			dirty(&asblk);
167 		}
168 	}
169 	if (cvtflag) {
170 		if (sblock.fs_postblformat == FS_42POSTBLFMT) {
171 			/*
172 			 * Requested to convert from old format to new format
173 			 */
174 			if (preen)
175 				pwarn("CONVERTING TO NEW FILE SYSTEM FORMAT\n");
176 			else if (!reply("CONVERT TO NEW FILE SYSTEM FORMAT"))
177 				return(0);
178 			sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
179 			sblock.fs_nrpos = 8;
180 			sblock.fs_postbloff =
181 			    (char *)(&sblock.fs_opostbl[0][0]) -
182 			    (char *)(&sblock.fs_link);
183 			sblock.fs_rotbloff = &sblock.fs_space[0] -
184 			    (u_char *)(&sblock.fs_link);
185 			sblock.fs_cgsize =
186 				fragroundup(&sblock, CGSIZE(&sblock));
187 			/*
188 			 * Planning now for future expansion.
189 			 */
190 #			if (BYTE_ORDER == BIG_ENDIAN)
191 				sblock.fs_qbmask.val[0] = 0;
192 				sblock.fs_qbmask.val[1] = ~sblock.fs_bmask;
193 				sblock.fs_qfmask.val[0] = 0;
194 				sblock.fs_qfmask.val[1] = ~sblock.fs_fmask;
195 #			endif /* BIG_ENDIAN */
196 #			if (BYTE_ORDER == LITTLE_ENDIAN)
197 				sblock.fs_qbmask.val[0] = ~sblock.fs_bmask;
198 				sblock.fs_qbmask.val[1] = 0;
199 				sblock.fs_qfmask.val[0] = ~sblock.fs_fmask;
200 				sblock.fs_qfmask.val[1] = 0;
201 #			endif /* LITTLE_ENDIAN */
202 			sbdirty();
203 			dirty(&asblk);
204 		} else if (sblock.fs_postblformat == FS_DYNAMICPOSTBLFMT) {
205 			/*
206 			 * Requested to convert from new format to old format
207 			 */
208 			if (sblock.fs_nrpos != 8 || sblock.fs_ipg > 2048 ||
209 			    sblock.fs_cpg > 32 || sblock.fs_cpc > 16) {
210 				printf(
211 				"PARAMETERS OF CURRENT FILE SYSTEM DO NOT\n\t");
212 				errexit(
213 				"ALLOW CONVERSION TO OLD FILE SYSTEM FORMAT\n");
214 			}
215 			if (preen)
216 				pwarn("CONVERTING TO OLD FILE SYSTEM FORMAT\n");
217 			else if (!reply("CONVERT TO OLD FILE SYSTEM FORMAT"))
218 				return(0);
219 			sblock.fs_postblformat = FS_42POSTBLFMT;
220 			sblock.fs_cgsize = fragroundup(&sblock,
221 			    sizeof(struct ocg) + howmany(sblock.fs_fpg, NBBY));
222 			sbdirty();
223 			dirty(&asblk);
224 		} else {
225 			errexit("UNKNOWN FILE SYSTEM FORMAT\n");
226 		}
227 	}
228 	if (asblk.b_dirty) {
229 		bcopy((char *)&sblock, (char *)&altsblock, sblock.fs_sbsize);
230 		flush(&dfile, &asblk);
231 	}
232 	/*
233 	 * read in the summary info.
234 	 */
235 	asked = 0;
236 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
237 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
238 		    sblock.fs_cssize - i : sblock.fs_bsize;
239 		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
240 		if (bread(&dfile, (char *)sblock.fs_csp[j],
241 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
242 		    size) != 0 && !asked) {
243 			pfatal("BAD SUMMARY INFORMATION");
244 			if (reply("CONTINUE") == 0)
245 				errexit("");
246 			asked++;
247 		}
248 	}
249 	/*
250 	 * allocate and initialize the necessary maps
251 	 */
252 	bmapsz = roundup(howmany(fmax, NBBY), sizeof(short));
253 	blockmap = calloc((unsigned)bmapsz, sizeof (char));
254 	if (blockmap == NULL) {
255 		printf("cannot alloc %d bytes for blockmap\n", bmapsz);
256 		goto badsb;
257 	}
258 	statemap = calloc((unsigned)(imax + 1), sizeof(char));
259 	if (statemap == NULL) {
260 		printf("cannot alloc %d bytes for statemap\n", imax + 1);
261 		goto badsb;
262 	}
263 	lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short));
264 	if (lncntp == NULL) {
265 		printf("cannot alloc %d bytes for lncntp\n",
266 		    (imax + 1) * sizeof(short));
267 		goto badsb;
268 	}
269 
270 	bufinit();
271 	return (1);
272 
273 badsb:
274 	ckfini();
275 	return (0);
276 }
277 
278 /*
279  * Read in the super block and its summary info.
280  */
281 readsb(listerr)
282 	int listerr;
283 {
284 	off_t sboff;
285 	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
286 
287 	if (bread(&dfile, (char *)&sblock, super, (long)SBSIZE) != 0)
288 		return (0);
289 	sblk.b_bno = super;
290 	sblk.b_size = SBSIZE;
291 	/*
292 	 * run a few consistency checks of the super block
293 	 */
294 	if (sblock.fs_magic != FS_MAGIC)
295 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
296 	if (sblock.fs_ncg < 1)
297 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
298 	if (sblock.fs_cpg < 1)
299 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
300 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
301 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
302 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
303 	if (sblock.fs_sbsize > SBSIZE)
304 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
305 	/*
306 	 * Compute block size that the filesystem is based on,
307 	 * according to fsbtodb, and adjust superblock block number
308 	 * so we can tell if this is an alternate later.
309 	 */
310 	super *= dev_bsize;
311 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
312 	sblk.b_bno = super / dev_bsize;
313 	/*
314 	 * Set all possible fields that could differ, then do check
315 	 * of whole super block against an alternate super block.
316 	 * When an alternate super-block is specified this check is skipped.
317 	 */
318 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
319 	if (asblk.b_errs)
320 		return (0);
321 	if (bflag) {
322 		havesb = 1;
323 		return (1);
324 	}
325 	altsblock.fs_link = sblock.fs_link;
326 	altsblock.fs_rlink = sblock.fs_rlink;
327 	altsblock.fs_time = sblock.fs_time;
328 	altsblock.fs_cstotal = sblock.fs_cstotal;
329 	altsblock.fs_cgrotor = sblock.fs_cgrotor;
330 	altsblock.fs_fmod = sblock.fs_fmod;
331 	altsblock.fs_clean = sblock.fs_clean;
332 	altsblock.fs_ronly = sblock.fs_ronly;
333 	altsblock.fs_flags = sblock.fs_flags;
334 	altsblock.fs_maxcontig = sblock.fs_maxcontig;
335 	altsblock.fs_minfree = sblock.fs_minfree;
336 	altsblock.fs_optim = sblock.fs_optim;
337 	altsblock.fs_rotdelay = sblock.fs_rotdelay;
338 	altsblock.fs_maxbpg = sblock.fs_maxbpg;
339 	bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
340 		sizeof sblock.fs_csp);
341 	bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
342 		sizeof sblock.fs_fsmnt);
343 	bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon,
344 		sizeof sblock.fs_sparecon);
345 	/*
346 	 * The following should not have to be copied.
347 	 */
348 	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
349 	altsblock.fs_interleave = sblock.fs_interleave;
350 	altsblock.fs_npsect = sblock.fs_npsect;
351 	altsblock.fs_nrpos = sblock.fs_nrpos;
352 	if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) {
353 		badsb(listerr,
354 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
355 		return (0);
356 	}
357 	havesb = 1;
358 	return (1);
359 }
360 
361 badsb(listerr, s)
362 	int listerr;
363 	char *s;
364 {
365 
366 	if (!listerr)
367 		return;
368 	if (preen)
369 		printf("%s: ", devname);
370 	pfatal("BAD SUPER BLOCK: %s\n", s);
371 }
372 
373 /*
374  * Calculate a prototype superblock based on information in the disk label.
375  * When done the cgsblock macro can be calculated and the fs_ncg field
376  * can be used. Do NOT attempt to use other macros without verifying that
377  * their needed information is available!
378  */
379 calcsb(dev, devfd, fs)
380 	char *dev;
381 	int devfd;
382 	register struct fs *fs;
383 {
384 	register struct disklabel *lp;
385 	register struct partition *pp;
386 	register char *cp;
387 	int i;
388 
389 	cp = index(dev, '\0') - 1;
390 	if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) {
391 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
392 		return (0);
393 	}
394 	lp = getdisklabel(dev, devfd);
395 	if (isdigit(*cp))
396 		pp = &lp->d_partitions[0];
397 	else
398 		pp = &lp->d_partitions[*cp - 'a'];
399 	if (pp->p_fstype != FS_BSDFFS) {
400 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
401 			dev, pp->p_fstype < FSMAXTYPES ?
402 			fstypenames[pp->p_fstype] : "unknown");
403 		return (0);
404 	}
405 	bzero(fs, sizeof(struct fs));
406 	fs->fs_fsize = pp->p_fsize;
407 	fs->fs_frag = pp->p_frag;
408 	fs->fs_cpg = pp->p_cpg;
409 	fs->fs_size = pp->p_size;
410 	fs->fs_ntrak = lp->d_ntracks;
411 	fs->fs_nsect = lp->d_nsectors;
412 	fs->fs_spc = lp->d_secpercyl;
413 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
414 	fs->fs_sblkno = roundup(
415 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
416 		fs->fs_frag);
417 	fs->fs_cgmask = 0xffffffff;
418 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
419 		fs->fs_cgmask <<= 1;
420 	if (!POWEROF2(fs->fs_ntrak))
421 		fs->fs_cgmask <<= 1;
422 	fs->fs_cgoffset = roundup(
423 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
424 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
425 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
426 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
427 		fs->fs_fsbtodb++;
428 	dev_bsize = lp->d_secsize;
429 	return (1);
430 }
431 
432 struct disklabel *
433 getdisklabel(s, fd)
434 	char *s;
435 	int	fd;
436 {
437 	static struct disklabel lab;
438 
439 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
440 		if (s == NULL)
441 			return ((struct disklabel *)NULL);
442 		pwarn("");
443 		perror("ioctl (GDINFO)");
444 		errexit("%s: can't read disk label", s);
445 	}
446 	return (&lab);
447 }
448