setup.c revision 1.79 1 /* $NetBSD: setup.c,v 1.79 2006/03/17 15:53:46 rumble 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.79 2006/03/17 15:53:46 rumble 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 #define FSTYPENAMES
46 #include <sys/disklabel.h>
47 #include <sys/file.h>
48
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ufs/dir.h>
51 #include <ufs/ufs/ufs_bswap.h>
52 #include <ufs/ffs/fs.h>
53 #include <ufs/ffs/ffs_extern.h>
54
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61
62 #include "fsck.h"
63 #include "extern.h"
64 #include "fsutil.h"
65
66 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
67
68 static void badsb(int, const char *);
69 static int calcsb(const char *, int, struct fs *);
70 static struct disklabel *getdisklabel(const char *, int);
71 static struct partition *getdisklabelpart(const char *, struct disklabel *);
72 static int readsb(int);
73 static int readappleufs(void);
74
75 int16_t sblkpostbl[256];
76
77 /*
78 * Read in a superblock finding an alternate if necessary.
79 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
80 * is already clean (preen mode only).
81 */
82 int
83 setup(const char *dev)
84 {
85 long cg, size, asked, i, j;
86 long bmapsize;
87 struct disklabel *lp = NULL;
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 errx(EEXIT, "cannot allocate space for superblock");
131 if (!forceimage && (lp = getdisklabel(NULL, fsreadfd)) != NULL)
132 dev_bsize = secsize = lp->d_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(EEXIT);
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 {
475 struct partition *pp = 0;
476 if (!forceimage && lp)
477 pp = getdisklabelpart(dev,lp);
478 if (pp && (pp->p_fstype == FS_APPLEUFS)) {
479 isappleufs = 1;
480 }
481 }
482 if (readappleufs()) {
483 isappleufs = 1;
484 }
485
486 dirblksiz = DIRBLKSIZ;
487 if (isappleufs)
488 dirblksiz = APPLEUFS_DIRBLKSIZ;
489
490 if (debug)
491 printf("isappleufs = %d, dirblksiz = %d\n", isappleufs, dirblksiz);
492
493 return (1);
494
495 badsblabel:
496 markclean=0;
497 ckfini();
498 return (0);
499 }
500
501 static int
502 readappleufs(void)
503 {
504 daddr_t label = APPLEUFS_LABEL_OFFSET / dev_bsize;
505 struct appleufslabel *appleufs;
506 int i;
507
508 /* XXX do we have to deal with APPLEUFS_LABEL_OFFSET not
509 * being block aligned (CD's?)
510 */
511 if (bread(fsreadfd, (char *)appleufsblk.b_un.b_fs, label,
512 (long)APPLEUFS_LABEL_SIZE) != 0)
513 return 0;
514 appleufsblk.b_bno = label;
515 appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
516
517 appleufs = appleufsblk.b_un.b_appleufs;
518
519 if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
520 if (!isappleufs) {
521 return 0;
522 } else {
523 pfatal("MISSING APPLEUFS VOLUME LABEL\n");
524 if (reply("FIX") == 0) {
525 return 1;
526 }
527 ffs_appleufs_set(appleufs, NULL, -1, 0);
528 appleufsdirty();
529 }
530 }
531
532 if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
533 pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
534 ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
535 if (preen) {
536 printf(" (CORRECTED)\n");
537 }
538 if (preen || reply("CORRECT")) {
539 appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
540 appleufsdirty();
541 }
542 }
543
544 if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
545 pwarn("APPLE UFS LABEL NAME TOO LONG");
546 if (preen) {
547 printf(" (TRUNCATED)\n");
548 }
549 if (preen || reply("TRUNCATE")) {
550 appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
551 appleufsdirty();
552 }
553 }
554
555 if (ntohs(appleufs->ul_namelen) == 0) {
556 pwarn("MISSING APPLE UFS LABEL NAME");
557 if (preen) {
558 printf(" (FIXED)\n");
559 }
560 if (preen || reply("FIX")) {
561 ffs_appleufs_set(appleufs, NULL, -1, 0);
562 appleufsdirty();
563 }
564 }
565
566 /* Scan name for first illegal character */
567 for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
568 if ((appleufs->ul_name[i] == '\0') ||
569 (appleufs->ul_name[i] == ':') ||
570 (appleufs->ul_name[i] == '/')) {
571 pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
572 if (preen) {
573 printf(" (TRUNCATED)\n");
574 }
575 if (preen || reply("TRUNCATE")) {
576 appleufs->ul_namelen = i+1;
577 appleufsdirty();
578 }
579 break;
580 }
581 }
582
583 /* Check the checksum last, because if anything else was wrong,
584 * then the checksum gets reset anyway.
585 */
586 appleufs->ul_checksum = 0;
587 appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
588 if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
589 pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
590 appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
591 if (preen) {
592 printf(" (CORRECTED)\n");
593 }
594 if (preen || reply("CORRECT")) {
595 appleufsdirty();
596 } else {
597 /* put the incorrect checksum back in place */
598 appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
599 }
600 }
601 return 1;
602 }
603
604 /*
605 * Detect byte order. Return 0 if valid magic found, -1 otherwise.
606 */
607 static int
608 detect_byteorder(struct fs *fs, int sblockoff)
609 {
610 if (sblockoff == SBLOCK_UFS2 && (fs->fs_magic == FS_UFS1_MAGIC ||
611 fs->fs_magic == bswap32(FS_UFS1_MAGIC)))
612 /* Likely to be the first alternate of a fs with 64k blocks */
613 return -1;
614 if (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC) {
615 if (endian == 0 || BYTE_ORDER == endian) {
616 needswap = 0;
617 doswap = do_blkswap = do_dirswap = 0;
618 } else {
619 needswap = 1;
620 doswap = do_blkswap = do_dirswap = 1;
621 }
622 return 0;
623 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC) ||
624 fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
625 if (endian == 0 || BYTE_ORDER != endian) {
626 needswap = 1;
627 doswap = do_blkswap = do_dirswap = 0;
628 } else {
629 needswap = 0;
630 doswap = do_blkswap = do_dirswap = 1;
631 }
632 return 0;
633 }
634 return -1;
635 }
636
637 /*
638 * Possible superblock locations ordered from most to least likely.
639 */
640 static off_t sblock_try[] = SBLOCKSEARCH;
641
642 /*
643 * Read in the super block and its summary info.
644 */
645 static int
646 readsb(int listerr)
647 {
648 daddr_t super = 0;
649 struct fs *fs;
650 int i;
651
652 if (bflag) {
653 super = bflag;
654 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super,
655 (long)SBLOCKSIZE) != 0)
656 return (0);
657 fs = sblk.b_un.b_fs;
658 if (detect_byteorder(fs, -1) < 0) {
659 badsb(listerr, "MAGIC NUMBER WRONG");
660 return (0);
661 }
662 } else {
663 for (i = 0; sblock_try[i] != -1; i++) {
664 super = sblock_try[i] / dev_bsize;
665 if (bread(fsreadfd, (char *)sblk.b_un.b_fs,
666 super, (long)SBLOCKSIZE) != 0)
667 continue;
668 fs = sblk.b_un.b_fs;
669 if (detect_byteorder(fs, sblock_try[i]) == 0)
670 break;
671 }
672 if (sblock_try[i] == -1) {
673 badsb(listerr, "CAN'T FIND SUPERBLOCK");
674 return (0);
675 }
676 }
677 if (doswap) {
678 if (preen)
679 errx(EEXIT, "incompatible options -B and -p");
680 if (nflag)
681 errx(EEXIT, "incompatible options -B and -n");
682 if (endian == LITTLE_ENDIAN) {
683 if (!reply("CONVERT TO LITTLE ENDIAN"))
684 return 0;
685 } else if (endian == BIG_ENDIAN) {
686 if (!reply("CONVERT TO BIG ENDIAN"))
687 return 0;
688 } else
689 pfatal("INTERNAL ERROR: unknown endian");
690 }
691 if (needswap)
692 pwarn("** Swapped byte order\n");
693 /* swap SB byte order if asked */
694 if (doswap)
695 ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
696
697 memmove(sblock, sblk.b_un.b_fs, SBLOCKSIZE);
698 if (needswap)
699 ffs_sb_swap(sblk.b_un.b_fs, sblock);
700
701 is_ufs2 = sblock->fs_magic == FS_UFS2_MAGIC;
702
703 /*
704 * run a few consistency checks of the super block
705 */
706 if (sblock->fs_sbsize > SBLOCKSIZE)
707 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
708 /*
709 * Compute block size that the filesystem is based on,
710 * according to fsbtodb, and adjust superblock block number
711 * so we can tell if this is an alternate later.
712 */
713 super *= dev_bsize;
714 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
715 sblk.b_bno = super / dev_bsize;
716 sblk.b_size = SBLOCKSIZE;
717 if (bflag)
718 goto out;
719 /*
720 * Set all possible fields that could differ, then do check
721 * of whole super block against an alternate super block->
722 * When an alternate super-block is specified this check is skipped.
723 */
724 getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
725 if (asblk.b_errs)
726 return (0);
727 /* swap SB byte order if asked */
728 if (doswap)
729 ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
730
731 memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
732 if (needswap)
733 ffs_sb_swap(asblk.b_un.b_fs, altsblock);
734 if (cmpsblks(sblock, altsblock)) {
735 if (debug) {
736 uint32_t *nlp, *olp, *endlp;
737
738 printf("superblock mismatches\n");
739 nlp = (uint32_t *)altsblock;
740 olp = (uint32_t *)sblock;
741 endlp = olp + (sblock->fs_sbsize / sizeof *olp);
742 for ( ; olp < endlp; olp++, nlp++) {
743 if (*olp == *nlp)
744 continue;
745 printf("offset %#x, original 0x%08x, alternate "
746 "0x%08x\n",
747 (int)((uint8_t *)olp-(uint8_t *)sblock),
748 *olp, *nlp);
749 }
750 }
751 badsb(listerr,
752 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
753 return (0);
754 }
755 out:
756
757 sb_oldfscompat_read(sblock, &sblocksave);
758
759 /* Now we know the SB is valid, we can write it back if needed */
760 if (doswap) {
761 sbdirty();
762 dirty(&asblk);
763 }
764 havesb = 1;
765 return (1);
766 }
767
768 int
769 cmpsblks(const struct fs *sb, struct fs *asb)
770 {
771 if (!is_ufs2 && ((sb->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
772 if (sb->fs_old_postblformat < FS_DYNAMICPOSTBLFMT)
773 return cmpsblks42(sb, asb);
774 else
775 return cmpsblks44(sb, asb);
776 }
777 if (asb->fs_sblkno != sb->fs_sblkno ||
778 asb->fs_cblkno != sb->fs_cblkno ||
779 asb->fs_iblkno != sb->fs_iblkno ||
780 asb->fs_dblkno != sb->fs_dblkno ||
781 asb->fs_ncg != sb->fs_ncg ||
782 asb->fs_bsize != sb->fs_bsize ||
783 asb->fs_fsize != sb->fs_fsize ||
784 asb->fs_frag != sb->fs_frag ||
785 asb->fs_bmask != sb->fs_bmask ||
786 asb->fs_fmask != sb->fs_fmask ||
787 asb->fs_bshift != sb->fs_bshift ||
788 asb->fs_fshift != sb->fs_fshift ||
789 asb->fs_fragshift != sb->fs_fragshift ||
790 asb->fs_fsbtodb != sb->fs_fsbtodb ||
791 asb->fs_sbsize != sb->fs_sbsize ||
792 asb->fs_nindir != sb->fs_nindir ||
793 asb->fs_inopb != sb->fs_inopb ||
794 asb->fs_cssize != sb->fs_cssize ||
795 asb->fs_ipg != sb->fs_ipg ||
796 asb->fs_fpg != sb->fs_fpg ||
797 asb->fs_magic != sb->fs_magic)
798 return 1;
799 return 0;
800 }
801
802 /* BSD 4.2 performed the following superblock comparison
803 * It should correspond to FS_42POSTBLFMT
804 * (although note that in 4.2, the fs_old_postblformat
805 * field didn't exist and the corresponding bits are
806 * located near the end of the postbl itself, where they
807 * are not likely to be used.)
808 */
809 int
810 cmpsblks42(const struct fs *sb, struct fs *asb)
811 {
812 asb->fs_firstfield = sb->fs_firstfield; /* fs_link */
813 asb->fs_unused_1 = sb->fs_unused_1; /* fs_rlink */
814 asb->fs_old_time = sb->fs_old_time; /* fs_time */
815 asb->fs_old_cstotal = sb->fs_old_cstotal; /* fs_cstotal */
816 asb->fs_cgrotor = sb->fs_cgrotor;
817 asb->fs_fmod = sb->fs_fmod;
818 asb->fs_clean = sb->fs_clean;
819 asb->fs_ronly = sb->fs_ronly;
820 asb->fs_old_flags = sb->fs_old_flags;
821 asb->fs_maxcontig = sb->fs_maxcontig;
822 asb->fs_minfree = sb->fs_minfree;
823 asb->fs_old_rotdelay = sb->fs_old_rotdelay;
824 asb->fs_maxbpg = sb->fs_maxbpg;
825
826 /* The former fs_csp, totaling 128 bytes */
827 memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
828 asb->fs_contigdirs = sb->fs_contigdirs;
829 asb->fs_csp = sb->fs_csp;
830 asb->fs_maxcluster = sb->fs_maxcluster;
831 asb->fs_active = sb->fs_active;
832
833 /* The former fs_fsmnt, totaling 512 bytes */
834 memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
835 memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
836
837 return memcmp(sb, asb, sb->fs_sbsize);
838 }
839
840 /* BSD 4.4 performed the following superblock comparison
841 * This was used in NetBSD through 1.6.1
842 *
843 * Note that this implementation is destructive to asb.
844 */
845 int
846 cmpsblks44(const struct fs *sb, struct fs *asb)
847 {
848 /*
849 * "Copy fields which we don't care if they're different in the
850 * alternate superblocks, as they're either likely to be
851 * different because they're per-cylinder-group specific, or
852 * because they're transient details which are only maintained
853 * in the primary superblock."
854 */
855 asb->fs_firstfield = sb->fs_firstfield;
856 asb->fs_unused_1 = sb->fs_unused_1;
857 asb->fs_old_time = sb->fs_old_time;
858 asb->fs_old_cstotal = sb->fs_old_cstotal;
859 asb->fs_cgrotor = sb->fs_cgrotor;
860 asb->fs_fmod = sb->fs_fmod;
861 asb->fs_clean = sb->fs_clean;
862 asb->fs_ronly = sb->fs_ronly;
863 asb->fs_old_flags = sb->fs_old_flags;
864 asb->fs_maxcontig = sb->fs_maxcontig;
865 asb->fs_minfree = sb->fs_minfree;
866 asb->fs_optim = sb->fs_optim;
867 asb->fs_old_rotdelay = sb->fs_old_rotdelay;
868 asb->fs_maxbpg = sb->fs_maxbpg;
869
870 /* The former fs_csp and fs_maxcluster, totaling 128 bytes */
871 memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
872 asb->fs_contigdirs = sb->fs_contigdirs;
873 asb->fs_csp = sb->fs_csp;
874 asb->fs_maxcluster = sb->fs_maxcluster;
875 asb->fs_active = sb->fs_active;
876
877 /* The former fs_fsmnt, totaling 512 bytes */
878 memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
879 memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
880
881 /* The former fs_sparecon, totaling 200 bytes */
882 memmove(asb->fs_snapinum,
883 sb->fs_snapinum, sizeof sb->fs_snapinum);
884 asb->fs_avgfilesize = sb->fs_avgfilesize;
885 asb->fs_avgfpdir = sb->fs_avgfpdir;
886 asb->fs_save_cgsize = sb->fs_save_cgsize;
887 memmove(asb->fs_sparecon32,
888 sb->fs_sparecon32, sizeof sb->fs_sparecon32);
889 asb->fs_flags = sb->fs_flags;
890
891 /* Original comment:
892 * "The following should not have to be copied, but need to be."
893 */
894 asb->fs_fsbtodb = sb->fs_fsbtodb;
895 asb->fs_old_interleave = sb->fs_old_interleave;
896 asb->fs_old_npsect = sb->fs_old_npsect;
897 asb->fs_old_nrpos = sb->fs_old_nrpos;
898 asb->fs_state = sb->fs_state;
899 asb->fs_qbmask = sb->fs_qbmask;
900 asb->fs_qfmask = sb->fs_qfmask;
901 asb->fs_state = sb->fs_state;
902 asb->fs_maxfilesize = sb->fs_maxfilesize;
903
904 /*
905 * "Compare the superblocks, effectively checking every other
906 * field to see if they differ."
907 */
908 return memcmp(sb, asb, sb->fs_sbsize);
909 }
910
911
912 static void
913 badsb(int listerr, const char *s)
914 {
915
916 if (!listerr)
917 return;
918 if (preen)
919 printf("%s: ", cdevname());
920 pfatal("BAD SUPER BLOCK: %s\n", s);
921 }
922
923 /*
924 * Calculate a prototype superblock based on information in the disk label.
925 * When done the cgsblock macro can be calculated and the fs_ncg field
926 * can be used. Do NOT attempt to use other macros without verifying that
927 * their needed information is available!
928 */
929 static int
930 calcsb(const char *dev, int devfd, struct fs *fs)
931 {
932 struct disklabel *lp;
933 struct partition *pp;
934 int i, nspf;
935
936 lp = getdisklabel(dev, devfd);
937 pp = getdisklabelpart(dev,lp);
938 if (pp == 0) {
939 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
940 return (0);
941 }
942 if ((pp->p_fstype != FS_BSDFFS) && (pp->p_fstype != FS_APPLEUFS)) {
943 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
944 dev, pp->p_fstype < FSMAXTYPES ?
945 fstypenames[pp->p_fstype] : "unknown");
946 return (0);
947 }
948 /* avoid divide by 0 */
949 if (pp->p_fsize == 0 || pp->p_frag == 0 || pp->p_cpg == 0) {
950 pfatal("%s: LABEL DOES NOT CONTAIN FILE SYSTEM PARAMETERS\n", dev);
951 return (0);
952 }
953 memset(fs, 0, sizeof(struct fs));
954 fs->fs_fsize = pp->p_fsize;
955 fs->fs_frag = pp->p_frag;
956 fs->fs_size = pp->p_size;
957 fs->fs_sblkno = roundup(
958 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
959 fs->fs_frag);
960 nspf = fs->fs_fsize / lp->d_secsize;
961 fs->fs_old_nspf = nspf;
962 for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
963 fs->fs_fsbtodb++;
964 dev_bsize = lp->d_secsize;
965 if (fs->fs_magic == FS_UFS2_MAGIC) {
966 fs->fs_fpg = pp->p_cpg;
967 fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
968 } else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
969 fs->fs_old_cpg = pp->p_cpg;
970 fs->fs_old_cgmask = 0xffffffff;
971 for (i = lp->d_ntracks; i > 1; i >>= 1)
972 fs->fs_old_cgmask <<= 1;
973 if (!POWEROF2(lp->d_ntracks))
974 fs->fs_old_cgmask <<= 1;
975 fs->fs_old_cgoffset = roundup(
976 howmany(lp->d_nsectors, nspf), fs->fs_frag);
977 fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf;
978 fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl,
979 fs->fs_old_cpg);
980 }
981 return (1);
982 }
983
984 static struct disklabel *
985 getdisklabel(const char *s, int fd)
986 {
987 static struct disklabel lab;
988
989 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
990 if (s == NULL)
991 return ((struct disklabel *)NULL);
992 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
993 errx(EEXIT, "%s: can't read disk label", s);
994 }
995 return (&lab);
996 }
997
998 static struct partition *
999 getdisklabelpart(const char *dev, struct disklabel *lp)
1000 {
1001 char *cp;
1002 int c;
1003
1004 cp = strchr(dev, '\0');
1005 if (cp == dev)
1006 return NULL;
1007
1008 c = (unsigned char)cp[-1];
1009 if (isdigit(c))
1010 /* eg "wd0", return info for first partition */
1011 return &lp->d_partitions[0];
1012
1013 if (c >= 'a' && c <= 'p')
1014 /* eg "wd0f", return info for specified partition */
1015 return &lp->d_partitions[c - 'a'];
1016 return NULL;
1017 }
1018
1019