setup.c revision 1.62 1 /* $NetBSD: setup.c,v 1.62 2003/08/07 10:04:21 agc 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.62 2003/08/07 10:04:21 agc 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 __P((int, char *));
69 static int calcsb __P((const char *, int, struct fs *));
70 static struct disklabel *getdisklabel __P((const char *, int));
71 static struct partition *getdisklabelpart __P((const char *, struct disklabel *));
72 static int readsb __P((int));
73 static int readappleufs __P((void));
74
75 /*
76 * Read in a superblock finding an alternate if necessary.
77 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
78 * is already clean (preen mode only).
79 */
80 int
81 setup(dev)
82 const char *dev;
83 {
84 long cg, size, asked, i, j;
85 long bmapsize;
86 struct disklabel *lp;
87 off_t sizepb;
88 struct stat statb;
89 struct fs proto;
90 int doskipclean;
91 u_int64_t maxfilesize;
92 struct csum *ccsp;
93
94 havesb = 0;
95 fswritefd = -1;
96 doskipclean = skipclean;
97 if (stat(dev, &statb) < 0) {
98 printf("Can't stat %s: %s\n", dev, strerror(errno));
99 return (0);
100 }
101 if (!forceimage && !S_ISCHR(statb.st_mode)) {
102 pfatal("%s is not a character device", dev);
103 if (reply("CONTINUE") == 0)
104 return (0);
105 }
106 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
107 printf("Can't open %s: %s\n", dev, strerror(errno));
108 return (0);
109 }
110 if (preen == 0)
111 printf("** %s", dev);
112 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
113 fswritefd = -1;
114 if (preen)
115 pfatal("NO WRITE ACCESS");
116 printf(" (NO WRITE)");
117 }
118 if (preen == 0)
119 printf("\n");
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_old_postblformat == FS_42POSTBLFMT) {
167 pwarn("%sile system is in 4.2BSD format, check skipped\n",
168 preen ? "f" : "** F");
169 return -1;
170 }
171 if (sblock->fs_clean & FS_ISCLEAN) {
172 if (doskipclean) {
173 pwarn("%sile system is clean; not checking\n",
174 preen ? "f" : "** F");
175 return (-1);
176 }
177 if (!preen && !doswap)
178 pwarn("** File system is already clean\n");
179 }
180 maxfsblock = sblock->fs_size;
181 maxino = sblock->fs_ncg * sblock->fs_ipg;
182 sizepb = sblock->fs_bsize;
183 maxfilesize = sblock->fs_bsize * NDADDR - 1;
184 for (i = 0; i < NIADDR; i++) {
185 sizepb *= NINDIR(sblock);
186 maxfilesize += sizepb;
187 }
188 /*
189 * Check and potentially fix certain fields in the super block.
190 */
191 if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
192 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
193 if (reply("SET TO DEFAULT") == 1) {
194 sblock->fs_optim = FS_OPTTIME;
195 sbdirty();
196 }
197 }
198 if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
199 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
200 sblock->fs_minfree);
201 if (reply("SET TO DEFAULT") == 1) {
202 sblock->fs_minfree = 10;
203 sbdirty();
204 }
205 }
206 if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
207 (sblock->fs_old_interleave < 1 ||
208 sblock->fs_old_interleave > sblock->fs_old_nsect)) {
209 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
210 sblock->fs_old_interleave);
211 sblock->fs_old_interleave = 1;
212 if (preen)
213 printf(" (FIXED)\n");
214 if (preen || reply("SET TO DEFAULT") == 1) {
215 sbdirty();
216 dirty(&asblk);
217 }
218 }
219 if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
220 (sblock->fs_old_npsect < sblock->fs_old_nsect ||
221 sblock->fs_old_npsect > sblock->fs_old_nsect*2)) {
222 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
223 sblock->fs_old_npsect);
224 sblock->fs_old_npsect = sblock->fs_old_nsect;
225 if (preen)
226 printf(" (FIXED)\n");
227 if (preen || reply("SET TO DEFAULT") == 1) {
228 sbdirty();
229 dirty(&asblk);
230 }
231 }
232 if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
233 pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
234 sblock->fs_bmask);
235 sblock->fs_bmask = ~(sblock->fs_bsize - 1);
236 if (preen)
237 printf(" (FIXED)\n");
238 if (preen || reply("FIX") == 1) {
239 sbdirty();
240 dirty(&asblk);
241 }
242 }
243 if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
244 pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
245 sblock->fs_fmask);
246 sblock->fs_fmask = ~(sblock->fs_fsize - 1);
247 if (preen)
248 printf(" (FIXED)\n");
249 if (preen || reply("FIX") == 1) {
250 sbdirty();
251 dirty(&asblk);
252 }
253 }
254 if (sblock->fs_old_inodefmt >= FS_44INODEFMT) {
255 if (sblock->fs_maxfilesize != maxfilesize) {
256 pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
257 (unsigned long long)sblock->fs_maxfilesize);
258 sblock->fs_maxfilesize = maxfilesize;
259 if (preen)
260 printf(" (FIXED)\n");
261 if (preen || reply("FIX") == 1) {
262 sbdirty();
263 dirty(&asblk);
264 }
265 }
266 if ((is_ufs2 && sblock->fs_maxsymlinklen != MAXSYMLINKLEN_UFS2)
267 ||
268 (!is_ufs2 && sblock->fs_maxsymlinklen != MAXSYMLINKLEN_UFS1))
269 {
270 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
271 sblock->fs_maxsymlinklen);
272 sblock->fs_maxsymlinklen = is_ufs2 ?
273 MAXSYMLINKLEN_UFS2 : MAXSYMLINKLEN_UFS1;
274 if (preen)
275 printf(" (FIXED)\n");
276 if (preen || reply("FIX") == 1) {
277 sbdirty();
278 dirty(&asblk);
279 }
280 }
281 if (sblock->fs_qbmask != ~sblock->fs_bmask) {
282 pwarn("INCORRECT QBMASK=%llx IN SUPERBLOCK",
283 (unsigned long long)sblock->fs_qbmask);
284 sblock->fs_qbmask = ~sblock->fs_bmask;
285 if (preen)
286 printf(" (FIXED)\n");
287 if (preen || reply("FIX") == 1) {
288 sbdirty();
289 dirty(&asblk);
290 }
291 }
292 if (sblock->fs_qfmask != ~sblock->fs_fmask) {
293 pwarn("INCORRECT QFMASK=%llx IN SUPERBLOCK",
294 (unsigned long long)sblock->fs_qfmask);
295 sblock->fs_qfmask = ~sblock->fs_fmask;
296 if (preen)
297 printf(" (FIXED)\n");
298 if (preen || reply("FIX") == 1) {
299 sbdirty();
300 dirty(&asblk);
301 }
302 }
303 newinofmt = 1;
304 } else {
305 sblock->fs_qbmask = ~sblock->fs_bmask;
306 sblock->fs_qfmask = ~sblock->fs_fmask;
307 newinofmt = 0;
308 }
309 /*
310 * Convert to new inode format.
311 */
312 if (!is_ufs2 && cvtlevel >= 2 &&
313 sblock->fs_old_inodefmt < FS_44INODEFMT) {
314 if (preen)
315 pwarn("CONVERTING TO NEW INODE FORMAT\n");
316 else if (!reply("CONVERT TO NEW INODE FORMAT"))
317 return(0);
318 doinglevel2++;
319 sblock->fs_old_inodefmt = FS_44INODEFMT;
320 sblock->fs_maxfilesize = maxfilesize;
321 sblock->fs_maxsymlinklen = MAXSYMLINKLEN_UFS1;
322 sblock->fs_qbmask = ~sblock->fs_bmask;
323 sblock->fs_qfmask = ~sblock->fs_fmask;
324 sbdirty();
325 dirty(&asblk);
326 }
327 /*
328 * Convert to new cylinder group format.
329 */
330 if (!is_ufs2 && cvtlevel >= 1 &&
331 sblock->fs_old_postblformat == FS_42POSTBLFMT) {
332 if (preen)
333 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
334 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
335 return(0);
336 doinglevel1++;
337 sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
338 sblock->fs_old_nrpos = 0;
339 sbdirty();
340 dirty(&asblk);
341 }
342 if (asblk.b_dirty && !bflag) {
343 memmove((struct fs*)sblk.b_un.b_fs, sblock, SBLOCKSIZE);
344 if (needswap)
345 ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs);
346 memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
347 flush(fswritefd, &asblk);
348 }
349 /*
350 * read in the summary info.
351 */
352 asked = 0;
353 sblock->fs_csp = (struct csum *)calloc(1, sblock->fs_cssize);
354 for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
355 size = sblock->fs_cssize - i < sblock->fs_bsize ?
356 sblock->fs_cssize - i : sblock->fs_bsize;
357 ccsp = (struct csum *)((char *)sblock->fs_csp + i);
358 if (bread(fsreadfd, (char *)ccsp,
359 fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
360 size) != 0 && !asked) {
361 pfatal("BAD SUMMARY INFORMATION");
362 if (reply("CONTINUE") == 0) {
363 markclean = 0;
364 exit(EEXIT);
365 }
366 asked++;
367 }
368 if (doswap) {
369 ffs_csum_swap(ccsp, ccsp, size);
370 bwrite(fswritefd, (char *)ccsp,
371 fsbtodb(sblock,
372 sblock->fs_csaddr + j * sblock->fs_frag),
373 size);
374 }
375 if (needswap)
376 ffs_csum_swap(ccsp, ccsp, size);
377 }
378 /*
379 * allocate and initialize the necessary maps
380 */
381 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
382 blockmap = calloc((unsigned)bmapsize, sizeof (char));
383 if (blockmap == NULL) {
384 printf("cannot alloc %u bytes for blockmap\n",
385 (unsigned)bmapsize);
386 goto badsblabel;
387 }
388 inostathead = calloc((unsigned)(sblock->fs_ncg),
389 sizeof(struct inostatlist));
390 if (inostathead == NULL) {
391 printf("cannot alloc %u bytes for inostathead\n",
392 (unsigned)(sizeof(struct inostatlist) * (sblock->fs_ncg)));
393 goto badsblabel;
394 }
395 /*
396 * cs_ndir may be inaccurate, particularly if we're using the -b
397 * option, so set a minimum to prevent bogus subdirectory reconnects
398 * and really inefficient directory scans.
399 * Also set a maximum in case the value is too large.
400 */
401 numdirs = sblock->fs_cstotal.cs_ndir;
402 if (numdirs < 1024)
403 numdirs = 1024;
404 if (numdirs > maxino + 1)
405 numdirs = maxino + 1;
406 dirhash = numdirs;
407 inplast = 0;
408 listmax = numdirs + 10;
409 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
410 sizeof(struct inoinfo *));
411 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
412 sizeof(struct inoinfo *));
413 if (inpsort == NULL || inphead == NULL) {
414 printf("cannot alloc %u bytes for inphead\n",
415 (unsigned)(numdirs * sizeof(struct inoinfo *)));
416 goto badsblabel;
417 }
418 cgrp = malloc(sblock->fs_cgsize);
419 if (cgrp == NULL) {
420 printf("cannot alloc %u bytes for cylinder group\n",
421 sblock->fs_cgsize);
422 goto badsblabel;
423 }
424 bufinit();
425 if (sblock->fs_flags & FS_DOSOFTDEP)
426 usedsoftdep = 1;
427 else
428 usedsoftdep = 0;
429
430 {
431 struct partition *pp = 0;
432 if (!forceimage && lp)
433 pp = getdisklabelpart(dev,lp);
434 if (pp && (pp->p_fstype == FS_APPLEUFS)) {
435 isappleufs = 1;
436 }
437 }
438 if (readappleufs()) {
439 isappleufs = 1;
440 }
441
442 dirblksiz = DIRBLKSIZ;
443 if (isappleufs)
444 dirblksiz = APPLEUFS_DIRBLKSIZ;
445
446 if (debug)
447 printf("isappleufs = %d, dirblksiz = %d\n", isappleufs, dirblksiz);
448
449 return (1);
450
451 badsblabel:
452 markclean=0;
453 ckfini();
454 return (0);
455 }
456
457 static int
458 readappleufs()
459 {
460 daddr_t label = APPLEUFS_LABEL_OFFSET / dev_bsize;
461 struct appleufslabel *appleufs;
462 int i;
463
464 /* XXX do we have to deal with APPLEUFS_LABEL_OFFSET not
465 * being block aligned (CD's?)
466 */
467 if (bread(fsreadfd, (char *)appleufsblk.b_un.b_fs, label, (long)APPLEUFS_LABEL_SIZE) != 0)
468 return 0;
469 appleufsblk.b_bno = label;
470 appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
471
472 appleufs = appleufsblk.b_un.b_appleufs;
473
474 if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
475 if (!isappleufs) {
476 return 0;
477 } else {
478 pfatal("MISSING APPLEUFS VOLUME LABEL\n");
479 if (reply("FIX") == 0) {
480 return 1;
481 }
482 ffs_appleufs_set(appleufs,NULL,-1);
483 appleufsdirty();
484 }
485 }
486
487 if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
488 pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
489 ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
490 if (preen) {
491 printf(" (CORRECTED)\n");
492 }
493 if (preen || reply("CORRECT")) {
494 appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
495 appleufsdirty();
496 }
497 }
498
499 if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
500 pwarn("APPLE UFS LABEL NAME TOO LONG");
501 if (preen) {
502 printf(" (TRUNCATED)\n");
503 }
504 if (preen || reply("TRUNCATE")) {
505 appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
506 appleufsdirty();
507 }
508 }
509
510 if (ntohs(appleufs->ul_namelen) == 0) {
511 pwarn("MISSING APPLE UFS LABEL NAME");
512 if (preen) {
513 printf(" (FIXED)\n");
514 }
515 if (preen || reply("FIX")) {
516 ffs_appleufs_set(appleufs,NULL,-1);
517 appleufsdirty();
518 }
519 }
520
521 /* Scan name for first illegal character */
522 for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
523 if ((appleufs->ul_name[i] == '\0') ||
524 (appleufs->ul_name[i] == ':') ||
525 (appleufs->ul_name[i] == '/')) {
526 pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
527 if (preen) {
528 printf(" (TRUNCATED)\n");
529 }
530 if (preen || reply("TRUNCATE")) {
531 appleufs->ul_namelen = i+1;
532 appleufsdirty();
533 }
534 break;
535 }
536 }
537
538 /* Check the checksum last, because if anything else was wrong,
539 * then the checksum gets reset anyway.
540 */
541 appleufs->ul_checksum = 0;
542 appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
543 if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
544 pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
545 appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
546 if (preen) {
547 printf(" (CORRECTED)\n");
548 }
549 if (preen || reply("CORRECT")) {
550 appleufsdirty();
551 } else {
552 /* put the incorrect checksum back in place */
553 appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
554 }
555 }
556 return 1;
557 }
558
559 /*
560 * Detect byte order. Return 0 if valid magic found, -1 otherwise.
561 */
562 static int
563 detect_byteorder(struct fs *fs)
564 {
565 if (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC) {
566 if (endian == 0 || BYTE_ORDER == endian) {
567 needswap = 0;
568 doswap = do_blkswap = do_dirswap = 0;
569 } else {
570 needswap = 1;
571 doswap = do_blkswap = do_dirswap = 1;
572 }
573 return 0;
574 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC) ||
575 fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
576 if (endian == 0 || BYTE_ORDER != endian) {
577 needswap = 1;
578 doswap = do_blkswap = do_dirswap = 0;
579 } else {
580 needswap = 0;
581 doswap = do_blkswap = do_dirswap = 1;
582 }
583 return 0;
584 }
585 return -1;
586 }
587
588 /*
589 * Possible superblock locations ordered from most to least likely.
590 */
591 static off_t sblock_try[] = SBLOCKSEARCH;
592
593 /*
594 * Read in the super block and its summary info.
595 */
596 static int
597 readsb(listerr)
598 int listerr;
599 {
600 daddr_t super;
601 struct fs *fs;
602 int i;
603
604 if (bflag) {
605 super = bflag;
606 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super,
607 (long)SBLOCKSIZE) != 0)
608 return (0);
609 fs = sblk.b_un.b_fs;
610 if (detect_byteorder(fs) < 0) {
611 badsb(listerr, "MAGIC NUMBER WRONG");
612 return (0);
613 }
614 } else {
615 for (i = 0; sblock_try[i] != -1; i++) {
616 super = sblock_try[i] / dev_bsize;
617 if (bread(fsreadfd, (char *)sblk.b_un.b_fs,
618 super, (long)SBLOCKSIZE) != 0)
619 continue;
620 fs = sblk.b_un.b_fs;
621 if (detect_byteorder(fs) == 0)
622 break;
623 }
624 if (sblock_try[i] == -1) {
625 badsb(listerr, "CAN'T FIND SUPERBLOCK");
626 return (0);
627 }
628 }
629 if (doswap) {
630 if (preen)
631 errx(EEXIT, "incompatible options -B and -p");
632 if (nflag)
633 errx(EEXIT, "incompatible options -B and -n");
634 if (endian == LITTLE_ENDIAN) {
635 if (!reply("CONVERT TO LITTLE ENDIAN"))
636 return 0;
637 } else if (endian == BIG_ENDIAN) {
638 if (!reply("CONVERT TO BIG ENDIAN"))
639 return 0;
640 } else
641 pfatal("INTERNAL ERROR: unknown endian");
642 }
643 if (needswap)
644 printf("** Swapped byte order\n");
645 /* swap SB byte order if asked */
646 if (doswap)
647 ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
648
649 memmove(sblock, sblk.b_un.b_fs, SBLOCKSIZE);
650 if (needswap)
651 ffs_sb_swap(sblk.b_un.b_fs, sblock);
652
653 is_ufs2 = sblock->fs_magic == FS_UFS2_MAGIC;
654
655 /*
656 * run a few consistency checks of the super block
657 */
658 if (sblock->fs_sbsize > SBLOCKSIZE)
659 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
660 /*
661 * Compute block size that the filesystem is based on,
662 * according to fsbtodb, and adjust superblock block number
663 * so we can tell if this is an alternate later.
664 */
665 super *= dev_bsize;
666 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
667 sblk.b_bno = super / dev_bsize;
668 sblk.b_size = SBLOCKSIZE;
669 if (bflag)
670 goto out;
671 /*
672 * Set all possible fields that could differ, then do check
673 * of whole super block against an alternate super block->
674 * When an alternate super-block is specified this check is skipped.
675 */
676 getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
677 if (asblk.b_errs)
678 return (0);
679 /* swap SB byte order if asked */
680 if (doswap)
681 ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
682
683 memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
684 if (needswap)
685 ffs_sb_swap(asblk.b_un.b_fs, altsblock);
686 if (cmpsblks(sblock, altsblock)) {
687 badsb(listerr,
688 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
689 return (0);
690 }
691 out:
692 /*
693 * If not yet done, update UFS1 superblock with new wider fields.
694 */
695 if (sblock->fs_magic == FS_UFS1_MAGIC) {
696 if (sblock->fs_maxbsize != sblock->fs_bsize ||
697 sblock->fs_time < sblock->fs_old_time) {
698 sblock->fs_cstotal.cs_ndir =
699 sblock->fs_old_cstotal.cs_ndir;
700 sblock->fs_cstotal.cs_nbfree =
701 sblock->fs_old_cstotal.cs_nbfree;
702 sblock->fs_cstotal.cs_nifree =
703 sblock->fs_old_cstotal.cs_nifree;
704 sblock->fs_cstotal.cs_nffree =
705 sblock->fs_old_cstotal.cs_nffree;
706 }
707 if (sblock->fs_maxbsize != sblock->fs_bsize) {
708 sblock->fs_maxbsize = sblock->fs_bsize;
709 sblock->fs_time = sblock->fs_old_time;
710 sblock->fs_size = sblock->fs_old_size;
711 sblock->fs_dsize = sblock->fs_old_dsize;
712 sblock->fs_csaddr = sblock->fs_old_csaddr;
713 }
714 }
715
716 /* Now we know the SB is valid, we can write it back if needed */
717 if (doswap) {
718 sbdirty();
719 dirty(&asblk);
720 }
721 havesb = 1;
722 return (1);
723 }
724
725 int
726 cmpsblks(const struct fs *sb, struct fs *asb)
727 {
728 if (asb->fs_sblkno != sb->fs_sblkno ||
729 asb->fs_cblkno != sb->fs_cblkno ||
730 asb->fs_iblkno != sb->fs_iblkno ||
731 asb->fs_dblkno != sb->fs_dblkno ||
732 asb->fs_ncg != sb->fs_ncg ||
733 asb->fs_bsize != sb->fs_bsize ||
734 asb->fs_fsize != sb->fs_fsize ||
735 asb->fs_frag != sb->fs_frag ||
736 asb->fs_bmask != sb->fs_bmask ||
737 asb->fs_fmask != sb->fs_fmask ||
738 asb->fs_bshift != sb->fs_bshift ||
739 asb->fs_fshift != sb->fs_fshift ||
740 asb->fs_fragshift != sb->fs_fragshift ||
741 asb->fs_fsbtodb != sb->fs_fsbtodb ||
742 asb->fs_sbsize != sb->fs_sbsize ||
743 asb->fs_nindir != sb->fs_nindir ||
744 asb->fs_inopb != sb->fs_inopb ||
745 asb->fs_cssize != sb->fs_cssize ||
746 asb->fs_ipg != sb->fs_ipg ||
747 asb->fs_fpg != sb->fs_fpg ||
748 asb->fs_magic != sb->fs_magic)
749 return 1;
750 return 0;
751 }
752
753 static void
754 badsb(listerr, s)
755 int listerr;
756 char *s;
757 {
758
759 if (!listerr)
760 return;
761 if (preen)
762 printf("%s: ", cdevname());
763 pfatal("BAD SUPER BLOCK: %s\n", s);
764 }
765
766 /*
767 * Calculate a prototype superblock based on information in the disk label.
768 * When done the cgsblock macro can be calculated and the fs_ncg field
769 * can be used. Do NOT attempt to use other macros without verifying that
770 * their needed information is available!
771 */
772 static int
773 calcsb(dev, devfd, fs)
774 const char *dev;
775 int devfd;
776 struct fs *fs;
777 {
778 struct disklabel *lp;
779 struct partition *pp;
780 int i, nspf;
781
782 lp = getdisklabel(dev, devfd);
783 pp = getdisklabelpart(dev,lp);
784 if (pp == 0) {
785 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
786 return (0);
787 }
788 if ((pp->p_fstype != FS_BSDFFS) && (pp->p_fstype != FS_APPLEUFS)) {
789 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
790 dev, pp->p_fstype < FSMAXTYPES ?
791 fstypenames[pp->p_fstype] : "unknown");
792 return (0);
793 }
794 /* avoid divide by 0 */
795 if (pp->p_fsize == 0 || pp->p_frag == 0)
796 return (0);
797 memset(fs, 0, sizeof(struct fs));
798 fs->fs_fsize = pp->p_fsize;
799 fs->fs_frag = pp->p_frag;
800 fs->fs_size = pp->p_size;
801 fs->fs_sblkno = roundup(
802 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
803 fs->fs_frag);
804 nspf = fs->fs_fsize / lp->d_secsize;
805 fs->fs_old_nspf = nspf;
806 for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
807 fs->fs_fsbtodb++;
808 dev_bsize = lp->d_secsize;
809 if (fs->fs_magic == FS_UFS2_MAGIC) {
810 fs->fs_fpg = pp->p_cpg;
811 fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
812 } else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
813 fs->fs_old_cpg = pp->p_cpg;
814 fs->fs_old_cgmask = 0xffffffff;
815 for (i = lp->d_ntracks; i > 1; i >>= 1)
816 fs->fs_old_cgmask <<= 1;
817 if (!POWEROF2(lp->d_ntracks))
818 fs->fs_old_cgmask <<= 1;
819 fs->fs_old_cgoffset = roundup(
820 howmany(lp->d_nsectors, nspf), fs->fs_frag);
821 fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf;
822 fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl,
823 fs->fs_old_cpg);
824 }
825 return (1);
826 }
827
828 static struct disklabel *
829 getdisklabel(s, fd)
830 const char *s;
831 int fd;
832 {
833 static struct disklabel lab;
834
835 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
836 if (s == NULL)
837 return ((struct disklabel *)NULL);
838 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
839 errx(EEXIT, "%s: can't read disk label", s);
840 }
841 return (&lab);
842 }
843
844 static struct partition *
845 getdisklabelpart(dev, lp)
846 const char *dev;
847 struct disklabel *lp;
848 {
849 char *cp;
850
851 cp = strchr(dev, '\0') - 1;
852 if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'p')) && !isdigit(*cp)) {
853 return 0;
854 }
855 if (isdigit(*cp))
856 return &lp->d_partitions[0];
857 else
858 return &lp->d_partitions[*cp - 'a'];
859 }
860
861