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