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