setup.c revision 6280
1218885Sdim/*
2218885Sdim * Copyright (c) 1980, 1986, 1993
3218885Sdim *	The Regents of the University of California.  All rights reserved.
4218885Sdim *
5218885Sdim * Redistribution and use in source and binary forms, with or without
6218885Sdim * modification, are permitted provided that the following conditions
7218885Sdim * are met:
8218885Sdim * 1. Redistributions of source code must retain the above copyright
9218885Sdim *    notice, this list of conditions and the following disclaimer.
10218885Sdim * 2. Redistributions in binary form must reproduce the above copyright
11218885Sdim *    notice, this list of conditions and the following disclaimer in the
12218885Sdim *    documentation and/or other materials provided with the distribution.
13218885Sdim * 3. All advertising materials mentioning features or use of this software
14249423Sdim *    must display the following acknowledgement:
15276479Sdim *	This product includes software developed by the University of
16249423Sdim *	California, Berkeley and its contributors.
17280031Sdim * 4. Neither the name of the University nor the names of its contributors
18218885Sdim *    may be used to endorse or promote products derived from this software
19218885Sdim *    without specific prior written permission.
20218885Sdim *
21280031Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22249423Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23276479Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24249423Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25218885Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26288943Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27218885Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28218885Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29218885Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30276479Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31276479Sdim * SUCH DAMAGE.
32218885Sdim */
33218885Sdim
34218885Sdim#ifndef lint
35218885Sdimstatic char sccsid[] = "@(#)setup.c	8.2 (Berkeley) 2/21/94";
36218885Sdim#endif /* not lint */
37218885Sdim
38218885Sdim#define DKTYPENAMES
39218885Sdim#include <sys/param.h>
40218885Sdim#include <sys/time.h>
41218885Sdim#include <ufs/ufs/dinode.h>
42276479Sdim#include <ufs/ffs/fs.h>
43218885Sdim#include <sys/stat.h>
44276479Sdim#include <sys/ioctl.h>
45218885Sdim#include <sys/disklabel.h>
46280031Sdim#include <sys/file.h>
47288943Sdim#include <errno.h>
48218885Sdim#include <stdlib.h>
49218885Sdim#include <string.h>
50218885Sdim#include <ctype.h>
51296417Sdim#include "fsck.h"
52288943Sdim
53218885Sdimstruct bufarea asblk;
54218885Sdim#define altsblock (*asblk.b_un.b_fs)
55218885Sdim#define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
56239462Sdim
57218885Sdimstruct	disklabel *getdisklabel();
58218885Sdim
59218885Sdimsetup(dev)
60280031Sdim	char *dev;
61288943Sdim{
62276479Sdim	long cg, size, asked, i, j;
63288943Sdim	long bmapsize;
64218885Sdim	struct disklabel *lp;
65218885Sdim	off_t sizepb;
66218885Sdim	struct stat statb;
67218885Sdim	struct fs proto;
68218885Sdim
69218885Sdim	havesb = 0;
70218885Sdim	fswritefd = -1;
71218885Sdim	if (stat(dev, &statb) < 0) {
72218885Sdim		printf("Can't stat %s: %s\n", dev, strerror(errno));
73276479Sdim		return (0);
74276479Sdim	}
75276479Sdim	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
76276479Sdim		pfatal("%s is not a character device", dev);
77276479Sdim		if (reply("CONTINUE") == 0)
78276479Sdim			return (0);
79288943Sdim	}
80288943Sdim	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
81288943Sdim		printf("Can't open %s: %s\n", dev, strerror(errno));
82280031Sdim		return (0);
83280031Sdim	}
84218885Sdim	if (preen == 0)
85218885Sdim		printf("** %s", dev);
86218885Sdim	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
87218885Sdim		fswritefd = -1;
88218885Sdim		if (preen)
89218885Sdim			pfatal("NO WRITE ACCESS");
90218885Sdim		printf(" (NO WRITE)");
91218885Sdim	}
92218885Sdim	if (preen == 0)
93218885Sdim		printf("\n");
94218885Sdim	fsmodified = 0;
95218885Sdim	lfdir = 0;
96218885Sdim	initbarea(&sblk);
97218885Sdim	initbarea(&asblk);
98218885Sdim	sblk.b_un.b_buf = malloc(SBSIZE);
99218885Sdim	asblk.b_un.b_buf = malloc(SBSIZE);
100218885Sdim	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
101218885Sdim		errexit("cannot allocate space for superblock\n");
102218885Sdim	if (lp = getdisklabel((char *)NULL, fsreadfd))
103218885Sdim		dev_bsize = secsize = lp->d_secsize;
104218885Sdim	else
105218885Sdim		dev_bsize = secsize = DEV_BSIZE;
106218885Sdim	/*
107218885Sdim	 * Read in the superblock, looking for alternates if necessary
108218885Sdim	 */
109218885Sdim	if (readsb(1) == 0) {
110218885Sdim		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
111288943Sdim			return(0);
112218885Sdim		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
113218885Sdim			return (0);
114218885Sdim		for (cg = 0; cg < proto.fs_ncg; cg++) {
115296417Sdim			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
116218885Sdim			if (readsb(0) != 0)
117218885Sdim				break;
118218885Sdim		}
119218885Sdim		if (cg >= proto.fs_ncg) {
120218885Sdim			printf("%s %s\n%s %s\n%s %s\n",
121218885Sdim				"SEARCH FOR ALTERNATE SUPER-BLOCK",
122218885Sdim				"FAILED. YOU MUST USE THE",
123218885Sdim				"-b OPTION TO FSCK TO SPECIFY THE",
124218885Sdim				"LOCATION OF AN ALTERNATE",
125280031Sdim				"SUPER-BLOCK TO SUPPLY NEEDED",
126218885Sdim				"INFORMATION; SEE fsck(8).");
127218885Sdim			bflag = 0;
128276479Sdim			return(0);
129276479Sdim		}
130218885Sdim		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
131218885Sdim		bflag = 0;
132218885Sdim	}
133218885Sdim	maxfsblock = sblock.fs_size;
134218885Sdim	maxino = sblock.fs_ncg * sblock.fs_ipg;
135218885Sdim	/*
136276479Sdim	 * Check and potentially fix certain fields in the super block.
137276479Sdim	 */
138276479Sdim	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
139276479Sdim		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
140276479Sdim		if (reply("SET TO DEFAULT") == 1) {
141276479Sdim			sblock.fs_optim = FS_OPTTIME;
142276479Sdim			sbdirty();
143276479Sdim		}
144276479Sdim	}
145218885Sdim	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
146218885Sdim		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
147218885Sdim			sblock.fs_minfree);
148218885Sdim		if (reply("SET TO DEFAULT") == 1) {
149218885Sdim			sblock.fs_minfree = 10;
150218885Sdim			sbdirty();
151218885Sdim		}
152218885Sdim	}
153218885Sdim	if (sblock.fs_interleave < 1 ||
154218885Sdim	    sblock.fs_interleave > sblock.fs_nsect) {
155218885Sdim		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
156218885Sdim			sblock.fs_interleave);
157280031Sdim		sblock.fs_interleave = 1;
158218885Sdim		if (preen)
159218885Sdim			printf(" (FIXED)\n");
160218885Sdim		if (preen || reply("SET TO DEFAULT") == 1) {
161218885Sdim			sbdirty();
162218885Sdim			dirty(&asblk);
163218885Sdim		}
164218885Sdim	}
165218885Sdim	if (sblock.fs_npsect < sblock.fs_nsect ||
166218885Sdim	    sblock.fs_npsect > sblock.fs_nsect*2) {
167218885Sdim		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
168218885Sdim			sblock.fs_npsect);
169218885Sdim		sblock.fs_npsect = sblock.fs_nsect;
170280031Sdim		if (preen)
171218885Sdim			printf(" (FIXED)\n");
172218885Sdim		if (preen || reply("SET TO DEFAULT") == 1) {
173218885Sdim			sbdirty();
174218885Sdim			dirty(&asblk);
175218885Sdim		}
176218885Sdim	}
177218885Sdim	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
178218885Sdim		newinofmt = 1;
179218885Sdim	} else {
180218885Sdim		sblock.fs_qbmask = ~sblock.fs_bmask;
181218885Sdim		sblock.fs_qfmask = ~sblock.fs_fmask;
182218885Sdim		newinofmt = 0;
183218885Sdim	}
184218885Sdim	/*
185218885Sdim	 * Convert to new inode format.
186218885Sdim	 */
187218885Sdim	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
188218885Sdim		if (preen)
189218885Sdim			pwarn("CONVERTING TO NEW INODE FORMAT\n");
190218885Sdim		else if (!reply("CONVERT TO NEW INODE FORMAT"))
191218885Sdim			return(0);
192218885Sdim		doinglevel2++;
193218885Sdim		sblock.fs_inodefmt = FS_44INODEFMT;
194218885Sdim		sizepb = sblock.fs_bsize;
195218885Sdim		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
196		for (i = 0; i < NIADDR; i++) {
197			sizepb *= NINDIR(&sblock);
198			sblock.fs_maxfilesize += sizepb;
199		}
200		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
201		sblock.fs_qbmask = ~sblock.fs_bmask;
202		sblock.fs_qfmask = ~sblock.fs_fmask;
203		sbdirty();
204		dirty(&asblk);
205	}
206	/*
207	 * Convert to new cylinder group format.
208	 */
209	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
210		if (preen)
211			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
212		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
213			return(0);
214		doinglevel1++;
215		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
216		sblock.fs_nrpos = 8;
217		sblock.fs_postbloff =
218		    (char *)(&sblock.fs_opostbl[0][0]) -
219		    (char *)(&sblock.fs_link);
220		sblock.fs_rotbloff = &sblock.fs_space[0] -
221		    (u_char *)(&sblock.fs_link);
222		sblock.fs_cgsize =
223			fragroundup(&sblock, CGSIZE(&sblock));
224		sbdirty();
225		dirty(&asblk);
226	}
227	if (asblk.b_dirty) {
228		bcopy((char *)&sblock, (char *)&altsblock,
229			(size_t)sblock.fs_sbsize);
230		flush(fswritefd, &asblk);
231	}
232	/*
233	 * read in the summary info.
234	 */
235	asked = 0;
236	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
237		size = sblock.fs_cssize - i < sblock.fs_bsize ?
238		    sblock.fs_cssize - i : sblock.fs_bsize;
239		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
240		if (bread(fsreadfd, (char *)sblock.fs_csp[j],
241		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
242		    size) != 0 && !asked) {
243			pfatal("BAD SUMMARY INFORMATION");
244			if (reply("CONTINUE") == 0)
245				errexit("");
246			asked++;
247		}
248	}
249	/*
250	 * allocate and initialize the necessary maps
251	 */
252	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
253	blockmap = calloc((unsigned)bmapsize, sizeof (char));
254	if (blockmap == NULL) {
255		printf("cannot alloc %u bytes for blockmap\n",
256		    (unsigned)bmapsize);
257		goto badsb;
258	}
259	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
260	if (statemap == NULL) {
261		printf("cannot alloc %u bytes for statemap\n",
262		    (unsigned)(maxino + 1));
263		goto badsb;
264	}
265	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
266	if (typemap == NULL) {
267		printf("cannot alloc %u bytes for typemap\n",
268		    (unsigned)(maxino + 1));
269		goto badsb;
270	}
271	lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short));
272	if (lncntp == NULL) {
273		printf("cannot alloc %u bytes for lncntp\n",
274		    (unsigned)(maxino + 1) * sizeof(short));
275		goto badsb;
276	}
277	numdirs = sblock.fs_cstotal.cs_ndir;
278	inplast = 0;
279	listmax = numdirs + 10;
280	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
281	    sizeof(struct inoinfo *));
282	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
283	    sizeof(struct inoinfo *));
284	if (inpsort == NULL || inphead == NULL) {
285		printf("cannot alloc %u bytes for inphead\n",
286		    (unsigned)numdirs * sizeof(struct inoinfo *));
287		goto badsb;
288	}
289	bufinit();
290	return (1);
291
292badsb:
293	ckfini();
294	return (0);
295}
296
297/*
298 * Read in the super block and its summary info.
299 */
300readsb(listerr)
301	int listerr;
302{
303	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
304
305	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
306		return (0);
307	sblk.b_bno = super;
308	sblk.b_size = SBSIZE;
309	/*
310	 * run a few consistency checks of the super block
311	 */
312	if (sblock.fs_magic != FS_MAGIC)
313		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
314	if (sblock.fs_ncg < 1)
315		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
316	if (sblock.fs_cpg < 1)
317		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
318	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
319	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
320		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
321	if (sblock.fs_sbsize > SBSIZE)
322		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
323	/*
324	 * Compute block size that the filesystem is based on,
325	 * according to fsbtodb, and adjust superblock block number
326	 * so we can tell if this is an alternate later.
327	 */
328	super *= dev_bsize;
329	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
330	sblk.b_bno = super / dev_bsize;
331	if (bflag) {
332		havesb = 1;
333		return (1);
334	}
335	/*
336	 * Set all possible fields that could differ, then do check
337	 * of whole super block against an alternate super block.
338	 * When an alternate super-block is specified this check is skipped.
339	 */
340	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
341	if (asblk.b_errs)
342		return (0);
343	altsblock.fs_link = sblock.fs_link;
344	altsblock.fs_rlink = sblock.fs_rlink;
345	altsblock.fs_time = sblock.fs_time;
346	altsblock.fs_cstotal = sblock.fs_cstotal;
347	altsblock.fs_cgrotor = sblock.fs_cgrotor;
348	altsblock.fs_fmod = sblock.fs_fmod;
349	altsblock.fs_clean = sblock.fs_clean;
350	altsblock.fs_ronly = sblock.fs_ronly;
351	altsblock.fs_flags = sblock.fs_flags;
352	altsblock.fs_maxcontig = sblock.fs_maxcontig;
353	altsblock.fs_minfree = sblock.fs_minfree;
354	altsblock.fs_optim = sblock.fs_optim;
355	altsblock.fs_rotdelay = sblock.fs_rotdelay;
356	altsblock.fs_maxbpg = sblock.fs_maxbpg;
357	bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
358		sizeof sblock.fs_csp);
359	bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
360		sizeof sblock.fs_fsmnt);
361	bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon,
362		sizeof sblock.fs_sparecon);
363	/*
364	 * The following should not have to be copied.
365	 */
366	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
367	altsblock.fs_interleave = sblock.fs_interleave;
368	altsblock.fs_npsect = sblock.fs_npsect;
369	altsblock.fs_nrpos = sblock.fs_nrpos;
370	altsblock.fs_qbmask = sblock.fs_qbmask;
371	altsblock.fs_qfmask = sblock.fs_qfmask;
372	altsblock.fs_state = sblock.fs_state;
373	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
374	if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) {
375		badsb(listerr,
376		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
377		return (0);
378	}
379	havesb = 1;
380	return (1);
381}
382
383badsb(listerr, s)
384	int listerr;
385	char *s;
386{
387
388	if (!listerr)
389		return;
390	if (preen)
391		printf("%s: ", cdevname);
392	pfatal("BAD SUPER BLOCK: %s\n", s);
393}
394
395/*
396 * Calculate a prototype superblock based on information in the disk label.
397 * When done the cgsblock macro can be calculated and the fs_ncg field
398 * can be used. Do NOT attempt to use other macros without verifying that
399 * their needed information is available!
400 */
401calcsb(dev, devfd, fs)
402	char *dev;
403	int devfd;
404	register struct fs *fs;
405{
406	register struct disklabel *lp;
407	register struct partition *pp;
408	register char *cp;
409	int i;
410
411	cp = index(dev, '\0') - 1;
412	if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) {
413		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
414		return (0);
415	}
416	lp = getdisklabel(dev, devfd);
417	if (isdigit(*cp))
418		pp = &lp->d_partitions[0];
419	else
420		pp = &lp->d_partitions[*cp - 'a'];
421	if (pp->p_fstype != FS_BSDFFS) {
422		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
423			dev, pp->p_fstype < FSMAXTYPES ?
424			fstypenames[pp->p_fstype] : "unknown");
425		return (0);
426	}
427	bzero((char *)fs, sizeof(struct fs));
428	fs->fs_fsize = pp->p_fsize;
429	fs->fs_frag = pp->p_frag;
430	fs->fs_cpg = pp->p_cpg;
431	fs->fs_size = pp->p_size;
432	fs->fs_ntrak = lp->d_ntracks;
433	fs->fs_nsect = lp->d_nsectors;
434	fs->fs_spc = lp->d_secpercyl;
435	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
436	fs->fs_sblkno = roundup(
437		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
438		fs->fs_frag);
439	fs->fs_cgmask = 0xffffffff;
440	for (i = fs->fs_ntrak; i > 1; i >>= 1)
441		fs->fs_cgmask <<= 1;
442	if (!POWEROF2(fs->fs_ntrak))
443		fs->fs_cgmask <<= 1;
444	fs->fs_cgoffset = roundup(
445		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
446	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
447	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
448	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
449		fs->fs_fsbtodb++;
450	dev_bsize = lp->d_secsize;
451	return (1);
452}
453
454struct disklabel *
455getdisklabel(s, fd)
456	char *s;
457	int	fd;
458{
459	static struct disklabel lab;
460
461	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
462		if (s == NULL)
463			return ((struct disklabel *)NULL);
464		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
465		errexit("%s: can't read disk label\n", s);
466	}
467	return (&lab);
468}
469