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