xref: /freebsd/sbin/fsck_ffs/setup.c (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. 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 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41 
42 #define DKTYPENAMES
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/disklabel.h>
46 #include <sys/file.h>
47 #include <sys/sysctl.h>
48 
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ffs/fs.h>
51 
52 #include <ctype.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <string.h>
56 
57 #include "fsck.h"
58 
59 struct bufarea asblk;
60 #define altsblock (*asblk.b_un.b_fs)
61 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
62 
63 static void badsb(int listerr, char *s);
64 static int calcsb(char *dev, int devfd, struct fs *fs);
65 static struct disklabel *getdisklabel(char *s, int fd);
66 
67 /*
68  * Read in a superblock finding an alternate if necessary.
69  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
70  * is already clean (preen mode only).
71  */
72 int
73 setup(char *dev)
74 {
75 	long cg, asked, i, j;
76 	long bmapsize;
77 	off_t sizepb;
78 	struct stat statb;
79 	struct fs proto;
80 	size_t size;
81 
82 	havesb = 0;
83 	fswritefd = -1;
84 	cursnapshot = 0;
85 	if (stat(dev, &statb) < 0) {
86 		printf("Can't stat %s: %s\n", dev, strerror(errno));
87 		if (bkgrdflag) {
88 			unlink(snapname);
89 			bkgrdflag = 0;
90 		}
91 		return (0);
92 	}
93 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
94 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
95 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
96 			unlink(snapname);
97 			printf("background fsck lacks a snapshot\n");
98 			exit(EEXIT);
99 		}
100 		if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
101 			cursnapshot = statb.st_ino;
102 		} else {
103 			if (cvtlevel == 0 ||
104 			    (statb.st_flags & SF_SNAPSHOT) == 0) {
105 				if (preen && bkgrdflag) {
106 					unlink(snapname);
107 					bkgrdflag = 0;
108 				}
109 				pfatal("%s is not a disk device", dev);
110 				if (reply("CONTINUE") == 0) {
111 					if (bkgrdflag) {
112 						unlink(snapname);
113 						bkgrdflag = 0;
114 					}
115 					return (0);
116 				}
117 			} else {
118 				if (bkgrdflag) {
119 					unlink(snapname);
120 					bkgrdflag = 0;
121 				}
122 				pfatal("cannot convert a snapshot");
123 				exit(EEXIT);
124 			}
125 		}
126 	}
127 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
128 		if (bkgrdflag) {
129 			unlink(snapname);
130 			bkgrdflag = 0;
131 		}
132 		printf("Can't open %s: %s\n", dev, strerror(errno));
133 		return (0);
134 	}
135 	if (bkgrdflag) {
136 		unlink(snapname);
137 		size = MIBSIZE;
138 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
139 		    sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
140 		    sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
141 		    sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
142 		    sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
143 			pfatal("kernel lacks background fsck support\n");
144 			exit(EEXIT);
145 		}
146 		cmd.version = FFS_CMD_VERSION;
147 		cmd.handle = fsreadfd;
148 		fswritefd = -1;
149 	}
150 	if (preen == 0)
151 		printf("** %s", dev);
152 	if (bkgrdflag == 0 &&
153 	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
154 		fswritefd = -1;
155 		if (preen)
156 			pfatal("NO WRITE ACCESS");
157 		printf(" (NO WRITE)");
158 	}
159 	if (preen == 0)
160 		printf("\n");
161 	/*
162 	 * Read in the superblock, looking for alternates if necessary
163 	 */
164 	if (readsb(1) == 0) {
165 		skipclean = 0;
166 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
167 			return(0);
168 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
169 			return (0);
170 		for (cg = 0; cg < proto.fs_ncg; cg++) {
171 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
172 			if (readsb(0) != 0)
173 				break;
174 		}
175 		if (cg >= proto.fs_ncg) {
176 			printf("%s %s\n%s %s\n%s %s\n",
177 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
178 				"FAILED. YOU MUST USE THE",
179 				"-b OPTION TO FSCK TO SPECIFY THE",
180 				"LOCATION OF AN ALTERNATE",
181 				"SUPER-BLOCK TO SUPPLY NEEDED",
182 				"INFORMATION; SEE fsck(8).");
183 			bflag = 0;
184 			return(0);
185 		}
186 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
187 		bflag = 0;
188 	}
189 	if (skipclean && preen && sblock.fs_clean) {
190 		pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
191 		return (-1);
192 	}
193 	maxfsblock = sblock.fs_size;
194 	maxino = sblock.fs_ncg * sblock.fs_ipg;
195 	/*
196 	 * Check and potentially fix certain fields in the super block.
197 	 */
198 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
199 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
200 		if (reply("SET TO DEFAULT") == 1) {
201 			sblock.fs_optim = FS_OPTTIME;
202 			sbdirty();
203 		}
204 	}
205 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
206 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
207 			sblock.fs_minfree);
208 		if (reply("SET TO DEFAULT") == 1) {
209 			sblock.fs_minfree = 10;
210 			sbdirty();
211 		}
212 	}
213 	if (sblock.fs_interleave < 1 ||
214 	    sblock.fs_interleave > sblock.fs_nsect) {
215 		pfatal("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
216 			sblock.fs_interleave);
217 		sblock.fs_interleave = 1;
218 		if (preen)
219 			printf(" (FIXED)\n");
220 		if (preen || reply("SET TO DEFAULT") == 1) {
221 			sbdirty();
222 			dirty(&asblk);
223 		}
224 	}
225 	if (sblock.fs_npsect < sblock.fs_nsect ||
226 	    sblock.fs_npsect > sblock.fs_nsect*2) {
227 		pfatal("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
228 			sblock.fs_npsect);
229 		sblock.fs_npsect = sblock.fs_nsect;
230 		if (preen)
231 			printf(" (FIXED)\n");
232 		if (preen || reply("SET TO DEFAULT") == 1) {
233 			sbdirty();
234 			dirty(&asblk);
235 		}
236 	}
237 	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
238 		newinofmt = 1;
239 	} else {
240 		sblock.fs_qbmask = ~sblock.fs_bmask;
241 		sblock.fs_qfmask = ~sblock.fs_fmask;
242 		/* This should match the kernel limit in ffs_oldfscompat(). */
243 		sblock.fs_maxfilesize = (u_int64_t)1 << 39;
244 		newinofmt = 0;
245 	}
246 	/*
247 	 * Convert to new inode format.
248 	 */
249 	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
250 		if (preen)
251 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
252 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
253 			return(0);
254 		doinglevel2++;
255 		sblock.fs_inodefmt = FS_44INODEFMT;
256 		sizepb = sblock.fs_bsize;
257 		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
258 		for (i = 0; i < NIADDR; i++) {
259 			sizepb *= NINDIR(&sblock);
260 			sblock.fs_maxfilesize += sizepb;
261 		}
262 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
263 		sblock.fs_qbmask = ~sblock.fs_bmask;
264 		sblock.fs_qfmask = ~sblock.fs_fmask;
265 		sbdirty();
266 		dirty(&asblk);
267 	}
268 	/*
269 	 * Convert to new cylinder group format.
270 	 */
271 	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
272 		if (preen)
273 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
274 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
275 			return(0);
276 		doinglevel1++;
277 		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
278 		sblock.fs_nrpos = 8;
279 		sblock.fs_postbloff =
280 		    (char *)(&sblock.fs_opostbl[0][0]) -
281 		    (char *)(&sblock.fs_firstfield);
282 		sblock.fs_rotbloff = &sblock.fs_space[0] -
283 		    (u_char *)(&sblock.fs_firstfield);
284 		sblock.fs_cgsize =
285 			fragroundup(&sblock, CGSIZE(&sblock));
286 		sbdirty();
287 		dirty(&asblk);
288 	}
289 	if (asblk.b_dirty && !bflag) {
290 		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
291 		flush(fswritefd, &asblk);
292 	}
293 	/*
294 	 * read in the summary info.
295 	 */
296 	asked = 0;
297 	sblock.fs_csp = calloc(1, sblock.fs_cssize);
298 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
299 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
300 		    sblock.fs_cssize - i : sblock.fs_bsize;
301 		if (bread(fsreadfd, (char *)sblock.fs_csp + i,
302 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
303 		    size) != 0 && !asked) {
304 			pfatal("BAD SUMMARY INFORMATION");
305 			if (reply("CONTINUE") == 0) {
306 				ckfini(0);
307 				exit(EEXIT);
308 			}
309 			asked++;
310 		}
311 	}
312 	/*
313 	 * allocate and initialize the necessary maps
314 	 */
315 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
316 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
317 	if (blockmap == NULL) {
318 		printf("cannot alloc %u bytes for blockmap\n",
319 		    (unsigned)bmapsize);
320 		goto badsb;
321 	}
322 	inostathead = calloc((unsigned)(sblock.fs_ncg),
323 	    sizeof(struct inostatlist));
324 	if (inostathead == NULL) {
325 		printf("cannot alloc %u bytes for inostathead\n",
326 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
327 		goto badsb;
328 	}
329 	numdirs = sblock.fs_cstotal.cs_ndir;
330 	dirhash = numdirs;
331 	if (numdirs == 0) {
332 		printf("numdirs is zero, try using an alternate superblock\n");
333 		goto badsb;
334 	}
335 	inplast = 0;
336 	listmax = numdirs + 10;
337 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
338 	    sizeof(struct inoinfo *));
339 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
340 	    sizeof(struct inoinfo *));
341 	if (inpsort == NULL || inphead == NULL) {
342 		printf("cannot alloc %u bytes for inphead\n",
343 		    (unsigned)numdirs * sizeof(struct inoinfo *));
344 		goto badsb;
345 	}
346 	bufinit();
347 	if (sblock.fs_flags & FS_DOSOFTDEP)
348 		usedsoftdep = 1;
349 	else
350 		usedsoftdep = 0;
351 	return (1);
352 
353 badsb:
354 	ckfini(0);
355 	return (0);
356 }
357 
358 /*
359  * Read in the super block and its summary info.
360  */
361 int
362 readsb(int listerr)
363 {
364 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
365 
366 	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
367 		return (0);
368 	sblk.b_bno = super;
369 	sblk.b_size = SBSIZE;
370 	/*
371 	 * run a few consistency checks of the super block
372 	 */
373 	if (sblock.fs_magic != FS_MAGIC)
374 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
375 	if (sblock.fs_ncg < 1)
376 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
377 	if (sblock.fs_cpg < 1)
378 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
379 	if (sblock.fs_sbsize > SBSIZE)
380 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
381 	/*
382 	 * Compute block size that the filesystem is based on,
383 	 * according to fsbtodb, and adjust superblock block number
384 	 * so we can tell if this is an alternate later.
385 	 */
386 	super *= dev_bsize;
387 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
388 	sblk.b_bno = super / dev_bsize;
389 	if (bflag) {
390 		havesb = 1;
391 		return (1);
392 	}
393 	/*
394 	 * Compare all fields that should not differ in alternate super block.
395 	 * When an alternate super-block is specified this check is skipped.
396 	 */
397 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
398 	if (asblk.b_errs)
399 		return (0);
400 	if (altsblock.fs_sblkno != sblock.fs_sblkno ||
401 	    altsblock.fs_cblkno != sblock.fs_cblkno ||
402 	    altsblock.fs_iblkno != sblock.fs_iblkno ||
403 	    altsblock.fs_dblkno != sblock.fs_dblkno ||
404 	    altsblock.fs_cgoffset != sblock.fs_cgoffset ||
405 	    altsblock.fs_cgmask != sblock.fs_cgmask ||
406 	    altsblock.fs_ncg != sblock.fs_ncg ||
407 	    altsblock.fs_bsize != sblock.fs_bsize ||
408 	    altsblock.fs_fsize != sblock.fs_fsize ||
409 	    altsblock.fs_frag != sblock.fs_frag ||
410 	    altsblock.fs_bmask != sblock.fs_bmask ||
411 	    altsblock.fs_fmask != sblock.fs_fmask ||
412 	    altsblock.fs_bshift != sblock.fs_bshift ||
413 	    altsblock.fs_fshift != sblock.fs_fshift ||
414 	    altsblock.fs_fragshift != sblock.fs_fragshift ||
415 	    altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
416 	    altsblock.fs_sbsize != sblock.fs_sbsize ||
417 	    altsblock.fs_nindir != sblock.fs_nindir ||
418 	    altsblock.fs_inopb != sblock.fs_inopb ||
419 	    altsblock.fs_cssize != sblock.fs_cssize ||
420 	    altsblock.fs_cpg != sblock.fs_cpg ||
421 	    altsblock.fs_ipg != sblock.fs_ipg ||
422 	    altsblock.fs_fpg != sblock.fs_fpg ||
423 	    altsblock.fs_magic != sblock.fs_magic) {
424 		badsb(listerr,
425 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
426 		return (0);
427 	}
428 	havesb = 1;
429 	return (1);
430 }
431 
432 static void
433 badsb(int listerr, char *s)
434 {
435 
436 	if (!listerr)
437 		return;
438 	if (preen)
439 		printf("%s: ", cdevname);
440 	pfatal("BAD SUPER BLOCK: %s\n", s);
441 }
442 
443 void
444 sblock_init(void)
445 {
446 	struct disklabel *lp;
447 
448 	fswritefd = -1;
449 	fsmodified = 0;
450 	lfdir = 0;
451 	initbarea(&sblk);
452 	initbarea(&asblk);
453 	sblk.b_un.b_buf = malloc(SBSIZE);
454 	asblk.b_un.b_buf = malloc(SBSIZE);
455 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
456 		errx(EEXIT, "cannot allocate space for superblock");
457 	if ((lp = getdisklabel(NULL, fsreadfd)))
458 		dev_bsize = secsize = lp->d_secsize;
459 	else
460 		dev_bsize = secsize = DEV_BSIZE;
461 }
462 
463 /*
464  * Calculate a prototype superblock based on information in the disk label.
465  * When done the cgsblock macro can be calculated and the fs_ncg field
466  * can be used. Do NOT attempt to use other macros without verifying that
467  * their needed information is available!
468  */
469 static int
470 calcsb(char *dev, int devfd, struct fs *fs)
471 {
472 	struct disklabel *lp;
473 	struct partition *pp;
474 	char *cp;
475 	int i;
476 
477 	cp = strchr(dev, '\0') - 1;
478 	if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
479 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
480 		return (0);
481 	}
482 	lp = getdisklabel(dev, devfd);
483 	if (isdigit(*cp))
484 		pp = &lp->d_partitions[0];
485 	else
486 		pp = &lp->d_partitions[*cp - 'a'];
487 	if (pp->p_fstype != FS_BSDFFS) {
488 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
489 			dev, pp->p_fstype < FSMAXTYPES ?
490 			fstypenames[pp->p_fstype] : "unknown");
491 		return (0);
492 	}
493 	if (pp->p_fsize == 0 || pp->p_frag == 0 ||
494 	    pp->p_cpg == 0 || pp->p_size == 0) {
495 		pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
496 		    dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
497 		    pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
498 		return (0);
499 	}
500 	memset(fs, 0, sizeof(struct fs));
501 	fs->fs_fsize = pp->p_fsize;
502 	fs->fs_frag = pp->p_frag;
503 	fs->fs_cpg = pp->p_cpg;
504 	fs->fs_size = pp->p_size;
505 	fs->fs_ntrak = lp->d_ntracks;
506 	fs->fs_nsect = lp->d_nsectors;
507 	fs->fs_spc = lp->d_secpercyl;
508 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
509 	fs->fs_sblkno = roundup(
510 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
511 		fs->fs_frag);
512 	fs->fs_cgmask = 0xffffffff;
513 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
514 		fs->fs_cgmask <<= 1;
515 	if (!POWEROF2(fs->fs_ntrak))
516 		fs->fs_cgmask <<= 1;
517 	fs->fs_cgoffset = roundup(
518 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
519 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
520 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
521 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
522 		fs->fs_fsbtodb++;
523 	dev_bsize = lp->d_secsize;
524 	return (1);
525 }
526 
527 static struct disklabel *
528 getdisklabel(char *s, int fd)
529 {
530 	static struct disklabel lab;
531 
532 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
533 		if (s == NULL)
534 			return ((struct disklabel *)NULL);
535 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
536 		errx(EEXIT, "%s: can't read disk label", s);
537 	}
538 	return (&lab);
539 }
540