setup.c revision 1.25
1/*	$OpenBSD: setup.c,v 1.25 2015/01/16 06:39:57 deraadt Exp $	*/
2/*	$NetBSD: setup.c,v 1.1 1997/06/11 11:22:01 bouyer Exp $	*/
3
4/*
5 * Copyright (c) 1997 Manuel Bouyer.
6 * Copyright (c) 1980, 1986, 1993
7 *	The Regents of the University of California.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *	notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *	notice, this list of conditions and the following disclaimer in the
16 *	documentation and/or other materials provided with the distribution.
17 * 3. 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#define DKTYPENAMES
35#include <sys/param.h>	/* DEV_BSIZE roundup */
36#include <sys/time.h>
37#include <ufs/ext2fs/ext2fs_dinode.h>
38#include <ufs/ext2fs/ext2fs.h>
39#include <sys/stat.h>
40#include <sys/ioctl.h>
41#include <sys/dkio.h>
42#include <sys/disklabel.h>
43
44#include <errno.h>
45#include <fcntl.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <ctype.h>
50
51#include "fsck.h"
52#include "extern.h"
53#include "fsutil.h"
54
55#define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
56
57void badsb(int, char *);
58int calcsb(char *, int, struct m_ext2fs *);
59static struct disklabel *getdisklabel(char *, int);
60static int readsb(int);
61
62int
63setup(char *dev)
64{
65	long cg, asked, i;
66	long bmapsize;
67	struct disklabel *lp;
68	off_t sizepb;
69	struct stat statb;
70	struct m_ext2fs proto;
71	int doskipclean;
72	u_int64_t maxfilesize;
73
74	havesb = 0;
75	fswritefd = -1;
76	doskipclean = skipclean;
77	if (stat(dev, &statb) < 0) {
78		printf("Can't stat %s: %s\n", dev, strerror(errno));
79		return (0);
80	}
81	if (!S_ISCHR(statb.st_mode)) {
82		pfatal("%s is not a character device", dev);
83		if (reply("CONTINUE") == 0)
84			return (0);
85	}
86	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
87		printf("Can't open %s: %s\n", dev, strerror(errno));
88		return (0);
89	}
90	if (preen == 0)
91		printf("** %s", dev);
92	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
93		fswritefd = -1;
94		if (preen)
95			pfatal("NO WRITE ACCESS");
96		printf(" (NO WRITE)");
97	}
98	if (preen == 0)
99		printf("\n");
100	fsmodified = 0;
101	lfdir = 0;
102	initbarea(&sblk);
103	initbarea(&asblk);
104	sblk.b_un.b_buf = malloc(SBSIZE);
105	asblk.b_un.b_buf = malloc(SBSIZE);
106	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
107		errexit("cannot allocate space for superblock\n");
108	if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL)
109		secsize = lp->d_secsize;
110	else
111		secsize = DEV_BSIZE;
112	/*
113	 * Read in the superblock, looking for alternates if necessary
114	 */
115	if (readsb(1) == 0) {
116		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
117			return(0);
118		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
119			return (0);
120		for (cg = 1; cg < proto.e2fs_ncg; cg++) {
121			bflag = fsbtodb(&proto,
122				cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock);
123			if (readsb(0) != 0)
124				break;
125		}
126		if (cg >= proto.e2fs_ncg) {
127			printf("%s %s\n%s %s\n%s %s\n",
128				"SEARCH FOR ALTERNATE SUPER-BLOCK",
129				"FAILED. YOU MUST USE THE",
130				"-b OPTION TO FSCK_FFS TO SPECIFY THE",
131				"LOCATION OF AN ALTERNATE",
132				"SUPER-BLOCK TO SUPPLY NEEDED",
133				"INFORMATION; SEE fsck_ext2fs(8).");
134			return(0);
135		}
136		doskipclean = 0;
137		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
138	}
139	if (debug)
140		printf("state = %d\n", sblock.e2fs.e2fs_state);
141	if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) {
142		if (doskipclean) {
143			pwarn("%sile system is clean; not checking\n",
144				preen ? "f" : "** F");
145			return (-1);
146		}
147		if (!preen)
148			pwarn("** File system is already clean\n");
149	}
150	maxfsblock = sblock.e2fs.e2fs_bcount;
151	maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
152	sizepb = sblock.e2fs_bsize;
153	maxfilesize = sblock.e2fs_bsize * NDADDR - 1;
154	for (i = 0; i < NIADDR; i++) {
155		sizepb *= NINDIR(&sblock);
156		maxfilesize += sizepb;
157	}
158	/*
159	 * Check and potentially fix certain fields in the super block.
160	 */
161	if ((sblock.e2fs.e2fs_rbcount < 0) ||
162			(sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
163		pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
164			sblock.e2fs.e2fs_rbcount);
165		if (reply("SET TO DEFAULT") == 1) {
166			sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * 0.1;
167			sbdirty();
168			dirty(&asblk);
169		}
170	}
171	if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
172		pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
173			sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
174		return 0;
175	}
176	if (asblk.b_dirty && !bflag) {
177		copyback_sb(&asblk);
178		flush(fswritefd, &asblk);
179	}
180	/*
181	 * read in the summary info.
182	 */
183
184	sblock.e2fs_gd = calloc(sblock.e2fs_ngdb, sblock.e2fs_bsize);
185	if (sblock.e2fs_gd == NULL)
186		errexit("out of memory\n");
187	asked = 0;
188	for (i=0; i < sblock.e2fs_ngdb; i++) {
189		if (bread(fsreadfd,(char *)
190			&sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
191			fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
192			sblock.e2fs_bsize) != 0 && !asked) {
193			pfatal("BAD SUMMARY INFORMATION");
194			if (reply("CONTINUE") == 0)
195				errexit("%s\n", "");
196			asked++;
197		}
198	}
199	/*
200	 * allocate and initialize the necessary maps
201	 */
202	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
203	blockmap = calloc((unsigned)bmapsize, sizeof (char));
204	if (blockmap == NULL) {
205		printf("cannot alloc %u bytes for blockmap\n",
206			(unsigned)bmapsize);
207		goto badsblabel;
208	}
209	statemap = calloc((unsigned)(maxino + 2), sizeof(char));
210	if (statemap == NULL) {
211		printf("cannot alloc %u bytes for statemap\n",
212			(unsigned)(maxino + 1));
213		goto badsblabel;
214	}
215	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
216	if (typemap == NULL) {
217		printf("cannot alloc %u bytes for typemap\n",
218		    (unsigned)(maxino + 1));
219		goto badsblabel;
220	}
221	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
222	if (lncntp == NULL) {
223		printf("cannot alloc %u bytes for lncntp\n",
224			(unsigned)((maxino + 1) * sizeof(int16_t)));
225		goto badsblabel;
226	}
227	for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
228		numdirs += letoh16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
229	}
230	inplast = 0;
231	listmax = numdirs + 10;
232	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
233		sizeof(struct inoinfo *));
234	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
235		sizeof(struct inoinfo *));
236	if (inpsort == NULL || inphead == NULL) {
237		printf("cannot alloc %u bytes for inphead\n",
238			(unsigned)(numdirs * sizeof(struct inoinfo *)));
239		goto badsblabel;
240	}
241	bufinit();
242	return (1);
243
244badsblabel:
245	ckfini(0);
246	return (0);
247}
248
249/*
250 * Read in the super block and its summary info.
251 */
252static int
253readsb(int listerr)
254{
255	daddr32_t super = bflag ? bflag : SBOFF / DEV_BSIZE;
256
257	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
258		return (0);
259	sblk.b_bno = super;
260	sblk.b_size = SBSIZE;
261
262	/* Copy the superblock in memory */
263	e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
264
265	/*
266	 * run a few consistency checks of the super block
267	 */
268	if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
269		badsb(listerr, "MAGIC NUMBER WRONG"); return (0);
270	}
271	if (sblock.e2fs.e2fs_log_bsize > 2) {
272		badsb(listerr, "BAD LOG_BSIZE"); return (0);
273	}
274	if (sblock.e2fs.e2fs_bpg == 0) {
275		badsb(listerr, "BAD BLOCKS PER GROUP"); return (0);
276	}
277
278	/* compute the dynamic fields of the in-memory sb */
279	/* compute dynamic sb infos */
280	sblock.e2fs_ncg =
281		howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
282		sblock.e2fs.e2fs_bpg);
283	/* XXX assume hw bsize = 512 */
284	sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
285	sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
286	sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
287	sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
288	sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
289	sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
290		sblock.e2fs_bsize / sizeof(struct ext2_gd));
291	sblock.e2fs_ipb = sblock.e2fs_bsize / sizeof(struct ext2fs_dinode);
292	sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb;
293
294	/*
295	 * Compute block size that the filesystem is based on,
296	 * according to fsbtodb, and adjust superblock block number
297	 * so we can tell if this is an alternate later.
298	 */
299	sblk.b_bno = super / DEV_BSIZE;
300
301	if (sblock.e2fs_ncg == 1) {
302		/* no alternate superblock; assume it's okey */
303		havesb = 1;
304		return 1;
305	}
306
307	getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
308		(long)SBSIZE);
309	if (asblk.b_errs)
310		return (0);
311	if (bflag) {
312		havesb = 1;
313		return (1);
314	}
315
316	/*
317	 * Set all possible fields that could differ, then do check
318	 * of whole super block against an alternate super block.
319	 * When an alternate super-block is specified this check is skipped.
320	 */
321	asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
322	asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
323	asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
324	asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
325	asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
326	asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
327	asblk.b_un.b_fs->e2fs_max_mnt_count = sblk.b_un.b_fs->e2fs_max_mnt_count;
328	asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
329	asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
330	asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
331	asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
332	asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
333	asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
334	asblk.b_un.b_fs->e2fs_block_group_nr =
335	    sblk.b_un.b_fs->e2fs_block_group_nr;
336	asblk.b_un.b_fs->e2fs_features_rocompat &= ~EXT2F_ROCOMPAT_LARGEFILE;
337	asblk.b_un.b_fs->e2fs_features_rocompat |=
338	    sblk.b_un.b_fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE;
339	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
340	    ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) ||
341	    (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) {
342		if (debug) {
343			printf("compat 0x%08x, incompat 0x%08x, compat_ro "
344			    "0x%08x\n",
345			    sblock.e2fs.e2fs_features_compat,
346			    sblock.e2fs.e2fs_features_incompat,
347			    sblock.e2fs.e2fs_features_rocompat);
348		}
349		badsb(listerr,"INCOMPATIBLE FEATURE BITS IN SUPER BLOCK");
350		return 0;
351	}
352	if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
353		if (debug) {
354			u_int32_t *nlp, *olp, *endlp;
355
356			printf("superblock mismatches\n");
357			nlp = (u_int32_t *)asblk.b_un.b_fs;
358			olp = (u_int32_t *)sblk.b_un.b_fs;
359			endlp = olp + (SBSIZE / sizeof *olp);
360			for ( ; olp < endlp; olp++, nlp++) {
361				if (*olp == *nlp)
362					continue;
363				printf("offset %ld, original %ld, alternate %ld\n",
364					(long)(olp - (u_int32_t *)sblk.b_un.b_fs),
365					(long)letoh32(*olp),
366					(long)letoh32(*nlp));
367			}
368		}
369		badsb(listerr,
370		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
371		return (0);
372	}
373	havesb = 1;
374	return (1);
375}
376
377void
378copyback_sb(struct bufarea *bp)
379{
380	/* Copy the in-memory superblock back to buffer */
381	bp->b_un.b_fs->e2fs_icount = letoh32(sblock.e2fs.e2fs_icount);
382	bp->b_un.b_fs->e2fs_bcount = letoh32(sblock.e2fs.e2fs_bcount);
383	bp->b_un.b_fs->e2fs_rbcount = letoh32(sblock.e2fs.e2fs_rbcount);
384	bp->b_un.b_fs->e2fs_fbcount = letoh32(sblock.e2fs.e2fs_fbcount);
385	bp->b_un.b_fs->e2fs_ficount = letoh32(sblock.e2fs.e2fs_ficount);
386	bp->b_un.b_fs->e2fs_first_dblock =
387					letoh32(sblock.e2fs.e2fs_first_dblock);
388	bp->b_un.b_fs->e2fs_log_bsize = letoh32(sblock.e2fs.e2fs_log_bsize);
389	bp->b_un.b_fs->e2fs_log_fsize = letoh32(sblock.e2fs.e2fs_log_fsize);
390	bp->b_un.b_fs->e2fs_bpg = letoh32(sblock.e2fs.e2fs_bpg);
391	bp->b_un.b_fs->e2fs_fpg = letoh32(sblock.e2fs.e2fs_fpg);
392	bp->b_un.b_fs->e2fs_ipg = letoh32(sblock.e2fs.e2fs_ipg);
393	bp->b_un.b_fs->e2fs_mtime = letoh32(sblock.e2fs.e2fs_mtime);
394	bp->b_un.b_fs->e2fs_wtime = letoh32(sblock.e2fs.e2fs_wtime);
395	bp->b_un.b_fs->e2fs_lastfsck = letoh32(sblock.e2fs.e2fs_lastfsck);
396	bp->b_un.b_fs->e2fs_fsckintv = letoh32(sblock.e2fs.e2fs_fsckintv);
397	bp->b_un.b_fs->e2fs_creator = letoh32(sblock.e2fs.e2fs_creator);
398	bp->b_un.b_fs->e2fs_rev = letoh32(sblock.e2fs.e2fs_rev);
399	bp->b_un.b_fs->e2fs_mnt_count = letoh16(sblock.e2fs.e2fs_mnt_count);
400	bp->b_un.b_fs->e2fs_max_mnt_count =
401					letoh16(sblock.e2fs.e2fs_max_mnt_count);
402	bp->b_un.b_fs->e2fs_magic = letoh16(sblock.e2fs.e2fs_magic);
403	bp->b_un.b_fs->e2fs_state = letoh16(sblock.e2fs.e2fs_state);
404	bp->b_un.b_fs->e2fs_beh = letoh16(sblock.e2fs.e2fs_beh);
405	bp->b_un.b_fs->e2fs_ruid = letoh16(sblock.e2fs.e2fs_ruid);
406	bp->b_un.b_fs->e2fs_rgid = letoh16(sblock.e2fs.e2fs_rgid);
407}
408
409void
410badsb(int listerr, char *s)
411{
412
413	if (!listerr)
414		return;
415	if (preen)
416		printf("%s: ", cdevname());
417	pfatal("BAD SUPER BLOCK: %s\n", s);
418}
419
420/*
421 * Calculate a prototype superblock based on information in the disk label.
422 * When done the cgsblock macro can be calculated and the fs_ncg field
423 * can be used. Do NOT attempt to use other macros without verifying that
424 * their needed information is available!
425 */
426
427int
428calcsb(char *dev, int devfd, struct m_ext2fs *fs)
429{
430	struct disklabel *lp;
431	struct partition *pp;
432	char *cp;
433
434	cp = strchr(dev, '\0');
435	if ((cp == NULL || (cp[-1] < 'a' || cp[-1] >= 'a' + MAXPARTITIONS)) &&
436	    !isdigit((unsigned char)cp[-1])) {
437		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
438		return (0);
439	}
440	cp--;
441	lp = getdisklabel(dev, devfd);
442	if (isdigit((unsigned char)*cp))
443		pp = &lp->d_partitions[0];
444	else
445		pp = &lp->d_partitions[*cp - 'a'];
446	if (pp->p_fstype != FS_EXT2FS) {
447		pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
448			dev, pp->p_fstype < FSMAXTYPES ?
449			fstypenames[pp->p_fstype] : "unknown");
450		return (0);
451	}
452	memset(fs, 0, sizeof(struct m_ext2fs));
453	fs->e2fs_bsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); /* XXX */
454	fs->e2fs.e2fs_log_bsize = fs->e2fs_bsize / 1024;
455	fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
456	fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
457	fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
458	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
459	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
460	fs->e2fs_bmask = ~fs->e2fs_qbmask;
461	fs->e2fs_ncg =
462		howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
463		fs->e2fs.e2fs_bpg);
464	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
465	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
466		fs->e2fs_bsize / sizeof(struct ext2_gd));
467
468	return (1);
469}
470
471static struct disklabel *
472getdisklabel(char *s, int fd)
473{
474	static struct disklabel lab;
475
476	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
477		if (s == NULL)
478			return ((struct disklabel *)NULL);
479		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
480		errexit("%s: can't read disk label\n", s);
481	}
482	return (&lab);
483}
484
485daddr32_t
486cgoverhead(int c)
487{
488	int overh;
489	overh =	1 /* block bitmap */ +
490		1 /* inode bitmap */ +
491		sblock.e2fs_itpg;
492	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
493	    sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
494		if (cg_has_sb(c) == 0)
495			return overh;
496	}
497	overh += 1 + sblock.e2fs_ngdb;
498	return overh;
499}
500