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