Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sbin / fsck / setup.c
CommitLineData
984263bc
MD
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.
1de703da
MD
32 *
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 $
b13267a5 35 * $DragonFly: src/sbin/fsck/setup.c,v 1.7 2006/09/10 01:26:27 dillon Exp $
984263bc
MD
36 */
37
984263bc
MD
38#define DKTYPENAMES
39#include <sys/param.h>
40#include <sys/stat.h>
41#include <sys/disklabel.h>
42#include <sys/file.h>
43
38a690d7
MD
44#include <vfs/ufs/dinode.h>
45#include <vfs/ufs/fs.h>
984263bc
MD
46
47#include <ctype.h>
48#include <err.h>
49#include <errno.h>
50#include <string.h>
51
52#include "fsck.h"
53
54struct bufarea asblk;
55#define altsblock (*asblk.b_un.b_fs)
56#define POWEROF2(num) (((num) & ((num) - 1)) == 0)
57
9a76cd75
DR
58static void badsb(int listerr, char *s);
59static int calcsb(char *dev, int devfd, struct fs *fs);
60static struct disklabel *getdisklabel(char *s, int fd);
61static int readsb(int listerr);
984263bc
MD
62
63/*
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).
67 */
68int
b5744197 69setup(char *dev)
984263bc
MD
70{
71 long cg, size, asked, i, j;
72 long skipclean, bmapsize;
73 struct disklabel *lp;
74 off_t sizepb;
75 struct stat statb;
76 struct fs proto;
77
78 havesb = 0;
79 fswritefd = -1;
80 skipclean = fflag ? 0 : preen;
81 if (stat(dev, &statb) < 0) {
82 printf("Can't stat %s: %s\n", dev, strerror(errno));
83 return (0);
84 }
85 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
86 (statb.st_mode & S_IFMT) != S_IFBLK) {
87 pfatal("%s is not a disk device", dev);
88 if (reply("CONTINUE") == 0)
89 return (0);
90 }
91 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
92 printf("Can't open %s: %s\n", dev, strerror(errno));
93 return (0);
94 }
95 if (preen == 0)
96 printf("** %s", dev);
97 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
98 fswritefd = -1;
99 if (preen)
100 pfatal("NO WRITE ACCESS");
101 printf(" (NO WRITE)");
102 }
103 if (preen == 0)
104 printf("\n");
105 fsmodified = 0;
106 lfdir = 0;
107 initbarea(&sblk);
108 initbarea(&asblk);
109 sblk.b_un.b_buf = malloc(SBSIZE);
110 asblk.b_un.b_buf = malloc(SBSIZE);
111 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
112 errx(EEXIT, "cannot allocate space for superblock");
113 if ((lp = getdisklabel(NULL, fsreadfd)))
114 dev_bsize = secsize = lp->d_secsize;
115 else
116 dev_bsize = secsize = DEV_BSIZE;
117 /*
118 * Read in the superblock, looking for alternates if necessary
119 */
120 if (readsb(1) == 0) {
121 skipclean = 0;
122 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
123 return(0);
124 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
125 return (0);
126 for (cg = 0; cg < proto.fs_ncg; cg++) {
127 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
128 if (readsb(0) != 0)
129 break;
130 }
131 if (cg >= proto.fs_ncg) {
132 printf("%s %s\n%s %s\n%s %s\n",
133 "SEARCH FOR ALTERNATE SUPER-BLOCK",
134 "FAILED. YOU MUST USE THE",
135 "-b OPTION TO FSCK TO SPECIFY THE",
136 "LOCATION OF AN ALTERNATE",
137 "SUPER-BLOCK TO SUPPLY NEEDED",
138 "INFORMATION; SEE fsck(8).");
139 bflag = 0;
140 return(0);
141 }
142 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
143 bflag = 0;
144 }
145 if (skipclean && sblock.fs_clean) {
146 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
147 return (-1);
148 }
149 maxfsblock = sblock.fs_size;
150 maxino = sblock.fs_ncg * sblock.fs_ipg;
151 /*
152 * Check and potentially fix certain fields in the super block.
153 */
154 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
155 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
156 if (reply("SET TO DEFAULT") == 1) {
157 sblock.fs_optim = FS_OPTTIME;
158 sbdirty();
159 }
160 }
161 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
162 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
163 sblock.fs_minfree);
164 if (reply("SET TO DEFAULT") == 1) {
165 sblock.fs_minfree = 10;
166 sbdirty();
167 }
168 }
169 if (sblock.fs_interleave < 1 ||
170 sblock.fs_interleave > sblock.fs_nsect) {
171 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
172 sblock.fs_interleave);
173 sblock.fs_interleave = 1;
174 if (preen)
175 printf(" (FIXED)\n");
176 if (preen || reply("SET TO DEFAULT") == 1) {
177 sbdirty();
178 dirty(&asblk);
179 }
180 }
181 if (sblock.fs_npsect < sblock.fs_nsect ||
182 sblock.fs_npsect > sblock.fs_nsect*2) {
183 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
184 sblock.fs_npsect);
185 sblock.fs_npsect = sblock.fs_nsect;
186 if (preen)
187 printf(" (FIXED)\n");
188 if (preen || reply("SET TO DEFAULT") == 1) {
189 sbdirty();
190 dirty(&asblk);
191 }
192 }
193 if (sblock.fs_inodefmt >= FS_44INODEFMT) {
194 newinofmt = 1;
195 } else {
196 sblock.fs_qbmask = ~sblock.fs_bmask;
197 sblock.fs_qfmask = ~sblock.fs_fmask;
198 /* This should match the kernel limit in ffs_oldfscompat(). */
199 sblock.fs_maxfilesize = (u_int64_t)1 << 39;
200 newinofmt = 0;
201 }
202 /*
203 * Convert to new inode format.
204 */
205 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
206 if (preen)
207 pwarn("CONVERTING TO NEW INODE FORMAT\n");
208 else if (!reply("CONVERT TO NEW INODE FORMAT"))
209 return(0);
210 doinglevel2++;
211 sblock.fs_inodefmt = FS_44INODEFMT;
212 sizepb = sblock.fs_bsize;
213 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
214 for (i = 0; i < NIADDR; i++) {
215 sizepb *= NINDIR(&sblock);
216 sblock.fs_maxfilesize += sizepb;
217 }
218 sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
219 sblock.fs_qbmask = ~sblock.fs_bmask;
220 sblock.fs_qfmask = ~sblock.fs_fmask;
221 sbdirty();
222 dirty(&asblk);
223 }
224 /*
225 * Convert to new cylinder group format.
226 */
227 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
228 if (preen)
229 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
230 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
231 return(0);
232 doinglevel1++;
233 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
234 sblock.fs_nrpos = 8;
235 sblock.fs_postbloff =
236 (char *)(&sblock.fs_opostbl[0][0]) -
237 (char *)(&sblock.fs_firstfield);
238 sblock.fs_rotbloff = &sblock.fs_space[0] -
239 (u_char *)(&sblock.fs_firstfield);
240 sblock.fs_cgsize =
241 fragroundup(&sblock, CGSIZE(&sblock));
242 sbdirty();
243 dirty(&asblk);
244 }
245 if (asblk.b_dirty && !bflag) {
246 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
247 flush(fswritefd, &asblk);
248 }
249 /*
250 * read in the summary info.
251 */
252 asked = 0;
253 sblock.fs_csp = calloc(1, sblock.fs_cssize);
254 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
255 size = sblock.fs_cssize - i < sblock.fs_bsize ?
256 sblock.fs_cssize - i : sblock.fs_bsize;
257 if (bread(fsreadfd, (char *)sblock.fs_csp + i,
258 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
259 size) != 0 && !asked) {
260 pfatal("BAD SUMMARY INFORMATION");
261 if (reply("CONTINUE") == 0) {
262 ckfini(0);
263 exit(EEXIT);
264 }
265 asked++;
266 }
267 }
268 /*
269 * allocate and initialize the necessary maps
270 */
271 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
272 blockmap = calloc((unsigned)bmapsize, sizeof (char));
273 if (blockmap == NULL) {
274 printf("cannot alloc %u bytes for blockmap\n",
275 (unsigned)bmapsize);
276 goto badsb;
277 }
278 inostathead = calloc((unsigned)(sblock.fs_ncg),
279 sizeof(struct inostatlist));
280 if (inostathead == NULL) {
281 printf("cannot alloc %u bytes for inostathead\n",
282 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
283 goto badsb;
284 }
285 numdirs = sblock.fs_cstotal.cs_ndir;
b13267a5
MD
286
287 /*
288 * Calculate the directory hash table size. Do not allocate
289 * a ridiculous amount of memory if we have a lot of directories.
290 */
291 for (dirhash = 16; dirhash < numdirs; dirhash <<= 1)
292 ;
293 if (dirhash > 1024*1024)
294 dirhash /= 8;
295 dirhashmask = dirhash - 1;
296
984263bc
MD
297 if (numdirs == 0) {
298 printf("numdirs is zero, try using an alternate superblock\n");
299 goto badsb;
300 }
301 inplast = 0;
302 listmax = numdirs + 10;
b13267a5
MD
303 inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *));
304 inphead = calloc((unsigned)dirhash, sizeof(struct inoinfo *));
984263bc 305 if (inpsort == NULL || inphead == NULL) {
b13267a5
MD
306 printf("cannot allocate base structures for %ld directories\n",
307 numdirs);
984263bc
MD
308 goto badsb;
309 }
310 bufinit();
311 if (sblock.fs_flags & FS_DOSOFTDEP)
312 usedsoftdep = 1;
313 else
314 usedsoftdep = 0;
315 return (1);
316
317badsb:
318 ckfini(0);
319 return (0);
320}
321
322/*
323 * Read in the super block and its summary info.
324 */
325static int
b5744197 326readsb(int listerr)
984263bc
MD
327{
328 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
329
330 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
331 return (0);
332 sblk.b_bno = super;
333 sblk.b_size = SBSIZE;
334 /*
335 * run a few consistency checks of the super block
336 */
337 if (sblock.fs_magic != FS_MAGIC)
338 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
339 if (sblock.fs_ncg < 1)
340 { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
341 if (sblock.fs_cpg < 1)
342 { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
343 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
344 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
345 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
346 if (sblock.fs_sbsize > SBSIZE)
347 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
348 /*
349 * Compute block size that the filesystem is based on,
350 * according to fsbtodb, and adjust superblock block number
351 * so we can tell if this is an alternate later.
352 */
353 super *= dev_bsize;
354 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
355 sblk.b_bno = super / dev_bsize;
356 if (bflag) {
357 havesb = 1;
358 return (1);
359 }
360 /*
361 * Compare all fields that should not differ in alternate super block.
362 * When an alternate super-block is specified this check is skipped.
363 */
364 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
365 if (asblk.b_errs)
366 return (0);
367 if (altsblock.fs_sblkno != sblock.fs_sblkno ||
368 altsblock.fs_cblkno != sblock.fs_cblkno ||
369 altsblock.fs_iblkno != sblock.fs_iblkno ||
370 altsblock.fs_dblkno != sblock.fs_dblkno ||
371 altsblock.fs_cgoffset != sblock.fs_cgoffset ||
372 altsblock.fs_cgmask != sblock.fs_cgmask ||
373 altsblock.fs_ncg != sblock.fs_ncg ||
374 altsblock.fs_bsize != sblock.fs_bsize ||
375 altsblock.fs_fsize != sblock.fs_fsize ||
376 altsblock.fs_frag != sblock.fs_frag ||
377 altsblock.fs_bmask != sblock.fs_bmask ||
378 altsblock.fs_fmask != sblock.fs_fmask ||
379 altsblock.fs_bshift != sblock.fs_bshift ||
380 altsblock.fs_fshift != sblock.fs_fshift ||
381 altsblock.fs_fragshift != sblock.fs_fragshift ||
382 altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
383 altsblock.fs_sbsize != sblock.fs_sbsize ||
384 altsblock.fs_nindir != sblock.fs_nindir ||
385 altsblock.fs_inopb != sblock.fs_inopb ||
386 altsblock.fs_cssize != sblock.fs_cssize ||
387 altsblock.fs_cpg != sblock.fs_cpg ||
388 altsblock.fs_ipg != sblock.fs_ipg ||
389 altsblock.fs_fpg != sblock.fs_fpg ||
390 altsblock.fs_magic != sblock.fs_magic) {
391 badsb(listerr,
392 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
393 return (0);
394 }
395 havesb = 1;
396 return (1);
397}
398
399static void
b5744197 400badsb(int listerr, char *s)
984263bc
MD
401{
402
403 if (!listerr)
404 return;
405 if (preen)
406 printf("%s: ", cdevname);
407 pfatal("BAD SUPER BLOCK: %s\n", s);
408}
409
410/*
411 * Calculate a prototype superblock based on information in the disk label.
412 * When done the cgsblock macro can be calculated and the fs_ncg field
413 * can be used. Do NOT attempt to use other macros without verifying that
414 * their needed information is available!
415 */
416static int
feb65525 417calcsb(char *dev, int devfd, struct fs *fs)
984263bc 418{
feb65525
SW
419 struct disklabel *lp;
420 struct partition *pp;
421 char *cp;
984263bc
MD
422 int i;
423
424 cp = strchr(dev, '\0') - 1;
425 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
426 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
427 return (0);
428 }
429 lp = getdisklabel(dev, devfd);
430 if (isdigit(*cp))
431 pp = &lp->d_partitions[0];
432 else
433 pp = &lp->d_partitions[*cp - 'a'];
434 if (pp->p_fstype != FS_BSDFFS) {
435 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
436 dev, pp->p_fstype < FSMAXTYPES ?
437 fstypenames[pp->p_fstype] : "unknown");
438 return (0);
439 }
440 if (pp->p_fsize == 0 || pp->p_frag == 0 ||
441 pp->p_cpg == 0 || pp->p_size == 0) {
442 pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
443 dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
444 pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
445 return (0);
446 }
447 memset(fs, 0, sizeof(struct fs));
448 fs->fs_fsize = pp->p_fsize;
449 fs->fs_frag = pp->p_frag;
450 fs->fs_cpg = pp->p_cpg;
451 fs->fs_size = pp->p_size;
452 fs->fs_ntrak = lp->d_ntracks;
453 fs->fs_nsect = lp->d_nsectors;
454 fs->fs_spc = lp->d_secpercyl;
455 fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
456 fs->fs_sblkno = roundup(
457 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
458 fs->fs_frag);
459 fs->fs_cgmask = 0xffffffff;
460 for (i = fs->fs_ntrak; i > 1; i >>= 1)
461 fs->fs_cgmask <<= 1;
462 if (!POWEROF2(fs->fs_ntrak))
463 fs->fs_cgmask <<= 1;
464 fs->fs_cgoffset = roundup(
465 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
466 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
467 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
468 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
469 fs->fs_fsbtodb++;
470 dev_bsize = lp->d_secsize;
471 return (1);
472}
473
474static struct disklabel *
b5744197 475getdisklabel(char *s, int fd)
984263bc
MD
476{
477 static struct disklabel lab;
478
479 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
480 if (s == NULL)
481 return ((struct disklabel *)NULL);
482 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
483 errx(EEXIT, "%s: can't read disk label", s);
484 }
485 return (&lab);
486}