pass0.c revision 1.7 1 /* $NetBSD: pass0.c,v 1.7 2000/06/14 18:43:59 perseant Exp $ */
2
3 /*
4 * Copyright (c) 1998 Konrad E. Schroder.
5 * Copyright (c) 1980, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <ufs/ufs/dinode.h>
40 #include <ufs/ufs/dir.h>
41 #include <sys/mount.h>
42 #include <ufs/lfs/lfs.h>
43 #include <ufs/lfs/lfs_extern.h>
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "fsck.h"
50 #include "extern.h"
51 #include "fsutil.h"
52
53 /* Flags for check_segment */
54 #define CKSEG_VERBOSE 1
55 #define CKSEG_IGNORECLEAN 2
56
57 extern int fake_cleanseg;
58 void
59 check_segment(int, int, daddr_t, struct lfs *, int,
60 int (*)(struct lfs *, SEGSUM *, daddr_t));
61
62 /*
63 * Pass 0. Check the LFS partial segments for valid checksums, correcting
64 * if necessary. Also check for valid offsets for inode and finfo blocks.
65 */
66 /*
67 * XXX more could be done here---consistency between inode-held blocks and
68 * finfo blocks, for one thing.
69 */
70
71 #define dbshift (sblock.lfs_bshift - sblock.lfs_fsbtodb)
72
73 void
74 pass0()
75 {
76 daddr_t daddr;
77 IFILE *ifp;
78 struct bufarea *bp;
79 ino_t ino, lastino, nextino, *visited;
80
81 /*
82 * Check the inode free list for inuse inodes, and cycles.
83 * Make sure that all free inodes are in fact on the list.
84 */
85 visited = (ino_t *)malloc(maxino * sizeof(ino_t));
86 memset(visited, 0, maxino * sizeof(ino_t));
87
88 lastino = 0;
89 ino = sblock.lfs_free;
90 while (ino) {
91 if (ino >= maxino) {
92 printf("! Ino %d out of range (last was %d)\n", ino,
93 lastino);
94 break;
95 }
96 if (visited[ino]) {
97 pwarn("! Ino %d already found on the free list!\n",
98 ino);
99 if (preen || reply("FIX") == 1) {
100 /* lastino can't be zero */
101 ifp = lfs_ientry(lastino, &bp);
102 ifp->if_nextfree = 0;
103 dirty(bp);
104 bp->b_flags &= ~B_INUSE;
105 }
106 break;
107 }
108 ++visited[ino];
109 ifp = lfs_ientry(ino, &bp);
110 nextino = ifp->if_nextfree;
111 daddr = ifp->if_daddr;
112 bp->b_flags &= ~B_INUSE;
113 if (daddr) {
114 pwarn("! Ino %d with daddr 0x%x is on the free list!\n",
115 ino, daddr);
116 if (preen || reply("FIX") == 1) {
117 if (lastino == 0) {
118 sblock.lfs_free = nextino;
119 sbdirty();
120 } else {
121 ifp = lfs_ientry(lastino, &bp);
122 ifp->if_nextfree = nextino;
123 dirty(bp);
124 bp->b_flags &= ~B_INUSE;
125 }
126 ino = nextino;
127 continue;
128 }
129 }
130 lastino = ino;
131 ino = nextino;
132 }
133 /*
134 * Make sure all free inodes were found on the list
135 */
136 for (ino = ROOTINO+1; ino < maxino; ++ino) {
137 if (visited[ino])
138 continue;
139
140 ifp = lfs_ientry(ino, &bp);
141 if (ifp->if_daddr) {
142 bp->b_flags &= ~B_INUSE;
143 continue;
144 }
145
146 pwarn("! Ino %d free, but not on the free list\n", ino);
147 if (preen || reply("FIX") == 1) {
148 ifp->if_nextfree = sblock.lfs_free;
149 sblock.lfs_free = ino;
150 sbdirty();
151 dirty(bp);
152 }
153 bp->b_flags &= ~B_INUSE;
154 }
155 }
156
157 static void
158 dump_segsum(SEGSUM * sump, daddr_t addr)
159 {
160 printf("Dump partial summary block 0x%x\n", addr);
161 printf("\tsumsum: %x (%d)\n", sump->ss_sumsum, sump->ss_sumsum);
162 printf("\tdatasum: %x (%d)\n", sump->ss_datasum, sump->ss_datasum);
163 printf("\tnext: %x (%d)\n", sump->ss_next, sump->ss_next);
164 printf("\tcreate: %x (%d)\n", sump->ss_create, sump->ss_create);
165 printf("\tnfinfo: %x (%d)\n", sump->ss_nfinfo, sump->ss_nfinfo);
166 printf("\tninos: %x (%d)\n", sump->ss_ninos, sump->ss_ninos);
167 printf("\tflags: %c%c\n",
168 sump->ss_flags & SS_DIROP ? 'd' : '-',
169 sump->ss_flags & SS_CONT ? 'c' : '-');
170 }
171
172 void
173 check_segment(int fd, int segnum, daddr_t addr, struct lfs * fs, int flags, int (*func)(struct lfs *, SEGSUM *, daddr_t))
174 {
175 struct lfs *sbp;
176 SEGSUM *sump = NULL;
177 SEGUSE *su;
178 struct bufarea *bp = NULL;
179 int psegnum = 0, ninos = 0;
180 off_t sum_offset, db_ssize;
181 int bc, su_flags, su_nsums, su_ninos;
182
183 db_ssize = sblock.lfs_ssize << sblock.lfs_fsbtodb;
184
185 su = lfs_gseguse(segnum, &bp);
186 su_flags = su->su_flags;
187 su_nsums = su->su_nsums;
188 su_ninos = su->su_ninos;
189 bp->b_flags &= ~B_INUSE;
190
191 /* printf("Seg at 0x%x\n",addr); */
192 if ((flags & CKSEG_VERBOSE) && segnum * db_ssize + fs->lfs_sboffs[0] != addr)
193 pwarn("WARNING: segment begins at 0x%qx, should be 0x%qx\n",
194 (long long unsigned)addr,
195 (long long unsigned)(segnum * db_ssize + fs->lfs_sboffs[0]));
196 sum_offset = ((off_t)addr << dbshift);
197
198 /* If this segment should have a superblock, look for one */
199 if (su_flags & SEGUSE_SUPERBLOCK) {
200 bp = getddblk(sum_offset >> dbshift, LFS_SBPAD);
201 sum_offset += LFS_SBPAD;
202
203 /* check for a superblock -- XXX this is crude */
204 sbp = (struct lfs *)(bp->b_un.b_buf);
205 if (sbp->lfs_magic == LFS_MAGIC) {
206 #if 0
207 if (sblock.lfs_tstamp == sbp->lfs_tstamp &&
208 memcmp(sbp, &sblock, sizeof(*sbp)) &&
209 (flags & CKSEG_VERBOSE))
210 pwarn("SUPERBLOCK MISMATCH SEGMENT %d\n", segnum);
211 #endif
212 } else {
213 if (flags & CKSEG_VERBOSE)
214 pwarn("SEGMENT %d SUPERBLOCK INVALID\n", segnum);
215 /* XXX allow to fix */
216 }
217 bp->b_flags &= ~B_INUSE;
218 }
219 /* XXX need to also check whether this one *should* be dirty */
220 if ((flags & CKSEG_IGNORECLEAN) && (su_flags & SEGUSE_DIRTY) == 0)
221 return;
222
223 while (1) {
224 if (su_nsums <= psegnum)
225 break;
226 bp = getddblk(sum_offset >> dbshift, LFS_SUMMARY_SIZE);
227 sump = (SEGSUM *)(bp->b_un.b_buf);
228 if (sump->ss_magic != SS_MAGIC) {
229 if (flags & CKSEG_VERBOSE)
230 printf("PARTIAL SEGMENT %d SEGMENT %d BAD PARTIAL SEGMENT MAGIC (0x%x should be 0x%x)\n",
231 psegnum, segnum, sump->ss_magic, SS_MAGIC);
232 bp->b_flags &= ~B_INUSE;
233 break;
234 }
235 if (sump->ss_sumsum != cksum(&sump->ss_datasum, LFS_SUMMARY_SIZE - sizeof(sump->ss_sumsum))) {
236 if (flags & CKSEG_VERBOSE) {
237 /* Corrupt partial segment */
238 pwarn("CORRUPT PARTIAL SEGMENT %d/%d OF SEGMENT %d AT BLK 0x%qx",
239 psegnum, su_nsums, segnum,
240 (unsigned long long)sum_offset >> dbshift);
241 if (db_ssize < (sum_offset >> dbshift) - addr)
242 pwarn(" (+0x%qx/0x%qx)",
243 (unsigned long long)(((sum_offset >> dbshift) - addr) -
244 db_ssize),
245 (unsigned long long)db_ssize);
246 else
247 pwarn(" (-0x%qx/0x%qx)",
248 (unsigned long long)(db_ssize -
249 ((sum_offset >> dbshift) - addr)),
250 (unsigned long long)db_ssize);
251 pwarn("\n");
252 dump_segsum(sump, sum_offset >> dbshift);
253 }
254 /* XXX fix it maybe */
255 bp->b_flags &= ~B_INUSE;
256 break; /* XXX could be throwing away data, but if
257 * this segsum is invalid, how to know where
258 * the next summary begins? */
259 }
260 /*
261 * Good partial segment
262 */
263 bc = (*func)(&sblock, sump, (daddr_t)(sum_offset >> dbshift));
264 if (bc) {
265 sum_offset += LFS_SUMMARY_SIZE + bc;
266 ninos += (sump->ss_ninos + INOPB(&sblock) - 1) / INOPB(&sblock);
267 psegnum++;
268 } else {
269 bp->b_flags &= ~B_INUSE;
270 break;
271 }
272 bp->b_flags &= ~B_INUSE;
273 }
274 if (flags & CKSEG_VERBOSE) {
275 if (ninos != su_ninos)
276 pwarn("SEGMENT %d has %d ninos, not %d\n",
277 segnum, ninos, su_ninos);
278 if (psegnum != su_nsums)
279 pwarn("SEGMENT %d has %d summaries, not %d\n",
280 segnum, psegnum, su_nsums);
281 }
282 return;
283 }
284
285 int
286 check_summary(struct lfs * fs, SEGSUM * sp, daddr_t pseg_addr)
287 {
288 FINFO *fp;
289 int bc; /* Bytes in partial segment */
290 int nblocks;
291 daddr_t seg_addr, *dp, *idp, daddr;
292 struct bufarea *bp;
293 int i, j, k, datac, len;
294 long sn;
295 u_long *datap;
296 u_int32_t ccksum;
297
298 sn = datosn(fs, pseg_addr);
299 seg_addr = sntoda(fs, sn);
300
301 /*
302 * printf("Pseg at 0x%x, %d inos, %d
303 * finfos\n",addr>>dbshift,sp->ss_ninos,sp->ss_nfinfo);
304 */
305 /* We've already checked the sumsum, just do the data bounds and sum */
306
307 /* 1. Count the blocks. */
308 nblocks = ((sp->ss_ninos + INOPB(fs) - 1) / INOPB(fs));
309 bc = nblocks << fs->lfs_bshift;
310
311 fp = (FINFO *)(sp + 1);
312 for (i = 0; i < sp->ss_nfinfo; i++) {
313 nblocks += fp->fi_nblocks;
314 bc += fp->fi_lastlength + ((fp->fi_nblocks - 1) << fs->lfs_bshift);
315 fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
316 }
317 datap = (u_long *)malloc(nblocks * sizeof(*datap));
318 datac = 0;
319
320 dp = (daddr_t *)sp;
321 dp += LFS_SUMMARY_SIZE / sizeof(daddr_t);
322 dp--;
323
324 idp = dp;
325 daddr = pseg_addr + (LFS_SUMMARY_SIZE / dev_bsize);
326 fp = (FINFO *)(sp + 1);
327 for (i = 0, j = 0; i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
328 /* printf("*idp=%x, daddr=%x\n", *idp, daddr); */
329 if (i >= sp->ss_nfinfo && *idp != daddr) {
330 pwarn("Not enough inode blocks in pseg at 0x%x: found %d, wanted %d\n",
331 pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
332 pwarn("*idp=%x, daddr=%x\n", *idp, daddr);
333 break;
334 }
335 while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
336 bp = getddblk(daddr, (1 << fs->lfs_bshift));
337 datap[datac++] = ((u_long *)(bp->b_un.b_buf))[0];
338 bp->b_flags &= ~B_INUSE;
339
340 ++j;
341 daddr += (1 << fs->lfs_bshift) / dev_bsize;
342 --idp;
343 }
344 if (i < sp->ss_nfinfo) {
345 for (k = 0; k < fp->fi_nblocks; k++) {
346 len = (k == fp->fi_nblocks - 1 ? fp->fi_lastlength
347 : (1 << fs->lfs_bshift));
348 bp = getddblk(daddr, len);
349 datap[datac++] = ((u_long *)(bp->b_un.b_buf))[0];
350 bp->b_flags &= ~B_INUSE;
351 daddr += len / dev_bsize;
352 }
353 fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
354 }
355 }
356
357 if (datac != nblocks) {
358 pwarn("Partial segment at 0x%x expected %d blocks counted %d\n",
359 pseg_addr, nblocks, datac);
360 }
361 ccksum = cksum(datap, nblocks * sizeof(u_long));
362 /* Check the data checksum */
363 if (ccksum != sp->ss_datasum) {
364 pwarn("Partial segment at 0x%x data checksum mismatch: got 0x%x, expected 0x%x\n",
365 pseg_addr, sp->ss_datasum, ccksum);
366 /* return 0; */
367 }
368 return bc;
369 }
370