setup.c revision 1.82 1 /* $NetBSD: setup.c,v 1.82 2008/02/23 21:41:48 christos Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
36 #else
37 __RCSID("$NetBSD: setup.c,v 1.82 2008/02/23 21:41:48 christos Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/stat.h>
44 #include <sys/ioctl.h>
45 #include <sys/file.h>
46 #include <sys/disk.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ufs/ufs_bswap.h>
51 #include <ufs/ffs/fs.h>
52 #include <ufs/ffs/ffs_extern.h>
53
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60
61 #include "fsck.h"
62 #include "extern.h"
63 #include "fsutil.h"
64 #include "partutil.h"
65 #include "exitvalues.h"
66
67 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
68
69 static void badsb(int, const char *);
70 static int calcsb(const char *, int, struct fs *);
71 static int readsb(int);
72 static int readappleufs(void);
73
74 int16_t sblkpostbl[256];
75
76 /*
77 * Read in a superblock finding an alternate if necessary.
78 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
79 * is already clean (preen mode only).
80 */
81 int
82 setup(const char *dev)
83 {
84 long cg, size, asked, i, j;
85 long bmapsize;
86 struct disk_geom geo;
87 struct dkwedge_info dkw;
88 off_t sizepb;
89 struct stat statb;
90 struct fs proto;
91 int doskipclean;
92 u_int64_t maxfilesize;
93 struct csum *ccsp;
94
95 havesb = 0;
96 fswritefd = -1;
97 doskipclean = skipclean;
98 if (stat(dev, &statb) < 0) {
99 printf("Can't stat %s: %s\n", dev, strerror(errno));
100 return (0);
101 }
102 if (!forceimage && !S_ISCHR(statb.st_mode)) {
103 pfatal("%s is not a character device", dev);
104 if (reply("CONTINUE") == 0)
105 return (0);
106 }
107 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
108 printf("Can't open %s: %s\n", dev, strerror(errno));
109 return (0);
110 }
111 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
112 fswritefd = -1;
113 if (preen)
114 pfatal("NO WRITE ACCESS");
115 printf("** %s (NO WRITE)\n", dev);
116 quiet = 0;
117 } else
118 if (!preen && !quiet)
119 printf("** %s\n", dev);
120 fsmodified = 0;
121 lfdir = 0;
122 initbarea(&sblk);
123 initbarea(&asblk);
124 sblk.b_un.b_buf = malloc(SBLOCKSIZE);
125 sblock = malloc(SBLOCKSIZE);
126 asblk.b_un.b_buf = malloc(SBLOCKSIZE);
127 altsblock = malloc(SBLOCKSIZE);
128 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
129 sblock == NULL || altsblock == NULL)
130 errexit("Cannot allocate space for superblock");
131 if (!forceimage && getdiskinfo(dev, fsreadfd, NULL, &geo, &dkw) != -1)
132 dev_bsize = secsize = geo.dg_secsize;
133 else
134 dev_bsize = secsize = DEV_BSIZE;
135 /*
136 * Read in the superblock, looking for alternates if necessary
137 */
138 if (readsb(1) == 0) {
139 if (bflag || preen || forceimage ||
140 calcsb(dev, fsreadfd, &proto) == 0)
141 return(0);
142 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
143 return (0);
144 for (cg = 0; cg < proto.fs_ncg; cg++) {
145 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
146 if (readsb(0) != 0)
147 break;
148 }
149 if (cg >= proto.fs_ncg) {
150 printf("%s %s\n%s %s\n%s %s\n",
151 "SEARCH FOR ALTERNATE SUPER-BLOCK",
152 "FAILED. YOU MUST USE THE",
153 "-b OPTION TO fsck_ffs TO SPECIFY THE",
154 "LOCATION OF AN ALTERNATE",
155 "SUPER-BLOCK TO SUPPLY NEEDED",
156 "INFORMATION; SEE fsck_ffs(8).");
157 return(0);
158 }
159 doskipclean = 0;
160 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
161 }
162 if (debug)
163 printf("clean = %d\n", sblock->fs_clean);
164 if (doswap)
165 doskipclean = 0;
166 if (sblock->fs_clean & FS_ISCLEAN) {
167 if (doskipclean) {
168 if (!quiet)
169 pwarn("%sile system is clean; not checking\n",
170 preen ? "f" : "** F");
171 return (-1);
172 }
173 if (!preen && !doswap)
174 pwarn("** File system is already clean\n");
175 }
176 maxfsblock = sblock->fs_size;
177 maxino = sblock->fs_ncg * sblock->fs_ipg;
178 sizepb = sblock->fs_bsize;
179 maxfilesize = sblock->fs_bsize * NDADDR - 1;
180 for (i = 0; i < NIADDR; i++) {
181 sizepb *= NINDIR(sblock);
182 maxfilesize += sizepb;
183 }
184 if ((!is_ufs2 && cvtlevel >= 4) &&
185 (sblock->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
186 if (preen)
187 pwarn("CONVERTING TO NEW SUPERBLOCK LAYOUT\n");
188 else if (!reply("CONVERT TO NEW SUPERBLOCK LAYOUT"))
189 return(0);
190 sblock->fs_old_flags |= FS_FLAGS_UPDATED;
191 /* Disable the postbl tables */
192 sblock->fs_old_cpc = 0;
193 sblock->fs_old_nrpos = 1;
194 sblock->fs_old_trackskew = 0;
195 /* The other fields have already been updated by
196 * sb_oldfscompat_read
197 */
198 sbdirty();
199 }
200 if (!is_ufs2 && cvtlevel == 3 &&
201 (sblock->fs_old_flags & FS_FLAGS_UPDATED)) {
202 if (preen)
203 pwarn("DOWNGRADING TO OLD SUPERBLOCK LAYOUT\n");
204 else if (!reply("DOWNGRADE TO OLD SUPERBLOCK LAYOUT"))
205 return(0);
206 sblock->fs_old_flags &= ~FS_FLAGS_UPDATED;
207 sb_oldfscompat_write(sblock, sblock);
208 sblock->fs_old_flags &= ~FS_FLAGS_UPDATED; /* just in case */
209 /* Leave postbl tables disabled, but blank its superblock region anyway */
210 sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
211 sblock->fs_old_cpc = 0;
212 sblock->fs_old_nrpos = 1;
213 sblock->fs_old_trackskew = 0;
214 memset(&sblock->fs_old_postbl_start, 0xff, 256);
215 sb_oldfscompat_read(sblock, &sblocksave);
216 sbdirty();
217 }
218 /*
219 * Check and potentially fix certain fields in the super block.
220 */
221 if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
222 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
223 if (reply("SET TO DEFAULT") == 1) {
224 sblock->fs_optim = FS_OPTTIME;
225 sbdirty();
226 }
227 }
228 if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
229 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
230 sblock->fs_minfree);
231 if (reply("SET TO DEFAULT") == 1) {
232 sblock->fs_minfree = 10;
233 sbdirty();
234 }
235 }
236 if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
237 (sblock->fs_old_interleave < 1 ||
238 sblock->fs_old_interleave > sblock->fs_old_nsect)) {
239 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
240 sblock->fs_old_interleave);
241 sblock->fs_old_interleave = 1;
242 if (preen)
243 printf(" (FIXED)\n");
244 if (preen || reply("SET TO DEFAULT") == 1) {
245 sbdirty();
246 dirty(&asblk);
247 }
248 }
249 if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
250 (sblock->fs_old_npsect < sblock->fs_old_nsect ||
251 sblock->fs_old_npsect > sblock->fs_old_nsect*2)) {
252 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
253 sblock->fs_old_npsect);
254 sblock->fs_old_npsect = sblock->fs_old_nsect;
255 if (preen)
256 printf(" (FIXED)\n");
257 if (preen || reply("SET TO DEFAULT") == 1) {
258 sbdirty();
259 dirty(&asblk);
260 }
261 }
262 if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
263 pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
264 sblock->fs_bmask);
265 sblock->fs_bmask = ~(sblock->fs_bsize - 1);
266 if (preen)
267 printf(" (FIXED)\n");
268 if (preen || reply("FIX") == 1) {
269 sbdirty();
270 dirty(&asblk);
271 }
272 }
273 if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
274 pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
275 sblock->fs_fmask);
276 sblock->fs_fmask = ~(sblock->fs_fsize - 1);
277 if (preen)
278 printf(" (FIXED)\n");
279 if (preen || reply("FIX") == 1) {
280 sbdirty();
281 dirty(&asblk);
282 }
283 }
284 if (is_ufs2 || sblock->fs_old_inodefmt >= FS_44INODEFMT) {
285 if (sblock->fs_maxfilesize != maxfilesize) {
286 pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
287 (unsigned long long)sblock->fs_maxfilesize);
288 sblock->fs_maxfilesize = maxfilesize;
289 if (preen)
290 printf(" (FIXED)\n");
291 if (preen || reply("FIX") == 1) {
292 sbdirty();
293 dirty(&asblk);
294 }
295 }
296 if ((is_ufs2 && sblock->fs_maxsymlinklen != MAXSYMLINKLEN_UFS2)
297 ||
298 (!is_ufs2 && sblock->fs_maxsymlinklen != MAXSYMLINKLEN_UFS1))
299 {
300 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
301 sblock->fs_maxsymlinklen);
302 sblock->fs_maxsymlinklen = is_ufs2 ?
303 MAXSYMLINKLEN_UFS2 : MAXSYMLINKLEN_UFS1;
304 if (preen)
305 printf(" (FIXED)\n");
306 if (preen || reply("FIX") == 1) {
307 sbdirty();
308 dirty(&asblk);
309 }
310 }
311 if (sblock->fs_qbmask != ~sblock->fs_bmask) {
312 pwarn("INCORRECT QBMASK=%#llx IN SUPERBLOCK",
313 (unsigned long long)sblock->fs_qbmask);
314 sblock->fs_qbmask = ~sblock->fs_bmask;
315 if (preen)
316 printf(" (FIXED)\n");
317 if (preen || reply("FIX") == 1) {
318 sbdirty();
319 dirty(&asblk);
320 }
321 }
322 if (sblock->fs_qfmask != ~sblock->fs_fmask) {
323 pwarn("INCORRECT QFMASK=%#llx IN SUPERBLOCK",
324 (unsigned long long)sblock->fs_qfmask);
325 sblock->fs_qfmask = ~sblock->fs_fmask;
326 if (preen)
327 printf(" (FIXED)\n");
328 if (preen || reply("FIX") == 1) {
329 sbdirty();
330 dirty(&asblk);
331 }
332 }
333 newinofmt = 1;
334 } else {
335 sblock->fs_qbmask = ~sblock->fs_bmask;
336 sblock->fs_qfmask = ~sblock->fs_fmask;
337 newinofmt = 0;
338 }
339 /*
340 * Convert to new inode format.
341 */
342 if (!is_ufs2 && cvtlevel >= 2 &&
343 sblock->fs_old_inodefmt < FS_44INODEFMT) {
344 if (preen)
345 pwarn("CONVERTING TO NEW INODE FORMAT\n");
346 else if (!reply("CONVERT TO NEW INODE FORMAT"))
347 return(0);
348 doinglevel2++;
349 sblock->fs_old_inodefmt = FS_44INODEFMT;
350 sblock->fs_maxfilesize = maxfilesize;
351 sblock->fs_maxsymlinklen = MAXSYMLINKLEN_UFS1;
352 sblock->fs_qbmask = ~sblock->fs_bmask;
353 sblock->fs_qfmask = ~sblock->fs_fmask;
354 sbdirty();
355 dirty(&asblk);
356 }
357 /*
358 * Convert to new cylinder group format.
359 */
360 if (!is_ufs2 && cvtlevel >= 1 &&
361 sblock->fs_old_postblformat == FS_42POSTBLFMT) {
362 if (preen)
363 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
364 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
365 return(0);
366 doinglevel1++;
367 sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
368 sblock->fs_old_nrpos = 8;
369 sblock->fs_old_postbloff =
370 (char *)(&sblock->fs_old_postbl_start) -
371 (char *)(&sblock->fs_firstfield);
372 sblock->fs_old_rotbloff =
373 (char *)(&sblock->fs_magic+1) -
374 (char *)(&sblock->fs_firstfield);
375 sblock->fs_cgsize =
376 fragroundup(sblock, CGSIZE(sblock));
377 sbdirty();
378 dirty(&asblk);
379 }
380 if (asblk.b_dirty && !bflag) {
381 memmove(sblk.b_un.b_fs, sblock, SBLOCKSIZE);
382 sb_oldfscompat_write(sblk.b_un.b_fs, sblocksave);
383 if (needswap)
384 ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
385 memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
386 flush(fswritefd, &asblk);
387 }
388 /*
389 * read in the summary info.
390 */
391 asked = 0;
392 sblock->fs_csp = (struct csum *)calloc(1, sblock->fs_cssize);
393 if (sblock->fs_csp == NULL) {
394 pwarn("cannot alloc %u bytes for summary info\n",
395 sblock->fs_cssize);
396 goto badsblabel;
397 }
398 for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
399 size = sblock->fs_cssize - i < sblock->fs_bsize ?
400 sblock->fs_cssize - i : sblock->fs_bsize;
401 ccsp = (struct csum *)((char *)sblock->fs_csp + i);
402 if (bread(fsreadfd, (char *)ccsp,
403 fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
404 size) != 0 && !asked) {
405 pfatal("BAD SUMMARY INFORMATION");
406 if (reply("CONTINUE") == 0) {
407 markclean = 0;
408 exit(FSCK_EXIT_CHECK_FAILED);
409 }
410 asked++;
411 }
412 if (doswap) {
413 ffs_csum_swap(ccsp, ccsp, size);
414 bwrite(fswritefd, (char *)ccsp,
415 fsbtodb(sblock,
416 sblock->fs_csaddr + j * sblock->fs_frag),
417 size);
418 }
419 if (needswap)
420 ffs_csum_swap(ccsp, ccsp, size);
421 }
422 /*
423 * allocate and initialize the necessary maps
424 */
425 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
426 blockmap = calloc((unsigned)bmapsize, sizeof (char));
427 if (blockmap == NULL) {
428 pwarn("cannot alloc %u bytes for blockmap\n",
429 (unsigned)bmapsize);
430 goto badsblabel;
431 }
432 inostathead = calloc((unsigned)(sblock->fs_ncg),
433 sizeof(struct inostatlist));
434 if (inostathead == NULL) {
435 pwarn("cannot alloc %u bytes for inostathead\n",
436 (unsigned)(sizeof(struct inostatlist) * (sblock->fs_ncg)));
437 goto badsblabel;
438 }
439 /*
440 * cs_ndir may be inaccurate, particularly if we're using the -b
441 * option, so set a minimum to prevent bogus subdirectory reconnects
442 * and really inefficient directory scans.
443 * Also set a maximum in case the value is too large.
444 */
445 numdirs = sblock->fs_cstotal.cs_ndir;
446 if (numdirs < 1024)
447 numdirs = 1024;
448 if (numdirs > maxino + 1)
449 numdirs = maxino + 1;
450 dirhash = numdirs;
451 inplast = 0;
452 listmax = numdirs + 10;
453 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
454 sizeof(struct inoinfo *));
455 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
456 sizeof(struct inoinfo *));
457 if (inpsort == NULL || inphead == NULL) {
458 pwarn("cannot alloc %u bytes for inphead\n",
459 (unsigned)(numdirs * sizeof(struct inoinfo *)));
460 goto badsblabel;
461 }
462 cgrp = malloc(sblock->fs_cgsize);
463 if (cgrp == NULL) {
464 pwarn("cannot alloc %u bytes for cylinder group\n",
465 sblock->fs_cgsize);
466 goto badsblabel;
467 }
468 bufinit();
469 if (sblock->fs_flags & FS_DOSOFTDEP)
470 usedsoftdep = 1;
471 else
472 usedsoftdep = 0;
473
474 if (!forceimage && dkw.dkw_parent[0])
475 if (strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
476 isappleufs = 1;
477
478 if (readappleufs())
479 isappleufs = 1;
480
481 dirblksiz = DIRBLKSIZ;
482 if (isappleufs)
483 dirblksiz = APPLEUFS_DIRBLKSIZ;
484
485 if (debug)
486 printf("isappleufs = %d, dirblksiz = %d\n", isappleufs, dirblksiz);
487
488 return (1);
489
490 badsblabel:
491 markclean=0;
492 ckfini();
493 return (0);
494 }
495
496 static int
497 readappleufs(void)
498 {
499 daddr_t label = APPLEUFS_LABEL_OFFSET / dev_bsize;
500 struct appleufslabel *appleufs;
501 int i;
502
503 /* XXX do we have to deal with APPLEUFS_LABEL_OFFSET not
504 * being block aligned (CD's?)
505 */
506 if (bread(fsreadfd, (char *)appleufsblk.b_un.b_fs, label,
507 (long)APPLEUFS_LABEL_SIZE) != 0)
508 return 0;
509 appleufsblk.b_bno = label;
510 appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
511
512 appleufs = appleufsblk.b_un.b_appleufs;
513
514 if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
515 if (!isappleufs) {
516 return 0;
517 } else {
518 pfatal("MISSING APPLEUFS VOLUME LABEL\n");
519 if (reply("FIX") == 0) {
520 return 1;
521 }
522 ffs_appleufs_set(appleufs, NULL, -1, 0);
523 appleufsdirty();
524 }
525 }
526
527 if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
528 pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
529 ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
530 if (preen) {
531 printf(" (CORRECTED)\n");
532 }
533 if (preen || reply("CORRECT")) {
534 appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
535 appleufsdirty();
536 }
537 }
538
539 if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
540 pwarn("APPLE UFS LABEL NAME TOO LONG");
541 if (preen) {
542 printf(" (TRUNCATED)\n");
543 }
544 if (preen || reply("TRUNCATE")) {
545 appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
546 appleufsdirty();
547 }
548 }
549
550 if (ntohs(appleufs->ul_namelen) == 0) {
551 pwarn("MISSING APPLE UFS LABEL NAME");
552 if (preen) {
553 printf(" (FIXED)\n");
554 }
555 if (preen || reply("FIX")) {
556 ffs_appleufs_set(appleufs, NULL, -1, 0);
557 appleufsdirty();
558 }
559 }
560
561 /* Scan name for first illegal character */
562 for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
563 if ((appleufs->ul_name[i] == '\0') ||
564 (appleufs->ul_name[i] == ':') ||
565 (appleufs->ul_name[i] == '/')) {
566 pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
567 if (preen) {
568 printf(" (TRUNCATED)\n");
569 }
570 if (preen || reply("TRUNCATE")) {
571 appleufs->ul_namelen = i+1;
572 appleufsdirty();
573 }
574 break;
575 }
576 }
577
578 /* Check the checksum last, because if anything else was wrong,
579 * then the checksum gets reset anyway.
580 */
581 appleufs->ul_checksum = 0;
582 appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
583 if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
584 pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
585 appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
586 if (preen) {
587 printf(" (CORRECTED)\n");
588 }
589 if (preen || reply("CORRECT")) {
590 appleufsdirty();
591 } else {
592 /* put the incorrect checksum back in place */
593 appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
594 }
595 }
596 return 1;
597 }
598
599 /*
600 * Detect byte order. Return 0 if valid magic found, -1 otherwise.
601 */
602 static int
603 detect_byteorder(struct fs *fs, int sblockoff)
604 {
605 if (sblockoff == SBLOCK_UFS2 && (fs->fs_magic == FS_UFS1_MAGIC ||
606 fs->fs_magic == bswap32(FS_UFS1_MAGIC)))
607 /* Likely to be the first alternate of a fs with 64k blocks */
608 return -1;
609 if (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC) {
610 if (endian == 0 || BYTE_ORDER == endian) {
611 needswap = 0;
612 doswap = do_blkswap = do_dirswap = 0;
613 } else {
614 needswap = 1;
615 doswap = do_blkswap = do_dirswap = 1;
616 }
617 return 0;
618 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC) ||
619 fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
620 if (endian == 0 || BYTE_ORDER != endian) {
621 needswap = 1;
622 doswap = do_blkswap = do_dirswap = 0;
623 } else {
624 needswap = 0;
625 doswap = do_blkswap = do_dirswap = 1;
626 }
627 return 0;
628 }
629 return -1;
630 }
631
632 /*
633 * Possible superblock locations ordered from most to least likely.
634 */
635 static off_t sblock_try[] = SBLOCKSEARCH;
636
637 /*
638 * Read in the super block and its summary info.
639 */
640 static int
641 readsb(int listerr)
642 {
643 daddr_t super = 0;
644 struct fs *fs;
645 int i;
646
647 if (bflag) {
648 super = bflag;
649 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super,
650 (long)SBLOCKSIZE) != 0)
651 return (0);
652 fs = sblk.b_un.b_fs;
653 if (detect_byteorder(fs, -1) < 0) {
654 badsb(listerr, "MAGIC NUMBER WRONG");
655 return (0);
656 }
657 } else {
658 for (i = 0; sblock_try[i] != -1; i++) {
659 super = sblock_try[i] / dev_bsize;
660 if (bread(fsreadfd, (char *)sblk.b_un.b_fs,
661 super, (long)SBLOCKSIZE) != 0)
662 continue;
663 fs = sblk.b_un.b_fs;
664 if (detect_byteorder(fs, sblock_try[i]) == 0)
665 break;
666 }
667 if (sblock_try[i] == -1) {
668 badsb(listerr, "CAN'T FIND SUPERBLOCK");
669 return (0);
670 }
671 }
672 if (doswap) {
673 if (preen)
674 errx(FSCK_EXIT_USAGE,
675 "Incompatible options -B and -p");
676 if (nflag)
677 errx(FSCK_EXIT_USAGE,
678 "Incompatible options -B and -n");
679 if (endian == LITTLE_ENDIAN) {
680 if (!reply("CONVERT TO LITTLE ENDIAN"))
681 return 0;
682 } else if (endian == BIG_ENDIAN) {
683 if (!reply("CONVERT TO BIG ENDIAN"))
684 return 0;
685 } else
686 pfatal("INTERNAL ERROR: unknown endian");
687 }
688 if (needswap)
689 pwarn("** Swapped byte order\n");
690 /* swap SB byte order if asked */
691 if (doswap)
692 ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
693
694 memmove(sblock, sblk.b_un.b_fs, SBLOCKSIZE);
695 if (needswap)
696 ffs_sb_swap(sblk.b_un.b_fs, sblock);
697
698 is_ufs2 = sblock->fs_magic == FS_UFS2_MAGIC;
699
700 /*
701 * run a few consistency checks of the super block
702 */
703 if (sblock->fs_sbsize > SBLOCKSIZE)
704 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
705 /*
706 * Compute block size that the filesystem is based on,
707 * according to fsbtodb, and adjust superblock block number
708 * so we can tell if this is an alternate later.
709 */
710 super *= dev_bsize;
711 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
712 sblk.b_bno = super / dev_bsize;
713 sblk.b_size = SBLOCKSIZE;
714 if (bflag)
715 goto out;
716 /*
717 * Set all possible fields that could differ, then do check
718 * of whole super block against an alternate super block->
719 * When an alternate super-block is specified this check is skipped.
720 */
721 getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
722 if (asblk.b_errs)
723 return (0);
724 /* swap SB byte order if asked */
725 if (doswap)
726 ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
727
728 memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
729 if (needswap)
730 ffs_sb_swap(asblk.b_un.b_fs, altsblock);
731 if (cmpsblks(sblock, altsblock)) {
732 if (debug) {
733 uint32_t *nlp, *olp, *endlp;
734
735 printf("superblock mismatches\n");
736 nlp = (uint32_t *)altsblock;
737 olp = (uint32_t *)sblock;
738 endlp = olp + (sblock->fs_sbsize / sizeof *olp);
739 for ( ; olp < endlp; olp++, nlp++) {
740 if (*olp == *nlp)
741 continue;
742 printf("offset %#x, original 0x%08x, alternate "
743 "0x%08x\n",
744 (int)((uint8_t *)olp-(uint8_t *)sblock),
745 *olp, *nlp);
746 }
747 }
748 badsb(listerr,
749 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
750 /*
751 return (0);
752 */
753 }
754 out:
755
756 sb_oldfscompat_read(sblock, &sblocksave);
757
758 /* Now we know the SB is valid, we can write it back if needed */
759 if (doswap) {
760 sbdirty();
761 dirty(&asblk);
762 }
763 havesb = 1;
764 return (1);
765 }
766
767 int
768 cmpsblks(const struct fs *sb, struct fs *asb)
769 {
770 if (!is_ufs2 && ((sb->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
771 if (sb->fs_old_postblformat < FS_DYNAMICPOSTBLFMT)
772 return cmpsblks42(sb, asb);
773 else
774 return cmpsblks44(sb, asb);
775 }
776 if (asb->fs_sblkno != sb->fs_sblkno ||
777 asb->fs_cblkno != sb->fs_cblkno ||
778 asb->fs_iblkno != sb->fs_iblkno ||
779 asb->fs_dblkno != sb->fs_dblkno ||
780 asb->fs_ncg != sb->fs_ncg ||
781 asb->fs_bsize != sb->fs_bsize ||
782 asb->fs_fsize != sb->fs_fsize ||
783 asb->fs_frag != sb->fs_frag ||
784 asb->fs_bmask != sb->fs_bmask ||
785 asb->fs_fmask != sb->fs_fmask ||
786 asb->fs_bshift != sb->fs_bshift ||
787 asb->fs_fshift != sb->fs_fshift ||
788 asb->fs_fragshift != sb->fs_fragshift ||
789 asb->fs_fsbtodb != sb->fs_fsbtodb ||
790 asb->fs_sbsize != sb->fs_sbsize ||
791 asb->fs_nindir != sb->fs_nindir ||
792 asb->fs_inopb != sb->fs_inopb ||
793 asb->fs_cssize != sb->fs_cssize ||
794 asb->fs_ipg != sb->fs_ipg ||
795 asb->fs_fpg != sb->fs_fpg ||
796 asb->fs_magic != sb->fs_magic)
797 return 1;
798 return 0;
799 }
800
801 /* BSD 4.2 performed the following superblock comparison
802 * It should correspond to FS_42POSTBLFMT
803 * (although note that in 4.2, the fs_old_postblformat
804 * field didn't exist and the corresponding bits are
805 * located near the end of the postbl itself, where they
806 * are not likely to be used.)
807 */
808 int
809 cmpsblks42(const struct fs *sb, struct fs *asb)
810 {
811 asb->fs_firstfield = sb->fs_firstfield; /* fs_link */
812 asb->fs_unused_1 = sb->fs_unused_1; /* fs_rlink */
813 asb->fs_old_time = sb->fs_old_time; /* fs_time */
814 asb->fs_old_cstotal = sb->fs_old_cstotal; /* fs_cstotal */
815 asb->fs_cgrotor = sb->fs_cgrotor;
816 asb->fs_fmod = sb->fs_fmod;
817 asb->fs_clean = sb->fs_clean;
818 asb->fs_ronly = sb->fs_ronly;
819 asb->fs_old_flags = sb->fs_old_flags;
820 asb->fs_maxcontig = sb->fs_maxcontig;
821 asb->fs_minfree = sb->fs_minfree;
822 asb->fs_old_rotdelay = sb->fs_old_rotdelay;
823 asb->fs_maxbpg = sb->fs_maxbpg;
824
825 /* The former fs_csp, totaling 128 bytes */
826 memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
827 asb->fs_contigdirs = sb->fs_contigdirs;
828 asb->fs_csp = sb->fs_csp;
829 asb->fs_maxcluster = sb->fs_maxcluster;
830 asb->fs_active = sb->fs_active;
831
832 /* The former fs_fsmnt, totaling 512 bytes */
833 memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
834 memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
835
836 return memcmp(sb, asb, sb->fs_sbsize);
837 }
838
839 /* BSD 4.4 performed the following superblock comparison
840 * This was used in NetBSD through 1.6.1
841 *
842 * Note that this implementation is destructive to asb.
843 */
844 int
845 cmpsblks44(const struct fs *sb, struct fs *asb)
846 {
847 /*
848 * "Copy fields which we don't care if they're different in the
849 * alternate superblocks, as they're either likely to be
850 * different because they're per-cylinder-group specific, or
851 * because they're transient details which are only maintained
852 * in the primary superblock."
853 */
854 asb->fs_firstfield = sb->fs_firstfield;
855 asb->fs_unused_1 = sb->fs_unused_1;
856 asb->fs_old_time = sb->fs_old_time;
857 asb->fs_old_cstotal = sb->fs_old_cstotal;
858 asb->fs_cgrotor = sb->fs_cgrotor;
859 asb->fs_fmod = sb->fs_fmod;
860 asb->fs_clean = sb->fs_clean;
861 asb->fs_ronly = sb->fs_ronly;
862 asb->fs_old_flags = sb->fs_old_flags;
863 asb->fs_maxcontig = sb->fs_maxcontig;
864 asb->fs_minfree = sb->fs_minfree;
865 asb->fs_optim = sb->fs_optim;
866 asb->fs_old_rotdelay = sb->fs_old_rotdelay;
867 asb->fs_maxbpg = sb->fs_maxbpg;
868
869 /* The former fs_csp and fs_maxcluster, totaling 128 bytes */
870 memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
871 asb->fs_contigdirs = sb->fs_contigdirs;
872 asb->fs_csp = sb->fs_csp;
873 asb->fs_maxcluster = sb->fs_maxcluster;
874 asb->fs_active = sb->fs_active;
875
876 /* The former fs_fsmnt, totaling 512 bytes */
877 memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
878 memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
879
880 /* The former fs_sparecon, totaling 200 bytes */
881 memmove(asb->fs_snapinum,
882 sb->fs_snapinum, sizeof sb->fs_snapinum);
883 asb->fs_avgfilesize = sb->fs_avgfilesize;
884 asb->fs_avgfpdir = sb->fs_avgfpdir;
885 asb->fs_save_cgsize = sb->fs_save_cgsize;
886 memmove(asb->fs_sparecon32,
887 sb->fs_sparecon32, sizeof sb->fs_sparecon32);
888 asb->fs_flags = sb->fs_flags;
889
890 /* Original comment:
891 * "The following should not have to be copied, but need to be."
892 */
893 asb->fs_fsbtodb = sb->fs_fsbtodb;
894 asb->fs_old_interleave = sb->fs_old_interleave;
895 asb->fs_old_npsect = sb->fs_old_npsect;
896 asb->fs_old_nrpos = sb->fs_old_nrpos;
897 asb->fs_state = sb->fs_state;
898 asb->fs_qbmask = sb->fs_qbmask;
899 asb->fs_qfmask = sb->fs_qfmask;
900 asb->fs_state = sb->fs_state;
901 asb->fs_maxfilesize = sb->fs_maxfilesize;
902
903 /*
904 * "Compare the superblocks, effectively checking every other
905 * field to see if they differ."
906 */
907 return memcmp(sb, asb, sb->fs_sbsize);
908 }
909
910
911 static void
912 badsb(int listerr, const char *s)
913 {
914
915 if (!listerr)
916 return;
917 if (preen)
918 printf("%s: ", cdevname());
919 pfatal("BAD SUPER BLOCK: %s\n", s);
920 }
921
922 /*
923 * Calculate a prototype superblock based on information in the disk label.
924 * When done the cgsblock macro can be calculated and the fs_ncg field
925 * can be used. Do NOT attempt to use other macros without verifying that
926 * their needed information is available!
927 */
928 static int
929 calcsb(const char *dev, int devfd, struct fs *fs)
930 {
931 struct dkwedge_info dkw;
932 struct disk_geom geo;
933 int i, nspf;
934
935 if (getdiskinfo(dev, fsreadfd, NULL, &geo, &dkw) == -1)
936 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
937 if (dkw.dkw_parent[0] == '\0') {
938 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
939 return (0);
940 }
941 if (strcmp(dkw.dkw_ptype, DKW_PTYPE_FFS) &&
942 strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS)) {
943 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
944 dev, dkw.dkw_ptype);
945 return (0);
946 }
947 if (geo.dg_secsize == 0) {
948 pfatal("%s: CANNOT FIGURE OUT SECTOR SIZE\n", dev);
949 return 0;
950 }
951 if (geo.dg_secpercyl == 0) {
952 pfatal("%s: CANNOT FIGURE OUT SECTORS PER CYLINDER\n", dev);
953 return 0;
954 }
955 if (sblk.b_un.b_fs->fs_fsize == 0) {
956 pfatal("%s: CANNOT FIGURE OUT FRAG BLOCK SIZE\n", dev);
957 return 0;
958 }
959 if (sblk.b_un.b_fs->fs_fpg == 0) {
960 pfatal("%s: CANNOT FIGURE OUT FRAGS PER GROUP\n", dev);
961 return 0;
962 }
963 if (sblk.b_un.b_fs->fs_old_cpg == 0) {
964 pfatal("%s: CANNOT FIGURE OUT OLD CYLINDERS PER GROUP\n", dev);
965 return 0;
966 }
967 memcpy(fs, &sblk.b_un.b_fs, sizeof(struct fs));
968 nspf = fs->fs_fsize / geo.dg_secsize;
969 fs->fs_old_nspf = nspf;
970 for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
971 fs->fs_fsbtodb++;
972 dev_bsize = geo.dg_secsize;
973 if (fs->fs_magic == FS_UFS2_MAGIC) {
974 fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
975 } else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
976 fs->fs_old_cgmask = 0xffffffff;
977 for (i = geo.dg_ntracks; i > 1; i >>= 1)
978 fs->fs_old_cgmask <<= 1;
979 if (!POWEROF2(geo.dg_ntracks))
980 fs->fs_old_cgmask <<= 1;
981 fs->fs_old_cgoffset = roundup(
982 howmany(geo.dg_nsectors, nspf), fs->fs_frag);
983 fs->fs_fpg = (fs->fs_old_cpg * geo.dg_secpercyl) / nspf;
984 fs->fs_ncg = howmany(fs->fs_size / geo.dg_secpercyl,
985 fs->fs_old_cpg);
986 }
987 return (1);
988 }
989