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