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