xref: /dragonfly/sbin/fsck/setup.c (revision b4f25088)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)setup.c	8.10 (Berkeley) 5/9/95
30  * $FreeBSD: src/sbin/fsck/setup.c,v 1.17.2.4 2002/06/24 05:10:41 dillon Exp $
31  * $DragonFly: src/sbin/fsck/setup.c,v 1.8 2007/05/20 20:29:01 dillon Exp $
32  */
33 
34 #define DKTYPENAMES
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/diskslice.h>
38 #include <sys/file.h>
39 
40 #include <vfs/ufs/dinode.h>
41 #include <vfs/ufs/fs.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <string.h>
47 
48 #include "fsck.h"
49 
50 struct bufarea asblk;
51 #define altsblock (*asblk.b_un.b_fs)
52 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
53 
54 static void badsb(int listerr, char *s);
55 static int readsb(int listerr);
56 
57 /*
58  * Read in a superblock finding an alternate if necessary.
59  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
60  * is already clean (preen mode only).
61  */
62 int
63 setup(char *dev)
64 {
65 	long cg, size, asked, i, j;
66 	long skipclean, bmapsize;
67 	off_t sizepb;
68 	struct stat statb;
69 	struct fs proto;
70 
71 	havesb = 0;
72 	fswritefd = -1;
73 	skipclean = fflag ? 0 : preen;
74 	if (stat(dev, &statb) < 0) {
75 		printf("Can't stat %s: %s\n", dev, strerror(errno));
76 		return (0);
77 	}
78 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
79 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
80 		pfatal("%s is not a disk device", dev);
81 		if (reply("CONTINUE") == 0)
82 			return (0);
83 	}
84 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
85 		printf("Can't open %s: %s\n", dev, strerror(errno));
86 		return (0);
87 	}
88 	if (preen == 0)
89 		printf("** %s", dev);
90 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
91 		fswritefd = -1;
92 		if (preen)
93 			pfatal("NO WRITE ACCESS");
94 		printf(" (NO WRITE)");
95 	}
96 	if (preen == 0)
97 		printf("\n");
98 	fsmodified = 0;
99 	lfdir = 0;
100 	initbarea(&sblk);
101 	initbarea(&asblk);
102 	sblk.b_un.b_buf = malloc(SBSIZE);
103 	asblk.b_un.b_buf = malloc(SBSIZE);
104 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
105 		errx(EEXIT, "cannot allocate space for superblock");
106 
107 	/*
108 	 * Figure out the device block size and the sector size.  The
109 	 * block size is updated by readsb() later on.
110 	 */
111 	{
112 		struct partinfo pinfo;
113 
114 		if (ioctl(fsreadfd, DIOCGPART, &pinfo) == 0) {
115 			dev_bsize = secsize = pinfo.media_blksize;
116 		} else {
117 			dev_bsize = secsize = DEV_BSIZE;
118 		}
119 	}
120 
121 	/*
122 	 * Read in the superblock, looking for alternates if necessary
123 	 */
124 	if (readsb(1) == 0) {
125 		skipclean = 0;
126 		if (bflag || preen)
127 			return(0);
128 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
129 			return (0);
130 		bflag = 32;
131 		if (readsb(0) == 0) {
132 			printf(
133 			    "YOU MUST USE THE -b OPTION TO FSCK TO SPECIFY\n"
134 			    "THE LOCATION OF AN ALTERNATE SUPER-BLOCK TO\n"
135 			    "SUPPLY NEEDED INFORMATION; SEE fsck(8).");
136 			bflag = 0;
137 			return(0);
138 		}
139 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
140 		bflag = 0;
141 	}
142 	if (skipclean && sblock.fs_clean) {
143 		pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
144 		return (-1);
145 	}
146 	maxfsblock = sblock.fs_size;
147 	maxino = sblock.fs_ncg * sblock.fs_ipg;
148 	/*
149 	 * Check and potentially fix certain fields in the super block.
150 	 */
151 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
152 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
153 		if (reply("SET TO DEFAULT") == 1) {
154 			sblock.fs_optim = FS_OPTTIME;
155 			sbdirty();
156 		}
157 	}
158 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
159 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
160 			sblock.fs_minfree);
161 		if (reply("SET TO DEFAULT") == 1) {
162 			sblock.fs_minfree = 10;
163 			sbdirty();
164 		}
165 	}
166 	if (sblock.fs_interleave < 1 ||
167 	    sblock.fs_interleave > sblock.fs_nsect) {
168 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
169 			sblock.fs_interleave);
170 		sblock.fs_interleave = 1;
171 		if (preen)
172 			printf(" (FIXED)\n");
173 		if (preen || reply("SET TO DEFAULT") == 1) {
174 			sbdirty();
175 			dirty(&asblk);
176 		}
177 	}
178 	if (sblock.fs_npsect < sblock.fs_nsect ||
179 	    sblock.fs_npsect > sblock.fs_nsect*2) {
180 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
181 			sblock.fs_npsect);
182 		sblock.fs_npsect = sblock.fs_nsect;
183 		if (preen)
184 			printf(" (FIXED)\n");
185 		if (preen || reply("SET TO DEFAULT") == 1) {
186 			sbdirty();
187 			dirty(&asblk);
188 		}
189 	}
190 	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
191 		newinofmt = 1;
192 	} else {
193 		sblock.fs_qbmask = ~sblock.fs_bmask;
194 		sblock.fs_qfmask = ~sblock.fs_fmask;
195 		/* This should match the kernel limit in ffs_oldfscompat(). */
196 		sblock.fs_maxfilesize = (u_int64_t)1 << 39;
197 		newinofmt = 0;
198 	}
199 	/*
200 	 * Convert to new inode format.
201 	 */
202 	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
203 		if (preen)
204 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
205 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
206 			return(0);
207 		doinglevel2++;
208 		sblock.fs_inodefmt = FS_44INODEFMT;
209 		sizepb = sblock.fs_bsize;
210 		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
211 		for (i = 0; i < NIADDR; i++) {
212 			sizepb *= NINDIR(&sblock);
213 			sblock.fs_maxfilesize += sizepb;
214 		}
215 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
216 		sblock.fs_qbmask = ~sblock.fs_bmask;
217 		sblock.fs_qfmask = ~sblock.fs_fmask;
218 		sbdirty();
219 		dirty(&asblk);
220 	}
221 	/*
222 	 * Convert to new cylinder group format.
223 	 */
224 	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
225 		if (preen)
226 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
227 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
228 			return(0);
229 		doinglevel1++;
230 		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
231 		sblock.fs_nrpos = 8;
232 		sblock.fs_postbloff =
233 		    (char *)(&sblock.fs_opostbl[0][0]) -
234 		    (char *)(&sblock.fs_firstfield);
235 		sblock.fs_rotbloff = &sblock.fs_space[0] -
236 		    (u_char *)(&sblock.fs_firstfield);
237 		sblock.fs_cgsize =
238 			fragroundup(&sblock, CGSIZE(&sblock));
239 		sbdirty();
240 		dirty(&asblk);
241 	}
242 	if (asblk.b_dirty && !bflag) {
243 		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
244 		flush(fswritefd, &asblk);
245 	}
246 	/*
247 	 * read in the summary info.
248 	 */
249 	asked = 0;
250 	sblock.fs_csp = calloc(1, sblock.fs_cssize);
251 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
252 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
253 		    sblock.fs_cssize - i : sblock.fs_bsize;
254 		if (bread(fsreadfd, (char *)sblock.fs_csp + i,
255 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
256 		    size) != 0 && !asked) {
257 			pfatal("BAD SUMMARY INFORMATION");
258 			if (reply("CONTINUE") == 0) {
259 				ckfini(0);
260 				exit(EEXIT);
261 			}
262 			asked++;
263 		}
264 	}
265 	/*
266 	 * allocate and initialize the necessary maps
267 	 */
268 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
269 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
270 	if (blockmap == NULL) {
271 		printf("cannot alloc %u bytes for blockmap\n",
272 		    (unsigned)bmapsize);
273 		goto badsb;
274 	}
275 	inostathead = calloc((unsigned)(sblock.fs_ncg),
276 	    sizeof(struct inostatlist));
277 	if (inostathead == NULL) {
278 		printf("cannot alloc %u bytes for inostathead\n",
279 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
280 		goto badsb;
281 	}
282 	numdirs = sblock.fs_cstotal.cs_ndir;
283 
284 	/*
285 	 * Calculate the directory hash table size.  Do not allocate
286 	 * a ridiculous amount of memory if we have a lot of directories.
287 	 */
288 	for (dirhash = 16; dirhash < numdirs; dirhash <<= 1)
289 		;
290 	if (dirhash > 1024*1024)
291 		dirhash /= 8;
292 	dirhashmask = dirhash - 1;
293 
294 	if (numdirs == 0) {
295 		printf("numdirs is zero, try using an alternate superblock\n");
296 		goto badsb;
297 	}
298 	inplast = 0;
299 	listmax = numdirs + 10;
300 	inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *));
301 	inphead = calloc((unsigned)dirhash, sizeof(struct inoinfo *));
302 	if (inpsort == NULL || inphead == NULL) {
303 		printf("cannot allocate base structures for %ld directories\n",
304 			numdirs);
305 		goto badsb;
306 	}
307 	bufinit();
308 	if (sblock.fs_flags & FS_DOSOFTDEP)
309 		usedsoftdep = 1;
310 	else
311 		usedsoftdep = 0;
312 	return (1);
313 
314 badsb:
315 	ckfini(0);
316 	return (0);
317 }
318 
319 /*
320  * Read in the super block and its summary info.
321  */
322 static int
323 readsb(int listerr)
324 {
325 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
326 
327 	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
328 		return (0);
329 	sblk.b_bno = super;
330 	sblk.b_size = SBSIZE;
331 	/*
332 	 * run a few consistency checks of the super block
333 	 */
334 	if (sblock.fs_magic != FS_MAGIC)
335 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
336 	if (sblock.fs_ncg < 1)
337 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
338 	if (sblock.fs_cpg < 1)
339 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
340 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
341 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
342 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
343 	if (sblock.fs_sbsize > SBSIZE)
344 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
345 	/*
346 	 * Compute block size that the filesystem is based on,
347 	 * according to fsbtodb, and adjust superblock block number
348 	 * so we can tell if this is an alternate later.
349 	 */
350 	super *= dev_bsize;
351 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
352 	sblk.b_bno = super / dev_bsize;
353 	if (bflag) {
354 		havesb = 1;
355 		return (1);
356 	}
357 	/*
358 	 * Compare all fields that should not differ in alternate super block.
359 	 * When an alternate super-block is specified this check is skipped.
360 	 */
361 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
362 	if (asblk.b_errs)
363 		return (0);
364 	if (altsblock.fs_sblkno != sblock.fs_sblkno ||
365 	    altsblock.fs_cblkno != sblock.fs_cblkno ||
366 	    altsblock.fs_iblkno != sblock.fs_iblkno ||
367 	    altsblock.fs_dblkno != sblock.fs_dblkno ||
368 	    altsblock.fs_cgoffset != sblock.fs_cgoffset ||
369 	    altsblock.fs_cgmask != sblock.fs_cgmask ||
370 	    altsblock.fs_ncg != sblock.fs_ncg ||
371 	    altsblock.fs_bsize != sblock.fs_bsize ||
372 	    altsblock.fs_fsize != sblock.fs_fsize ||
373 	    altsblock.fs_frag != sblock.fs_frag ||
374 	    altsblock.fs_bmask != sblock.fs_bmask ||
375 	    altsblock.fs_fmask != sblock.fs_fmask ||
376 	    altsblock.fs_bshift != sblock.fs_bshift ||
377 	    altsblock.fs_fshift != sblock.fs_fshift ||
378 	    altsblock.fs_fragshift != sblock.fs_fragshift ||
379 	    altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
380 	    altsblock.fs_sbsize != sblock.fs_sbsize ||
381 	    altsblock.fs_nindir != sblock.fs_nindir ||
382 	    altsblock.fs_inopb != sblock.fs_inopb ||
383 	    altsblock.fs_cssize != sblock.fs_cssize ||
384 	    altsblock.fs_cpg != sblock.fs_cpg ||
385 	    altsblock.fs_ipg != sblock.fs_ipg ||
386 	    altsblock.fs_fpg != sblock.fs_fpg ||
387 	    altsblock.fs_magic != sblock.fs_magic) {
388 		badsb(listerr,
389 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
390 		return (0);
391 	}
392 	havesb = 1;
393 	return (1);
394 }
395 
396 static void
397 badsb(int listerr, char *s)
398 {
399 
400 	if (!listerr)
401 		return;
402 	if (preen)
403 		printf("%s: ", cdevname);
404 	pfatal("BAD SUPER BLOCK: %s\n", s);
405 }
406 
407