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