xref: /csrg-svn/sbin/fsck/setup.c (revision 38339)
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.21 (Berkeley) 06/26/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	*calloc();
43 char	*index();
44 
45 setup(dev)
46 	char *dev;
47 {
48 	dev_t rootdev;
49 	long cg, ncg, size, asked, i, j;
50 	struct disklabel *getdisklabel(), *lp;
51 	struct stat statb;
52 	struct fs proto;
53 
54 	havesb = 0;
55 	if (stat("/", &statb) < 0)
56 		errexit("Can't stat root\n");
57 	rootdev = statb.st_dev;
58 	if (stat(dev, &statb) < 0) {
59 		perror(dev);
60 		printf("Can't stat %s\n", dev);
61 		return (0);
62 	}
63 	rawflg = 0;
64 	if ((statb.st_mode & S_IFMT) == S_IFBLK)
65 		;
66 	else if ((statb.st_mode & S_IFMT) == S_IFCHR)
67 		rawflg++;
68 	else {
69 		if (reply("file is not a block or character device; OK") == 0)
70 			return (0);
71 	}
72 	if (rootdev == statb.st_rdev)
73 		hotroot++;
74 	if ((dfile.rfdes = open(dev, O_RDONLY)) < 0) {
75 		perror(dev);
76 		printf("Can't open %s\n", dev);
77 		return (0);
78 	}
79 	if (preen == 0)
80 		printf("** %s", dev);
81 	if (nflag || (dfile.wfdes = open(dev, O_WRONLY)) < 0) {
82 		dfile.wfdes = -1;
83 		if (preen)
84 			pfatal("NO WRITE ACCESS");
85 		printf(" (NO WRITE)");
86 	}
87 	if (preen == 0)
88 		printf("\n");
89 	dfile.mod = 0;
90 	lfdir = 0;
91 	initbarea(&sblk);
92 	initbarea(&asblk);
93 	sblk.b_un.b_buf = (char *)malloc(SBSIZE);
94 	asblk.b_un.b_buf = (char *)malloc(SBSIZE);
95 	if (sblk.b_un.b_buf == 0 || asblk.b_un.b_buf == 0)
96 		errexit("cannot allocate space for superblock\n");
97 	if (lp = getdisklabel((char *)NULL, dfile.rfdes))
98 		dev_bsize = secsize = lp->d_secsize;
99 	else
100 		dev_bsize = secsize = DEV_BSIZE;
101 	/*
102 	 * Read in the superblock, looking for alternates if necessary
103 	 */
104 	if (readsb(1) == 0) {
105 		if (bflag || preen || calcsb(dev, dfile.rfdes, &proto) == 0)
106 			return(0);
107 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
108 			return (0);
109 		for (cg = 0; cg < proto.fs_ncg; cg++) {
110 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
111 			if (readsb(0) != 0)
112 				break;
113 		}
114 		if (cg >= proto.fs_ncg) {
115 			printf("%s %s\n%s %s\n%s %s\n",
116 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
117 				"FAILED. YOU MUST USE THE",
118 				"-b OPTION TO FSCK TO SPECIFY THE",
119 				"LOCATION OF AN ALTERNATE",
120 				"SUPER-BLOCK TO SUPPLY NEEDED",
121 				"INFORMATION; SEE fsck(8).");
122 			return(0);
123 		}
124 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
125 	}
126 	fmax = sblock.fs_size;
127 	imax = sblock.fs_ncg * sblock.fs_ipg;
128 	/*
129 	 * Check and potentially fix certain fields in the super block.
130 	 */
131 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
132 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
133 		if (reply("SET TO DEFAULT") == 1) {
134 			sblock.fs_optim = FS_OPTTIME;
135 			sbdirty();
136 		}
137 	}
138 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
139 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
140 			sblock.fs_minfree);
141 		if (reply("SET TO DEFAULT") == 1) {
142 			sblock.fs_minfree = 10;
143 			sbdirty();
144 		}
145 	}
146 	if (sblock.fs_interleave < 1) {
147 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
148 			sblock.fs_interleave);
149 		sblock.fs_interleave = 1;
150 		if (preen)
151 			printf(" (FIXED)\n");
152 		if (preen || reply("SET TO DEFAULT") == 1) {
153 			sbdirty();
154 			dirty(&asblk);
155 		}
156 	}
157 	if (sblock.fs_npsect < sblock.fs_nsect) {
158 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
159 			sblock.fs_npsect);
160 		sblock.fs_npsect = sblock.fs_nsect;
161 		if (preen)
162 			printf(" (FIXED)\n");
163 		if (preen || reply("SET TO DEFAULT") == 1) {
164 			sbdirty();
165 			dirty(&asblk);
166 		}
167 	}
168 	if (cvtflag) {
169 		if (sblock.fs_postblformat == FS_42POSTBLFMT) {
170 			/*
171 			 * Requested to convert from old format to new format
172 			 */
173 			if (preen)
174 				pwarn("CONVERTING TO NEW FILE SYSTEM FORMAT\n");
175 			else if (!reply("CONVERT TO NEW FILE SYSTEM FORMAT"))
176 				return(0);
177 			sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
178 			sblock.fs_nrpos = 8;
179 			sblock.fs_postbloff =
180 			    (char *)(&sblock.fs_opostbl[0][0]) -
181 			    (char *)(&sblock.fs_link);
182 			sblock.fs_rotbloff = &sblock.fs_space[0] -
183 			    (u_char *)(&sblock.fs_link);
184 			sblock.fs_cgsize =
185 				fragroundup(&sblock, CGSIZE(&sblock));
186 			/*
187 			 * Planning now for future expansion.
188 			 */
189 #			if (BYTE_ORDER == BIG_ENDIAN)
190 				sblock.fs_qbmask.val[0] = 0;
191 				sblock.fs_qbmask.val[1] = ~sblock.fs_bmask;
192 				sblock.fs_qfmask.val[0] = 0;
193 				sblock.fs_qfmask.val[1] = ~sblock.fs_fmask;
194 #			endif /* BIG_ENDIAN */
195 #			if (BYTE_ORDER == LITTLE_ENDIAN)
196 				sblock.fs_qbmask.val[0] = ~sblock.fs_bmask;
197 				sblock.fs_qbmask.val[1] = 0;
198 				sblock.fs_qfmask.val[0] = ~sblock.fs_fmask;
199 				sblock.fs_qfmask.val[1] = 0;
200 #			endif /* LITTLE_ENDIAN */
201 			sbdirty();
202 			dirty(&asblk);
203 		} else if (sblock.fs_postblformat == FS_DYNAMICPOSTBLFMT) {
204 			/*
205 			 * Requested to convert from new format to old format
206 			 */
207 			if (sblock.fs_nrpos != 8 || sblock.fs_ipg > 2048 ||
208 			    sblock.fs_cpg > 32 || sblock.fs_cpc > 16) {
209 				printf(
210 				"PARAMETERS OF CURRENT FILE SYSTEM DO NOT\n\t");
211 				errexit(
212 				"ALLOW CONVERSION TO OLD FILE SYSTEM FORMAT\n");
213 			}
214 			if (preen)
215 				pwarn("CONVERTING TO OLD FILE SYSTEM FORMAT\n");
216 			else if (!reply("CONVERT TO OLD FILE SYSTEM FORMAT"))
217 				return(0);
218 			sblock.fs_postblformat = FS_42POSTBLFMT;
219 			sblock.fs_cgsize = fragroundup(&sblock,
220 			    sizeof(struct ocg) + howmany(sblock.fs_fpg, NBBY));
221 			sbdirty();
222 			dirty(&asblk);
223 		} else {
224 			errexit("UNKNOWN FILE SYSTEM FORMAT\n");
225 		}
226 	}
227 	if (asblk.b_dirty) {
228 		bcopy((char *)&sblock, (char *)&altsblock, sblock.fs_sbsize);
229 		flush(&dfile, &asblk);
230 	}
231 	/*
232 	 * read in the summary info.
233 	 */
234 	asked = 0;
235 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
236 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
237 		    sblock.fs_cssize - i : sblock.fs_bsize;
238 		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
239 		if (bread(&dfile, (char *)sblock.fs_csp[j],
240 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
241 		    size) != 0 && !asked) {
242 			pfatal("BAD SUMMARY INFORMATION");
243 			if (reply("CONTINUE") == 0)
244 				errexit("");
245 			asked++;
246 		}
247 	}
248 	/*
249 	 * allocate and initialize the necessary maps
250 	 */
251 	bmapsz = roundup(howmany(fmax, NBBY), sizeof(short));
252 	blockmap = calloc((unsigned)bmapsz, sizeof (char));
253 	if (blockmap == NULL) {
254 		printf("cannot alloc %d bytes for blockmap\n", bmapsz);
255 		goto badsb;
256 	}
257 	statemap = calloc((unsigned)(imax + 1), sizeof(char));
258 	if (statemap == NULL) {
259 		printf("cannot alloc %d bytes for statemap\n", imax + 1);
260 		goto badsb;
261 	}
262 	lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short));
263 	if (lncntp == NULL) {
264 		printf("cannot alloc %d bytes for lncntp\n",
265 		    (imax + 1) * sizeof(short));
266 		goto badsb;
267 	}
268 
269 	bufinit();
270 	return (1);
271 
272 badsb:
273 	ckfini();
274 	return (0);
275 }
276 
277 /*
278  * Read in the super block and its summary info.
279  */
280 readsb(listerr)
281 	int listerr;
282 {
283 	off_t sboff;
284 	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
285 
286 	if (bread(&dfile, (char *)&sblock, super, (long)SBSIZE) != 0)
287 		return (0);
288 	sblk.b_bno = super;
289 	sblk.b_size = SBSIZE;
290 	/*
291 	 * run a few consistency checks of the super block
292 	 */
293 	if (sblock.fs_magic != FS_MAGIC)
294 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
295 	if (sblock.fs_ncg < 1)
296 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
297 	if (sblock.fs_cpg < 1)
298 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
299 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
300 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
301 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
302 	if (sblock.fs_sbsize > SBSIZE)
303 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
304 	/*
305 	 * Compute block size that the filesystem is based on,
306 	 * according to fsbtodb, and adjust superblock block number
307 	 * so we can tell if this is an alternate later.
308 	 */
309 	super *= dev_bsize;
310 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
311 	sblk.b_bno = super / dev_bsize;
312 	/*
313 	 * Set all possible fields that could differ, then do check
314 	 * of whole super block against an alternate super block.
315 	 * When an alternate super-block is specified this check is skipped.
316 	 */
317 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
318 	if (asblk.b_errs != NULL)
319 		return (0);
320 	if (bflag) {
321 		havesb = 1;
322 		return (1);
323 	}
324 	altsblock.fs_link = sblock.fs_link;
325 	altsblock.fs_rlink = sblock.fs_rlink;
326 	altsblock.fs_time = sblock.fs_time;
327 	altsblock.fs_cstotal = sblock.fs_cstotal;
328 	altsblock.fs_cgrotor = sblock.fs_cgrotor;
329 	altsblock.fs_fmod = sblock.fs_fmod;
330 	altsblock.fs_clean = sblock.fs_clean;
331 	altsblock.fs_ronly = sblock.fs_ronly;
332 	altsblock.fs_flags = sblock.fs_flags;
333 	altsblock.fs_maxcontig = sblock.fs_maxcontig;
334 	altsblock.fs_minfree = sblock.fs_minfree;
335 	altsblock.fs_optim = sblock.fs_optim;
336 	altsblock.fs_rotdelay = sblock.fs_rotdelay;
337 	altsblock.fs_maxbpg = sblock.fs_maxbpg;
338 	bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
339 		sizeof sblock.fs_csp);
340 	bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
341 		sizeof sblock.fs_fsmnt);
342 	bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon,
343 		sizeof sblock.fs_sparecon);
344 	/*
345 	 * The following should not have to be copied.
346 	 */
347 	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
348 	altsblock.fs_interleave = sblock.fs_interleave;
349 	altsblock.fs_npsect = sblock.fs_npsect;
350 	altsblock.fs_nrpos = sblock.fs_nrpos;
351 	if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) {
352 		badsb(listerr,
353 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
354 		return (0);
355 	}
356 	havesb = 1;
357 	return (1);
358 }
359 
360 badsb(listerr, s)
361 	int listerr;
362 	char *s;
363 {
364 
365 	if (!listerr)
366 		return;
367 	if (preen)
368 		printf("%s: ", devname);
369 	pfatal("BAD SUPER BLOCK: %s\n", s);
370 }
371 
372 /*
373  * Calculate a prototype superblock based on information in the disk label.
374  * When done the cgsblock macro can be calculated and the fs_ncg field
375  * can be used. Do NOT attempt to use other macros without verifying that
376  * their needed information is available!
377  */
378 calcsb(dev, devfd, fs)
379 	char *dev;
380 	int devfd;
381 	register struct fs *fs;
382 {
383 	register struct disklabel *lp;
384 	register struct partition *pp;
385 	register char *cp;
386 	int i;
387 	struct disklabel *getdisklabel();
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