xref: /freebsd/sbin/fsck_ffs/setup.c (revision 0782240958117692ab9861abb5483c9e64f6bc39)
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  * 4. 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 
30 #if 0
31 #ifndef lint
32 static const char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
33 #endif /* not lint */
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #define FSTYPENAMES
41 #include <sys/disklabel.h>
42 #include <sys/file.h>
43 #include <sys/sysctl.h>
44 
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <limits.h>
52 #include <stdint.h>
53 #include <string.h>
54 
55 #include "fsck.h"
56 
57 struct bufarea asblk;
58 #define altsblock (*asblk.b_un.b_fs)
59 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
60 
61 static void badsb(int listerr, const char *s);
62 static int calcsb(char *dev, int devfd, struct fs *fs);
63 static struct disklabel *getdisklabel(char *s, int fd);
64 
65 /*
66  * Read in a superblock finding an alternate if necessary.
67  * Return 1 if successful, 0 if unsuccessful, -1 if file system
68  * is already clean (ckclean and preen mode only).
69  */
70 int
71 setup(char *dev)
72 {
73 	long cg, asked, i, j;
74 	long bmapsize;
75 	struct stat statb;
76 	struct fs proto;
77 	size_t size;
78 
79 	havesb = 0;
80 	fswritefd = -1;
81 	cursnapshot = 0;
82 	if (stat(dev, &statb) < 0) {
83 		printf("Can't stat %s: %s\n", dev, strerror(errno));
84 		if (bkgrdflag) {
85 			unlink(snapname);
86 			bkgrdflag = 0;
87 		}
88 		return (0);
89 	}
90 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
91 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
92 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
93 			unlink(snapname);
94 			printf("background fsck lacks a snapshot\n");
95 			exit(EEXIT);
96 		}
97 		if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
98 			cursnapshot = statb.st_ino;
99 		} else {
100 			if (cvtlevel == 0 ||
101 			    (statb.st_flags & SF_SNAPSHOT) == 0) {
102 				if (preen && bkgrdflag) {
103 					unlink(snapname);
104 					bkgrdflag = 0;
105 				}
106 				pfatal("%s is not a disk device", dev);
107 				if (reply("CONTINUE") == 0) {
108 					if (bkgrdflag) {
109 						unlink(snapname);
110 						bkgrdflag = 0;
111 					}
112 					return (0);
113 				}
114 			} else {
115 				if (bkgrdflag) {
116 					unlink(snapname);
117 					bkgrdflag = 0;
118 				}
119 				pfatal("cannot convert a snapshot");
120 				exit(EEXIT);
121 			}
122 		}
123 	}
124 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
125 		if (bkgrdflag) {
126 			unlink(snapname);
127 			bkgrdflag = 0;
128 		}
129 		printf("Can't open %s: %s\n", dev, strerror(errno));
130 		return (0);
131 	}
132 	if (bkgrdflag) {
133 		unlink(snapname);
134 		size = MIBSIZE;
135 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
136 		    sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
137 		    sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
138 		    sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
139 		    sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
140 			pfatal("kernel lacks background fsck support\n");
141 			exit(EEXIT);
142 		}
143 		/*
144 		 * When kernel is lack of runtime bgfsck superblock summary
145 		 * adjustment functionality, it does not mean we can not
146 		 * continue, as old kernels will recompute the summary at
147 		 * mount time.  However, it will be an unexpected softupdates
148 		 * inconsistency if it turns out that the summary is still
149 		 * incorrect.  Set a flag so subsequent operation can know
150 		 * this.
151 		 */
152 		bkgrdsumadj = 1;
153 		if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
154 		    sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
155 		    sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
156 		    sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
157 		    sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) {
158 			bkgrdsumadj = 0;
159 			pwarn("kernel lacks runtime superblock summary adjustment support");
160 		}
161 		cmd.version = FFS_CMD_VERSION;
162 		cmd.handle = fsreadfd;
163 		fswritefd = -1;
164 	}
165 	if (preen == 0)
166 		printf("** %s", dev);
167 	if (bkgrdflag == 0 &&
168 	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
169 		fswritefd = -1;
170 		if (preen)
171 			pfatal("NO WRITE ACCESS");
172 		printf(" (NO WRITE)");
173 	}
174 	if (preen == 0)
175 		printf("\n");
176 	/*
177 	 * Read in the superblock, looking for alternates if necessary
178 	 */
179 	if (readsb(1) == 0) {
180 		skipclean = 0;
181 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
182 			return(0);
183 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
184 			return (0);
185 		for (cg = 0; cg < proto.fs_ncg; cg++) {
186 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
187 			if (readsb(0) != 0)
188 				break;
189 		}
190 		if (cg >= proto.fs_ncg) {
191 			printf("%s %s\n%s %s\n%s %s\n",
192 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
193 				"FAILED. YOU MUST USE THE",
194 				"-b OPTION TO FSCK TO SPECIFY THE",
195 				"LOCATION OF AN ALTERNATE",
196 				"SUPER-BLOCK TO SUPPLY NEEDED",
197 				"INFORMATION; SEE fsck_ffs(8).");
198 			bflag = 0;
199 			return(0);
200 		}
201 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
202 		bflag = 0;
203 	}
204 	if (skipclean && ckclean && sblock.fs_clean) {
205 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
206 		return (-1);
207 	}
208 	maxfsblock = sblock.fs_size;
209 	maxino = sblock.fs_ncg * sblock.fs_ipg;
210 	/*
211 	 * Check and potentially fix certain fields in the super block.
212 	 */
213 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
214 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
215 		if (reply("SET TO DEFAULT") == 1) {
216 			sblock.fs_optim = FS_OPTTIME;
217 			sbdirty();
218 		}
219 	}
220 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
221 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
222 			sblock.fs_minfree);
223 		if (reply("SET TO DEFAULT") == 1) {
224 			sblock.fs_minfree = 10;
225 			sbdirty();
226 		}
227 	}
228 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
229 	    sblock.fs_old_inodefmt < FS_44INODEFMT) {
230 		pwarn("Format of file system is too old.\n");
231 		pwarn("Must update to modern format using a version of fsck\n");
232 		pfatal("from before 2002 with the command ``fsck -c 2''\n");
233 		exit(EEXIT);
234 	}
235 	if (asblk.b_dirty && !bflag) {
236 		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
237 		flush(fswritefd, &asblk);
238 	}
239 	/*
240 	 * read in the summary info.
241 	 */
242 	asked = 0;
243 	sblock.fs_csp = Calloc(1, sblock.fs_cssize);
244 	if (sblock.fs_csp == NULL) {
245 		printf("cannot alloc %u bytes for cg summary info\n",
246 		    (unsigned)sblock.fs_cssize);
247 		goto badsb;
248 	}
249 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
250 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
251 		    sblock.fs_cssize - i : sblock.fs_bsize;
252 		readcnt[sblk.b_type]++;
253 		if (blread(fsreadfd, (char *)sblock.fs_csp + i,
254 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
255 		    size) != 0 && !asked) {
256 			pfatal("BAD SUMMARY INFORMATION");
257 			if (reply("CONTINUE") == 0) {
258 				ckfini(0);
259 				exit(EEXIT);
260 			}
261 			asked++;
262 		}
263 	}
264 	/*
265 	 * allocate and initialize the necessary maps
266 	 */
267 	bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
268 	blockmap = Calloc((unsigned)bmapsize, sizeof (char));
269 	if (blockmap == NULL) {
270 		printf("cannot alloc %u bytes for blockmap\n",
271 		    (unsigned)bmapsize);
272 		goto badsb;
273 	}
274 	inostathead = Calloc((unsigned)(sblock.fs_ncg),
275 	    sizeof(struct inostatlist));
276 	if (inostathead == NULL) {
277 		printf("cannot alloc %u bytes for inostathead\n",
278 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
279 		goto badsb;
280 	}
281 	numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
282 	dirhash = numdirs;
283 	inplast = 0;
284 	listmax = numdirs + 10;
285 	inpsort = (struct inoinfo **)Calloc((unsigned)listmax,
286 	    sizeof(struct inoinfo *));
287 	inphead = (struct inoinfo **)Calloc((unsigned)numdirs,
288 	    sizeof(struct inoinfo *));
289 	if (inpsort == NULL || inphead == NULL) {
290 		printf("cannot alloc %ju bytes for inphead\n",
291 		    (uintmax_t)numdirs * sizeof(struct inoinfo *));
292 		goto badsb;
293 	}
294 	bufinit();
295 	if (sblock.fs_flags & FS_DOSOFTDEP)
296 		usedsoftdep = 1;
297 	else
298 		usedsoftdep = 0;
299 	return (1);
300 
301 badsb:
302 	ckfini(0);
303 	return (0);
304 }
305 
306 /*
307  * Possible superblock locations ordered from most to least likely.
308  */
309 static int sblock_try[] = SBLOCKSEARCH;
310 
311 #define BAD_MAGIC_MSG \
312 "The previous newfs operation on this volume did not complete.\n" \
313 "You must complete newfs before mounting this volume.\n"
314 
315 /*
316  * Read in the super block and its summary info.
317  */
318 int
319 readsb(int listerr)
320 {
321 	ufs2_daddr_t super;
322 	int i;
323 
324 	if (bflag) {
325 		super = bflag;
326 		readcnt[sblk.b_type]++;
327 		if ((blread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE)))
328 			return (0);
329 		if (sblock.fs_magic == FS_BAD_MAGIC) {
330 			fprintf(stderr, BAD_MAGIC_MSG);
331 			exit(11);
332 		}
333 		if (sblock.fs_magic != FS_UFS1_MAGIC &&
334 		    sblock.fs_magic != FS_UFS2_MAGIC) {
335 			fprintf(stderr, "%d is not a file system superblock\n",
336 			    bflag);
337 			return (0);
338 		}
339 	} else {
340 		for (i = 0; sblock_try[i] != -1; i++) {
341 			super = sblock_try[i] / dev_bsize;
342 			readcnt[sblk.b_type]++;
343 			if ((blread(fsreadfd, (char *)&sblock, super,
344 			    (long)SBLOCKSIZE)))
345 				return (0);
346 			if (sblock.fs_magic == FS_BAD_MAGIC) {
347 				fprintf(stderr, BAD_MAGIC_MSG);
348 				exit(11);
349 			}
350 			if ((sblock.fs_magic == FS_UFS1_MAGIC ||
351 			     (sblock.fs_magic == FS_UFS2_MAGIC &&
352 			      sblock.fs_sblockloc == sblock_try[i])) &&
353 			    sblock.fs_ncg >= 1 &&
354 			    sblock.fs_bsize >= MINBSIZE &&
355 			    sblock.fs_sbsize >= roundup(sizeof(struct fs), dev_bsize))
356 				break;
357 		}
358 		if (sblock_try[i] == -1) {
359 			fprintf(stderr, "Cannot find file system superblock\n");
360 			return (0);
361 		}
362 	}
363 	/*
364 	 * Compute block size that the file system is based on,
365 	 * according to fsbtodb, and adjust superblock block number
366 	 * so we can tell if this is an alternate later.
367 	 */
368 	super *= dev_bsize;
369 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
370 	sblk.b_bno = super / dev_bsize;
371 	sblk.b_size = SBLOCKSIZE;
372 	if (bflag)
373 		goto out;
374 	/*
375 	 * Compare all fields that should not differ in alternate super block.
376 	 * When an alternate super-block is specified this check is skipped.
377 	 */
378 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
379 	if (asblk.b_errs)
380 		return (0);
381 	if (altsblock.fs_sblkno != sblock.fs_sblkno ||
382 	    altsblock.fs_cblkno != sblock.fs_cblkno ||
383 	    altsblock.fs_iblkno != sblock.fs_iblkno ||
384 	    altsblock.fs_dblkno != sblock.fs_dblkno ||
385 	    altsblock.fs_ncg != sblock.fs_ncg ||
386 	    altsblock.fs_bsize != sblock.fs_bsize ||
387 	    altsblock.fs_fsize != sblock.fs_fsize ||
388 	    altsblock.fs_frag != sblock.fs_frag ||
389 	    altsblock.fs_bmask != sblock.fs_bmask ||
390 	    altsblock.fs_fmask != sblock.fs_fmask ||
391 	    altsblock.fs_bshift != sblock.fs_bshift ||
392 	    altsblock.fs_fshift != sblock.fs_fshift ||
393 	    altsblock.fs_fragshift != sblock.fs_fragshift ||
394 	    altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
395 	    altsblock.fs_sbsize != sblock.fs_sbsize ||
396 	    altsblock.fs_nindir != sblock.fs_nindir ||
397 	    altsblock.fs_inopb != sblock.fs_inopb ||
398 	    altsblock.fs_cssize != sblock.fs_cssize ||
399 	    altsblock.fs_ipg != sblock.fs_ipg ||
400 	    altsblock.fs_fpg != sblock.fs_fpg ||
401 	    altsblock.fs_magic != sblock.fs_magic) {
402 		badsb(listerr,
403 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
404 		return (0);
405 	}
406 out:
407 	/*
408 	 * If not yet done, update UFS1 superblock with new wider fields.
409 	 */
410 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
411 	    sblock.fs_maxbsize != sblock.fs_bsize) {
412 		sblock.fs_maxbsize = sblock.fs_bsize;
413 		sblock.fs_time = sblock.fs_old_time;
414 		sblock.fs_size = sblock.fs_old_size;
415 		sblock.fs_dsize = sblock.fs_old_dsize;
416 		sblock.fs_csaddr = sblock.fs_old_csaddr;
417 		sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
418 		sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
419 		sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
420 		sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
421 	}
422 	havesb = 1;
423 	return (1);
424 }
425 
426 static void
427 badsb(int listerr, const char *s)
428 {
429 
430 	if (!listerr)
431 		return;
432 	if (preen)
433 		printf("%s: ", cdevname);
434 	pfatal("BAD SUPER BLOCK: %s\n", s);
435 }
436 
437 void
438 sblock_init(void)
439 {
440 	struct disklabel *lp;
441 
442 	fswritefd = -1;
443 	fsmodified = 0;
444 	lfdir = 0;
445 	initbarea(&sblk, BT_SUPERBLK);
446 	initbarea(&asblk, BT_SUPERBLK);
447 	sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
448 	asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
449 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
450 		errx(EEXIT, "cannot allocate space for superblock");
451 	if ((lp = getdisklabel(NULL, fsreadfd)))
452 		real_dev_bsize = dev_bsize = secsize = lp->d_secsize;
453 	else
454 		dev_bsize = secsize = DEV_BSIZE;
455 }
456 
457 /*
458  * Calculate a prototype superblock based on information in the disk label.
459  * When done the cgsblock macro can be calculated and the fs_ncg field
460  * can be used. Do NOT attempt to use other macros without verifying that
461  * their needed information is available!
462  */
463 static int
464 calcsb(char *dev, int devfd, struct fs *fs)
465 {
466 	struct disklabel *lp;
467 	struct partition *pp;
468 	char *cp;
469 	int i, nspf;
470 
471 	cp = strchr(dev, '\0') - 1;
472 	if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
473 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
474 		return (0);
475 	}
476 	lp = getdisklabel(dev, devfd);
477 	if (isdigit(*cp))
478 		pp = &lp->d_partitions[0];
479 	else
480 		pp = &lp->d_partitions[*cp - 'a'];
481 	if (pp->p_fstype != FS_BSDFFS) {
482 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
483 			dev, pp->p_fstype < FSMAXTYPES ?
484 			fstypenames[pp->p_fstype] : "unknown");
485 		return (0);
486 	}
487 	if (pp->p_fsize == 0 || pp->p_frag == 0 ||
488 	    pp->p_cpg == 0 || pp->p_size == 0) {
489 		pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
490 		    dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
491 		    pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
492 		return (0);
493 	}
494 	memset(fs, 0, sizeof(struct fs));
495 	fs->fs_fsize = pp->p_fsize;
496 	fs->fs_frag = pp->p_frag;
497 	fs->fs_size = pp->p_size;
498 	fs->fs_sblkno = roundup(
499 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
500 		fs->fs_frag);
501 	nspf = fs->fs_fsize / lp->d_secsize;
502 	for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
503 		fs->fs_fsbtodb++;
504 	dev_bsize = lp->d_secsize;
505 	if (fs->fs_magic == FS_UFS2_MAGIC) {
506 		fs->fs_fpg = pp->p_cpg;
507 		fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
508 	} else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
509 		fs->fs_old_cpg = pp->p_cpg;
510 		fs->fs_old_cgmask = 0xffffffff;
511 		for (i = lp->d_ntracks; i > 1; i >>= 1)
512 			fs->fs_old_cgmask <<= 1;
513 		if (!POWEROF2(lp->d_ntracks))
514 			fs->fs_old_cgmask <<= 1;
515 		fs->fs_old_cgoffset = roundup(howmany(lp->d_nsectors, nspf),
516 		    fs->fs_frag);
517 		fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf;
518 		fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl,
519 		    fs->fs_old_cpg);
520 	}
521 	return (1);
522 }
523 
524 static struct disklabel *
525 getdisklabel(char *s, int fd)
526 {
527 	static struct disklabel lab;
528 
529 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
530 		if (s == NULL)
531 			return ((struct disklabel *)NULL);
532 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
533 		errx(EEXIT, "%s: can't read disk label", s);
534 	}
535 	return (&lab);
536 }
537