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