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