setup.c revision 1.49 1 /* $NetBSD: setup.c,v 1.49 2001/09/06 02:16:00 lukem 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
40 #else
41 __RCSID("$NetBSD: setup.c,v 1.49 2001/09/06 02:16:00 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <sys/ioctl.h>
49 #define FSTYPENAMES
50 #include <sys/disklabel.h>
51 #include <sys/file.h>
52
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ufs/ufs_bswap.h>
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ffs/ffs_extern.h>
57
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 #include "fsck.h"
66 #include "extern.h"
67 #include "fsutil.h"
68
69 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
70
71 static void badsb __P((int, char *));
72 static int calcsb __P((const char *, int, struct fs *));
73 static struct disklabel *getdisklabel __P((const char *, int));
74 static int readsb __P((int));
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(dev)
83 const char *dev;
84 {
85 long cg, size, asked, i, j;
86 long bmapsize;
87 struct disklabel *lp;
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) {
103 if (!S_ISREG(statb.st_mode)) {
104 pfatal("%s is not a regular file", dev);
105 if (reply("CONTINUE") == 0)
106 return (0);
107 }
108 } else if (!S_ISCHR(statb.st_mode)) {
109 pfatal("%s is not a character device", dev);
110 if (reply("CONTINUE") == 0)
111 return (0);
112 }
113 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
114 printf("Can't open %s: %s\n", dev, strerror(errno));
115 return (0);
116 }
117 if (preen == 0)
118 printf("** %s", dev);
119 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
120 fswritefd = -1;
121 if (preen)
122 pfatal("NO WRITE ACCESS");
123 printf(" (NO WRITE)");
124 }
125 if (preen == 0)
126 printf("\n");
127 fsmodified = 0;
128 lfdir = 0;
129 initbarea(&sblk);
130 initbarea(&asblk);
131 sblk.b_un.b_buf = malloc(SBSIZE);
132 sblock = malloc(SBSIZE);
133 asblk.b_un.b_buf = malloc(SBSIZE);
134 altsblock = malloc(SBSIZE);
135 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
136 sblock == NULL || altsblock == NULL)
137 errx(EEXIT, "cannot allocate space for superblock");
138 if ((lp = getdisklabel(NULL, fsreadfd)) != NULL)
139 dev_bsize = secsize = lp->d_secsize;
140 else
141 dev_bsize = secsize = DEV_BSIZE;
142 /*
143 * Read in the superblock, looking for alternates if necessary
144 */
145 if (readsb(1) == 0) {
146 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
147 return(0);
148 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
149 return (0);
150 for (cg = 0; cg < proto.fs_ncg; cg++) {
151 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
152 if (readsb(0) != 0)
153 break;
154 }
155 if (cg >= proto.fs_ncg) {
156 printf("%s %s\n%s %s\n%s %s\n",
157 "SEARCH FOR ALTERNATE SUPER-BLOCK",
158 "FAILED. YOU MUST USE THE",
159 "-b OPTION TO fsck_ffs TO SPECIFY THE",
160 "LOCATION OF AN ALTERNATE",
161 "SUPER-BLOCK TO SUPPLY NEEDED",
162 "INFORMATION; SEE fsck_ffs(8).");
163 return(0);
164 }
165 doskipclean = 0;
166 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
167 }
168 if (debug)
169 printf("clean = %d\n", sblock->fs_clean);
170 if (doswap)
171 doskipclean = 0;
172 if (sblock->fs_clean & FS_ISCLEAN) {
173 if (doskipclean) {
174 pwarn("%sile system is clean; not checking\n",
175 preen ? "f" : "** F");
176 return (-1);
177 }
178 if (!preen && !doswap)
179 pwarn("** File system is already clean\n");
180 }
181 maxfsblock = sblock->fs_size;
182 maxino = sblock->fs_ncg * sblock->fs_ipg;
183 sizepb = sblock->fs_bsize;
184 maxfilesize = sblock->fs_bsize * NDADDR - 1;
185 for (i = 0; i < NIADDR; i++) {
186 sizepb *= NINDIR(sblock);
187 maxfilesize += sizepb;
188 }
189 /*
190 * Check and potentially fix certain fields in the super block.
191 */
192 if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
193 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
194 if (reply("SET TO DEFAULT") == 1) {
195 sblock->fs_optim = FS_OPTTIME;
196 sbdirty();
197 }
198 }
199 if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
200 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
201 sblock->fs_minfree);
202 if (reply("SET TO DEFAULT") == 1) {
203 sblock->fs_minfree = 10;
204 sbdirty();
205 }
206 }
207 if (sblock->fs_interleave < 1 ||
208 sblock->fs_interleave > sblock->fs_nsect) {
209 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
210 sblock->fs_interleave);
211 sblock->fs_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 (sblock->fs_npsect < sblock->fs_nsect ||
220 sblock->fs_npsect > sblock->fs_nsect*2) {
221 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
222 sblock->fs_npsect);
223 sblock->fs_npsect = sblock->fs_nsect;
224 if (preen)
225 printf(" (FIXED)\n");
226 if (preen || reply("SET TO DEFAULT") == 1) {
227 sbdirty();
228 dirty(&asblk);
229 }
230 }
231 if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
232 pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
233 sblock->fs_bmask);
234 sblock->fs_bmask = ~(sblock->fs_bsize - 1);
235 if (preen)
236 printf(" (FIXED)\n");
237 if (preen || reply("FIX") == 1) {
238 sbdirty();
239 dirty(&asblk);
240 }
241 }
242 if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
243 pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
244 sblock->fs_fmask);
245 sblock->fs_fmask = ~(sblock->fs_fsize - 1);
246 if (preen)
247 printf(" (FIXED)\n");
248 if (preen || reply("FIX") == 1) {
249 sbdirty();
250 dirty(&asblk);
251 }
252 }
253 if (sblock->fs_inodefmt >= FS_44INODEFMT) {
254 if (sblock->fs_maxfilesize != maxfilesize) {
255 pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
256 (unsigned long long)sblock->fs_maxfilesize);
257 sblock->fs_maxfilesize = maxfilesize;
258 if (preen)
259 printf(" (FIXED)\n");
260 if (preen || reply("FIX") == 1) {
261 sbdirty();
262 dirty(&asblk);
263 }
264 }
265 if (sblock->fs_maxsymlinklen != MAXSYMLINKLEN) {
266 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
267 sblock->fs_maxsymlinklen);
268 sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
269 if (preen)
270 printf(" (FIXED)\n");
271 if (preen || reply("FIX") == 1) {
272 sbdirty();
273 dirty(&asblk);
274 }
275 }
276 if (sblock->fs_qbmask != ~sblock->fs_bmask) {
277 pwarn("INCORRECT QBMASK=%llx IN SUPERBLOCK",
278 (unsigned long long)sblock->fs_qbmask);
279 sblock->fs_qbmask = ~sblock->fs_bmask;
280 if (preen)
281 printf(" (FIXED)\n");
282 if (preen || reply("FIX") == 1) {
283 sbdirty();
284 dirty(&asblk);
285 }
286 }
287 if (sblock->fs_qfmask != ~sblock->fs_fmask) {
288 pwarn("INCORRECT QFMASK=%llx IN SUPERBLOCK",
289 (unsigned long long)sblock->fs_qfmask);
290 sblock->fs_qfmask = ~sblock->fs_fmask;
291 if (preen)
292 printf(" (FIXED)\n");
293 if (preen || reply("FIX") == 1) {
294 sbdirty();
295 dirty(&asblk);
296 }
297 }
298 newinofmt = 1;
299 } else {
300 sblock->fs_qbmask = ~sblock->fs_bmask;
301 sblock->fs_qfmask = ~sblock->fs_fmask;
302 newinofmt = 0;
303 }
304 /*
305 * Convert to new inode format.
306 */
307 if (cvtlevel >= 2 && sblock->fs_inodefmt < FS_44INODEFMT) {
308 if (preen)
309 pwarn("CONVERTING TO NEW INODE FORMAT\n");
310 else if (!reply("CONVERT TO NEW INODE FORMAT"))
311 return(0);
312 doinglevel2++;
313 sblock->fs_inodefmt = FS_44INODEFMT;
314 sblock->fs_maxfilesize = maxfilesize;
315 sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
316 sblock->fs_qbmask = ~sblock->fs_bmask;
317 sblock->fs_qfmask = ~sblock->fs_fmask;
318 sbdirty();
319 dirty(&asblk);
320 }
321 /*
322 * Convert to new cylinder group format.
323 */
324 if (cvtlevel >= 1 && sblock->fs_postblformat == FS_42POSTBLFMT) {
325 if (preen)
326 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
327 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
328 return(0);
329 doinglevel1++;
330 sblock->fs_postblformat = FS_DYNAMICPOSTBLFMT;
331 sblock->fs_nrpos = 8;
332 sblock->fs_postbloff =
333 (char *)(&sblock->fs_opostbl[0][0]) -
334 (char *)(&sblock->fs_firstfield);
335 sblock->fs_rotbloff = &sblock->fs_space[0] -
336 (u_char *)(&sblock->fs_firstfield);
337 sblock->fs_cgsize =
338 fragroundup(sblock, CGSIZE(sblock));
339 sbdirty();
340 dirty(&asblk);
341 }
342 if (asblk.b_dirty && !bflag) {
343 memmove((struct fs*)sblk.b_un.b_fs, sblock, SBSIZE);
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 statemap = calloc((unsigned)(maxino + 1), sizeof(char));
389 if (statemap == NULL) {
390 printf("cannot alloc %u bytes for statemap\n",
391 (unsigned)(maxino + 1));
392 goto badsblabel;
393 }
394 typemap = calloc((unsigned)(maxino + 1), sizeof(char));
395 if (typemap == NULL) {
396 printf("cannot alloc %u bytes for typemap\n",
397 (unsigned)(maxino + 1));
398 goto badsblabel;
399 }
400 lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
401 if (lncntp == NULL) {
402 printf("cannot alloc %u bytes for lncntp\n",
403 (unsigned)((maxino + 1) * sizeof(int16_t)));
404 goto badsblabel;
405 }
406 /*
407 * cs_ndir may be inaccurate, particularly if we're using the -b
408 * option, so set a minimum to prevent bogus subdirectory reconnects
409 * and really inefficient directory scans.
410 * Also set a maximum in case the value is too large.
411 */
412 numdirs = sblock->fs_cstotal.cs_ndir;
413 if (numdirs < 1024)
414 numdirs = 1024;
415 if (numdirs > maxino + 1)
416 numdirs = maxino + 1;
417 inplast = 0;
418 listmax = numdirs + 10;
419 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
420 sizeof(struct inoinfo *));
421 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
422 sizeof(struct inoinfo *));
423 if (inpsort == NULL || inphead == NULL) {
424 printf("cannot alloc %u bytes for inphead\n",
425 (unsigned)(numdirs * sizeof(struct inoinfo *)));
426 goto badsblabel;
427 }
428 cgrp = malloc(sblock->fs_cgsize);
429 if (cgrp == NULL) {
430 printf("cannot alloc %u bytes for cylinder group\n",
431 sblock->fs_cgsize);
432 goto badsblabel;
433 }
434 bufinit();
435 if (sblock->fs_flags & FS_DOSOFTDEP)
436 usedsoftdep = 1;
437 else
438 usedsoftdep = 0;
439 return (1);
440
441 badsblabel:
442 markclean=0;
443 ckfini();
444 return (0);
445 }
446
447 /*
448 * Read in the super block and its summary info.
449 */
450 static int
451 readsb(listerr)
452 int listerr;
453 {
454 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
455 struct fs *fs;
456
457 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
458 return (0);
459 sblk.b_bno = super;
460 sblk.b_size = SBSIZE;
461
462 fs = sblk.b_un.b_fs;
463 /* auto detect byte order */
464 if( fs->fs_magic == FS_MAGIC) {
465 if (endian == 0 || BYTE_ORDER == endian) {
466 needswap = 0;
467 doswap = do_blkswap = do_dirswap = 0;
468 } else {
469 needswap = 1;
470 doswap = do_blkswap = do_dirswap = 1;
471 }
472 } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
473 if (endian == 0 || BYTE_ORDER != endian) {
474 needswap = 1;
475 doswap = do_blkswap = do_dirswap = 0;
476 } else {
477 needswap = 0;
478 doswap = do_blkswap = do_dirswap = 1;
479 }
480 } else {
481 badsb(listerr, "MAGIC NUMBER WRONG");
482 return (0);
483 }
484 if (doswap) {
485 if (preen)
486 errx(EEXIT, "incompatible options -B and -p");
487 if (nflag)
488 errx(EEXIT, "incompatible options -B and -n");
489 if (endian == LITTLE_ENDIAN) {
490 if (!reply("CONVERT TO LITTLE ENDIAN"))
491 return 0;
492 } else if (endian == BIG_ENDIAN) {
493 if (!reply("CONVERT TO BIG ENDIAN"))
494 return 0;
495 } else
496 pfatal("INTERNAL ERROR: unknown endian");
497 }
498 if (needswap)
499 printf("** Swapped byte order\n");
500 /* swap SB byte order if asked */
501 if (doswap)
502 ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
503
504 memmove(sblock, sblk.b_un.b_fs, SBSIZE);
505 if (needswap)
506 ffs_sb_swap(sblk.b_un.b_fs, sblock);
507
508 /*
509 * run a few consistency checks of the super block
510 */
511 if (sblock->fs_ncg < 1)
512 { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
513 if (sblock->fs_cpg < 1)
514 { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
515 if (sblock->fs_ncg * sblock->fs_cpg < sblock->fs_ncyl ||
516 (sblock->fs_ncg - 1) * sblock->fs_cpg >= sblock->fs_ncyl)
517 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
518 if (sblock->fs_sbsize > SBSIZE)
519 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
520 /*
521 * Compute block size that the filesystem is based on,
522 * according to fsbtodb, and adjust superblock block number
523 * so we can tell if this is an alternate later.
524 */
525 super *= dev_bsize;
526 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
527 sblk.b_bno = super / dev_bsize;
528
529 if (bflag) {
530 havesb = 1;
531 return (1);
532 }
533 /*
534 * Set all possible fields that could differ, then do check
535 * of whole super block against an alternate super block.
536 * When an alternate super-block is specified this check is skipped.
537 */
538 getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
539 if (asblk.b_errs)
540 return (0);
541 /* swap SB byte order if asked */
542 if (doswap)
543 ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
544
545 memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
546 if (needswap)
547 ffs_sb_swap(asblk.b_un.b_fs, altsblock);
548 if (cmpsblks(sblock, altsblock)) {
549 if (debug) {
550 long *nlp, *olp, *endlp;
551
552 printf("superblock mismatches\n");
553 nlp = (long *)altsblock;
554 olp = (long *)sblock;
555 endlp = olp + (sblock->fs_sbsize / sizeof *olp);
556 for ( ; olp < endlp; olp++, nlp++) {
557 if (*olp == *nlp)
558 continue;
559 printf("offset %ld, original %lx, alternate %lx\n",
560 (long)(olp - (long *)sblock), *olp, *nlp);
561 }
562 }
563 badsb(listerr,
564 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
565 return (0);
566 }
567 /* Now we know the SB is valid, we can write it back if needed */
568 if (doswap) {
569 sbdirty();
570 dirty(&asblk);
571 }
572 havesb = 1;
573 return (1);
574 }
575
576 int
577 cmpsblks(const struct fs *sb, struct fs *asb)
578 {
579
580 asb->fs_firstfield = sb->fs_firstfield;
581 asb->fs_unused_1 = sb->fs_unused_1;
582 asb->fs_time = sb->fs_time;
583 asb->fs_cstotal = sb->fs_cstotal;
584 asb->fs_cgrotor = sb->fs_cgrotor;
585 asb->fs_fmod = sb->fs_fmod;
586 asb->fs_clean = sb->fs_clean;
587 asb->fs_ronly = sb->fs_ronly;
588 asb->fs_flags = sb->fs_flags;
589 asb->fs_maxcontig = sb->fs_maxcontig;
590 asb->fs_minfree = sb->fs_minfree;
591 asb->fs_optim = sb->fs_optim;
592 asb->fs_rotdelay = sb->fs_rotdelay;
593 asb->fs_maxbpg = sb->fs_maxbpg;
594 memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
595 asb->fs_contigdirs = sb->fs_contigdirs;
596 asb->fs_csp = sb->fs_csp;
597 asb->fs_maxcluster = sb->fs_maxcluster;
598 memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
599 memmove(asb->fs_snapinum,
600 sb->fs_snapinum, sizeof sb->fs_snapinum);
601 asb->fs_avgfilesize = sb->fs_avgfilesize;
602 asb->fs_avgfpdir = sb->fs_avgfpdir;
603 memmove(asb->fs_sparecon,
604 sb->fs_sparecon, sizeof sb->fs_sparecon);
605 /*
606 * The following should not have to be copied.
607 */
608 asb->fs_fsbtodb = sb->fs_fsbtodb;
609 asb->fs_interleave = sb->fs_interleave;
610 asb->fs_npsect = sb->fs_npsect;
611 asb->fs_nrpos = sb->fs_nrpos;
612 asb->fs_qbmask = sb->fs_qbmask;
613 asb->fs_qfmask = sb->fs_qfmask;
614 asb->fs_state = sb->fs_state;
615 asb->fs_maxfilesize = sb->fs_maxfilesize;
616
617 return (memcmp(sb, asb, (int)sb->fs_sbsize));
618 }
619
620 static void
621 badsb(listerr, s)
622 int listerr;
623 char *s;
624 {
625
626 if (!listerr)
627 return;
628 if (preen)
629 printf("%s: ", cdevname());
630 pfatal("BAD SUPER BLOCK: %s\n", s);
631 }
632
633 /*
634 * Calculate a prototype superblock based on information in the disk label.
635 * When done the cgsblock macro can be calculated and the fs_ncg field
636 * can be used. Do NOT attempt to use other macros without verifying that
637 * their needed information is available!
638 */
639 static int
640 calcsb(dev, devfd, fs)
641 const char *dev;
642 int devfd;
643 struct fs *fs;
644 {
645 struct disklabel *lp;
646 struct partition *pp;
647 char *cp;
648 int i;
649
650 cp = strchr(dev, '\0') - 1;
651 if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
652 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
653 return (0);
654 }
655 lp = getdisklabel(dev, devfd);
656 if (isdigit(*cp))
657 pp = &lp->d_partitions[0];
658 else
659 pp = &lp->d_partitions[*cp - 'a'];
660 if (pp->p_fstype != FS_BSDFFS) {
661 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
662 dev, pp->p_fstype < FSMAXTYPES ?
663 fstypenames[pp->p_fstype] : "unknown");
664 return (0);
665 }
666 /* avoid divide by 0 */
667 if (pp->p_fsize == 0 || pp->p_frag == 0)
668 return (0);
669 memset(fs, 0, sizeof(struct fs));
670 fs->fs_fsize = pp->p_fsize;
671 fs->fs_frag = pp->p_frag;
672 fs->fs_cpg = pp->p_cpg;
673 fs->fs_size = pp->p_size;
674 fs->fs_ntrak = lp->d_ntracks;
675 fs->fs_nsect = lp->d_nsectors;
676 fs->fs_spc = lp->d_secpercyl;
677 fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
678 fs->fs_sblkno = roundup(
679 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
680 fs->fs_frag);
681 fs->fs_cgmask = 0xffffffff;
682 for (i = fs->fs_ntrak; i > 1; i >>= 1)
683 fs->fs_cgmask <<= 1;
684 if (!POWEROF2(fs->fs_ntrak))
685 fs->fs_cgmask <<= 1;
686 fs->fs_cgoffset = roundup(
687 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
688 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
689 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
690 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
691 fs->fs_fsbtodb++;
692 dev_bsize = lp->d_secsize;
693 return (1);
694 }
695
696 static struct disklabel *
697 getdisklabel(s, fd)
698 const char *s;
699 int fd;
700 {
701 static struct disklabel lab;
702
703 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
704 if (s == NULL)
705 return ((struct disklabel *)NULL);
706 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
707 errx(EEXIT, "%s: can't read disk label", s);
708 }
709 return (&lab);
710 }
711