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