1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/* Pass 2 of GNU fsck -- examine all directories for validity
Copyright (C) 1994, 1996 Free Software Foundation, Inc.
Written by Michael I. Bushnell.
This file is part of the GNU Hurd.
The GNU Hurd is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
The GNU Hurd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "fsck.h"
/* Verify root inode's allocation and check all directories for
viability. Set DIRSORTED array fully and check to make sure
each directory has a correct . and .. in it. */
void
pass2 ()
{
int nd;
struct dirinfo *dnp;
struct dinode dino;
/* Return negative, zero, or positive according to the
ordering of the first data block of **DNP1 and **DNP2. */
int
sortfunc (const void *ptr1, const void *ptr2)
{
struct dirinfo * const *dnp1 = ptr1;
struct dirinfo * const *dnp2 = ptr2;
return ((*dnp1)->i_blks[0] - (*dnp2)->i_blks[0]);
}
/* Called for each DIRBLKSIZ chunk of the directory.
BUF is the data of the directory block. Return
1 if this block has been modified and should be written
to disk; otherwise return 0. */
int
check1block (void *buf)
{
struct directory_entry *dp;
int mod = 0;
u_char namlen;
char type;
int i;
for (dp = buf; (void *)dp - buf < DIRBLKSIZ;
dp = (struct directory_entry *) ((void *)dp + dp->d_reclen))
{
/* Check RECLEN for basic validity */
if (dp->d_reclen == 0
|| dp->d_reclen + (void *)dp - buf > DIRBLKSIZ)
{
problem (1, "BAD RECLEN IN DIRECTORY");
if (reply ("SALVAGE"))
{
/* Skip over everything else in this dirblock;
mark this entry free. */
dp->d_ino = 0;
dp->d_reclen = DIRBLKSIZ - ((void *)dp - buf);
return 1;
}
else
/* But give up regardless */
return mod;
}
/* Check INO */
if (dp->d_ino > maxino)
{
problem (1, "BAD INODE NUMBER IN DIRECTORY");
if (reply ("SALVAGE"))
{
/* Mark this entry clear */
dp->d_ino = 0;
mod = 1;
}
}
if (!dp->d_ino)
continue;
/* Check INO */
if (inodestate[dp->d_ino] == UNALLOC)
{
pinode (0, dnp->i_number, "REF TO UNALLOCATED NODE IN");
if (preen || reply ("REMOVE"))
{
dp->d_ino = 0;
mod = 1;
pfix ("REMOVED");
continue;
}
}
/* Check NAMLEN */
namlen = DIRECT_NAMLEN (dp);
if (namlen > MAXNAMLEN)
{
problem (1, "BAD NAMLEN IN DIRECTORY");
if (reply ("SALVAGE"))
{
/* Mark this entry clear */
dp->d_ino = 0;
mod = 1;
}
}
else
{
/* Check for illegal characters */
for (i = 0; i < DIRECT_NAMLEN (dp); i++)
if (dp->d_name[i] == '\0' || dp->d_name[i] == '/')
{
problem (1, "ILLEGAL CHARACTER IN FILE NAME");
if (reply ("SALVAGE"))
{
/* Mark this entry clear */
dp->d_ino = 0;
mod = 1;
break;
}
}
if (dp->d_name[DIRECT_NAMLEN (dp)])
{
problem (1, "DIRECTORY NAME NOT TERMINATED");
if (reply ("SALVAGE"))
{
/* Mark this entry clear */
dp->d_ino = 0;
mod = 1;
}
}
}
if (!dp->d_ino)
continue;
/* Check TYPE */
type = DIRECT_TYPE (dp);
if (type != DT_UNKNOWN && type != typemap[dp->d_ino])
{
problem (0, "INCORRECT NODE TYPE IN DIRECTORY");
if (preen || reply ("CLEAR"))
{
pfix ("CLEARED");
dp->d_type = 0;
mod = 1;
}
}
/* Here we should check for duplicate directory entries;
that's too much trouble right now. */
/* Account for the inode in the linkfound map */
if (inodestate[dp->d_ino] != UNALLOC)
linkfound[dp->d_ino]++;
if (inodestate[dp->d_ino] == DIRECTORY
|| inodestate[dp->d_ino] == BADDIR)
{
if (DIRECT_NAMLEN (dp) == 1 && dp->d_name[0] == '.')
dnp->i_dot = dp->d_ino;
else if (DIRECT_NAMLEN (dp) == 2
&& dp->d_name[0] == '.' && dp->d_name[1] == '.')
dnp->i_dotdot = dp->d_ino;
else
{
struct dirinfo *targetdir;
targetdir = lookup_directory (dp->d_ino);
if (targetdir->i_parent)
{
problem (0, "EXTRANEOUS LINK `%s' TO DIR I=%ld",
dp->d_name, dp->d_ino);
pextend (" FOUND IN DIR I=%d", dnp->i_number);
if (preen || reply ("REMOVE"))
{
dp->d_ino = 0;
mod = 1;
pfix ("REMOVED");
}
}
else
targetdir->i_parent = dnp->i_number;
}
}
}
return mod;
}
/* Called for each filesystem block of the directory. Load BNO
into core and then call CHECK1BLOCK for each DIRBLKSIZ chunk.
OFFSET is the offset this block occupies ithe file.
Always return 1. */
int
checkdirblock (daddr_t bno, int nfrags, off_t offset)
{
void *buf = alloca (nfrags * sblock->fs_fsize);
void *bufp;
int rewrite;
readblock (fsbtodb (sblock, bno), buf, nfrags * sblock->fs_fsize);
rewrite = 0;
for (bufp = buf;
bufp - buf < nfrags * sblock->fs_fsize
&& offset + (bufp - buf) + DIRBLKSIZ <= dnp->i_isize;
bufp += DIRBLKSIZ)
{
if (check1block (bufp))
rewrite = 1;
}
if (rewrite)
writeblock (fsbtodb (sblock, bno), buf, nfrags * sblock->fs_fsize);
return 1;
}
switch (inodestate [ROOTINO])
{
default:
errexit ("BAD STATE %d FOR ROOT INODE", (int) (inodestate[ROOTINO]));
case DIRECTORY:
break;
case UNALLOC:
problem (1, "ROOT INODE UNALLOCATED");
if (!reply ("ALLOCATE"))
errexit ("ABORTING");
if (allocdir (ROOTINO, ROOTINO, 0755) != ROOTINO)
errexit ("CANNOT ALLOCATE ROOT INODE");
break;
case REG:
problem (1, "ROOT INODE NOT DIRECTORY");
if (reply ("REALLOCATE"))
freeino (ROOTINO);
if (allocdir (ROOTINO, ROOTINO, 0755) != ROOTINO)
errexit ("CANNOT ALLOCATE ROOT INODE");
break;
case BADDIR:
problem (1, "DUPLICATE or BAD BLOCKS IN ROOT INODE");
if (reply ("REALLOCATE"))
{
freeino (ROOTINO);
if (allocdir (ROOTINO, ROOTINO, 0755) != ROOTINO)
errexit ("CANNOT ALLOCATE ROOT INODE");
}
if (reply ("CONTINUE") == 0)
errexit ("ABORTING");
break;
}
/* Sort inpsort */
qsort (dirsorted, dirarrayused, sizeof (struct dirinfo *), sortfunc);
/* Check basic integrity of each directory */
for (nd = 0; nd < dirarrayused; nd++)
{
dnp = dirsorted[nd];
if (dnp->i_isize == 0)
continue;
if (dnp->i_isize % DIRBLKSIZ)
{
problem (0, "DIRECTORY INO=%d: LENGTH %d NOT MULTIPLE OF %d",
dnp->i_number, dnp->i_isize, DIRBLKSIZ);
if (preen || reply ("ADJUST"))
{
getinode (dnp->i_number, &dino);
dino.di_size = roundup (dnp->i_isize, DIRBLKSIZ);
write_inode (dnp->i_number, &dino);
pfix ("ADJUSTED");
}
}
bzero (&dino, sizeof (struct dinode));
dino.di_size = dnp->i_isize;
bcopy (dnp->i_blks, dino.di_db, dnp->i_numblks);
datablocks_iterate (&dino, checkdirblock);
}
/* At this point for each directory:
If this directory is an entry in another directory, then i_parent is
set to that node's number.
If this directory has a `..' entry, then i_dotdot is set to that link.
Check to see that `..' is set correctly. */
for (nd = 0; nd < dirarrayused; nd++)
{
dnp = dirsorted[nd];
if (dnp->i_isize == 0)
continue;
/* Root is considered to be its own parent even though it isn't
listed. */
if (dnp->i_number == ROOTINO && !dnp->i_parent)
dnp->i_parent = ROOTINO;
/* Check `..' to make sure it exists and is correct */
if (dnp->i_parent && dnp->i_dotdot == 0)
{
dnp->i_dotdot = dnp->i_parent;
pinode (0, dnp->i_number, "MISSING `..' IN");
if ((preen || reply ("FIX"))
&& makeentry (dnp->i_number, dnp->i_parent, ".."))
{
linkfound[dnp->i_parent]++;
pfix ("FIXED");
}
else
pfail (0);
}
else if (dnp->i_parent && dnp->i_dotdot != dnp->i_parent)
{
pinode (0, dnp->i_number, "BAD INODE NUMBER FOR `..' IN");
if (preen || reply ("FIX"))
{
ino_t parent = dnp->i_parent, old_dotdot = dnp->i_dotdot;
dnp->i_dotdot = parent;
if (changeino (dnp->i_number, "..", parent))
/* Adjust what the parent's link count should be; the actual
count will be corrected in an later pass. */
{
linkfound[parent]++;
if (inodestate[old_dotdot] != UNALLOC)
linkfound[old_dotdot]--;
pfix ("FIXED");
}
else
pfail (0);
}
}
/* Check `.' to make sure it exists and is correct */
if (dnp->i_dot == 0)
{
dnp->i_dot = dnp->i_number;
pinode (0, dnp->i_number, "MISSING `.' IN");
if ((preen || reply ("FIX"))
&& makeentry (dnp->i_number, dnp->i_number, "."))
{
linkfound[dnp->i_number]++;
pfix ("FIXED");
}
else
pfail (0);
}
else if (dnp->i_dot != dnp->i_number)
{
pinode (0, dnp->i_number, "BAD INODE NUMBER FOR `.' IN");
if (preen || reply ("FIX"))
{
ino_t old_dot = dnp->i_dot;
dnp->i_dot = dnp->i_number;
if (changeino (dnp->i_number, ".", dnp->i_number))
{
linkfound[dnp->i_number]++;
if (inodestate[old_dot] != UNALLOC)
linkfound[old_dot]--;
pfix ("FIXED");
}
else
pfail (0);
}
}
}
}
|