nrelease - fix/improve livecd
[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.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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.
1de703da
MD
28 *
29 * @(#)setup.c 8.10 (Berkeley) 5/9/95
30 * $FreeBSD: src/sbin/fsck/setup.c,v 1.17.2.4 2002/06/24 05:10:41 dillon Exp $
984263bc
MD
31 */
32
984263bc
MD
33#define DKTYPENAMES
34#include <sys/param.h>
35#include <sys/stat.h>
6d859f39 36#include <sys/diskslice.h>
984263bc 37
38a690d7
MD
38#include <vfs/ufs/dinode.h>
39#include <vfs/ufs/fs.h>
984263bc
MD
40
41#include <ctype.h>
42#include <err.h>
43#include <errno.h>
2c3b1d1b 44#include <fcntl.h>
984263bc
MD
45#include <string.h>
46
47#include "fsck.h"
48
7319acd8
KP
49struct bufarea sblk; /* file system superblock */
50struct inoinfo **inphead, **inpsort; /* Inode cache data structures. */
51long numdirs, dirhash, listmax, inplast, dirhashmask;
52long dev_bsize; /* computed value of DEV_BSIZE */
53long secsize; /* actual disk sector size */
54char fflag; /* force check, ignore clean flag */
55int bflag; /* location of alternate super block */
56int cvtlevel; /* convert to newer file system format */
57int doinglevel1; /* converting to new cylinder group format */
58int doinglevel2; /* converting to new inode format */
59char usedsoftdep; /* just fix soft dependency inconsistencies */
60char havesb; /* superblock has been read */
61int fsreadfd; /* file descriptor for reading file system */
62int fswritefd; /* file descriptor for writing file system */
63ufs_daddr_t maxfsblock; /* number of blocks in the file system */
64char *blockmap; /* ptr to primary blk allocation map */
65ufs1_ino_t maxino; /* number of inodes in file system */
66
67
984263bc
MD
68struct bufarea asblk;
69#define altsblock (*asblk.b_un.b_fs)
984263bc 70
9a76cd75 71static void badsb(int listerr, char *s);
9a76cd75 72static int readsb(int listerr);
984263bc
MD
73
74/*
75 * Read in a superblock finding an alternate if necessary.
76 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
77 * is already clean (preen mode only).
78 */
79int
b5744197 80setup(char *dev)
984263bc 81{
a3e9bf8f 82 long size, asked, i, j;
984263bc 83 long skipclean, bmapsize;
984263bc
MD
84 off_t sizepb;
85 struct stat statb;
984263bc
MD
86
87 havesb = 0;
88 fswritefd = -1;
89 skipclean = fflag ? 0 : preen;
90 if (stat(dev, &statb) < 0) {
91 printf("Can't stat %s: %s\n", dev, strerror(errno));
92 return (0);
93 }
94 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
95 (statb.st_mode & S_IFMT) != S_IFBLK) {
96 pfatal("%s is not a disk device", dev);
97 if (reply("CONTINUE") == 0)
98 return (0);
99 }
100 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
101 printf("Can't open %s: %s\n", dev, strerror(errno));
102 return (0);
103 }
104 if (preen == 0)
105 printf("** %s", dev);
106 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
107 fswritefd = -1;
108 if (preen)
109 pfatal("NO WRITE ACCESS");
110 printf(" (NO WRITE)");
111 }
112 if (preen == 0)
113 printf("\n");
114 fsmodified = 0;
115 lfdir = 0;
116 initbarea(&sblk);
117 initbarea(&asblk);
118 sblk.b_un.b_buf = malloc(SBSIZE);
119 asblk.b_un.b_buf = malloc(SBSIZE);
120 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
121 errx(EEXIT, "cannot allocate space for superblock");
6d859f39
MD
122
123 /*
124 * Figure out the device block size and the sector size. The
125 * block size is updated by readsb() later on.
126 */
127 {
128 struct partinfo pinfo;
129
130 if (ioctl(fsreadfd, DIOCGPART, &pinfo) == 0) {
131 dev_bsize = secsize = pinfo.media_blksize;
132 } else {
133 dev_bsize = secsize = DEV_BSIZE;
134 }
135 }
136
984263bc
MD
137 /*
138 * Read in the superblock, looking for alternates if necessary
139 */
140 if (readsb(1) == 0) {
141 skipclean = 0;
6d859f39 142 if (bflag || preen)
984263bc
MD
143 return(0);
144 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
145 return (0);
6d859f39
MD
146 bflag = 32;
147 if (readsb(0) == 0) {
148 printf(
149 "YOU MUST USE THE -b OPTION TO FSCK TO SPECIFY\n"
150 "THE LOCATION OF AN ALTERNATE SUPER-BLOCK TO\n"
151 "SUPPLY NEEDED INFORMATION; SEE fsck(8).");
984263bc
MD
152 bflag = 0;
153 return(0);
154 }
155 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
156 bflag = 0;
157 }
158 if (skipclean && sblock.fs_clean) {
159 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
160 return (-1);
161 }
162 maxfsblock = sblock.fs_size;
163 maxino = sblock.fs_ncg * sblock.fs_ipg;
164 /*
165 * Check and potentially fix certain fields in the super block.
166 */
167 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
168 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
169 if (reply("SET TO DEFAULT") == 1) {
170 sblock.fs_optim = FS_OPTTIME;
171 sbdirty();
172 }
173 }
174 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
175 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
176 sblock.fs_minfree);
177 if (reply("SET TO DEFAULT") == 1) {
178 sblock.fs_minfree = 10;
179 sbdirty();
180 }
181 }
182 if (sblock.fs_interleave < 1 ||
183 sblock.fs_interleave > sblock.fs_nsect) {
184 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
185 sblock.fs_interleave);
186 sblock.fs_interleave = 1;
187 if (preen)
188 printf(" (FIXED)\n");
189 if (preen || reply("SET TO DEFAULT") == 1) {
190 sbdirty();
191 dirty(&asblk);
192 }
193 }
194 if (sblock.fs_npsect < sblock.fs_nsect ||
195 sblock.fs_npsect > sblock.fs_nsect*2) {
196 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
197 sblock.fs_npsect);
198 sblock.fs_npsect = sblock.fs_nsect;
199 if (preen)
200 printf(" (FIXED)\n");
201 if (preen || reply("SET TO DEFAULT") == 1) {
202 sbdirty();
203 dirty(&asblk);
204 }
205 }
206 if (sblock.fs_inodefmt >= FS_44INODEFMT) {
207 newinofmt = 1;
208 } else {
209 sblock.fs_qbmask = ~sblock.fs_bmask;
210 sblock.fs_qfmask = ~sblock.fs_fmask;
211 /* This should match the kernel limit in ffs_oldfscompat(). */
212 sblock.fs_maxfilesize = (u_int64_t)1 << 39;
213 newinofmt = 0;
214 }
215 /*
216 * Convert to new inode format.
217 */
218 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
219 if (preen)
220 pwarn("CONVERTING TO NEW INODE FORMAT\n");
221 else if (!reply("CONVERT TO NEW INODE FORMAT"))
222 return(0);
223 doinglevel2++;
224 sblock.fs_inodefmt = FS_44INODEFMT;
225 sizepb = sblock.fs_bsize;
c309c6d4
SW
226 sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1;
227 for (i = 0; i < UFS_NIADDR; i++) {
984263bc
MD
228 sizepb *= NINDIR(&sblock);
229 sblock.fs_maxfilesize += sizepb;
230 }
c309c6d4 231 sblock.fs_maxsymlinklen = UFS1_MAXSYMLINKLEN;
984263bc
MD
232 sblock.fs_qbmask = ~sblock.fs_bmask;
233 sblock.fs_qfmask = ~sblock.fs_fmask;
234 sbdirty();
235 dirty(&asblk);
236 }
237 /*
238 * Convert to new cylinder group format.
239 */
240 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
241 if (preen)
242 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
243 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
244 return(0);
245 doinglevel1++;
246 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
247 sblock.fs_nrpos = 8;
248 sblock.fs_postbloff =
249 (char *)(&sblock.fs_opostbl[0][0]) -
250 (char *)(&sblock.fs_firstfield);
251 sblock.fs_rotbloff = &sblock.fs_space[0] -
252 (u_char *)(&sblock.fs_firstfield);
253 sblock.fs_cgsize =
254 fragroundup(&sblock, CGSIZE(&sblock));
255 sbdirty();
256 dirty(&asblk);
257 }
258 if (asblk.b_dirty && !bflag) {
259 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
260 flush(fswritefd, &asblk);
261 }
262 /*
263 * read in the summary info.
264 */
265 asked = 0;
266 sblock.fs_csp = calloc(1, sblock.fs_cssize);
267 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
268 size = sblock.fs_cssize - i < sblock.fs_bsize ?
269 sblock.fs_cssize - i : sblock.fs_bsize;
270 if (bread(fsreadfd, (char *)sblock.fs_csp + i,
271 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
272 size) != 0 && !asked) {
273 pfatal("BAD SUMMARY INFORMATION");
274 if (reply("CONTINUE") == 0) {
275 ckfini(0);
276 exit(EEXIT);
277 }
278 asked++;
279 }
280 }
281 /*
282 * allocate and initialize the necessary maps
283 */
284 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
285 blockmap = calloc((unsigned)bmapsize, sizeof (char));
286 if (blockmap == NULL) {
287 printf("cannot alloc %u bytes for blockmap\n",
288 (unsigned)bmapsize);
289 goto badsb;
290 }
291 inostathead = calloc((unsigned)(sblock.fs_ncg),
292 sizeof(struct inostatlist));
293 if (inostathead == NULL) {
294 printf("cannot alloc %u bytes for inostathead\n",
295 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
296 goto badsb;
297 }
298 numdirs = sblock.fs_cstotal.cs_ndir;
b13267a5
MD
299
300 /*
301 * Calculate the directory hash table size. Do not allocate
302 * a ridiculous amount of memory if we have a lot of directories.
303 */
304 for (dirhash = 16; dirhash < numdirs; dirhash <<= 1)
305 ;
306 if (dirhash > 1024*1024)
307 dirhash /= 8;
308 dirhashmask = dirhash - 1;
309
984263bc
MD
310 if (numdirs == 0) {
311 printf("numdirs is zero, try using an alternate superblock\n");
312 goto badsb;
313 }
314 inplast = 0;
315 listmax = numdirs + 10;
b13267a5
MD
316 inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *));
317 inphead = calloc((unsigned)dirhash, sizeof(struct inoinfo *));
984263bc 318 if (inpsort == NULL || inphead == NULL) {
b13267a5
MD
319 printf("cannot allocate base structures for %ld directories\n",
320 numdirs);
984263bc
MD
321 goto badsb;
322 }
323 bufinit();
324 if (sblock.fs_flags & FS_DOSOFTDEP)
325 usedsoftdep = 1;
326 else
327 usedsoftdep = 0;
328 return (1);
329
330badsb:
331 ckfini(0);
332 return (0);
333}
334
335/*
336 * Read in the super block and its summary info.
337 */
338static int
b5744197 339readsb(int listerr)
984263bc
MD
340{
341 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
342
343 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
344 return (0);
345 sblk.b_bno = super;
346 sblk.b_size = SBSIZE;
347 /*
348 * run a few consistency checks of the super block
349 */
350 if (sblock.fs_magic != FS_MAGIC)
351 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
352 if (sblock.fs_ncg < 1)
353 { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
354 if (sblock.fs_cpg < 1)
355 { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
356 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
357 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
358 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
359 if (sblock.fs_sbsize > SBSIZE)
360 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
361 /*
362 * Compute block size that the filesystem is based on,
363 * according to fsbtodb, and adjust superblock block number
364 * so we can tell if this is an alternate later.
365 */
366 super *= dev_bsize;
367 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
368 sblk.b_bno = super / dev_bsize;
369 if (bflag) {
370 havesb = 1;
371 return (1);
372 }
373 /*
374 * Compare all fields that should not differ in alternate super block.
375 * When an alternate super-block is specified this check is skipped.
376 */
377 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
378 if (asblk.b_errs)
379 return (0);
380 if (altsblock.fs_sblkno != sblock.fs_sblkno ||
381 altsblock.fs_cblkno != sblock.fs_cblkno ||
382 altsblock.fs_iblkno != sblock.fs_iblkno ||
383 altsblock.fs_dblkno != sblock.fs_dblkno ||
384 altsblock.fs_cgoffset != sblock.fs_cgoffset ||
385 altsblock.fs_cgmask != sblock.fs_cgmask ||
386 altsblock.fs_ncg != sblock.fs_ncg ||
387 altsblock.fs_bsize != sblock.fs_bsize ||
388 altsblock.fs_fsize != sblock.fs_fsize ||
389 altsblock.fs_frag != sblock.fs_frag ||
390 altsblock.fs_bmask != sblock.fs_bmask ||
391 altsblock.fs_fmask != sblock.fs_fmask ||
392 altsblock.fs_bshift != sblock.fs_bshift ||
393 altsblock.fs_fshift != sblock.fs_fshift ||
394 altsblock.fs_fragshift != sblock.fs_fragshift ||
395 altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
396 altsblock.fs_sbsize != sblock.fs_sbsize ||
397 altsblock.fs_nindir != sblock.fs_nindir ||
398 altsblock.fs_inopb != sblock.fs_inopb ||
399 altsblock.fs_cssize != sblock.fs_cssize ||
400 altsblock.fs_cpg != sblock.fs_cpg ||
401 altsblock.fs_ipg != sblock.fs_ipg ||
402 altsblock.fs_fpg != sblock.fs_fpg ||
403 altsblock.fs_magic != sblock.fs_magic) {
404 badsb(listerr,
405 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
406 return (0);
407 }
408 havesb = 1;
409 return (1);
410}
411
412static void
b5744197 413badsb(int listerr, char *s)
984263bc
MD
414{
415
416 if (!listerr)
417 return;
418 if (preen)
419 printf("%s: ", cdevname);
420 pfatal("BAD SUPER BLOCK: %s\n", s);
421}
422