setup.c revision 34266
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
35static const char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
36#endif /* not lint */
37
38#define DKTYPENAMES
39#include <sys/param.h>
40#include <sys/time.h>
41#include <sys/stat.h>
42#include <sys/ioctl.h>
43#include <sys/disklabel.h>
44#include <sys/file.h>
45
46#include <ufs/ufs/dinode.h>
47#include <ufs/ffs/fs.h>
48
49#include <ctype.h>
50#include <err.h>
51#include <errno.h>
52#include <string.h>
53
54#include "fsck.h"
55
56struct bufarea asblk;
57#define altsblock (*asblk.b_un.b_fs)
58#define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
59
60static void badsb __P((int listerr, char *s));
61static int calcsb __P((char *dev, int devfd, struct fs *fs));
62static struct disklabel *getdisklabel __P((char *s, int fd));
63static int readsb __P((int listerr));
64
65/*
66 * Read in a superblock finding an alternate if necessary.
67 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
68 * is already clean (preen mode only).
69 */
70int
71setup(dev)
72	char *dev;
73{
74	long cg, size, asked, i, j;
75	long skipclean, bmapsize;
76	struct disklabel *lp;
77	off_t sizepb;
78	struct stat statb;
79	struct fs proto;
80
81	havesb = 0;
82	fswritefd = -1;
83	skipclean = preen;
84	if (stat(dev, &statb) < 0) {
85		printf("Can't stat %s: %s\n", dev, strerror(errno));
86		return (0);
87	}
88	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
89		pfatal("%s is not a character device", dev);
90		if (reply("CONTINUE") == 0)
91			return (0);
92	}
93	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
94		printf("Can't open %s: %s\n", dev, strerror(errno));
95		return (0);
96	}
97	if (preen == 0)
98		printf("** %s", dev);
99	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
100		fswritefd = -1;
101		if (preen)
102			pfatal("NO WRITE ACCESS");
103		printf(" (NO WRITE)");
104	}
105	if (preen == 0)
106		printf("\n");
107	fsmodified = 0;
108	lfdir = 0;
109	initbarea(&sblk);
110	initbarea(&asblk);
111	sblk.b_un.b_buf = malloc(SBSIZE);
112	asblk.b_un.b_buf = malloc(SBSIZE);
113	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
114		errx(EEXIT, "cannot allocate space for superblock");
115	lp = getdisklabel((char *)NULL, fsreadfd);
116	if (lp)
117		dev_bsize = secsize = lp->d_secsize;
118	else
119		dev_bsize = secsize = DEV_BSIZE;
120	/*
121	 * Read in the superblock, looking for alternates if necessary
122	 */
123	if (readsb(1) == 0) {
124		skipclean = 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			bflag = 0;
143			return(0);
144		}
145		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
146		bflag = 0;
147	}
148	maxfsblock = sblock.fs_size;
149	maxino = sblock.fs_ncg * sblock.fs_ipg;
150	/*
151	 * Check and potentially fix certain fields in the super block.
152	 */
153	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
154		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
155		if (reply("SET TO DEFAULT") == 1) {
156			sblock.fs_optim = FS_OPTTIME;
157			sbdirty();
158		}
159	}
160	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
161		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
162			sblock.fs_minfree);
163		if (reply("SET TO DEFAULT") == 1) {
164			sblock.fs_minfree = 10;
165			sbdirty();
166		}
167	}
168	if (sblock.fs_interleave < 1 ||
169	    sblock.fs_interleave > sblock.fs_nsect) {
170		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
171			sblock.fs_interleave);
172		sblock.fs_interleave = 1;
173		if (preen)
174			printf(" (FIXED)\n");
175		if (preen || reply("SET TO DEFAULT") == 1) {
176			sbdirty();
177			dirty(&asblk);
178		}
179	}
180	if (sblock.fs_npsect < sblock.fs_nsect ||
181	    sblock.fs_npsect > sblock.fs_nsect*2) {
182		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
183			sblock.fs_npsect);
184		sblock.fs_npsect = sblock.fs_nsect;
185		if (preen)
186			printf(" (FIXED)\n");
187		if (preen || reply("SET TO DEFAULT") == 1) {
188			sbdirty();
189			dirty(&asblk);
190		}
191	}
192	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
193		newinofmt = 1;
194	} else {
195		sblock.fs_qbmask = ~sblock.fs_bmask;
196		sblock.fs_qfmask = ~sblock.fs_fmask;
197		newinofmt = 0;
198	}
199	/*
200	 * Convert to new inode format.
201	 */
202	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
203		if (preen)
204			pwarn("CONVERTING TO NEW INODE FORMAT\n");
205		else if (!reply("CONVERT TO NEW INODE FORMAT"))
206			return(0);
207		doinglevel2++;
208		sblock.fs_inodefmt = FS_44INODEFMT;
209		sizepb = sblock.fs_bsize;
210		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
211		for (i = 0; i < NIADDR; i++) {
212			sizepb *= NINDIR(&sblock);
213			sblock.fs_maxfilesize += sizepb;
214		}
215		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
216		sblock.fs_qbmask = ~sblock.fs_bmask;
217		sblock.fs_qfmask = ~sblock.fs_fmask;
218		sbdirty();
219		dirty(&asblk);
220	}
221	/*
222	 * Convert to new cylinder group format.
223	 */
224	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
225		if (preen)
226			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
227		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
228			return(0);
229		doinglevel1++;
230		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
231		sblock.fs_nrpos = 8;
232		sblock.fs_postbloff =
233		    (char *)(&sblock.fs_opostbl[0][0]) -
234		    (char *)(&sblock.fs_firstfield);
235		sblock.fs_rotbloff = &sblock.fs_space[0] -
236		    (u_char *)(&sblock.fs_firstfield);
237		sblock.fs_cgsize =
238			fragroundup(&sblock, CGSIZE(&sblock));
239		sbdirty();
240		dirty(&asblk);
241	}
242	if (asblk.b_dirty && !bflag) {
243		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
244		flush(fswritefd, &asblk);
245	}
246	/*
247	 * read in the summary info.
248	 */
249	asked = 0;
250	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
251		size = sblock.fs_cssize - i < sblock.fs_bsize ?
252		    sblock.fs_cssize - i : sblock.fs_bsize;
253		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
254		if (bread(fsreadfd, (char *)sblock.fs_csp[j],
255		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
256		    size) != 0 && !asked) {
257			pfatal("BAD SUMMARY INFORMATION");
258			if (reply("CONTINUE") == 0) {
259				ckfini(0);
260				exit(EEXIT);
261			}
262			asked++;
263		}
264	}
265	/*
266	 * If we survive the above basic checks and are preening,
267	 * quit here unless forced.
268	 */
269	if (skipclean && sblock.fs_clean && !fflag)
270		return (-1);
271	/*
272	 * allocate and initialize the necessary maps
273	 */
274	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
275	blockmap = calloc((unsigned)bmapsize, sizeof (char));
276	if (blockmap == NULL) {
277		printf("cannot alloc %u bytes for blockmap\n",
278		    (unsigned)bmapsize);
279		goto badsb;
280	}
281	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
282	if (statemap == NULL) {
283		printf("cannot alloc %u bytes for statemap\n",
284		    (unsigned)(maxino + 1));
285		goto badsb;
286	}
287	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
288	if (typemap == NULL) {
289		printf("cannot alloc %u bytes for typemap\n",
290		    (unsigned)(maxino + 1));
291		goto badsb;
292	}
293	lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short));
294	if (lncntp == NULL) {
295		printf("cannot alloc %u bytes for lncntp\n",
296		    (unsigned)(maxino + 1) * sizeof(short));
297		goto badsb;
298	}
299	numdirs = sblock.fs_cstotal.cs_ndir;
300	if (numdirs == 0) {
301		printf("numdirs is zero, try using an alternate superblock\n");
302		goto badsb;
303	}
304	inplast = 0;
305	listmax = numdirs + 10;
306	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
307	    sizeof(struct inoinfo *));
308	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
309	    sizeof(struct inoinfo *));
310	if (inpsort == NULL || inphead == NULL) {
311		printf("cannot alloc %u bytes for inphead\n",
312		    (unsigned)numdirs * sizeof(struct inoinfo *));
313		goto badsb;
314	}
315	bufinit();
316	if (sblock.fs_flags & FS_DOSOFTDEP)
317		usedsoftdep = 1;
318	else
319		usedsoftdep = 0;
320	return (1);
321
322badsb:
323	ckfini(0);
324	return (0);
325}
326
327/*
328 * Read in the super block and its summary info.
329 */
330static int
331readsb(listerr)
332	int listerr;
333{
334	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
335
336	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
337		return (0);
338	sblk.b_bno = super;
339	sblk.b_size = SBSIZE;
340	/*
341	 * run a few consistency checks of the super block
342	 */
343	if (sblock.fs_magic != FS_MAGIC)
344		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
345	if (sblock.fs_ncg < 1)
346		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
347	if (sblock.fs_cpg < 1)
348		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
349	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
350	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
351		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
352	if (sblock.fs_sbsize > SBSIZE)
353		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
354	/*
355	 * Compute block size that the filesystem is based on,
356	 * according to fsbtodb, and adjust superblock block number
357	 * so we can tell if this is an alternate later.
358	 */
359	super *= dev_bsize;
360	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
361	sblk.b_bno = super / dev_bsize;
362	if (bflag) {
363		havesb = 1;
364		return (1);
365	}
366	/*
367	 * Set all possible fields that could differ, then do check
368	 * of whole super block against an alternate super block.
369	 * When an alternate super-block is specified this check is skipped.
370	 */
371	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
372	if (asblk.b_errs)
373		return (0);
374	altsblock.fs_firstfield = sblock.fs_firstfield;
375	altsblock.fs_unused_1 = sblock.fs_unused_1;
376	altsblock.fs_time = sblock.fs_time;
377	altsblock.fs_cstotal = sblock.fs_cstotal;
378	altsblock.fs_cgrotor = sblock.fs_cgrotor;
379	altsblock.fs_fmod = sblock.fs_fmod;
380	altsblock.fs_clean = sblock.fs_clean;
381	altsblock.fs_ronly = sblock.fs_ronly;
382	altsblock.fs_flags = sblock.fs_flags;
383	altsblock.fs_maxcontig = sblock.fs_maxcontig;
384	altsblock.fs_minfree = sblock.fs_minfree;
385	altsblock.fs_optim = sblock.fs_optim;
386	altsblock.fs_rotdelay = sblock.fs_rotdelay;
387	altsblock.fs_maxbpg = sblock.fs_maxbpg;
388	memmove(altsblock.fs_csp, sblock.fs_csp, sizeof sblock.fs_csp);
389	altsblock.fs_maxcluster = sblock.fs_maxcluster;
390	memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt);
391	memmove(altsblock.fs_sparecon,
392		sblock.fs_sparecon, sizeof sblock.fs_sparecon);
393	/*
394	 * The following should not have to be copied.
395	 */
396	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
397	altsblock.fs_interleave = sblock.fs_interleave;
398	altsblock.fs_npsect = sblock.fs_npsect;
399	altsblock.fs_nrpos = sblock.fs_nrpos;
400	altsblock.fs_state = sblock.fs_state;
401	altsblock.fs_qbmask = sblock.fs_qbmask;
402	altsblock.fs_qfmask = sblock.fs_qfmask;
403	altsblock.fs_state = sblock.fs_state;
404	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
405	if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) {
406		if (debug) {
407			long *nlp, *olp, *endlp;
408
409			printf("superblock mismatches\n");
410			nlp = (long *)&altsblock;
411			olp = (long *)&sblock;
412			endlp = olp + (sblock.fs_sbsize / sizeof *olp);
413			for ( ; olp < endlp; olp++, nlp++) {
414				if (*olp == *nlp)
415					continue;
416				printf("offset %d, original %d, alternate %d\n",
417				    olp - (long *)&sblock, *olp, *nlp);
418			}
419		}
420		badsb(listerr,
421		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
422		return (0);
423	}
424	havesb = 1;
425	return (1);
426}
427
428static void
429badsb(listerr, s)
430	int listerr;
431	char *s;
432{
433
434	if (!listerr)
435		return;
436	if (preen)
437		printf("%s: ", cdevname);
438	pfatal("BAD SUPER BLOCK: %s\n", s);
439}
440
441/*
442 * Calculate a prototype superblock based on information in the disk label.
443 * When done the cgsblock macro can be calculated and the fs_ncg field
444 * can be used. Do NOT attempt to use other macros without verifying that
445 * their needed information is available!
446 */
447static int
448calcsb(dev, devfd, fs)
449	char *dev;
450	int devfd;
451	register struct fs *fs;
452{
453	register struct disklabel *lp;
454	register struct partition *pp;
455	register char *cp;
456	int i;
457
458	cp = strchr(dev, '\0') - 1;
459	if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
460		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
461		return (0);
462	}
463	lp = getdisklabel(dev, devfd);
464	if (isdigit(*cp))
465		pp = &lp->d_partitions[0];
466	else
467		pp = &lp->d_partitions[*cp - 'a'];
468	if (pp->p_fstype != FS_BSDFFS) {
469		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
470			dev, pp->p_fstype < FSMAXTYPES ?
471			fstypenames[pp->p_fstype] : "unknown");
472		return (0);
473	}
474	if (pp->p_fsize == 0 || pp->p_frag == 0) {
475		pfatal("%s: LABELED AS A %s FILE SYSTEM, BUT BLOCK SIZE IS 0\n",
476			dev, fstypenames[pp->p_fstype]);
477		return (0);
478	}
479	memset(fs, 0, sizeof(struct fs));
480	fs->fs_fsize = pp->p_fsize;
481	fs->fs_frag = pp->p_frag;
482	fs->fs_cpg = pp->p_cpg;
483	fs->fs_size = pp->p_size;
484	fs->fs_ntrak = lp->d_ntracks;
485	fs->fs_nsect = lp->d_nsectors;
486	fs->fs_spc = lp->d_secpercyl;
487	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
488	fs->fs_sblkno = roundup(
489		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
490		fs->fs_frag);
491	fs->fs_cgmask = 0xffffffff;
492	for (i = fs->fs_ntrak; i > 1; i >>= 1)
493		fs->fs_cgmask <<= 1;
494	if (!POWEROF2(fs->fs_ntrak))
495		fs->fs_cgmask <<= 1;
496	fs->fs_cgoffset = roundup(
497		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
498	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
499	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
500	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
501		fs->fs_fsbtodb++;
502	dev_bsize = lp->d_secsize;
503	return (1);
504}
505
506static struct disklabel *
507getdisklabel(s, fd)
508	char *s;
509	int	fd;
510{
511	static struct disklabel lab;
512
513	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
514		if (s == NULL)
515			return ((struct disklabel *)NULL);
516		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
517		errx(EEXIT, "%s: can't read disk label", s);
518	}
519	return (&lab);
520}
521