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