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