pass0.c revision 1.6 1 /* $NetBSD: pass0.c,v 1.6 2000/05/30 04:33:14 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 + 1) * sizeof(ino_t));
86 memset(visited, 0, (maxino + 1) * sizeof(ino_t));
87
88 lastino = 0;
89 ino = sblock.lfs_free;
90 while (ino) {
91 if (visited[ino]) {
92 pwarn("! Ino %d already found on the free list!\n",
93 ino);
94 if (preen || reply("FIX") == 1) {
95 /* lastino can't be zero */
96 ifp = lfs_ientry(lastino, &bp);
97 ifp->if_nextfree = 0;
98 dirty(bp);
99 bp->b_flags &= ~B_INUSE;
100 }
101 break;
102 }
103 ++visited[ino];
104 ifp = lfs_ientry(ino, &bp);
105 nextino = ifp->if_nextfree;
106 daddr = ifp->if_daddr;
107 bp->b_flags &= ~B_INUSE;
108 if (daddr) {
109 pwarn("! Ino %d with daddr 0x%x is on the free list!\n",
110 ino, daddr);
111 if (preen || reply("FIX") == 1) {
112 if (lastino == 0) {
113 sblock.lfs_free = nextino;
114 sbdirty();
115 } else {
116 ifp = lfs_ientry(lastino, &bp);
117 ifp->if_nextfree = nextino;
118 dirty(bp);
119 bp->b_flags &= ~B_INUSE;
120 }
121 ino = nextino;
122 continue;
123 }
124 }
125 lastino = ino;
126 ino = nextino;
127 }
128 /*
129 * Make sure all free inodes were found on the list
130 */
131 for (ino = ROOTINO+1; ino <= maxino; ++ino) {
132 if (visited[ino])
133 continue;
134
135 ifp = lfs_ientry(ino, &bp);
136 if (ifp->if_daddr) {
137 bp->b_flags &= ~B_INUSE;
138 continue;
139 }
140
141 pwarn("! Ino %d free, but not on the free list\n", ino);
142 if (preen || reply("FIX") == 1) {
143 ifp->if_nextfree = sblock.lfs_free;
144 sblock.lfs_free = ino;
145 sbdirty();
146 dirty(bp);
147 }
148 bp->b_flags &= ~B_INUSE;
149 }
150 }
151
152 static void
153 dump_segsum(SEGSUM * sump, daddr_t addr)
154 {
155 printf("Dump partial summary block 0x%x\n", addr);
156 printf("\tsumsum: %x (%d)\n", sump->ss_sumsum, sump->ss_sumsum);
157 printf("\tdatasum: %x (%d)\n", sump->ss_datasum, sump->ss_datasum);
158 printf("\tnext: %x (%d)\n", sump->ss_next, sump->ss_next);
159 printf("\tcreate: %x (%d)\n", sump->ss_create, sump->ss_create);
160 printf("\tnfinfo: %x (%d)\n", sump->ss_nfinfo, sump->ss_nfinfo);
161 printf("\tninos: %x (%d)\n", sump->ss_ninos, sump->ss_ninos);
162 printf("\tflags: %c%c\n",
163 sump->ss_flags & SS_DIROP ? 'd' : '-',
164 sump->ss_flags & SS_CONT ? 'c' : '-');
165 }
166
167 void
168 check_segment(int fd, int segnum, daddr_t addr, struct lfs * fs, int flags, int (*func)(struct lfs *, SEGSUM *, daddr_t))
169 {
170 struct lfs *sbp;
171 SEGSUM *sump = NULL;
172 SEGUSE *su;
173 struct bufarea *bp = NULL;
174 int psegnum = 0, ninos = 0;
175 off_t sum_offset, db_ssize;
176 int bc, su_flags, su_nsums, su_ninos;
177
178 db_ssize = sblock.lfs_ssize << sblock.lfs_fsbtodb;
179
180 su = lfs_gseguse(segnum, &bp);
181 su_flags = su->su_flags;
182 su_nsums = su->su_nsums;
183 su_ninos = su->su_ninos;
184 bp->b_flags &= ~B_INUSE;
185
186 /* printf("Seg at 0x%x\n",addr); */
187 if ((flags & CKSEG_VERBOSE) && segnum * db_ssize + fs->lfs_sboffs[0] != addr)
188 pwarn("WARNING: segment begins at 0x%qx, should be 0x%qx\n",
189 (long long unsigned)addr,
190 (long long unsigned)(segnum * db_ssize + fs->lfs_sboffs[0]));
191 sum_offset = ((off_t)addr << dbshift);
192
193 /* If this segment should have a superblock, look for one */
194 if (su_flags & SEGUSE_SUPERBLOCK) {
195 bp = getddblk(sum_offset >> dbshift, LFS_SBPAD);
196 sum_offset += LFS_SBPAD;
197
198 /* check for a superblock -- XXX this is crude */
199 sbp = (struct lfs *)(bp->b_un.b_buf);
200 if (sbp->lfs_magic == LFS_MAGIC) {
201 #if 0
202 if (sblock.lfs_tstamp == sbp->lfs_tstamp &&
203 memcmp(sbp, &sblock, sizeof(*sbp)) &&
204 (flags & CKSEG_VERBOSE))
205 pwarn("SUPERBLOCK MISMATCH SEGMENT %d\n", segnum);
206 #endif
207 } else {
208 if (flags & CKSEG_VERBOSE)
209 pwarn("SEGMENT %d SUPERBLOCK INVALID\n", segnum);
210 /* XXX allow to fix */
211 }
212 bp->b_flags &= ~B_INUSE;
213 }
214 /* XXX need to also check whether this one *should* be dirty */
215 if ((flags & CKSEG_IGNORECLEAN) && (su_flags & SEGUSE_DIRTY) == 0)
216 return;
217
218 while (1) {
219 if (su_nsums <= psegnum)
220 break;
221 bp = getddblk(sum_offset >> dbshift, LFS_SUMMARY_SIZE);
222 sump = (SEGSUM *)(bp->b_un.b_buf);
223 if (sump->ss_magic != SS_MAGIC) {
224 if (flags & CKSEG_VERBOSE)
225 printf("PARTIAL SEGMENT %d SEGMENT %d BAD PARTIAL SEGMENT MAGIC (0x%x should be 0x%x)\n",
226 psegnum, segnum, sump->ss_magic, SS_MAGIC);
227 bp->b_flags &= ~B_INUSE;
228 break;
229 }
230 if (sump->ss_sumsum != cksum(&sump->ss_datasum, LFS_SUMMARY_SIZE - sizeof(sump->ss_sumsum))) {
231 if (flags & CKSEG_VERBOSE) {
232 /* Corrupt partial segment */
233 pwarn("CORRUPT PARTIAL SEGMENT %d/%d OF SEGMENT %d AT BLK 0x%qx",
234 psegnum, su_nsums, segnum,
235 (unsigned long long)sum_offset >> dbshift);
236 if (db_ssize < (sum_offset >> dbshift) - addr)
237 pwarn(" (+0x%qx/0x%qx)",
238 (unsigned long long)(((sum_offset >> dbshift) - addr) -
239 db_ssize),
240 (unsigned long long)db_ssize);
241 else
242 pwarn(" (-0x%qx/0x%qx)",
243 (unsigned long long)(db_ssize -
244 ((sum_offset >> dbshift) - addr)),
245 (unsigned long long)db_ssize);
246 pwarn("\n");
247 dump_segsum(sump, sum_offset >> dbshift);
248 }
249 /* XXX fix it maybe */
250 bp->b_flags &= ~B_INUSE;
251 break; /* XXX could be throwing away data, but if
252 * this segsum is invalid, how to know where
253 * the next summary begins? */
254 }
255 /*
256 * Good partial segment
257 */
258 bc = (*func)(&sblock, sump, (daddr_t)(sum_offset >> dbshift));
259 if (bc) {
260 sum_offset += LFS_SUMMARY_SIZE + bc;
261 ninos += (sump->ss_ninos + INOPB(&sblock) - 1) / INOPB(&sblock);
262 psegnum++;
263 } else {
264 bp->b_flags &= ~B_INUSE;
265 break;
266 }
267 bp->b_flags &= ~B_INUSE;
268 }
269 if (flags & CKSEG_VERBOSE) {
270 if (ninos != su_ninos)
271 pwarn("SEGMENT %d has %d ninos, not %d\n",
272 segnum, ninos, su_ninos);
273 if (psegnum != su_nsums)
274 pwarn("SEGMENT %d has %d summaries, not %d\n",
275 segnum, psegnum, su_nsums);
276 }
277 return;
278 }
279
280 int
281 check_summary(struct lfs * fs, SEGSUM * sp, daddr_t pseg_addr)
282 {
283 FINFO *fp;
284 int bc; /* Bytes in partial segment */
285 int nblocks;
286 daddr_t seg_addr, *dp, *idp, daddr;
287 struct bufarea *bp;
288 int i, j, k, datac, len;
289 long sn;
290 u_long *datap;
291 u_int32_t ccksum;
292
293 sn = datosn(fs, pseg_addr);
294 seg_addr = sntoda(fs, sn);
295
296 /*
297 * printf("Pseg at 0x%x, %d inos, %d
298 * finfos\n",addr>>dbshift,sp->ss_ninos,sp->ss_nfinfo);
299 */
300 /* We've already checked the sumsum, just do the data bounds and sum */
301
302 /* 1. Count the blocks. */
303 nblocks = ((sp->ss_ninos + INOPB(fs) - 1) / INOPB(fs));
304 bc = nblocks << fs->lfs_bshift;
305
306 fp = (FINFO *)(sp + 1);
307 for (i = 0; i < sp->ss_nfinfo; i++) {
308 nblocks += fp->fi_nblocks;
309 bc += fp->fi_lastlength + ((fp->fi_nblocks - 1) << fs->lfs_bshift);
310 fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
311 }
312 datap = (u_long *)malloc(nblocks * sizeof(*datap));
313 datac = 0;
314
315 dp = (daddr_t *)sp;
316 dp += LFS_SUMMARY_SIZE / sizeof(daddr_t);
317 dp--;
318
319 idp = dp;
320 daddr = pseg_addr + (LFS_SUMMARY_SIZE / dev_bsize);
321 fp = (FINFO *)(sp + 1);
322 for (i = 0, j = 0; i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
323 /* printf("*idp=%x, daddr=%x\n", *idp, daddr); */
324 if (i >= sp->ss_nfinfo && *idp != daddr) {
325 pwarn("Not enough inode blocks in pseg at 0x%x: found %d, wanted %d\n",
326 pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
327 pwarn("*idp=%x, daddr=%x\n", *idp, daddr);
328 break;
329 }
330 while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
331 bp = getddblk(daddr, (1 << fs->lfs_bshift));
332 datap[datac++] = ((u_long *)(bp->b_un.b_buf))[0];
333 bp->b_flags &= ~B_INUSE;
334
335 ++j;
336 daddr += (1 << fs->lfs_bshift) / dev_bsize;
337 --idp;
338 }
339 if (i < sp->ss_nfinfo) {
340 for (k = 0; k < fp->fi_nblocks; k++) {
341 len = (k == fp->fi_nblocks - 1 ? fp->fi_lastlength
342 : (1 << fs->lfs_bshift));
343 bp = getddblk(daddr, len);
344 datap[datac++] = ((u_long *)(bp->b_un.b_buf))[0];
345 bp->b_flags &= ~B_INUSE;
346 daddr += len / dev_bsize;
347 }
348 fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
349 }
350 }
351
352 if (datac != nblocks) {
353 pwarn("Partial segment at 0x%x expected %d blocks counted %d\n",
354 pseg_addr, nblocks, datac);
355 }
356 ccksum = cksum(datap, nblocks * sizeof(u_long));
357 /* Check the data checksum */
358 if (ccksum != sp->ss_datasum) {
359 pwarn("Partial segment at 0x%x data checksum mismatch: got 0x%x, expected 0x%x\n",
360 pseg_addr, sp->ss_datasum, ccksum);
361 /* return 0; */
362 }
363 return bc;
364 }
365