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