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
36 static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
38 static const char rcsid[] =
43 #include <sys/param.h>
45 #include <sys/disklabel.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
59 #define altsblock (*asblk.b_un.b_fs)
60 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
62 static void badsb __P((int listerr, char *s));
63 static int calcsb __P((char *dev, int devfd, struct fs *fs));
64 static struct disklabel *getdisklabel __P((char *s, int fd));
65 static int readsb __P((int listerr));
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).
76 long cg, size, asked, i, j;
77 long skipclean, bmapsize;
85 skipclean = fflag ? 0 : preen;
86 if (stat(dev, &statb) < 0) {
87 printf("Can't stat %s: %s\n", dev, strerror(errno));
90 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
91 (statb.st_mode & S_IFMT) != S_IFBLK) {
92 pfatal("%s is not a disk device", dev);
93 if (reply("CONTINUE") == 0)
96 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
97 printf("Can't open %s: %s\n", dev, strerror(errno));
101 printf("** %s", dev);
102 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
105 pfatal("NO WRITE ACCESS");
106 printf(" (NO WRITE)");
114 sblk.b_un.b_buf = malloc(SBSIZE);
115 asblk.b_un.b_buf = malloc(SBSIZE);
116 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
117 errx(EEXIT, "cannot allocate space for superblock");
118 if ((lp = getdisklabel(NULL, fsreadfd)))
119 dev_bsize = secsize = lp->d_secsize;
121 dev_bsize = secsize = DEV_BSIZE;
123 * Read in the superblock, looking for alternates if necessary
125 if (readsb(1) == 0) {
127 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
129 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
131 for (cg = 0; cg < proto.fs_ncg; cg++) {
132 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
136 if (cg >= proto.fs_ncg) {
137 printf("%s %s\n%s %s\n%s %s\n",
138 "SEARCH FOR ALTERNATE SUPER-BLOCK",
139 "FAILED. YOU MUST USE THE",
140 "-b OPTION TO FSCK TO SPECIFY THE",
141 "LOCATION OF AN ALTERNATE",
142 "SUPER-BLOCK TO SUPPLY NEEDED",
143 "INFORMATION; SEE fsck(8).");
147 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
150 if (skipclean && sblock.fs_clean) {
151 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
154 maxfsblock = sblock.fs_size;
155 maxino = sblock.fs_ncg * sblock.fs_ipg;
157 * Check and potentially fix certain fields in the super block.
159 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
160 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
161 if (reply("SET TO DEFAULT") == 1) {
162 sblock.fs_optim = FS_OPTTIME;
166 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
167 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
169 if (reply("SET TO DEFAULT") == 1) {
170 sblock.fs_minfree = 10;
174 if (sblock.fs_interleave < 1 ||
175 sblock.fs_interleave > sblock.fs_nsect) {
176 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
177 sblock.fs_interleave);
178 sblock.fs_interleave = 1;
180 printf(" (FIXED)\n");
181 if (preen || reply("SET TO DEFAULT") == 1) {
186 if (sblock.fs_npsect < sblock.fs_nsect ||
187 sblock.fs_npsect > sblock.fs_nsect*2) {
188 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
190 sblock.fs_npsect = sblock.fs_nsect;
192 printf(" (FIXED)\n");
193 if (preen || reply("SET TO DEFAULT") == 1) {
198 if (sblock.fs_inodefmt >= FS_44INODEFMT) {
201 sblock.fs_qbmask = ~sblock.fs_bmask;
202 sblock.fs_qfmask = ~sblock.fs_fmask;
206 * Convert to new inode format.
208 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
210 pwarn("CONVERTING TO NEW INODE FORMAT\n");
211 else if (!reply("CONVERT TO NEW INODE FORMAT"))
214 sblock.fs_inodefmt = FS_44INODEFMT;
215 sizepb = sblock.fs_bsize;
216 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
217 for (i = 0; i < NIADDR; i++) {
218 sizepb *= NINDIR(&sblock);
219 sblock.fs_maxfilesize += sizepb;
221 sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
222 sblock.fs_qbmask = ~sblock.fs_bmask;
223 sblock.fs_qfmask = ~sblock.fs_fmask;
228 * Convert to new cylinder group format.
230 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
232 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
233 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
236 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
238 sblock.fs_postbloff =
239 (char *)(&sblock.fs_opostbl[0][0]) -
240 (char *)(&sblock.fs_firstfield);
241 sblock.fs_rotbloff = &sblock.fs_space[0] -
242 (u_char *)(&sblock.fs_firstfield);
244 fragroundup(&sblock, CGSIZE(&sblock));
248 if (asblk.b_dirty && !bflag) {
249 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
250 flush(fswritefd, &asblk);
253 * read in the summary info.
256 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
257 size = sblock.fs_cssize - i < sblock.fs_bsize ?
258 sblock.fs_cssize - i : sblock.fs_bsize;
259 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
260 if (bread(fsreadfd, (char *)sblock.fs_csp[j],
261 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
262 size) != 0 && !asked) {
263 pfatal("BAD SUMMARY INFORMATION");
264 if (reply("CONTINUE") == 0) {
272 * allocate and initialize the necessary maps
274 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
275 blockmap = calloc((unsigned)bmapsize, sizeof (char));
276 if (blockmap == NULL) {
277 printf("cannot alloc %u bytes for blockmap\n",
281 inostathead = calloc((unsigned)(sblock.fs_ncg),
282 sizeof(struct inostatlist));
283 if (inostathead == NULL) {
284 printf("cannot alloc %u bytes for inostathead\n",
285 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
288 numdirs = sblock.fs_cstotal.cs_ndir;
291 printf("numdirs is zero, try using an alternate superblock\n");
295 listmax = numdirs + 10;
296 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
297 sizeof(struct inoinfo *));
298 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
299 sizeof(struct inoinfo *));
300 if (inpsort == NULL || inphead == NULL) {
301 printf("cannot alloc %u bytes for inphead\n",
302 (unsigned)numdirs * sizeof(struct inoinfo *));
306 if (sblock.fs_flags & FS_DOSOFTDEP)
318 * Read in the super block and its summary info.
324 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
326 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
329 sblk.b_size = SBSIZE;
331 * run a few consistency checks of the super block
333 if (sblock.fs_magic != FS_MAGIC)
334 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
335 if (sblock.fs_ncg < 1)
336 { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
337 if (sblock.fs_cpg < 1)
338 { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
339 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
340 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
341 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
342 if (sblock.fs_sbsize > SBSIZE)
343 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
345 * Compute block size that the filesystem is based on,
346 * according to fsbtodb, and adjust superblock block number
347 * so we can tell if this is an alternate later.
350 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
351 sblk.b_bno = super / dev_bsize;
357 * Set all possible fields that could differ, then do check
358 * of whole super block against an alternate super block.
359 * When an alternate super-block is specified this check is skipped.
361 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
364 altsblock.fs_firstfield = sblock.fs_firstfield;
365 altsblock.fs_unused_1 = sblock.fs_unused_1;
366 altsblock.fs_time = sblock.fs_time;
367 altsblock.fs_cstotal = sblock.fs_cstotal;
368 altsblock.fs_cgrotor = sblock.fs_cgrotor;
369 altsblock.fs_fmod = sblock.fs_fmod;
370 altsblock.fs_clean = sblock.fs_clean;
371 altsblock.fs_ronly = sblock.fs_ronly;
372 altsblock.fs_flags = sblock.fs_flags;
373 altsblock.fs_maxcontig = sblock.fs_maxcontig;
374 altsblock.fs_minfree = sblock.fs_minfree;
375 altsblock.fs_optim = sblock.fs_optim;
376 altsblock.fs_rotdelay = sblock.fs_rotdelay;
377 altsblock.fs_maxbpg = sblock.fs_maxbpg;
378 memmove(altsblock.fs_csp, sblock.fs_csp, sizeof sblock.fs_csp);
379 altsblock.fs_maxcluster = sblock.fs_maxcluster;
380 memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt);
381 memmove(altsblock.fs_sparecon,
382 sblock.fs_sparecon, sizeof sblock.fs_sparecon);
384 * The following should not have to be copied.
386 altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
387 altsblock.fs_interleave = sblock.fs_interleave;
388 altsblock.fs_npsect = sblock.fs_npsect;
389 altsblock.fs_nrpos = sblock.fs_nrpos;
390 altsblock.fs_state = sblock.fs_state;
391 altsblock.fs_qbmask = sblock.fs_qbmask;
392 altsblock.fs_qfmask = sblock.fs_qfmask;
393 altsblock.fs_state = sblock.fs_state;
394 altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
395 if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) {
397 long *nlp, *olp, *endlp;
399 printf("superblock mismatches\n");
400 nlp = (long *)&altsblock;
401 olp = (long *)&sblock;
402 endlp = olp + (sblock.fs_sbsize / sizeof *olp);
403 for ( ; olp < endlp; olp++, nlp++) {
407 "offset %d, original %ld, alternate %ld\n",
408 olp - (long *)&sblock, *olp, *nlp);
412 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
428 printf("%s: ", cdevname);
429 pfatal("BAD SUPER BLOCK: %s\n", s);
433 * Calculate a prototype superblock based on information in the disk label.
434 * When done the cgsblock macro can be calculated and the fs_ncg field
435 * can be used. Do NOT attempt to use other macros without verifying that
436 * their needed information is available!
439 calcsb(dev, devfd, fs)
442 register struct fs *fs;
444 register struct disklabel *lp;
445 register struct partition *pp;
449 cp = strchr(dev, '\0') - 1;
450 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
451 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
454 lp = getdisklabel(dev, devfd);
456 pp = &lp->d_partitions[0];
458 pp = &lp->d_partitions[*cp - 'a'];
459 if (pp->p_fstype != FS_BSDFFS) {
460 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
461 dev, pp->p_fstype < FSMAXTYPES ?
462 fstypenames[pp->p_fstype] : "unknown");
465 if (pp->p_fsize == 0 || pp->p_frag == 0 ||
466 pp->p_cpg == 0 || pp->p_size == 0) {
467 pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
468 dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
469 pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
472 memset(fs, 0, sizeof(struct fs));
473 fs->fs_fsize = pp->p_fsize;
474 fs->fs_frag = pp->p_frag;
475 fs->fs_cpg = pp->p_cpg;
476 fs->fs_size = pp->p_size;
477 fs->fs_ntrak = lp->d_ntracks;
478 fs->fs_nsect = lp->d_nsectors;
479 fs->fs_spc = lp->d_secpercyl;
480 fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
481 fs->fs_sblkno = roundup(
482 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
484 fs->fs_cgmask = 0xffffffff;
485 for (i = fs->fs_ntrak; i > 1; i >>= 1)
487 if (!POWEROF2(fs->fs_ntrak))
489 fs->fs_cgoffset = roundup(
490 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
491 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
492 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
493 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
495 dev_bsize = lp->d_secsize;
499 static struct disklabel *
504 static struct disklabel lab;
506 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
508 return ((struct disklabel *)NULL);
509 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
510 errx(EEXIT, "%s: can't read disk label", s);