setup.c revision 1.26
1/*	$OpenBSD: setup.c,v 1.26 2015/08/20 22:02:20 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 = 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 = calloc((unsigned)listmax, sizeof(struct inoinfo *));
233	inphead = calloc((unsigned)numdirs, sizeof(struct inoinfo *));
234	if (inpsort == NULL || inphead == NULL) {
235		printf("cannot alloc %u bytes for inphead\n",
236			(unsigned)(numdirs * sizeof(struct inoinfo *)));
237		goto badsblabel;
238	}
239	bufinit();
240	return (1);
241
242badsblabel:
243	ckfini(0);
244	return (0);
245}
246
247/*
248 * Read in the super block and its summary info.
249 */
250static int
251readsb(int listerr)
252{
253	daddr32_t super = bflag ? bflag : SBOFF / DEV_BSIZE;
254
255	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
256		return (0);
257	sblk.b_bno = super;
258	sblk.b_size = SBSIZE;
259
260	/* Copy the superblock in memory */
261	e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
262
263	/*
264	 * run a few consistency checks of the super block
265	 */
266	if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
267		badsb(listerr, "MAGIC NUMBER WRONG"); return (0);
268	}
269	if (sblock.e2fs.e2fs_log_bsize > 2) {
270		badsb(listerr, "BAD LOG_BSIZE"); return (0);
271	}
272	if (sblock.e2fs.e2fs_bpg == 0) {
273		badsb(listerr, "BAD BLOCKS PER GROUP"); return (0);
274	}
275
276	/* compute the dynamic fields of the in-memory sb */
277	/* compute dynamic sb infos */
278	sblock.e2fs_ncg =
279		howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
280		sblock.e2fs.e2fs_bpg);
281	/* XXX assume hw bsize = 512 */
282	sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
283	sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
284	sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
285	sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
286	sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
287	sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
288		sblock.e2fs_bsize / sizeof(struct ext2_gd));
289	sblock.e2fs_ipb = sblock.e2fs_bsize / sizeof(struct ext2fs_dinode);
290	sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb;
291
292	/*
293	 * Compute block size that the filesystem is based on,
294	 * according to fsbtodb, and adjust superblock block number
295	 * so we can tell if this is an alternate later.
296	 */
297	sblk.b_bno = super / DEV_BSIZE;
298
299	if (sblock.e2fs_ncg == 1) {
300		/* no alternate superblock; assume it's okey */
301		havesb = 1;
302		return 1;
303	}
304
305	getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
306		(long)SBSIZE);
307	if (asblk.b_errs)
308		return (0);
309	if (bflag) {
310		havesb = 1;
311		return (1);
312	}
313
314	/*
315	 * Set all possible fields that could differ, then do check
316	 * of whole super block against an alternate super block.
317	 * When an alternate super-block is specified this check is skipped.
318	 */
319	asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
320	asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
321	asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
322	asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
323	asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
324	asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
325	asblk.b_un.b_fs->e2fs_max_mnt_count = sblk.b_un.b_fs->e2fs_max_mnt_count;
326	asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
327	asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
328	asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
329	asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
330	asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
331	asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
332	asblk.b_un.b_fs->e2fs_block_group_nr =
333	    sblk.b_un.b_fs->e2fs_block_group_nr;
334	asblk.b_un.b_fs->e2fs_features_rocompat &= ~EXT2F_ROCOMPAT_LARGEFILE;
335	asblk.b_un.b_fs->e2fs_features_rocompat |=
336	    sblk.b_un.b_fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE;
337	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
338	    ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) ||
339	    (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) {
340		if (debug) {
341			printf("compat 0x%08x, incompat 0x%08x, compat_ro "
342			    "0x%08x\n",
343			    sblock.e2fs.e2fs_features_compat,
344			    sblock.e2fs.e2fs_features_incompat,
345			    sblock.e2fs.e2fs_features_rocompat);
346		}
347		badsb(listerr,"INCOMPATIBLE FEATURE BITS IN SUPER BLOCK");
348		return 0;
349	}
350	if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
351		if (debug) {
352			u_int32_t *nlp, *olp, *endlp;
353
354			printf("superblock mismatches\n");
355			nlp = (u_int32_t *)asblk.b_un.b_fs;
356			olp = (u_int32_t *)sblk.b_un.b_fs;
357			endlp = olp + (SBSIZE / sizeof *olp);
358			for ( ; olp < endlp; olp++, nlp++) {
359				if (*olp == *nlp)
360					continue;
361				printf("offset %ld, original %ld, alternate %ld\n",
362					(long)(olp - (u_int32_t *)sblk.b_un.b_fs),
363					(long)letoh32(*olp),
364					(long)letoh32(*nlp));
365			}
366		}
367		badsb(listerr,
368		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
369		return (0);
370	}
371	havesb = 1;
372	return (1);
373}
374
375void
376copyback_sb(struct bufarea *bp)
377{
378	/* Copy the in-memory superblock back to buffer */
379	bp->b_un.b_fs->e2fs_icount = letoh32(sblock.e2fs.e2fs_icount);
380	bp->b_un.b_fs->e2fs_bcount = letoh32(sblock.e2fs.e2fs_bcount);
381	bp->b_un.b_fs->e2fs_rbcount = letoh32(sblock.e2fs.e2fs_rbcount);
382	bp->b_un.b_fs->e2fs_fbcount = letoh32(sblock.e2fs.e2fs_fbcount);
383	bp->b_un.b_fs->e2fs_ficount = letoh32(sblock.e2fs.e2fs_ficount);
384	bp->b_un.b_fs->e2fs_first_dblock =
385					letoh32(sblock.e2fs.e2fs_first_dblock);
386	bp->b_un.b_fs->e2fs_log_bsize = letoh32(sblock.e2fs.e2fs_log_bsize);
387	bp->b_un.b_fs->e2fs_log_fsize = letoh32(sblock.e2fs.e2fs_log_fsize);
388	bp->b_un.b_fs->e2fs_bpg = letoh32(sblock.e2fs.e2fs_bpg);
389	bp->b_un.b_fs->e2fs_fpg = letoh32(sblock.e2fs.e2fs_fpg);
390	bp->b_un.b_fs->e2fs_ipg = letoh32(sblock.e2fs.e2fs_ipg);
391	bp->b_un.b_fs->e2fs_mtime = letoh32(sblock.e2fs.e2fs_mtime);
392	bp->b_un.b_fs->e2fs_wtime = letoh32(sblock.e2fs.e2fs_wtime);
393	bp->b_un.b_fs->e2fs_lastfsck = letoh32(sblock.e2fs.e2fs_lastfsck);
394	bp->b_un.b_fs->e2fs_fsckintv = letoh32(sblock.e2fs.e2fs_fsckintv);
395	bp->b_un.b_fs->e2fs_creator = letoh32(sblock.e2fs.e2fs_creator);
396	bp->b_un.b_fs->e2fs_rev = letoh32(sblock.e2fs.e2fs_rev);
397	bp->b_un.b_fs->e2fs_mnt_count = letoh16(sblock.e2fs.e2fs_mnt_count);
398	bp->b_un.b_fs->e2fs_max_mnt_count =
399					letoh16(sblock.e2fs.e2fs_max_mnt_count);
400	bp->b_un.b_fs->e2fs_magic = letoh16(sblock.e2fs.e2fs_magic);
401	bp->b_un.b_fs->e2fs_state = letoh16(sblock.e2fs.e2fs_state);
402	bp->b_un.b_fs->e2fs_beh = letoh16(sblock.e2fs.e2fs_beh);
403	bp->b_un.b_fs->e2fs_ruid = letoh16(sblock.e2fs.e2fs_ruid);
404	bp->b_un.b_fs->e2fs_rgid = letoh16(sblock.e2fs.e2fs_rgid);
405}
406
407void
408badsb(int listerr, char *s)
409{
410
411	if (!listerr)
412		return;
413	if (preen)
414		printf("%s: ", cdevname());
415	pfatal("BAD SUPER BLOCK: %s\n", s);
416}
417
418/*
419 * Calculate a prototype superblock based on information in the disk label.
420 * When done the cgsblock macro can be calculated and the fs_ncg field
421 * can be used. Do NOT attempt to use other macros without verifying that
422 * their needed information is available!
423 */
424
425int
426calcsb(char *dev, int devfd, struct m_ext2fs *fs)
427{
428	struct disklabel *lp;
429	struct partition *pp;
430	char *cp;
431
432	cp = strchr(dev, '\0');
433	if ((cp == NULL || (cp[-1] < 'a' || cp[-1] >= 'a' + MAXPARTITIONS)) &&
434	    !isdigit((unsigned char)cp[-1])) {
435		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
436		return (0);
437	}
438	cp--;
439	lp = getdisklabel(dev, devfd);
440	if (isdigit((unsigned char)*cp))
441		pp = &lp->d_partitions[0];
442	else
443		pp = &lp->d_partitions[*cp - 'a'];
444	if (pp->p_fstype != FS_EXT2FS) {
445		pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
446			dev, pp->p_fstype < FSMAXTYPES ?
447			fstypenames[pp->p_fstype] : "unknown");
448		return (0);
449	}
450	memset(fs, 0, sizeof(struct m_ext2fs));
451	fs->e2fs_bsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); /* XXX */
452	fs->e2fs.e2fs_log_bsize = fs->e2fs_bsize / 1024;
453	fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
454	fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
455	fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
456	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
457	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
458	fs->e2fs_bmask = ~fs->e2fs_qbmask;
459	fs->e2fs_ncg =
460		howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
461		fs->e2fs.e2fs_bpg);
462	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
463	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
464		fs->e2fs_bsize / sizeof(struct ext2_gd));
465
466	return (1);
467}
468
469static struct disklabel *
470getdisklabel(char *s, int fd)
471{
472	static struct disklabel lab;
473
474	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
475		if (s == NULL)
476			return ((struct disklabel *)NULL);
477		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
478		errexit("%s: can't read disk label\n", s);
479	}
480	return (&lab);
481}
482
483daddr32_t
484cgoverhead(int c)
485{
486	int overh;
487	overh =	1 /* block bitmap */ +
488		1 /* inode bitmap */ +
489		sblock.e2fs_itpg;
490	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
491	    sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
492		if (cg_has_sb(c) == 0)
493			return overh;
494	}
495	overh += 1 + sblock.e2fs_ngdb;
496	return overh;
497}
498