setup.c revision 134011
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
32static 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: head/sbin/fsck_ffs/setup.c 134011 2004-08-19 11:09:13Z jhb $");
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
57struct bufarea asblk;
58#define altsblock (*asblk.b_un.b_fs)
59#define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
60
61static void badsb(int listerr, const char *s);
62static int calcsb(char *dev, int devfd, struct fs *fs);
63static 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 (preen mode only).
69 */
70int
71setup(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		cmd.version = FFS_CMD_VERSION;
144		cmd.handle = fsreadfd;
145		fswritefd = -1;
146	}
147	if (preen == 0)
148		printf("** %s", dev);
149	if (bkgrdflag == 0 &&
150	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
151		fswritefd = -1;
152		if (preen)
153			pfatal("NO WRITE ACCESS");
154		printf(" (NO WRITE)");
155	}
156	if (preen == 0)
157		printf("\n");
158	/*
159	 * Read in the superblock, looking for alternates if necessary
160	 */
161	if (readsb(1) == 0) {
162		skipclean = 0;
163		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
164			return(0);
165		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
166			return (0);
167		for (cg = 0; cg < proto.fs_ncg; cg++) {
168			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
169			if (readsb(0) != 0)
170				break;
171		}
172		if (cg >= proto.fs_ncg) {
173			printf("%s %s\n%s %s\n%s %s\n",
174				"SEARCH FOR ALTERNATE SUPER-BLOCK",
175				"FAILED. YOU MUST USE THE",
176				"-b OPTION TO FSCK TO SPECIFY THE",
177				"LOCATION OF AN ALTERNATE",
178				"SUPER-BLOCK TO SUPPLY NEEDED",
179				"INFORMATION; SEE fsck(8).");
180			bflag = 0;
181			return(0);
182		}
183		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
184		bflag = 0;
185	}
186	if (skipclean && preen && sblock.fs_clean) {
187		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
188		return (-1);
189	}
190	maxfsblock = sblock.fs_size;
191	maxino = sblock.fs_ncg * sblock.fs_ipg;
192	/*
193	 * Check and potentially fix certain fields in the super block.
194	 */
195	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
196		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
197		if (reply("SET TO DEFAULT") == 1) {
198			sblock.fs_optim = FS_OPTTIME;
199			sbdirty();
200		}
201	}
202	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
203		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
204			sblock.fs_minfree);
205		if (reply("SET TO DEFAULT") == 1) {
206			sblock.fs_minfree = 10;
207			sbdirty();
208		}
209	}
210	if (sblock.fs_magic == FS_UFS1_MAGIC &&
211	    sblock.fs_old_inodefmt < FS_44INODEFMT) {
212		pwarn("Format of file system is too old.\n");
213		pwarn("Must update to modern format using a version of fsck\n");
214		pfatal("from before 2002 with the command ``fsck -c 2''\n");
215		exit(EEXIT);
216	}
217	if (asblk.b_dirty && !bflag) {
218		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
219		flush(fswritefd, &asblk);
220	}
221	/*
222	 * read in the summary info.
223	 */
224	asked = 0;
225	sblock.fs_csp = calloc(1, sblock.fs_cssize);
226	if (sblock.fs_csp == NULL) {
227		printf("cannot alloc %u bytes for cg summary info\n",
228		    (unsigned)sblock.fs_cssize);
229		goto badsb;
230	}
231	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
232		size = sblock.fs_cssize - i < sblock.fs_bsize ?
233		    sblock.fs_cssize - i : sblock.fs_bsize;
234		if (bread(fsreadfd, (char *)sblock.fs_csp + i,
235		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
236		    size) != 0 && !asked) {
237			pfatal("BAD SUMMARY INFORMATION");
238			if (reply("CONTINUE") == 0) {
239				ckfini(0);
240				exit(EEXIT);
241			}
242			asked++;
243		}
244	}
245	/*
246	 * allocate and initialize the necessary maps
247	 */
248	bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
249	blockmap = calloc((unsigned)bmapsize, sizeof (char));
250	if (blockmap == NULL) {
251		printf("cannot alloc %u bytes for blockmap\n",
252		    (unsigned)bmapsize);
253		goto badsb;
254	}
255	inostathead = calloc((unsigned)(sblock.fs_ncg),
256	    sizeof(struct inostatlist));
257	if (inostathead == NULL) {
258		printf("cannot alloc %u bytes for inostathead\n",
259		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
260		goto badsb;
261	}
262	numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
263	dirhash = numdirs;
264	inplast = 0;
265	listmax = numdirs + 10;
266	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
267	    sizeof(struct inoinfo *));
268	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
269	    sizeof(struct inoinfo *));
270	if (inpsort == NULL || inphead == NULL) {
271		printf("cannot alloc %ju bytes for inphead\n",
272		    (uintmax_t)numdirs * sizeof(struct inoinfo *));
273		goto badsb;
274	}
275	bufinit();
276	if (sblock.fs_flags & FS_DOSOFTDEP)
277		usedsoftdep = 1;
278	else
279		usedsoftdep = 0;
280	return (1);
281
282badsb:
283	ckfini(0);
284	return (0);
285}
286
287/*
288 * Possible superblock locations ordered from most to least likely.
289 */
290static int sblock_try[] = SBLOCKSEARCH;
291
292#define BAD_MAGIC_MSG \
293"The previous newfs operation on this volume did not complete.\n" \
294"You must complete newfs before mounting this volume.\n"
295
296/*
297 * Read in the super block and its summary info.
298 */
299int
300readsb(int listerr)
301{
302	ufs2_daddr_t super;
303	int i;
304
305	if (bflag) {
306		super = bflag;
307		if ((bread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE)))
308			return (0);
309		if (sblock.fs_magic == FS_BAD_MAGIC) {
310			fprintf(stderr, BAD_MAGIC_MSG);
311			exit(11);
312		}
313		if (sblock.fs_magic != FS_UFS1_MAGIC &&
314		    sblock.fs_magic != FS_UFS2_MAGIC) {
315			fprintf(stderr, "%d is not a file system superblock\n",
316			    bflag);
317			return (0);
318		}
319	} else {
320		for (i = 0; sblock_try[i] != -1; i++) {
321			super = sblock_try[i] / dev_bsize;
322			if ((bread(fsreadfd, (char *)&sblock, super,
323			    (long)SBLOCKSIZE)))
324				return (0);
325			if (sblock.fs_magic == FS_BAD_MAGIC) {
326				fprintf(stderr, BAD_MAGIC_MSG);
327				exit(11);
328			}
329			if ((sblock.fs_magic == FS_UFS1_MAGIC ||
330			     (sblock.fs_magic == FS_UFS2_MAGIC &&
331			      sblock.fs_sblockloc == sblock_try[i])) &&
332			    sblock.fs_ncg >= 1 &&
333			    sblock.fs_bsize >= MINBSIZE &&
334			    sblock.fs_bsize >= sizeof(struct fs))
335				break;
336		}
337		if (sblock_try[i] == -1) {
338			fprintf(stderr, "Cannot find file system superblock\n");
339			return (0);
340		}
341	}
342	/*
343	 * Compute block size that the file system is based on,
344	 * according to fsbtodb, and adjust superblock block number
345	 * so we can tell if this is an alternate later.
346	 */
347	super *= dev_bsize;
348	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
349	sblk.b_bno = super / dev_bsize;
350	sblk.b_size = SBLOCKSIZE;
351	if (bflag)
352		goto out;
353	/*
354	 * Compare all fields that should not differ in alternate super block.
355	 * When an alternate super-block is specified this check is skipped.
356	 */
357	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
358	if (asblk.b_errs)
359		return (0);
360	if (altsblock.fs_sblkno != sblock.fs_sblkno ||
361	    altsblock.fs_cblkno != sblock.fs_cblkno ||
362	    altsblock.fs_iblkno != sblock.fs_iblkno ||
363	    altsblock.fs_dblkno != sblock.fs_dblkno ||
364	    altsblock.fs_ncg != sblock.fs_ncg ||
365	    altsblock.fs_bsize != sblock.fs_bsize ||
366	    altsblock.fs_fsize != sblock.fs_fsize ||
367	    altsblock.fs_frag != sblock.fs_frag ||
368	    altsblock.fs_bmask != sblock.fs_bmask ||
369	    altsblock.fs_fmask != sblock.fs_fmask ||
370	    altsblock.fs_bshift != sblock.fs_bshift ||
371	    altsblock.fs_fshift != sblock.fs_fshift ||
372	    altsblock.fs_fragshift != sblock.fs_fragshift ||
373	    altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
374	    altsblock.fs_sbsize != sblock.fs_sbsize ||
375	    altsblock.fs_nindir != sblock.fs_nindir ||
376	    altsblock.fs_inopb != sblock.fs_inopb ||
377	    altsblock.fs_cssize != sblock.fs_cssize ||
378	    altsblock.fs_ipg != sblock.fs_ipg ||
379	    altsblock.fs_fpg != sblock.fs_fpg ||
380	    altsblock.fs_magic != sblock.fs_magic) {
381		badsb(listerr,
382		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
383		return (0);
384	}
385out:
386	/*
387	 * If not yet done, update UFS1 superblock with new wider fields.
388	 */
389	if (sblock.fs_magic == FS_UFS1_MAGIC &&
390	    sblock.fs_maxbsize != sblock.fs_bsize) {
391		sblock.fs_maxbsize = sblock.fs_bsize;
392		sblock.fs_time = sblock.fs_old_time;
393		sblock.fs_size = sblock.fs_old_size;
394		sblock.fs_dsize = sblock.fs_old_dsize;
395		sblock.fs_csaddr = sblock.fs_old_csaddr;
396		sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
397		sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
398		sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
399		sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
400	}
401	havesb = 1;
402	return (1);
403}
404
405static void
406badsb(int listerr, const char *s)
407{
408
409	if (!listerr)
410		return;
411	if (preen)
412		printf("%s: ", cdevname);
413	pfatal("BAD SUPER BLOCK: %s\n", s);
414}
415
416void
417sblock_init(void)
418{
419	struct disklabel *lp;
420
421	fswritefd = -1;
422	fsmodified = 0;
423	lfdir = 0;
424	initbarea(&sblk);
425	initbarea(&asblk);
426	sblk.b_un.b_buf = malloc(SBLOCKSIZE);
427	asblk.b_un.b_buf = malloc(SBLOCKSIZE);
428	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
429		errx(EEXIT, "cannot allocate space for superblock");
430	if ((lp = getdisklabel(NULL, fsreadfd)))
431		dev_bsize = secsize = lp->d_secsize;
432	else
433		dev_bsize = secsize = DEV_BSIZE;
434}
435
436/*
437 * Calculate a prototype superblock based on information in the disk label.
438 * When done the cgsblock macro can be calculated and the fs_ncg field
439 * can be used. Do NOT attempt to use other macros without verifying that
440 * their needed information is available!
441 */
442static int
443calcsb(char *dev, int devfd, struct fs *fs)
444{
445	struct disklabel *lp;
446	struct partition *pp;
447	char *cp;
448	int i, nspf;
449
450	cp = strchr(dev, '\0') - 1;
451	if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
452		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
453		return (0);
454	}
455	lp = getdisklabel(dev, devfd);
456	if (isdigit(*cp))
457		pp = &lp->d_partitions[0];
458	else
459		pp = &lp->d_partitions[*cp - 'a'];
460	if (pp->p_fstype != FS_BSDFFS) {
461		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
462			dev, pp->p_fstype < FSMAXTYPES ?
463			fstypenames[pp->p_fstype] : "unknown");
464		return (0);
465	}
466	if (pp->p_fsize == 0 || pp->p_frag == 0 ||
467	    pp->p_cpg == 0 || pp->p_size == 0) {
468		pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
469		    dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
470		    pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
471		return (0);
472	}
473	memset(fs, 0, sizeof(struct fs));
474	fs->fs_fsize = pp->p_fsize;
475	fs->fs_frag = pp->p_frag;
476	fs->fs_size = pp->p_size;
477	fs->fs_sblkno = roundup(
478		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
479		fs->fs_frag);
480	nspf = fs->fs_fsize / lp->d_secsize;
481	for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
482		fs->fs_fsbtodb++;
483	dev_bsize = lp->d_secsize;
484	if (fs->fs_magic == FS_UFS2_MAGIC) {
485		fs->fs_fpg = pp->p_cpg;
486		fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
487	} else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
488		fs->fs_old_cpg = pp->p_cpg;
489		fs->fs_old_cgmask = 0xffffffff;
490		for (i = lp->d_ntracks; i > 1; i >>= 1)
491			fs->fs_old_cgmask <<= 1;
492		if (!POWEROF2(lp->d_ntracks))
493			fs->fs_old_cgmask <<= 1;
494		fs->fs_old_cgoffset = roundup(howmany(lp->d_nsectors, nspf),
495		    fs->fs_frag);
496		fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf;
497		fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl,
498		    fs->fs_old_cpg);
499	}
500	return (1);
501}
502
503static struct disklabel *
504getdisklabel(char *s, int fd)
505{
506	static struct disklabel lab;
507
508	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
509		if (s == NULL)
510			return ((struct disklabel *)NULL);
511		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
512		errx(EEXIT, "%s: can't read disk label", s);
513	}
514	return (&lab);
515}
516