setup.c revision 1.5 1 /* $NetBSD: setup.c,v 1.5 2000/05/23 01:48:55 perseant 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 /* #define DKTYPENAMES */
37 #define FSTYPENAMES
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42 #include <sys/disklabel.h>
43 #include <sys/file.h>
44
45 #include <ufs/ufs/dinode.h>
46 #include <sys/mount.h> /* XXX ufs/lfs/lfs.h should include this for
47 * us */
48 #include <ufs/lfs/lfs.h>
49 #include <ufs/lfs/lfs_extern.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57
58 #include "fsck.h"
59 #include "extern.h"
60 #include "fsutil.h"
61
62 struct bufarea asblk;
63 daddr_t *din_table;
64 SEGUSE *seg_table;
65 #define altsblock (*asblk.b_un.b_fs)
66 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
67
68 void badsb(int, char *);
69 int calcsb(const char *, int, struct lfs *);
70 static struct disklabel *getdisklabel(const char *, int);
71 static int readsb(int);
72 int lfs_maxino(void);
73
74 static daddr_t try_verify(struct lfs *, struct lfs *);
75
76 #ifdef DKTYPENAMES
77 int useless(void);
78
79 int
80 useless(void)
81 {
82 char **foo = (char **)dktypenames;
83 char **bar = (char **)fscknames;
84
85 return foo - bar;
86 }
87 #endif
88
89 static daddr_t
90 try_verify(struct lfs * osb, struct lfs * nsb)
91 {
92 daddr_t daddr;
93 SEGSUM *sp;
94 char summary[LFS_SUMMARY_SIZE];
95 int bc, flag;
96
97 daddr = osb->lfs_offset;
98 while (daddr != nsb->lfs_offset) {
99 flag = 0;
100 oncemore:
101 /* Read in summary block */
102 bread(fsreadfd, summary, daddr, LFS_SUMMARY_SIZE);
103 sp = (SEGSUM *)summary;
104
105 /*
106 * Could be a superblock instead of a segment summary.
107 * XXX should use gseguse, but right now we need to do more
108 * setup before we can...fix this
109 */
110 if (sp->ss_magic != SS_MAGIC ||
111 sp->ss_sumsum != cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE -
112 sizeof(sp->ss_sumsum))) {
113 if (flag == 0) {
114 daddr += LFS_SBPAD / dev_bsize;
115 goto oncemore;
116 }
117 return 0x0;
118 }
119 bc = check_summary(osb, sp, daddr);
120 if (bc == 0)
121 break;
122 daddr += (LFS_SUMMARY_SIZE + bc) / dev_bsize;
123 if (datosn(osb, daddr) != datosn(osb, daddr + (LFS_SUMMARY_SIZE + (1 << osb->lfs_bshift)) / dev_bsize)) {
124 daddr = ((SEGSUM *)summary)->ss_next;
125 }
126 }
127 return daddr;
128 }
129
130 int
131 setup(const char *dev)
132 {
133 long bmapsize;
134 struct disklabel *lp;
135 #if 0
136 long i;
137 off_t sizepb;
138 #endif
139 struct stat statb;
140 daddr_t daddr;
141 struct lfs proto;
142 int doskipclean;
143 u_int64_t maxfilesize;
144 struct lfs *sb0, *sb1, *osb, *nsb;
145 struct dinode *idinode;
146
147 havesb = 0;
148 fswritefd = -1;
149 doskipclean = skipclean;
150 if (stat(dev, &statb) < 0) {
151 printf("Can't stat %s: %s\n", dev, strerror(errno));
152 return (0);
153 }
154 if (!S_ISCHR(statb.st_mode)) {
155 pfatal("%s is not a character device", dev);
156 if (reply("CONTINUE") == 0)
157 return (0);
158 }
159 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
160 printf("Can't open %s: %s\n", dev, strerror(errno));
161 return (0);
162 }
163 if (preen == 0)
164 printf("** %s", dev);
165 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
166 fswritefd = -1;
167 if (preen)
168 pfatal("NO WRITE ACCESS");
169 printf(" (NO WRITE)");
170 }
171 if (preen == 0)
172 printf("\n");
173 fsmodified = 0;
174 lfdir = 0;
175 initbarea(&sblk);
176 initbarea(&asblk);
177 sblk.b_un.b_buf = malloc(LFS_SBPAD);
178 asblk.b_un.b_buf = malloc(LFS_SBPAD);
179 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
180 errexit("cannot allocate space for superblock\n");
181 if ((lp = getdisklabel((char *) NULL, fsreadfd)) != NULL)
182 dev_bsize = secsize = lp->d_secsize;
183 else
184 dev_bsize = secsize = DEV_BSIZE;
185
186 /*
187 * Read in the superblock, looking for alternates if necessary
188 */
189 if (readsb(1) == 0) {
190 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
191 return (0);
192 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
193 return (0);
194 #if 0 /* XXX find the LFS way to do this */
195 for (cg = 0; cg < proto.lfs_ncg; cg++) {
196 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
197 if (readsb(0) != 0)
198 break;
199 }
200 if (cg >= proto.lfs_ncg) {
201 printf("%s %s\n%s %s\n%s %s\n",
202 "SEARCH FOR ALTERNATE SUPER-BLOCK",
203 "FAILED. YOU MUST USE THE",
204 "-b OPTION TO FSCK_FFS TO SPECIFY THE",
205 "LOCATION OF AN ALTERNATE",
206 "SUPER-BLOCK TO SUPPLY NEEDED",
207 "INFORMATION; SEE fsck_ffs(8).");
208 return (0);
209 }
210 #else
211 pwarn("XXX Can't look for alternate superblocks yet\n");
212 return (0);
213 #endif
214 doskipclean = 0;
215 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
216 }
217 bufinit();
218
219 if (bflag == 0) {
220 /*
221 * Even if that superblock read in properly, it may not
222 * be guaranteed to point to a complete checkpoint.
223 * Read in the second superblock too, and take whichever
224 * of the two is *less* recent. --ks
225 */
226 sb0 = malloc(sizeof(*sb0));
227 sb1 = malloc(sizeof(*sb1));
228 memcpy(sb0, &sblock, sizeof(*sb0));
229 bflag = sblock.lfs_sboffs[1];
230 if (readsb(1) == 0) {
231 pwarn("COULDN'T READ ALT SUPERBLOCK AT BLK %d", bflag);
232 if (reply("ASSUME PRIMARY SUPERBLOCK IS GOOD") == 0) {
233 return (0);
234 } else {/* use primary as good */
235 memcpy(&sblock, sb0, sizeof(*sb0)); /* XXX cheating? */
236 }
237 } else {
238 memcpy(sb1, &sblock, sizeof(*sb1));
239 if (debug)
240 pwarn("sb0 %d, sb1 %d\n", sb0->lfs_tstamp, sblock.lfs_tstamp);
241 /*
242 * Verify the checkpoint of the newer superblock,
243 * if the timestamp of the two superblocks is
244 * different. XXX use lfs_offset instead, discover
245 * how to quickly discover "newness" based on that.
246 */
247 if (sb0->lfs_tstamp != sb1->lfs_tstamp) {
248 if (sb0->lfs_tstamp > sb1->lfs_tstamp) {
249 osb = sb1;
250 nsb = sb0;
251 } else {
252 osb = sb0;
253 nsb = sb1;
254 }
255 daddr = try_verify(osb, nsb);
256
257 if (debug)
258 printf("done.\n");
259 if (daddr == nsb->lfs_offset) {
260 pwarn("Checkpoint verified, recovered %d seconds of data\n", nsb->lfs_tstamp - osb->lfs_tstamp);
261 memcpy(&sblock, nsb, sizeof(*nsb));
262 sbdirty();
263 } else {
264 pwarn("Checkpoint invalid, lost %d seconds of data\n", nsb->lfs_tstamp - osb->lfs_tstamp);
265 memcpy(&sblock, osb, sizeof(*osb));
266 }
267 }
268 }
269 free(sb0);
270 free(sb1);
271 }
272 if (debug) {
273 printf("dev_bsize = %lu\n", dev_bsize);
274 printf("lfs_bsize = %lu\n", (unsigned long)sblock.lfs_bsize);
275 printf("lfs_fsize = %lu\n", (unsigned long)sblock.lfs_fsize);
276 printf("lfs_frag = %lu\n", (unsigned long)sblock.lfs_frag);
277 printf("INOPB(fs) = %lu\n", (unsigned long) INOPB(&sblock));
278 /* printf("fsbtodb(fs,1) = %lu\n",fsbtodb(&sblock,1)); */
279 }
280 #if 0 /* FFS-specific fs-clean check */
281 if (debug)
282 printf("clean = %d\n", sblock.lfs_clean);
283 if (sblock.lfs_clean & FS_ISCLEAN) {
284 if (doskipclean) {
285 pwarn("%sile system is clean; not checking\n",
286 preen ? "f" : "** F");
287 return (-1);
288 }
289 if (!preen)
290 pwarn("** File system is already clean\n");
291 }
292 maxino = sblock.lfs_ncg * sblock.lfs_ipg;
293 #else
294 #if 0
295 /* XXX - count the number of inodes here */
296 maxino = sblock.lfs_nfiles + 2;
297 #else
298 initbarea(&iblk);
299 iblk.b_un.b_buf = malloc(sblock.lfs_bsize);
300 if (bread(fsreadfd, (char *)iblk.b_un.b_buf,
301 sblock.lfs_idaddr,
302 (long)sblock.lfs_bsize) != 0) {
303 printf("Couldn't read disk block %d\n", sblock.lfs_idaddr);
304 exit(1);
305 }
306 idinode = lfs_difind(&sblock, sblock.lfs_ifile, &ifblock);
307 maxino = ((idinode->di_size
308 - (sblock.lfs_cleansz + sblock.lfs_segtabsz) * sblock.lfs_bsize)
309 / sblock.lfs_bsize) * sblock.lfs_ifpb - 1;
310 #endif
311 if (debug)
312 printf("maxino=%d\n", maxino);
313 din_table = (daddr_t *)malloc(maxino * sizeof(*din_table));
314 memset(din_table, 0, maxino * sizeof(*din_table));
315 seg_table = (SEGUSE *)malloc(sblock.lfs_nseg * sizeof(SEGUSE));
316 memset(seg_table, 0, sblock.lfs_nseg * sizeof(SEGUSE));
317 #endif
318 maxfsblock = sblock.lfs_size * (sblock.lfs_bsize / dev_bsize);
319 #if 0
320 sizepb = sblock.lfs_bsize;
321 maxfilesize = sblock.lfs_bsize * NDADDR - 1;
322 for (i = 0; i < NIADDR; i++) {
323 sizepb *= NINDIR(&sblock);
324 maxfilesize += sizepb;
325 }
326 maxfilesize++; /* XXX */
327 #else /* LFS way */
328 {
329 u_quad_t maxtable[] = {
330 /* 1 */ -1,
331 /* 2 */ -1,
332 /* 4 */ -1,
333 /* 8 */ -1,
334 /* 16 */ -1,
335 /* 32 */ -1,
336 /* 64 */ -1,
337 /* 128 */ -1,
338 /* 256 */ -1,
339 /* 512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128,
340 /* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256,
341 /* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512,
342 /* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024,
343 /* 8192 */ 1 << 31,
344 /* 16 K */ 1 << 31,
345 /* 32 K */ 1 << 31,
346 };
347 maxfilesize = maxtable[sblock.lfs_bshift] << sblock.lfs_bshift;
348 }
349 #endif
350 if ((sblock.lfs_minfree < 0 || sblock.lfs_minfree > 99)) {
351 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
352 sblock.lfs_minfree);
353 if (reply("SET TO DEFAULT") == 1) {
354 sblock.lfs_minfree = 10;
355 sbdirty();
356 }
357 }
358 /* XXX used to be ~(sblock.lfs_bsize - 1) */
359 if (sblock.lfs_bmask != sblock.lfs_bsize - 1) {
360 pwarn("INCORRECT BMASK=%x IN SUPERBLOCK (should be %x)",
361 (unsigned int)sblock.lfs_bmask,
362 (unsigned int)sblock.lfs_bsize - 1);
363 sblock.lfs_bmask = sblock.lfs_bsize - 1;
364 if (preen)
365 printf(" (FIXED)\n");
366 if (preen || reply("FIX") == 1) {
367 sbdirty();
368 dirty(&asblk);
369 }
370 }
371 #if 0 /* FFS-specific checks */
372 if (sblock.lfs_fmask != ~(sblock.lfs_fsize - 1)) {
373 pwarn("INCORRECT FMASK=%x IN SUPERBLOCK",
374 sblock.lfs_fmask);
375 sblock.lfs_fmask = ~(sblock.lfs_fsize - 1);
376 if (preen)
377 printf(" (FIXED)\n");
378 if (preen || reply("FIX") == 1) {
379 sbdirty();
380 dirty(&asblk);
381 }
382 }
383 #endif
384 if (sblock.lfs_maxfilesize != maxfilesize) {
385 pwarn("INCORRECT MAXFILESIZE=%qu IN SUPERBLOCK (should be %qu)",
386 (unsigned long long)sblock.lfs_maxfilesize,
387 (unsigned long long)maxfilesize);
388 sblock.lfs_maxfilesize = maxfilesize;
389 if (preen)
390 printf(" (FIXED)\n");
391 if (preen || reply("FIX") == 1) {
392 sbdirty();
393 dirty(&asblk);
394 }
395 }
396 if (sblock.lfs_maxsymlinklen != MAXSYMLINKLEN) {
397 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
398 sblock.lfs_maxsymlinklen);
399 sblock.lfs_maxsymlinklen = MAXSYMLINKLEN;
400 if (preen)
401 printf(" (FIXED)\n");
402 if (preen || reply("FIX") == 1) {
403 sbdirty();
404 dirty(&asblk);
405 }
406 }
407 newinofmt = 1;
408 /*
409 * allocate and initialize the necessary maps
410 */
411 #ifndef VERBOSE_BLOCKMAP
412 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
413 blockmap = malloc((unsigned)bmapsize * sizeof(char));
414 bzero(blockmap, bmapsize * sizeof(char));
415 #else
416 bmapsize = maxfsblock * sizeof(ino_t);
417 blockmap = (ino_t *)malloc(maxfsblock * sizeof(ino_t));
418 bzero(blockmap, maxfsblock * sizeof(ino_t));
419 #endif
420 if (blockmap == NULL) {
421 printf("cannot alloc %u bytes for blockmap\n",
422 (unsigned)bmapsize);
423 goto badsblabel;
424 }
425 statemap = calloc((unsigned)(maxino + 1), sizeof(char));
426 if (statemap == NULL) {
427 printf("cannot alloc %u bytes for statemap\n",
428 (unsigned)(maxino + 1));
429 goto badsblabel;
430 }
431 typemap = calloc((unsigned)(maxino + 1), sizeof(char));
432 if (typemap == NULL) {
433 printf("cannot alloc %u bytes for typemap\n",
434 (unsigned)(maxino + 1));
435 goto badsblabel;
436 }
437 lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
438 if (lncntp == NULL) {
439 printf("cannot alloc %lu bytes for lncntp\n",
440 (unsigned long)(maxino + 1) * sizeof(int16_t));
441 goto badsblabel;
442 }
443 return (1);
444
445 badsblabel:
446 ckfini(0);
447 return (0);
448 }
449
450 /*
451 * Read in the LFS super block and its summary info.
452 */
453 static int
454 readsb(int listerr)
455 {
456 daddr_t super = bflag ? bflag : LFS_LABELPAD / dev_bsize;
457 u_int32_t checksum;
458
459 if (bread(fsreadfd, (char *) &sblock, super, (long) LFS_SBPAD) != 0)
460 return (0);
461
462 sblk.b_bno = super;
463 sblk.b_size = LFS_SBPAD;
464 /*
465 * run a few consistency checks of the super block
466 */
467 if (sblock.lfs_magic != LFS_MAGIC) {
468 badsb(listerr, "MAGIC NUMBER WRONG");
469 return (0);
470 }
471 /* checksum */
472 checksum = lfs_sb_cksum(&(sblock.lfs_dlfs));
473 if (sblock.lfs_cksum != checksum) {
474 printf("Superblock checksum (%lu)does not match computed checksum %lu\n",
475 (unsigned long)sblock.lfs_cksum, (unsigned long) checksum);
476 }
477 #if 0 /* XXX - replace these checks with
478 * appropriate LFS sanity checks */
479 if (sblock.lfs_ncg < 1) {
480 badsb(listerr, "NCG OUT OF RANGE");
481 return (0);
482 }
483 if (sblock.lfs_cpg < 1) {
484 badsb(listerr, "CPG OUT OF RANGE");
485 return (0);
486 }
487 if (sblock.lfs_ncg * sblock.lfs_cpg < sblock.lfs_ncyl ||
488 (sblock.lfs_ncg - 1) * sblock.lfs_cpg >= sblock.lfs_ncyl) {
489 badsb(listerr, "NCYL LESS THAN NCG*CPG");
490 return (0);
491 }
492 if (sblock.lfs_sbsize > SBSIZE) {
493 badsb(listerr, "SIZE PREPOSTEROUSLY LARGE");
494 return (0);
495 }
496 #endif
497 /*
498 * Compute block size that the filesystem is based on,
499 * according to fsbtodb, and adjust superblock block number
500 * so we can tell if this is an alternate later.
501 */
502 super *= dev_bsize;
503 #if 0
504 dev_bsize = sblock.lfs_bsize / fsbtodb(&sblock, 1);
505 #endif
506 sblk.b_bno = super / dev_bsize;
507 if (bflag) {
508 havesb = 1;
509 return (1);
510 }
511 #if 0 /* XXX - for now skip the alt. superblock
512 * test as well */
513 /*
514 * Set all possible fields that could differ, then do check
515 * of whole super block against an alternate super block.
516 * When an alternate super-block is specified this check is skipped.
517 */
518 getblk(&asblk, cgsblock(&sblock, sblock.lfs_ncg - 1), sblock.lfs_sbsize);
519 if (asblk.b_errs)
520 return (0);
521 altsblock.lfs_firstfield = sblock.lfs_firstfield;
522 altsblock.lfs_fscktime = sblock.lfs_fscktime;
523 altsblock.lfs_time = sblock.lfs_time;
524 altsblock.lfs_cstotal = sblock.lfs_cstotal;
525 altsblock.lfs_cgrotor = sblock.lfs_cgrotor;
526 altsblock.lfs_fmod = sblock.lfs_fmod;
527 altsblock.lfs_clean = sblock.lfs_clean;
528 altsblock.lfs_ronly = sblock.lfs_ronly;
529 altsblock.lfs_flags = sblock.lfs_flags;
530 altsblock.lfs_maxcontig = sblock.lfs_maxcontig;
531 altsblock.lfs_minfree = sblock.lfs_minfree;
532 altsblock.lfs_optim = sblock.lfs_optim;
533 altsblock.lfs_rotdelay = sblock.lfs_rotdelay;
534 altsblock.lfs_maxbpg = sblock.lfs_maxbpg;
535 memcpy(altsblock.lfs_csp, sblock.lfs_csp,
536 sizeof sblock.lfs_csp);
537 altsblock.lfs_maxcluster = sblock.lfs_maxcluster;
538 memcpy(altsblock.lfs_fsmnt, sblock.lfs_fsmnt,
539 sizeof sblock.lfs_fsmnt);
540 memcpy(altsblock.lfs_sparecon, sblock.lfs_sparecon,
541 sizeof sblock.lfs_sparecon);
542 /*
543 * The following should not have to be copied.
544 */
545 altsblock.lfs_fsbtodb = sblock.lfs_fsbtodb;
546 altsblock.lfs_interleave = sblock.lfs_interleave;
547 altsblock.lfs_npsect = sblock.lfs_npsect;
548 altsblock.lfs_nrpos = sblock.lfs_nrpos;
549 altsblock.lfs_state = sblock.lfs_state;
550 altsblock.lfs_qbmask = sblock.lfs_qbmask;
551 altsblock.lfs_qfmask = sblock.lfs_qfmask;
552 altsblock.lfs_state = sblock.lfs_state;
553 altsblock.lfs_maxfilesize = sblock.lfs_maxfilesize;
554 if (memcmp(&sblock, &altsblock, (int)sblock.lfs_sbsize)) {
555 if (debug) {
556 long *nlp, *olp, *endlp;
557
558 printf("superblock mismatches\n");
559 nlp = (long *) &altsblock;
560 olp = (long *) &sblock;
561 endlp = olp + (sblock.lfs_sbsize / sizeof *olp);
562 for (; olp < endlp; olp++, nlp++) {
563 if (*olp == *nlp)
564 continue;
565 printf("offset %d, original %ld, alternate %ld\n",
566 olp - (long *) &sblock, *olp, *nlp);
567 }
568 }
569 badsb(listerr,
570 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
571 return (0);
572 }
573 #endif
574 havesb = 1;
575 return (1);
576 }
577
578 void
579 badsb(int listerr, char *s)
580 {
581
582 if (!listerr)
583 return;
584 if (preen)
585 printf("%s: ", cdevname());
586 pfatal("BAD SUPER BLOCK: %s\n", s);
587 }
588
589 /*
590 * Calculate a prototype superblock based on information in the disk label.
591 * When done the cgsblock macro can be calculated and the fs_ncg field
592 * can be used. Do NOT attempt to use other macros without verifying that
593 * their needed information is available!
594 */
595 int
596 calcsb(const char *dev, int devfd, struct lfs * fs)
597 {
598 register struct disklabel *lp;
599 register struct partition *pp;
600 register char *cp;
601 int i;
602
603 cp = strchr(dev, '\0') - 1;
604 if ((cp == (char *) -1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
605 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
606 return (0);
607 }
608 lp = getdisklabel(dev, devfd);
609 if (lp == NULL) {
610 dev_bsize = DEV_BSIZE;
611 } else {
612 if (isdigit(*cp))
613 pp = &lp->d_partitions[0];
614 else
615 pp = &lp->d_partitions[*cp - 'a'];
616 if (pp->p_fstype != FS_BSDLFS) {
617 pfatal("%s: NOT LABELED AS AN LFS FILE SYSTEM (%s)\n",
618 dev, pp->p_fstype < FSMAXTYPES ?
619 fstypenames[pp->p_fstype] : "unknown");
620 return (0);
621 }
622 memset(fs, 0, sizeof(struct lfs));
623 fs->lfs_fsize = pp->p_fsize;
624 fs->lfs_frag = pp->p_frag;
625 fs->lfs_size = pp->p_size;
626 fs->lfs_nspf = fs->lfs_fsize / lp->d_secsize;
627 dev_bsize = lp->d_secsize;
628 for (fs->lfs_fsbtodb = 0, i = fs->lfs_nspf; i > 1; i >>= 1)
629 fs->lfs_fsbtodb++;
630 }
631 return (1);
632 }
633
634 static struct disklabel *
635 getdisklabel(const char *s, int fd)
636 {
637 static struct disklabel lab;
638
639 if (ioctl(fd, DIOCGDINFO, (char *) &lab) < 0) {
640 if (s == NULL)
641 return ((struct disklabel *) NULL);
642 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
643 #if 0
644 errexit("%s: can't read disk label\n", s);
645 #else
646 return NULL;
647 #endif
648 }
649 return (&lab);
650 }
651