xref: /netbsd-src/sbin/fsck_ffs/setup.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: setup.c,v 1.21 1995/04/12 21:24:12 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)setup.c	8.5 (Berkeley) 11/23/94";
39 #else
40 static char rcsid[] = "$NetBSD: setup.c,v 1.21 1995/04/12 21:24:12 mycroft Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #define DKTYPENAMES
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ffs/fs.h>
49 #include <sys/stat.h>
50 #include <sys/ioctl.h>
51 #include <sys/disklabel.h>
52 #include <sys/file.h>
53 
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <ctype.h>
59 #include "fsck.h"
60 #include "extern.h"
61 
62 struct bufarea asblk;
63 #define altsblock (*asblk.b_un.b_fs)
64 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
65 
66 void badsb __P((int, char *));
67 int calcsb __P((char *, int, struct fs *));
68 struct disklabel *getdisklabel();
69 int readsb __P((int));
70 
71 int
72 setup(dev)
73 	char *dev;
74 {
75 	long cg, size, asked, i, j;
76 	long bmapsize;
77 	struct disklabel *lp;
78 	off_t sizepb;
79 	struct stat statb;
80 	struct fs proto;
81 	int doskipclean;
82 
83 	havesb = 0;
84 	fswritefd = -1;
85 	doskipclean = skipclean;
86 	if (stat(dev, &statb) < 0) {
87 		printf("Can't stat %s: %s\n", dev, strerror(errno));
88 		return (0);
89 	}
90 	if (!S_ISCHR(statb.st_mode)) {
91 		pfatal("%s is not a character device", dev);
92 		if (reply("CONTINUE") == 0)
93 			return (0);
94 	}
95 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
96 		printf("Can't open %s: %s\n", dev, strerror(errno));
97 		return (0);
98 	}
99 	if (preen == 0)
100 		printf("** %s", dev);
101 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
102 		fswritefd = -1;
103 		if (preen)
104 			pfatal("NO WRITE ACCESS");
105 		printf(" (NO WRITE)");
106 	}
107 	if (preen == 0)
108 		printf("\n");
109 	fsmodified = 0;
110 	lfdir = 0;
111 	initbarea(&sblk);
112 	initbarea(&asblk);
113 	sblk.b_un.b_buf = malloc(SBSIZE);
114 	asblk.b_un.b_buf = malloc(SBSIZE);
115 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
116 		errexit("cannot allocate space for superblock\n");
117 	if (lp = getdisklabel((char *)NULL, fsreadfd))
118 		dev_bsize = secsize = lp->d_secsize;
119 	else
120 		dev_bsize = secsize = DEV_BSIZE;
121 	/*
122 	 * Read in the superblock, looking for alternates if necessary
123 	 */
124 	if (readsb(1) == 0) {
125 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
126 			return(0);
127 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
128 			return (0);
129 		for (cg = 0; cg < proto.fs_ncg; cg++) {
130 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
131 			if (readsb(0) != 0)
132 				break;
133 		}
134 		if (cg >= proto.fs_ncg) {
135 			printf("%s %s\n%s %s\n%s %s\n",
136 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
137 				"FAILED. YOU MUST USE THE",
138 				"-b OPTION TO FSCK TO SPECIFY THE",
139 				"LOCATION OF AN ALTERNATE",
140 				"SUPER-BLOCK TO SUPPLY NEEDED",
141 				"INFORMATION; SEE fsck(8).");
142 			return(0);
143 		}
144 		doskipclean = 0;
145 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
146 	}
147 	if (debug)
148 		printf("clean = %d\n", sblock.fs_clean);
149 	if (sblock.fs_clean & FS_ISCLEAN) {
150 		if (preen && doskipclean) {
151 			pwarn("file system is clean; not checking\n");
152 			return (-1);
153 		}
154 		if (!preen)
155 			pwarn("** File system is already clean\n");
156 	}
157 	maxfsblock = sblock.fs_size;
158 	maxino = sblock.fs_ncg * sblock.fs_ipg;
159 	/*
160 	 * Check and potentially fix certain fields in the super block.
161 	 */
162 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
163 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
164 		if (reply("SET TO DEFAULT") == 1) {
165 			sblock.fs_optim = FS_OPTTIME;
166 			sbdirty();
167 		}
168 	}
169 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
170 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
171 			sblock.fs_minfree);
172 		if (reply("SET TO DEFAULT") == 1) {
173 			sblock.fs_minfree = 10;
174 			sbdirty();
175 		}
176 	}
177 	if (sblock.fs_interleave < 1 ||
178 	    sblock.fs_interleave > sblock.fs_nsect) {
179 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
180 			sblock.fs_interleave);
181 		sblock.fs_interleave = 1;
182 		if (preen)
183 			printf(" (FIXED)\n");
184 		if (preen || reply("SET TO DEFAULT") == 1) {
185 			sbdirty();
186 			dirty(&asblk);
187 		}
188 	}
189 	if (sblock.fs_npsect < sblock.fs_nsect ||
190 	    sblock.fs_npsect > sblock.fs_nsect*2) {
191 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
192 			sblock.fs_npsect);
193 		sblock.fs_npsect = sblock.fs_nsect;
194 		if (preen)
195 			printf(" (FIXED)\n");
196 		if (preen || reply("SET TO DEFAULT") == 1) {
197 			sbdirty();
198 			dirty(&asblk);
199 		}
200 	}
201 	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
202 		newinofmt = 1;
203 	} else {
204 		sblock.fs_qbmask = ~sblock.fs_bmask;
205 		sblock.fs_qfmask = ~sblock.fs_fmask;
206 		newinofmt = 0;
207 	}
208 	/*
209 	 * Convert to new inode format.
210 	 */
211 	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
212 		if (preen)
213 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
214 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
215 			return(0);
216 		doinglevel2++;
217 		sblock.fs_inodefmt = FS_44INODEFMT;
218 		sizepb = sblock.fs_bsize;
219 		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
220 		for (i = 0; i < NIADDR; i++) {
221 			sizepb *= NINDIR(&sblock);
222 			sblock.fs_maxfilesize += sizepb;
223 		}
224 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
225 		sblock.fs_qbmask = ~sblock.fs_bmask;
226 		sblock.fs_qfmask = ~sblock.fs_fmask;
227 		sbdirty();
228 		dirty(&asblk);
229 	}
230 	/*
231 	 * Convert to new cylinder group format.
232 	 */
233 	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
234 		if (preen)
235 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
236 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
237 			return(0);
238 		doinglevel1++;
239 		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
240 		sblock.fs_nrpos = 8;
241 		sblock.fs_postbloff =
242 		    (char *)(&sblock.fs_opostbl[0][0]) -
243 		    (char *)(&sblock.fs_firstfield);
244 		sblock.fs_rotbloff = &sblock.fs_space[0] -
245 		    (u_char *)(&sblock.fs_firstfield);
246 		sblock.fs_cgsize =
247 			fragroundup(&sblock, CGSIZE(&sblock));
248 		sbdirty();
249 		dirty(&asblk);
250 	}
251 	if (asblk.b_dirty && !bflag) {
252 		memcpy(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
253 		flush(fswritefd, &asblk);
254 	}
255 	/*
256 	 * read in the summary info.
257 	 */
258 	asked = 0;
259 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
260 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
261 		    sblock.fs_cssize - i : sblock.fs_bsize;
262 		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
263 		if (bread(fsreadfd, (char *)sblock.fs_csp[j],
264 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
265 		    size) != 0 && !asked) {
266 			pfatal("BAD SUMMARY INFORMATION");
267 			if (reply("CONTINUE") == 0)
268 				errexit("");
269 			asked++;
270 		}
271 	}
272 	/*
273 	 * allocate and initialize the necessary maps
274 	 */
275 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
276 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
277 	if (blockmap == NULL) {
278 		printf("cannot alloc %u bytes for blockmap\n",
279 		    (unsigned)bmapsize);
280 		goto badsblabel;
281 	}
282 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
283 	if (statemap == NULL) {
284 		printf("cannot alloc %u bytes for statemap\n",
285 		    (unsigned)(maxino + 1));
286 		goto badsblabel;
287 	}
288 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
289 	if (typemap == NULL) {
290 		printf("cannot alloc %u bytes for typemap\n",
291 		    (unsigned)(maxino + 1));
292 		goto badsblabel;
293 	}
294 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
295 	if (lncntp == NULL) {
296 		printf("cannot alloc %u bytes for lncntp\n",
297 		    (unsigned)(maxino + 1) * sizeof(int16_t));
298 		goto badsblabel;
299 	}
300 	numdirs = sblock.fs_cstotal.cs_ndir;
301 	inplast = 0;
302 	listmax = numdirs + 10;
303 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
304 	    sizeof(struct inoinfo *));
305 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
306 	    sizeof(struct inoinfo *));
307 	if (inpsort == NULL || inphead == NULL) {
308 		printf("cannot alloc %u bytes for inphead\n",
309 		    (unsigned)numdirs * sizeof(struct inoinfo *));
310 		goto badsblabel;
311 	}
312 	bufinit();
313 	return (1);
314 
315 badsblabel:
316 	ckfini(0);
317 	return (0);
318 }
319 
320 /*
321  * Read in the super block and its summary info.
322  */
323 int
324 readsb(listerr)
325 	int listerr;
326 {
327 	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
328 
329 	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
330 		return (0);
331 	sblk.b_bno = super;
332 	sblk.b_size = SBSIZE;
333 	/*
334 	 * run a few consistency checks of the super block
335 	 */
336 	if (sblock.fs_magic != FS_MAGIC)
337 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
338 	if (sblock.fs_ncg < 1)
339 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
340 	if (sblock.fs_cpg < 1)
341 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
342 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
343 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
344 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
345 	if (sblock.fs_sbsize > SBSIZE)
346 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
347 	/*
348 	 * Compute block size that the filesystem is based on,
349 	 * according to fsbtodb, and adjust superblock block number
350 	 * so we can tell if this is an alternate later.
351 	 */
352 	super *= dev_bsize;
353 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
354 	sblk.b_bno = super / dev_bsize;
355 	if (bflag) {
356 		havesb = 1;
357 		return (1);
358 	}
359 	/*
360 	 * Set all possible fields that could differ, then do check
361 	 * of whole super block against an alternate super block.
362 	 * When an alternate super-block is specified this check is skipped.
363 	 */
364 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
365 	if (asblk.b_errs)
366 		return (0);
367 	altsblock.fs_firstfield = sblock.fs_firstfield;
368 	altsblock.fs_fscktime = sblock.fs_fscktime;
369 	altsblock.fs_time = sblock.fs_time;
370 	altsblock.fs_cstotal = sblock.fs_cstotal;
371 	altsblock.fs_cgrotor = sblock.fs_cgrotor;
372 	altsblock.fs_fmod = sblock.fs_fmod;
373 	altsblock.fs_clean = sblock.fs_clean;
374 	altsblock.fs_ronly = sblock.fs_ronly;
375 	altsblock.fs_flags = sblock.fs_flags;
376 	altsblock.fs_maxcontig = sblock.fs_maxcontig;
377 	altsblock.fs_minfree = sblock.fs_minfree;
378 	altsblock.fs_optim = sblock.fs_optim;
379 	altsblock.fs_rotdelay = sblock.fs_rotdelay;
380 	altsblock.fs_maxbpg = sblock.fs_maxbpg;
381 	memcpy(altsblock.fs_csp, sblock.fs_csp,
382 		sizeof sblock.fs_csp);
383 	altsblock.fs_maxcluster = sblock.fs_maxcluster;
384 	memcpy(altsblock.fs_fsmnt, sblock.fs_fsmnt,
385 		sizeof sblock.fs_fsmnt);
386 	memcpy(altsblock.fs_sparecon, sblock.fs_sparecon,
387 		sizeof sblock.fs_sparecon);
388 	/*
389 	 * The following should not have to be copied.
390 	 */
391 	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
392 	altsblock.fs_interleave = sblock.fs_interleave;
393 	altsblock.fs_npsect = sblock.fs_npsect;
394 	altsblock.fs_nrpos = sblock.fs_nrpos;
395 	altsblock.fs_state = sblock.fs_state;
396 	altsblock.fs_qbmask = sblock.fs_qbmask;
397 	altsblock.fs_qfmask = sblock.fs_qfmask;
398 	altsblock.fs_state = sblock.fs_state;
399 	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
400 	if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) {
401 		if (debug) {
402 			long *nlp, *olp, *endlp;
403 
404 			printf("superblock mismatches\n");
405 			nlp = (long *)&altsblock;
406 			olp = (long *)&sblock;
407 			endlp = olp + (sblock.fs_sbsize / sizeof *olp);
408 			for ( ; olp < endlp; olp++, nlp++) {
409 				if (*olp == *nlp)
410 					continue;
411 				printf("offset %d, original %d, alternate %d\n",
412 				    olp - (long *)&sblock, *olp, *nlp);
413 			}
414 		}
415 		badsb(listerr,
416 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
417 		return (0);
418 	}
419 	havesb = 1;
420 	return (1);
421 }
422 
423 void
424 badsb(listerr, s)
425 	int listerr;
426 	char *s;
427 {
428 
429 	if (!listerr)
430 		return;
431 	if (preen)
432 		printf("%s: ", cdevname);
433 	pfatal("BAD SUPER BLOCK: %s\n", s);
434 }
435 
436 /*
437  * Calculate a prototype superblock based on information in the disk label.
438  * When done the cgsblock macro can be calculated and the fs_ncg field
439  * can be used. Do NOT attempt to use other macros without verifying that
440  * their needed information is available!
441  */
442 int
443 calcsb(dev, devfd, fs)
444 	char *dev;
445 	int devfd;
446 	register struct fs *fs;
447 {
448 	register struct disklabel *lp;
449 	register struct partition *pp;
450 	register char *cp;
451 	int i;
452 
453 	cp = strchr(dev, '\0') - 1;
454 	if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) {
455 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
456 		return (0);
457 	}
458 	lp = getdisklabel(dev, devfd);
459 	if (isdigit(*cp))
460 		pp = &lp->d_partitions[0];
461 	else
462 		pp = &lp->d_partitions[*cp - 'a'];
463 	if (pp->p_fstype != FS_BSDFFS) {
464 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
465 			dev, pp->p_fstype < FSMAXTYPES ?
466 			fstypenames[pp->p_fstype] : "unknown");
467 		return (0);
468 	}
469 	memset(fs, 0, sizeof(struct fs));
470 	fs->fs_fsize = pp->p_fsize;
471 	fs->fs_frag = pp->p_frag;
472 	fs->fs_cpg = pp->p_cpg;
473 	fs->fs_size = pp->p_size;
474 	fs->fs_ntrak = lp->d_ntracks;
475 	fs->fs_nsect = lp->d_nsectors;
476 	fs->fs_spc = lp->d_secpercyl;
477 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
478 	fs->fs_sblkno = roundup(
479 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
480 		fs->fs_frag);
481 	fs->fs_cgmask = 0xffffffff;
482 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
483 		fs->fs_cgmask <<= 1;
484 	if (!POWEROF2(fs->fs_ntrak))
485 		fs->fs_cgmask <<= 1;
486 	fs->fs_cgoffset = roundup(
487 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
488 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
489 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
490 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
491 		fs->fs_fsbtodb++;
492 	dev_bsize = lp->d_secsize;
493 	return (1);
494 }
495 
496 struct disklabel *
497 getdisklabel(s, fd)
498 	char *s;
499 	int	fd;
500 {
501 	static struct disklabel lab;
502 
503 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
504 		if (s == NULL)
505 			return ((struct disklabel *)NULL);
506 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
507 		errexit("%s: can't read disk label\n", s);
508 	}
509 	return (&lab);
510 }
511