xref: /original-bsd/sbin/fsck/setup.c (revision 95a66346)
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.33 (Berkeley) 02/22/91";
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 #ifdef tahoe
92 	/*
93 	 * On the tahoe, the disk label and the disk driver disagree.
94 	 * The label knows that sectors are 512 bytes, but the disk
95 	 * drivers will only transfer in 1024 sized pieces.
96 	 */
97 	secsize = 1024;
98 #endif
99 	/*
100 	 * Read in the superblock, looking for alternates if necessary
101 	 */
102 	if (readsb(1) == 0) {
103 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
104 			return(0);
105 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
106 			return (0);
107 		for (cg = 0; cg < proto.fs_ncg; cg++) {
108 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
109 			if (readsb(0) != 0)
110 				break;
111 		}
112 		if (cg >= proto.fs_ncg) {
113 			printf("%s %s\n%s %s\n%s %s\n",
114 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
115 				"FAILED. YOU MUST USE THE",
116 				"-b OPTION TO FSCK TO SPECIFY THE",
117 				"LOCATION OF AN ALTERNATE",
118 				"SUPER-BLOCK TO SUPPLY NEEDED",
119 				"INFORMATION; SEE fsck(8).");
120 			return(0);
121 		}
122 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
123 	}
124 	maxfsblock = sblock.fs_size;
125 	maxino = sblock.fs_ncg * sblock.fs_ipg;
126 	/*
127 	 * Check and potentially fix certain fields in the super block.
128 	 */
129 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
130 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
131 		if (reply("SET TO DEFAULT") == 1) {
132 			sblock.fs_optim = FS_OPTTIME;
133 			sbdirty();
134 		}
135 	}
136 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
137 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
138 			sblock.fs_minfree);
139 		if (reply("SET TO DEFAULT") == 1) {
140 			sblock.fs_minfree = 10;
141 			sbdirty();
142 		}
143 	}
144 	if (sblock.fs_interleave < 1 ||
145 	    sblock.fs_interleave > sblock.fs_nsect) {
146 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
147 			sblock.fs_interleave);
148 		sblock.fs_interleave = 1;
149 		if (preen)
150 			printf(" (FIXED)\n");
151 		if (preen || reply("SET TO DEFAULT") == 1) {
152 			sbdirty();
153 			dirty(&asblk);
154 		}
155 	}
156 	if (sblock.fs_npsect < sblock.fs_nsect ||
157 	    sblock.fs_npsect > sblock.fs_nsect*2) {
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,
229 			(size_t)sblock.fs_sbsize);
230 		flush(fswritefd, &asblk);
231 	}
232 	/*
233 	 * read in the summary info.
234 	 */
235 	asked = 0;
236 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
237 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
238 		    sblock.fs_cssize - i : sblock.fs_bsize;
239 		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
240 		if (bread(fsreadfd, (char *)sblock.fs_csp[j],
241 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
242 		    size) != 0 && !asked) {
243 			pfatal("BAD SUMMARY INFORMATION");
244 			if (reply("CONTINUE") == 0)
245 				errexit("");
246 			asked++;
247 		}
248 	}
249 	/*
250 	 * allocate and initialize the necessary maps
251 	 */
252 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
253 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
254 	if (blockmap == NULL) {
255 		printf("cannot alloc %u bytes for blockmap\n",
256 		    (unsigned)bmapsize);
257 		goto badsb;
258 	}
259 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
260 	if (statemap == NULL) {
261 		printf("cannot alloc %u bytes for statemap\n",
262 		    (unsigned)(maxino + 1));
263 		goto badsb;
264 	}
265 	lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short));
266 	if (lncntp == NULL) {
267 		printf("cannot alloc %u bytes for lncntp\n",
268 		    (unsigned)(maxino + 1) * sizeof(short));
269 		goto badsb;
270 	}
271 	numdirs = sblock.fs_cstotal.cs_ndir;
272 	inplast = 0;
273 	listmax = numdirs + 10;
274 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
275 	    sizeof(struct inoinfo *));
276 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
277 	    sizeof(struct inoinfo *));
278 	if (inpsort == NULL || inphead == NULL) {
279 		printf("cannot alloc %u bytes for inphead\n",
280 		    (unsigned)numdirs * sizeof(struct inoinfo *));
281 		goto badsb;
282 	}
283 	bufinit();
284 	return (1);
285 
286 badsb:
287 	ckfini();
288 	return (0);
289 }
290 
291 /*
292  * Read in the super block and its summary info.
293  */
294 readsb(listerr)
295 	int listerr;
296 {
297 	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
298 
299 	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
300 		return (0);
301 	sblk.b_bno = super;
302 	sblk.b_size = SBSIZE;
303 	/*
304 	 * run a few consistency checks of the super block
305 	 */
306 	if (sblock.fs_magic != FS_MAGIC)
307 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
308 	if (sblock.fs_ncg < 1)
309 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
310 	if (sblock.fs_cpg < 1)
311 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
312 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
313 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
314 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
315 	if (sblock.fs_sbsize > SBSIZE)
316 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
317 	/*
318 	 * Compute block size that the filesystem is based on,
319 	 * according to fsbtodb, and adjust superblock block number
320 	 * so we can tell if this is an alternate later.
321 	 */
322 	super *= dev_bsize;
323 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
324 	sblk.b_bno = super / dev_bsize;
325 	/*
326 	 * Set all possible fields that could differ, then do check
327 	 * of whole super block against an alternate super block.
328 	 * When an alternate super-block is specified this check is skipped.
329 	 */
330 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
331 	if (asblk.b_errs)
332 		return (0);
333 	if (bflag) {
334 		havesb = 1;
335 		return (1);
336 	}
337 	altsblock.fs_link = sblock.fs_link;
338 	altsblock.fs_rlink = sblock.fs_rlink;
339 	altsblock.fs_time = sblock.fs_time;
340 	altsblock.fs_cstotal = sblock.fs_cstotal;
341 	altsblock.fs_cgrotor = sblock.fs_cgrotor;
342 	altsblock.fs_fmod = sblock.fs_fmod;
343 	altsblock.fs_clean = sblock.fs_clean;
344 	altsblock.fs_ronly = sblock.fs_ronly;
345 	altsblock.fs_flags = sblock.fs_flags;
346 	altsblock.fs_maxcontig = sblock.fs_maxcontig;
347 	altsblock.fs_minfree = sblock.fs_minfree;
348 	altsblock.fs_optim = sblock.fs_optim;
349 	altsblock.fs_rotdelay = sblock.fs_rotdelay;
350 	altsblock.fs_maxbpg = sblock.fs_maxbpg;
351 	bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
352 		sizeof sblock.fs_csp);
353 	bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
354 		sizeof sblock.fs_fsmnt);
355 	bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon,
356 		sizeof sblock.fs_sparecon);
357 	/*
358 	 * The following should not have to be copied.
359 	 */
360 	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
361 	altsblock.fs_interleave = sblock.fs_interleave;
362 	altsblock.fs_npsect = sblock.fs_npsect;
363 	altsblock.fs_nrpos = sblock.fs_nrpos;
364 	if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) {
365 		badsb(listerr,
366 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
367 		return (0);
368 	}
369 	havesb = 1;
370 	return (1);
371 }
372 
373 badsb(listerr, s)
374 	int listerr;
375 	char *s;
376 {
377 
378 	if (!listerr)
379 		return;
380 	if (preen)
381 		printf("%s: ", devname);
382 	pfatal("BAD SUPER BLOCK: %s\n", s);
383 }
384 
385 /*
386  * Calculate a prototype superblock based on information in the disk label.
387  * When done the cgsblock macro can be calculated and the fs_ncg field
388  * can be used. Do NOT attempt to use other macros without verifying that
389  * their needed information is available!
390  */
391 calcsb(dev, devfd, fs)
392 	char *dev;
393 	int devfd;
394 	register struct fs *fs;
395 {
396 	register struct disklabel *lp;
397 	register struct partition *pp;
398 	register char *cp;
399 	int i;
400 
401 	cp = index(dev, '\0') - 1;
402 	if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) {
403 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
404 		return (0);
405 	}
406 	lp = getdisklabel(dev, devfd);
407 	if (isdigit(*cp))
408 		pp = &lp->d_partitions[0];
409 	else
410 		pp = &lp->d_partitions[*cp - 'a'];
411 	if (pp->p_fstype != FS_BSDFFS) {
412 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
413 			dev, pp->p_fstype < FSMAXTYPES ?
414 			fstypenames[pp->p_fstype] : "unknown");
415 		return (0);
416 	}
417 	bzero((char *)fs, sizeof(struct fs));
418 	fs->fs_fsize = pp->p_fsize;
419 	fs->fs_frag = pp->p_frag;
420 	fs->fs_cpg = pp->p_cpg;
421 	fs->fs_size = pp->p_size;
422 	fs->fs_ntrak = lp->d_ntracks;
423 	fs->fs_nsect = lp->d_nsectors;
424 	fs->fs_spc = lp->d_secpercyl;
425 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
426 	fs->fs_sblkno = roundup(
427 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
428 		fs->fs_frag);
429 	fs->fs_cgmask = 0xffffffff;
430 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
431 		fs->fs_cgmask <<= 1;
432 	if (!POWEROF2(fs->fs_ntrak))
433 		fs->fs_cgmask <<= 1;
434 	fs->fs_cgoffset = roundup(
435 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
436 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
437 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
438 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
439 		fs->fs_fsbtodb++;
440 	dev_bsize = lp->d_secsize;
441 	return (1);
442 }
443 
444 struct disklabel *
445 getdisklabel(s, fd)
446 	char *s;
447 	int	fd;
448 {
449 	static struct disklabel lab;
450 
451 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
452 		if (s == NULL)
453 			return ((struct disklabel *)NULL);
454 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
455 		errexit("%s: can't read disk label\n", s);
456 	}
457 	return (&lab);
458 }
459