setup.c revision 1.43 1 /* $NetBSD: setup.c,v 1.43 2001/07/04 22:43:35 hubertf 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.43 2001/07/04 22:43:35 hubertf 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
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 (!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(SBSIZE);
125 sblock = malloc(SBSIZE);
126 asblk.b_un.b_buf = malloc(SBSIZE);
127 altsblock = malloc(SBSIZE);
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 ((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 || calcsb(dev, fsreadfd, &proto) == 0)
140 return(0);
141 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
142 return (0);
143 for (cg = 0; cg < proto.fs_ncg; cg++) {
144 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
145 if (readsb(0) != 0)
146 break;
147 }
148 if (cg >= proto.fs_ncg) {
149 printf("%s %s\n%s %s\n%s %s\n",
150 "SEARCH FOR ALTERNATE SUPER-BLOCK",
151 "FAILED. YOU MUST USE THE",
152 "-b OPTION TO fsck_ffs TO SPECIFY THE",
153 "LOCATION OF AN ALTERNATE",
154 "SUPER-BLOCK TO SUPPLY NEEDED",
155 "INFORMATION; SEE fsck_ffs(8).");
156 return(0);
157 }
158 doskipclean = 0;
159 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
160 }
161 if (debug)
162 printf("clean = %d\n", sblock->fs_clean);
163 if (doswap)
164 doskipclean = 0;
165 if (sblock->fs_clean & FS_ISCLEAN) {
166 if (doskipclean) {
167 pwarn("%sile system is clean; not checking\n",
168 preen ? "f" : "** F");
169 return (-1);
170 }
171 if (!preen && !doswap)
172 pwarn("** File system is already clean\n");
173 }
174 maxfsblock = sblock->fs_size;
175 maxino = sblock->fs_ncg * sblock->fs_ipg;
176 sizepb = sblock->fs_bsize;
177 maxfilesize = sblock->fs_bsize * NDADDR - 1;
178 for (i = 0; i < NIADDR; i++) {
179 sizepb *= NINDIR(sblock);
180 maxfilesize += sizepb;
181 }
182 /*
183 * Check and potentially fix certain fields in the super block.
184 */
185 if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
186 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
187 if (reply("SET TO DEFAULT") == 1) {
188 sblock->fs_optim = FS_OPTTIME;
189 sbdirty();
190 }
191 }
192 if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
193 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
194 sblock->fs_minfree);
195 if (reply("SET TO DEFAULT") == 1) {
196 sblock->fs_minfree = 10;
197 sbdirty();
198 }
199 }
200 if (sblock->fs_interleave < 1 ||
201 sblock->fs_interleave > sblock->fs_nsect) {
202 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
203 sblock->fs_interleave);
204 sblock->fs_interleave = 1;
205 if (preen)
206 printf(" (FIXED)\n");
207 if (preen || reply("SET TO DEFAULT") == 1) {
208 sbdirty();
209 dirty(&asblk);
210 }
211 }
212 if (sblock->fs_npsect < sblock->fs_nsect ||
213 sblock->fs_npsect > sblock->fs_nsect*2) {
214 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
215 sblock->fs_npsect);
216 sblock->fs_npsect = sblock->fs_nsect;
217 if (preen)
218 printf(" (FIXED)\n");
219 if (preen || reply("SET TO DEFAULT") == 1) {
220 sbdirty();
221 dirty(&asblk);
222 }
223 }
224 if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
225 pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
226 sblock->fs_bmask);
227 sblock->fs_bmask = ~(sblock->fs_bsize - 1);
228 if (preen)
229 printf(" (FIXED)\n");
230 if (preen || reply("FIX") == 1) {
231 sbdirty();
232 dirty(&asblk);
233 }
234 }
235 if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
236 pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
237 sblock->fs_fmask);
238 sblock->fs_fmask = ~(sblock->fs_fsize - 1);
239 if (preen)
240 printf(" (FIXED)\n");
241 if (preen || reply("FIX") == 1) {
242 sbdirty();
243 dirty(&asblk);
244 }
245 }
246 if (sblock->fs_inodefmt >= FS_44INODEFMT) {
247 if (sblock->fs_maxfilesize != maxfilesize) {
248 pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
249 (unsigned long long)sblock->fs_maxfilesize);
250 sblock->fs_maxfilesize = maxfilesize;
251 if (preen)
252 printf(" (FIXED)\n");
253 if (preen || reply("FIX") == 1) {
254 sbdirty();
255 dirty(&asblk);
256 }
257 }
258 if (sblock->fs_maxsymlinklen != MAXSYMLINKLEN) {
259 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
260 sblock->fs_maxsymlinklen);
261 sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
262 if (preen)
263 printf(" (FIXED)\n");
264 if (preen || reply("FIX") == 1) {
265 sbdirty();
266 dirty(&asblk);
267 }
268 }
269 if (sblock->fs_qbmask != ~sblock->fs_bmask) {
270 pwarn("INCORRECT QBMASK=%llx IN SUPERBLOCK",
271 (unsigned long long)sblock->fs_qbmask);
272 sblock->fs_qbmask = ~sblock->fs_bmask;
273 if (preen)
274 printf(" (FIXED)\n");
275 if (preen || reply("FIX") == 1) {
276 sbdirty();
277 dirty(&asblk);
278 }
279 }
280 if (sblock->fs_qfmask != ~sblock->fs_fmask) {
281 pwarn("INCORRECT QFMASK=%llx IN SUPERBLOCK",
282 (unsigned long long)sblock->fs_qfmask);
283 sblock->fs_qfmask = ~sblock->fs_fmask;
284 if (preen)
285 printf(" (FIXED)\n");
286 if (preen || reply("FIX") == 1) {
287 sbdirty();
288 dirty(&asblk);
289 }
290 }
291 newinofmt = 1;
292 } else {
293 sblock->fs_qbmask = ~sblock->fs_bmask;
294 sblock->fs_qfmask = ~sblock->fs_fmask;
295 newinofmt = 0;
296 }
297 /*
298 * Convert to new inode format.
299 */
300 if (cvtlevel >= 2 && sblock->fs_inodefmt < FS_44INODEFMT) {
301 if (preen)
302 pwarn("CONVERTING TO NEW INODE FORMAT\n");
303 else if (!reply("CONVERT TO NEW INODE FORMAT"))
304 return(0);
305 doinglevel2++;
306 sblock->fs_inodefmt = FS_44INODEFMT;
307 sblock->fs_maxfilesize = maxfilesize;
308 sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
309 sblock->fs_qbmask = ~sblock->fs_bmask;
310 sblock->fs_qfmask = ~sblock->fs_fmask;
311 sbdirty();
312 dirty(&asblk);
313 }
314 /*
315 * Convert to new cylinder group format.
316 */
317 if (cvtlevel >= 1 && sblock->fs_postblformat == FS_42POSTBLFMT) {
318 if (preen)
319 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
320 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
321 return(0);
322 doinglevel1++;
323 sblock->fs_postblformat = FS_DYNAMICPOSTBLFMT;
324 sblock->fs_nrpos = 8;
325 sblock->fs_postbloff =
326 (char *)(&sblock->fs_opostbl[0][0]) -
327 (char *)(&sblock->fs_firstfield);
328 sblock->fs_rotbloff = &sblock->fs_space[0] -
329 (u_char *)(&sblock->fs_firstfield);
330 sblock->fs_cgsize =
331 fragroundup(sblock, CGSIZE(sblock));
332 sbdirty();
333 dirty(&asblk);
334 }
335 if (asblk.b_dirty && !bflag) {
336 memmove((struct fs*)sblk.b_un.b_fs, sblock, SBSIZE);
337 if (needswap)
338 ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs, 1);
339 memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
340 flush(fswritefd, &asblk);
341 }
342 /*
343 * read in the summary info.
344 */
345 asked = 0;
346 for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
347 size = sblock->fs_cssize - i < sblock->fs_bsize ?
348 sblock->fs_cssize - i : sblock->fs_bsize;
349 sblock->fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
350 if (bread(fsreadfd, (char *)sblock->fs_csp[j],
351 fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
352 size) != 0 && !asked) {
353 pfatal("BAD SUMMARY INFORMATION");
354 if (reply("CONTINUE") == 0) {
355 markclean = 0;
356 exit(EEXIT);
357 }
358 asked++;
359 }
360 /*
361 * The following assumes that struct csum is made of
362 * u_int32_t
363 */
364 if (doswap) {
365 int k;
366 u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
367
368 for (k = 0; k < size / sizeof(u_int32_t); k++)
369 cd[k] = bswap32(cd[k]);
370 bwrite(fswritefd, (char *)sblock->fs_csp[j],
371 fsbtodb(sblock,
372 sblock->fs_csaddr + j * sblock->fs_frag),
373 size);
374 }
375 if (needswap) {
376 int k;
377 u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
378
379 for (k = 0; k < size / sizeof(u_int32_t); k++)
380 cd[k] = bswap32(cd[k]);
381 }
382 }
383 /*
384 * allocate and initialize the necessary maps
385 */
386 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
387 blockmap = calloc((unsigned)bmapsize, sizeof (char));
388 if (blockmap == NULL) {
389 printf("cannot alloc %u bytes for blockmap\n",
390 (unsigned)bmapsize);
391 goto badsblabel;
392 }
393 statemap = calloc((unsigned)(maxino + 1), sizeof(char));
394 if (statemap == NULL) {
395 printf("cannot alloc %u bytes for statemap\n",
396 (unsigned)(maxino + 1));
397 goto badsblabel;
398 }
399 typemap = calloc((unsigned)(maxino + 1), sizeof(char));
400 if (typemap == NULL) {
401 printf("cannot alloc %u bytes for typemap\n",
402 (unsigned)(maxino + 1));
403 goto badsblabel;
404 }
405 lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
406 if (lncntp == NULL) {
407 printf("cannot alloc %u bytes for lncntp\n",
408 (unsigned)((maxino + 1) * sizeof(int16_t)));
409 goto badsblabel;
410 }
411 /*
412 * cs_ndir may be inaccurate, particularly if we're using the -b
413 * option, so set a minimum to prevent bogus subdirectory reconnects
414 * and really inefficient directory scans.
415 * Also set a maximum in case the value is too large.
416 */
417 numdirs = sblock->fs_cstotal.cs_ndir;
418 if (numdirs < 1024)
419 numdirs = 1024;
420 if (numdirs > maxino + 1)
421 numdirs = maxino + 1;
422 inplast = 0;
423 listmax = numdirs + 10;
424 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
425 sizeof(struct inoinfo *));
426 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
427 sizeof(struct inoinfo *));
428 if (inpsort == NULL || inphead == NULL) {
429 printf("cannot alloc %u bytes for inphead\n",
430 (unsigned)(numdirs * sizeof(struct inoinfo *)));
431 goto badsblabel;
432 }
433 cgrp = malloc(sblock->fs_cgsize);
434 if (cgrp == NULL) {
435 printf("cannot alloc %u bytes for cylinder group\n",
436 sblock->fs_cgsize);
437 goto badsblabel;
438 }
439 bufinit();
440 if (sblock->fs_flags & FS_DOSOFTDEP)
441 usedsoftdep = 1;
442 else
443 usedsoftdep = 0;
444 return (1);
445
446 badsblabel:
447 markclean=0;
448 ckfini();
449 return (0);
450 }
451
452 /*
453 * Read in the super block and its summary info.
454 */
455 static int
456 readsb(listerr)
457 int listerr;
458 {
459 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
460 struct fs *fs;
461
462 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
463 return (0);
464 sblk.b_bno = super;
465 sblk.b_size = SBSIZE;
466
467 fs = sblk.b_un.b_fs;
468 /* auto detect byte order */
469 if( fs->fs_magic == FS_MAGIC) {
470 if (endian == 0 || BYTE_ORDER == endian) {
471 needswap = 0;
472 doswap = do_blkswap = do_dirswap = 0;
473 } else {
474 needswap = 1;
475 doswap = do_blkswap = do_dirswap = 1;
476 }
477 } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
478 if (endian == 0 || BYTE_ORDER != endian) {
479 needswap = 1;
480 doswap = do_blkswap = do_dirswap = 0;
481 } else {
482 needswap = 0;
483 doswap = do_blkswap = do_dirswap = 1;
484 }
485 } else {
486 badsb(listerr, "MAGIC NUMBER WRONG");
487 return (0);
488 }
489 if (doswap) {
490 if (preen)
491 errx(EEXIT, "incompatible options -B and -p");
492 if (nflag)
493 errx(EEXIT, "incompatible options -B and -n");
494 if (endian == LITTLE_ENDIAN) {
495 if (!reply("CONVERT TO LITTLE ENDIAN"))
496 return 0;
497 } else if (endian == BIG_ENDIAN) {
498 if (!reply("CONVERT TO BIG ENDIAN"))
499 return 0;
500 } else
501 pfatal("INTERNAL ERROR: unknown endian");
502 }
503 if (needswap)
504 printf("** Swapped byte order\n");
505 /* swap SB byte order if asked */
506 if (doswap)
507 ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs, needswap);
508
509 memmove(sblock, sblk.b_un.b_fs, SBSIZE);
510 if (needswap)
511 ffs_sb_swap(sblk.b_un.b_fs, sblock, 0);
512
513 /*
514 * run a few consistency checks of the super block
515 */
516 if (sblock->fs_ncg < 1)
517 { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
518 if (sblock->fs_cpg < 1)
519 { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
520 if (sblock->fs_ncg * sblock->fs_cpg < sblock->fs_ncyl ||
521 (sblock->fs_ncg - 1) * sblock->fs_cpg >= sblock->fs_ncyl)
522 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
523 if (sblock->fs_sbsize > SBSIZE)
524 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
525 /*
526 * Compute block size that the filesystem is based on,
527 * according to fsbtodb, and adjust superblock block number
528 * so we can tell if this is an alternate later.
529 */
530 super *= dev_bsize;
531 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
532 sblk.b_bno = super / dev_bsize;
533
534 if (bflag) {
535 havesb = 1;
536 return (1);
537 }
538 /*
539 * Set all possible fields that could differ, then do check
540 * of whole super block against an alternate super block.
541 * When an alternate super-block is specified this check is skipped.
542 */
543 getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
544 if (asblk.b_errs)
545 return (0);
546 /* swap SB byte order if asked */
547 if (doswap)
548 ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs, needswap);
549
550 memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
551 if (needswap)
552 ffs_sb_swap(asblk.b_un.b_fs, altsblock, 0);
553 if (cmpsblks(sblock, altsblock)) {
554 if (debug) {
555 long *nlp, *olp, *endlp;
556
557 printf("superblock mismatches\n");
558 nlp = (long *)altsblock;
559 olp = (long *)sblock;
560 endlp = olp + (sblock->fs_sbsize / sizeof *olp);
561 for ( ; olp < endlp; olp++, nlp++) {
562 if (*olp == *nlp)
563 continue;
564 printf("offset %ld, original %lx, alternate %lx\n",
565 (long)(olp - (long *)sblock), *olp, *nlp);
566 }
567 }
568 badsb(listerr,
569 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
570 return (0);
571 }
572 /* Now we know the SB is valid, we can write it back if needed */
573 if (doswap) {
574 sbdirty();
575 dirty(&asblk);
576 }
577 havesb = 1;
578 return (1);
579 }
580
581 int
582 cmpsblks(const struct fs *sb, struct fs *asb)
583 {
584
585 asb->fs_firstfield = sb->fs_firstfield;
586 asb->fs_fscktime = sb->fs_fscktime;
587 asb->fs_unused_1 = sb->fs_unused_1;
588 asb->fs_time = sb->fs_time;
589 asb->fs_cstotal = sb->fs_cstotal;
590 asb->fs_cgrotor = sb->fs_cgrotor;
591 asb->fs_fmod = sb->fs_fmod;
592 asb->fs_clean = sb->fs_clean;
593 asb->fs_ronly = sb->fs_ronly;
594 asb->fs_flags = sb->fs_flags;
595 asb->fs_maxcontig = sb->fs_maxcontig;
596 asb->fs_minfree = sb->fs_minfree;
597 asb->fs_optim = sb->fs_optim;
598 asb->fs_rotdelay = sb->fs_rotdelay;
599 asb->fs_maxbpg = sb->fs_maxbpg;
600 memmove(asb->fs_csp, sb->fs_csp, sizeof sb->fs_csp);
601 asb->fs_maxcluster = sb->fs_maxcluster;
602 memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
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_state = sb->fs_state;
613 asb->fs_qbmask = sb->fs_qbmask;
614 asb->fs_qfmask = sb->fs_qfmask;
615 asb->fs_state = sb->fs_state;
616 asb->fs_maxfilesize = sb->fs_maxfilesize;
617
618 return (memcmp(sb, asb, (int)sb->fs_sbsize));
619 }
620
621 static void
622 badsb(listerr, s)
623 int listerr;
624 char *s;
625 {
626
627 if (!listerr)
628 return;
629 if (preen)
630 printf("%s: ", cdevname());
631 pfatal("BAD SUPER BLOCK: %s\n", s);
632 }
633
634 /*
635 * Calculate a prototype superblock based on information in the disk label.
636 * When done the cgsblock macro can be calculated and the fs_ncg field
637 * can be used. Do NOT attempt to use other macros without verifying that
638 * their needed information is available!
639 */
640 static int
641 calcsb(dev, devfd, fs)
642 const char *dev;
643 int devfd;
644 struct fs *fs;
645 {
646 struct disklabel *lp;
647 struct partition *pp;
648 char *cp;
649 int i;
650
651 cp = strchr(dev, '\0') - 1;
652 if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
653 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
654 return (0);
655 }
656 lp = getdisklabel(dev, devfd);
657 if (isdigit(*cp))
658 pp = &lp->d_partitions[0];
659 else
660 pp = &lp->d_partitions[*cp - 'a'];
661 if (pp->p_fstype != FS_BSDFFS) {
662 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
663 dev, pp->p_fstype < FSMAXTYPES ?
664 fstypenames[pp->p_fstype] : "unknown");
665 return (0);
666 }
667 /* avoid divide by 0 */
668 if (pp->p_fsize == 0 || pp->p_frag == 0)
669 return (0);
670 memset(fs, 0, sizeof(struct fs));
671 fs->fs_fsize = pp->p_fsize;
672 fs->fs_frag = pp->p_frag;
673 fs->fs_cpg = pp->p_cpg;
674 fs->fs_size = pp->p_size;
675 fs->fs_ntrak = lp->d_ntracks;
676 fs->fs_nsect = lp->d_nsectors;
677 fs->fs_spc = lp->d_secpercyl;
678 fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
679 fs->fs_sblkno = roundup(
680 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
681 fs->fs_frag);
682 fs->fs_cgmask = 0xffffffff;
683 for (i = fs->fs_ntrak; i > 1; i >>= 1)
684 fs->fs_cgmask <<= 1;
685 if (!POWEROF2(fs->fs_ntrak))
686 fs->fs_cgmask <<= 1;
687 fs->fs_cgoffset = roundup(
688 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
689 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
690 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
691 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
692 fs->fs_fsbtodb++;
693 dev_bsize = lp->d_secsize;
694 return (1);
695 }
696
697 static struct disklabel *
698 getdisklabel(s, fd)
699 const char *s;
700 int fd;
701 {
702 static struct disklabel lab;
703
704 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
705 if (s == NULL)
706 return ((struct disklabel *)NULL);
707 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
708 errx(EEXIT, "%s: can't read disk label", s);
709 }
710 return (&lab);
711 }
712