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