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