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