2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)setup.c 8.10 (Berkeley) 5/9/95
34 * $FreeBSD: src/sbin/fsck/setup.c,v 1.17.2.4 2002/06/24 05:10:41 dillon Exp $
35 * $DragonFly: src/sbin/fsck/setup.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
39 #include <sys/param.h>
41 #include <sys/disklabel.h>
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ffs/fs.h>
55 #define altsblock (*asblk.b_un.b_fs)
56 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
58 static void badsb __P((int listerr, char *s));
59 static int calcsb __P((char *dev, int devfd, struct fs *fs));
60 static struct disklabel *getdisklabel __P((char *s, int fd));
61 static int readsb __P((int listerr));
64 * Read in a superblock finding an alternate if necessary.
65 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
66 * is already clean (preen mode only).
72 long cg, size, asked, i, j;
73 long skipclean, bmapsize;
81 skipclean = fflag ? 0 : preen;
82 if (stat(dev, &statb) < 0) {
83 printf("Can't stat %s: %s\n", dev, strerror(errno));
86 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
87 (statb.st_mode & S_IFMT) != S_IFBLK) {
88 pfatal("%s is not a disk device", dev);
89 if (reply("CONTINUE") == 0)
92 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
93 printf("Can't open %s: %s\n", dev, strerror(errno));
98 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
101 pfatal("NO WRITE ACCESS");
102 printf(" (NO WRITE)");
110 sblk.b_un.b_buf = malloc(SBSIZE);
111 asblk.b_un.b_buf = malloc(SBSIZE);
112 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
113 errx(EEXIT, "cannot allocate space for superblock");
114 if ((lp = getdisklabel(NULL, fsreadfd)))
115 dev_bsize = secsize = lp->d_secsize;
117 dev_bsize = secsize = DEV_BSIZE;
119 * Read in the superblock, looking for alternates if necessary
121 if (readsb(1) == 0) {
123 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
125 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
127 for (cg = 0; cg < proto.fs_ncg; cg++) {
128 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
132 if (cg >= proto.fs_ncg) {
133 printf("%s %s\n%s %s\n%s %s\n",
134 "SEARCH FOR ALTERNATE SUPER-BLOCK",
135 "FAILED. YOU MUST USE THE",
136 "-b OPTION TO FSCK TO SPECIFY THE",
137 "LOCATION OF AN ALTERNATE",
138 "SUPER-BLOCK TO SUPPLY NEEDED",
139 "INFORMATION; SEE fsck(8).");
143 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
146 if (skipclean && sblock.fs_clean) {
147 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
150 maxfsblock = sblock.fs_size;
151 maxino = sblock.fs_ncg * sblock.fs_ipg;
153 * Check and potentially fix certain fields in the super block.
155 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
156 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
157 if (reply("SET TO DEFAULT") == 1) {
158 sblock.fs_optim = FS_OPTTIME;
162 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
163 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
165 if (reply("SET TO DEFAULT") == 1) {
166 sblock.fs_minfree = 10;
170 if (sblock.fs_interleave < 1 ||
171 sblock.fs_interleave > sblock.fs_nsect) {
172 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
173 sblock.fs_interleave);
174 sblock.fs_interleave = 1;
176 printf(" (FIXED)\n");
177 if (preen || reply("SET TO DEFAULT") == 1) {
182 if (sblock.fs_npsect < sblock.fs_nsect ||
183 sblock.fs_npsect > sblock.fs_nsect*2) {
184 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
186 sblock.fs_npsect = sblock.fs_nsect;
188 printf(" (FIXED)\n");
189 if (preen || reply("SET TO DEFAULT") == 1) {
194 if (sblock.fs_inodefmt >= FS_44INODEFMT) {
197 sblock.fs_qbmask = ~sblock.fs_bmask;
198 sblock.fs_qfmask = ~sblock.fs_fmask;
199 /* This should match the kernel limit in ffs_oldfscompat(). */
200 sblock.fs_maxfilesize = (u_int64_t)1 << 39;
204 * Convert to new inode format.
206 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
208 pwarn("CONVERTING TO NEW INODE FORMAT\n");
209 else if (!reply("CONVERT TO NEW INODE FORMAT"))
212 sblock.fs_inodefmt = FS_44INODEFMT;
213 sizepb = sblock.fs_bsize;
214 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
215 for (i = 0; i < NIADDR; i++) {
216 sizepb *= NINDIR(&sblock);
217 sblock.fs_maxfilesize += sizepb;
219 sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
220 sblock.fs_qbmask = ~sblock.fs_bmask;
221 sblock.fs_qfmask = ~sblock.fs_fmask;
226 * Convert to new cylinder group format.
228 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
230 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
231 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
234 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
236 sblock.fs_postbloff =
237 (char *)(&sblock.fs_opostbl[0][0]) -
238 (char *)(&sblock.fs_firstfield);
239 sblock.fs_rotbloff = &sblock.fs_space[0] -
240 (u_char *)(&sblock.fs_firstfield);
242 fragroundup(&sblock, CGSIZE(&sblock));
246 if (asblk.b_dirty && !bflag) {
247 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
248 flush(fswritefd, &asblk);
251 * read in the summary info.
254 sblock.fs_csp = calloc(1, sblock.fs_cssize);
255 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
256 size = sblock.fs_cssize - i < sblock.fs_bsize ?
257 sblock.fs_cssize - i : sblock.fs_bsize;
258 if (bread(fsreadfd, (char *)sblock.fs_csp + i,
259 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
260 size) != 0 && !asked) {
261 pfatal("BAD SUMMARY INFORMATION");
262 if (reply("CONTINUE") == 0) {
270 * allocate and initialize the necessary maps
272 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
273 blockmap = calloc((unsigned)bmapsize, sizeof (char));
274 if (blockmap == NULL) {
275 printf("cannot alloc %u bytes for blockmap\n",
279 inostathead = calloc((unsigned)(sblock.fs_ncg),
280 sizeof(struct inostatlist));
281 if (inostathead == NULL) {
282 printf("cannot alloc %u bytes for inostathead\n",
283 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
286 numdirs = sblock.fs_cstotal.cs_ndir;
289 printf("numdirs is zero, try using an alternate superblock\n");
293 listmax = numdirs + 10;
294 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
295 sizeof(struct inoinfo *));
296 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
297 sizeof(struct inoinfo *));
298 if (inpsort == NULL || inphead == NULL) {
299 printf("cannot alloc %u bytes for inphead\n",
300 (unsigned)numdirs * sizeof(struct inoinfo *));
304 if (sblock.fs_flags & FS_DOSOFTDEP)
316 * Read in the super block and its summary info.
322 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
324 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
327 sblk.b_size = SBSIZE;
329 * run a few consistency checks of the super block
331 if (sblock.fs_magic != FS_MAGIC)
332 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
333 if (sblock.fs_ncg < 1)
334 { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
335 if (sblock.fs_cpg < 1)
336 { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
337 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
338 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
339 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
340 if (sblock.fs_sbsize > SBSIZE)
341 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
343 * Compute block size that the filesystem is based on,
344 * according to fsbtodb, and adjust superblock block number
345 * so we can tell if this is an alternate later.
348 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
349 sblk.b_bno = super / dev_bsize;
355 * Compare all fields that should not differ in alternate super block.
356 * When an alternate super-block is specified this check is skipped.
358 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
361 if (altsblock.fs_sblkno != sblock.fs_sblkno ||
362 altsblock.fs_cblkno != sblock.fs_cblkno ||
363 altsblock.fs_iblkno != sblock.fs_iblkno ||
364 altsblock.fs_dblkno != sblock.fs_dblkno ||
365 altsblock.fs_cgoffset != sblock.fs_cgoffset ||
366 altsblock.fs_cgmask != sblock.fs_cgmask ||
367 altsblock.fs_ncg != sblock.fs_ncg ||
368 altsblock.fs_bsize != sblock.fs_bsize ||
369 altsblock.fs_fsize != sblock.fs_fsize ||
370 altsblock.fs_frag != sblock.fs_frag ||
371 altsblock.fs_bmask != sblock.fs_bmask ||
372 altsblock.fs_fmask != sblock.fs_fmask ||
373 altsblock.fs_bshift != sblock.fs_bshift ||
374 altsblock.fs_fshift != sblock.fs_fshift ||
375 altsblock.fs_fragshift != sblock.fs_fragshift ||
376 altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
377 altsblock.fs_sbsize != sblock.fs_sbsize ||
378 altsblock.fs_nindir != sblock.fs_nindir ||
379 altsblock.fs_inopb != sblock.fs_inopb ||
380 altsblock.fs_cssize != sblock.fs_cssize ||
381 altsblock.fs_cpg != sblock.fs_cpg ||
382 altsblock.fs_ipg != sblock.fs_ipg ||
383 altsblock.fs_fpg != sblock.fs_fpg ||
384 altsblock.fs_magic != sblock.fs_magic) {
386 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
402 printf("%s: ", cdevname);
403 pfatal("BAD SUPER BLOCK: %s\n", s);
407 * Calculate a prototype superblock based on information in the disk label.
408 * When done the cgsblock macro can be calculated and the fs_ncg field
409 * can be used. Do NOT attempt to use other macros without verifying that
410 * their needed information is available!
413 calcsb(dev, devfd, fs)
416 register struct fs *fs;
418 register struct disklabel *lp;
419 register struct partition *pp;
423 cp = strchr(dev, '\0') - 1;
424 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
425 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
428 lp = getdisklabel(dev, devfd);
430 pp = &lp->d_partitions[0];
432 pp = &lp->d_partitions[*cp - 'a'];
433 if (pp->p_fstype != FS_BSDFFS) {
434 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
435 dev, pp->p_fstype < FSMAXTYPES ?
436 fstypenames[pp->p_fstype] : "unknown");
439 if (pp->p_fsize == 0 || pp->p_frag == 0 ||
440 pp->p_cpg == 0 || pp->p_size == 0) {
441 pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
442 dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
443 pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
446 memset(fs, 0, sizeof(struct fs));
447 fs->fs_fsize = pp->p_fsize;
448 fs->fs_frag = pp->p_frag;
449 fs->fs_cpg = pp->p_cpg;
450 fs->fs_size = pp->p_size;
451 fs->fs_ntrak = lp->d_ntracks;
452 fs->fs_nsect = lp->d_nsectors;
453 fs->fs_spc = lp->d_secpercyl;
454 fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
455 fs->fs_sblkno = roundup(
456 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
458 fs->fs_cgmask = 0xffffffff;
459 for (i = fs->fs_ntrak; i > 1; i >>= 1)
461 if (!POWEROF2(fs->fs_ntrak))
463 fs->fs_cgoffset = roundup(
464 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
465 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
466 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
467 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
469 dev_bsize = lp->d_secsize;
473 static struct disklabel *
478 static struct disklabel lab;
480 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
482 return ((struct disklabel *)NULL);
483 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
484 errx(EEXIT, "%s: can't read disk label", s);