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