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