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
|
/* Pass 2 of GNU fsck -- examine all directories for validity
Copyright (C) 1994 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)
{
pfatal ("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)
{
pfatal ("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)
{
fileerror (dnp->i_number, dp->d_ino, "UNALLOCATED");
if (reply ("REMOVE"))
{
dp->d_ino = 0;
mod = 1;
continue;
}
}
/* Check NAMLEN */
namlen = DIRECT_NAMLEN (dp);
if (namlen > MAXNAMLEN)
{
pfatal ("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 < dp->d_namlen; i++)
if (dp->d_name[i] == '\0' || dp->d_name[i] == '/')
{
pfatal ("ILLEGAL CHARACTER IN FILE NAME");
if (reply ("SALVAGE"))
{
/* Mark this entry clear */
dp->d_ino = 0;
mod = 1;
break;
}
}
if (dp->d_name[dp->d_namlen])
{
pfatal ("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])
{
pfatal ("INCORRECT NODE TYPE IN DIRECTORY");
if (reply ("FIX"))
{
errexit ("NODE TYPE FOUND WHEN NOT SUPPORTED");
dp->d_type = typemap[dp->d_ino];
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 this is `.' or `..' then note the value for
later examination. */
if (dp->d_namlen == 1 && dp->d_name[0] == '.')
dnp->i_dot = dp->d_ino;
if (dp->d_namlen == 2
&& dp->d_name[0] == '.' && dp->d_name[1] == '.')
dnp->i_dotdot = dp->d_ino;
}
return mod;
}
/* Called for each filesystem block of the directory. Load BNO
into core and then call CHECK1BLOCK for each DIRBLKSIZ chunk.
Always return 1. */
int
checkdirblock (daddr_t bno, int nfrags)
{
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;
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:
pfatal ("ROOT INODE UNALLOCATED");
if (!reply ("ALLOCATE"))
errexit ("\n");
if (allocdir (ROOTINO, ROOTINO, 0755) != ROOTINO)
errexit ("CANNOT ALLOCATE ROOT INODE\n");
break;
case REG:
pfatal ("ROOT INODE NOT DIRECTORY");
if (reply ("REALLOCATE"))
freeino (ROOTINO);
if (allocdir (ROOTINO, ROOTINO, 0755) != ROOTINO)
errexit ("CANNOT ALLOCATE ROOT INODE\n");
break;
case BADDIR:
pfatal ("DUPLICATE or BAD BLOCKS IN ROOT INODE");
if (reply ("REALLOCATE"))
{
freeino (ROOTINO);
if (allocdir (ROOTINO, ROOTINO, 0755) != ROOTINO)
errexit ("CANNOT ALLOCATE ROOT INODE\n");
}
if (reply ("CONTINUE") == 0)
errexit ("\n");
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)
{
pwarn ("DIRECTORY INO=%d: LENGTH %d NOT MULTIPLE OF %d",
dnp->i_number, dnp->i_isize, DIRBLKSIZ);
if (preen || reply ("ADJUST"))
{
if (preen)
printf (" (ADJUSTED)");
getinode (dnp->i_number, &dino);
dino.di_size = roundup (dnp->i_isize, DIRBLKSIZ);
write_inode (dnp->i_number, &dino);
}
}
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;
/* Check `..' to make sure it exists and is correct */
if (dnp->i_parent && dnp->i_dotdot == 0)
{
dnp->i_dotdot = dnp->i_parent;
fileerror (dnp->i_parent, dnp->i_number, "MISSING `..'");
if (reply ("FIX"))
makeentry (dnp->i_number, dnp->i_parent, "..");
}
else if (dnp->i_parent && dnp->i_dotdot != dnp->i_parent)
{
fileerror (dnp->i_parent, dnp->i_number,
"BAD INODE NUMBER FOR `..'");
if (reply ("FIX"))
{
dnp->i_dotdot = dnp->i_parent;
changeino (dnp->i_number, "..", dnp->i_parent);
}
}
/* Check `.' to make sure it exists and is correct */
if (dnp->i_dot == 0)
{
dnp->i_dot = dnp->i_number;
fileerror (dnp->i_number, dnp->i_number, "MISSING `.'");
if (reply ("FIX"))
makeentry (dnp->i_number, dnp->i_number, ".");
}
else if (dnp->i_dot != dnp->i_number)
{
fileerror (dnp->i_number, dnp->i_number,
"MAD INODE NUMBER FOR `.'");
if (reply ("FIX"))
{
dnp->i_dot = dnp->i_number;
changeino (dnp->i_number, ".", dnp->i_number);
}
}
}
}
|