pass2.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1980, 1986 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd static char sccsid[] = "@(#)pass2.c 5.17 (Berkeley) 12/28/90";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #include <sys/param.h>
39 1.1 cgd #include <ufs/dinode.h>
40 1.1 cgd #include <ufs/fs.h>
41 1.1 cgd #define KERNEL
42 1.1 cgd #include <ufs/dir.h>
43 1.1 cgd #undef KERNEL
44 1.1 cgd #include <stdlib.h>
45 1.1 cgd #include <string.h>
46 1.1 cgd #include "fsck.h"
47 1.1 cgd
48 1.1 cgd #define MINDIRSIZE (sizeof (struct dirtemplate))
49 1.1 cgd
50 1.1 cgd int pass2check(), blksort();
51 1.1 cgd
52 1.1 cgd pass2()
53 1.1 cgd {
54 1.1 cgd register struct dinode *dp;
55 1.1 cgd register struct inoinfo **inpp, *inp;
56 1.1 cgd struct inoinfo **inpend;
57 1.1 cgd struct inodesc curino;
58 1.1 cgd struct dinode dino;
59 1.1 cgd char pathbuf[MAXPATHLEN + 1];
60 1.1 cgd
61 1.1 cgd switch (statemap[ROOTINO]) {
62 1.1 cgd
63 1.1 cgd case USTATE:
64 1.1 cgd pfatal("ROOT INODE UNALLOCATED");
65 1.1 cgd if (reply("ALLOCATE") == 0)
66 1.1 cgd errexit("");
67 1.1 cgd if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
68 1.1 cgd errexit("CANNOT ALLOCATE ROOT INODE\n");
69 1.1 cgd break;
70 1.1 cgd
71 1.1 cgd case DCLEAR:
72 1.1 cgd pfatal("DUPS/BAD IN ROOT INODE");
73 1.1 cgd if (reply("REALLOCATE")) {
74 1.1 cgd freeino(ROOTINO);
75 1.1 cgd if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
76 1.1 cgd errexit("CANNOT ALLOCATE ROOT INODE\n");
77 1.1 cgd break;
78 1.1 cgd }
79 1.1 cgd if (reply("CONTINUE") == 0)
80 1.1 cgd errexit("");
81 1.1 cgd break;
82 1.1 cgd
83 1.1 cgd case FSTATE:
84 1.1 cgd case FCLEAR:
85 1.1 cgd pfatal("ROOT INODE NOT DIRECTORY");
86 1.1 cgd if (reply("REALLOCATE")) {
87 1.1 cgd freeino(ROOTINO);
88 1.1 cgd if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
89 1.1 cgd errexit("CANNOT ALLOCATE ROOT INODE\n");
90 1.1 cgd break;
91 1.1 cgd }
92 1.1 cgd if (reply("FIX") == 0)
93 1.1 cgd errexit("");
94 1.1 cgd dp = ginode(ROOTINO);
95 1.1 cgd dp->di_mode &= ~IFMT;
96 1.1 cgd dp->di_mode |= IFDIR;
97 1.1 cgd inodirty();
98 1.1 cgd break;
99 1.1 cgd
100 1.1 cgd case DSTATE:
101 1.1 cgd break;
102 1.1 cgd
103 1.1 cgd default:
104 1.1 cgd errexit("BAD STATE %d FOR ROOT INODE", statemap[ROOTINO]);
105 1.1 cgd }
106 1.1 cgd statemap[ROOTINO] = DFOUND;
107 1.1 cgd /*
108 1.1 cgd * Sort the directory list into disk block order.
109 1.1 cgd */
110 1.1 cgd qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
111 1.1 cgd /*
112 1.1 cgd * Check the integrity of each directory.
113 1.1 cgd */
114 1.1 cgd bzero((char *)&curino, sizeof(struct inodesc));
115 1.1 cgd curino.id_type = DATA;
116 1.1 cgd curino.id_func = pass2check;
117 1.1 cgd dino.di_mode = IFDIR;
118 1.1 cgd dp = &dino;
119 1.1 cgd inpend = &inpsort[inplast];
120 1.1 cgd for (inpp = inpsort; inpp < inpend; inpp++) {
121 1.1 cgd inp = *inpp;
122 1.1 cgd if (inp->i_isize == 0)
123 1.1 cgd continue;
124 1.1 cgd if (inp->i_isize < MINDIRSIZE) {
125 1.1 cgd direrror(inp->i_number, "DIRECTORY TOO SHORT");
126 1.1 cgd inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
127 1.1 cgd if (reply("FIX") == 1) {
128 1.1 cgd dp = ginode(inp->i_number);
129 1.1 cgd dp->di_size = inp->i_isize;
130 1.1 cgd inodirty();
131 1.1 cgd dp = &dino;
132 1.1 cgd }
133 1.1 cgd } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
134 1.1 cgd getpathname(pathbuf, inp->i_number, inp->i_number);
135 1.1 cgd pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
136 1.1 cgd pathbuf, inp->i_isize, DIRBLKSIZ);
137 1.1 cgd if (preen)
138 1.1 cgd printf(" (ADJUSTED)\n");
139 1.1 cgd inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
140 1.1 cgd if (preen || reply("ADJUST") == 1) {
141 1.1 cgd dp = ginode(inp->i_number);
142 1.1 cgd dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
143 1.1 cgd inodirty();
144 1.1 cgd dp = &dino;
145 1.1 cgd }
146 1.1 cgd }
147 1.1 cgd dp->di_size = inp->i_isize;
148 1.1 cgd bcopy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
149 1.1 cgd (size_t)inp->i_numblks);
150 1.1 cgd curino.id_number = inp->i_number;
151 1.1 cgd curino.id_parent = inp->i_parent;
152 1.1 cgd (void)ckinode(dp, &curino);
153 1.1 cgd }
154 1.1 cgd /*
155 1.1 cgd * Now that the parents of all directories have been found,
156 1.1 cgd * make another pass to verify the value of `..'
157 1.1 cgd */
158 1.1 cgd for (inpp = inpsort; inpp < inpend; inpp++) {
159 1.1 cgd inp = *inpp;
160 1.1 cgd if (inp->i_parent == 0 || inp->i_isize == 0)
161 1.1 cgd continue;
162 1.1 cgd if (statemap[inp->i_parent] == DFOUND &&
163 1.1 cgd statemap[inp->i_number] == DSTATE)
164 1.1 cgd statemap[inp->i_number] = DFOUND;
165 1.1 cgd if (inp->i_dotdot == inp->i_parent ||
166 1.1 cgd inp->i_dotdot == (ino_t)-1)
167 1.1 cgd continue;
168 1.1 cgd if (inp->i_dotdot == 0) {
169 1.1 cgd inp->i_dotdot = inp->i_parent;
170 1.1 cgd fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
171 1.1 cgd if (reply("FIX") == 0)
172 1.1 cgd continue;
173 1.1 cgd (void)makeentry(inp->i_number, inp->i_parent, "..");
174 1.1 cgd lncntp[inp->i_parent]--;
175 1.1 cgd continue;
176 1.1 cgd }
177 1.1 cgd fileerror(inp->i_parent, inp->i_number,
178 1.1 cgd "BAD INODE NUMBER FOR '..'");
179 1.1 cgd if (reply("FIX") == 0)
180 1.1 cgd continue;
181 1.1 cgd lncntp[inp->i_dotdot]++;
182 1.1 cgd lncntp[inp->i_parent]--;
183 1.1 cgd inp->i_dotdot = inp->i_parent;
184 1.1 cgd (void)changeino(inp->i_number, "..", inp->i_parent);
185 1.1 cgd }
186 1.1 cgd /*
187 1.1 cgd * Mark all the directories that can be found from the root.
188 1.1 cgd */
189 1.1 cgd propagate();
190 1.1 cgd }
191 1.1 cgd
192 1.1 cgd pass2check(idesc)
193 1.1 cgd struct inodesc *idesc;
194 1.1 cgd {
195 1.1 cgd register struct direct *dirp = idesc->id_dirp;
196 1.1 cgd register struct inoinfo *inp;
197 1.1 cgd int n, entrysize, ret = 0;
198 1.1 cgd struct dinode *dp;
199 1.1 cgd char *errmsg;
200 1.1 cgd struct direct proto;
201 1.1 cgd char namebuf[MAXPATHLEN + 1];
202 1.1 cgd char pathbuf[MAXPATHLEN + 1];
203 1.1 cgd
204 1.1 cgd /*
205 1.1 cgd * check for "."
206 1.1 cgd */
207 1.1 cgd if (idesc->id_entryno != 0)
208 1.1 cgd goto chk1;
209 1.1 cgd if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
210 1.1 cgd if (dirp->d_ino != idesc->id_number) {
211 1.1 cgd direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
212 1.1 cgd dirp->d_ino = idesc->id_number;
213 1.1 cgd if (reply("FIX") == 1)
214 1.1 cgd ret |= ALTERED;
215 1.1 cgd }
216 1.1 cgd goto chk1;
217 1.1 cgd }
218 1.1 cgd direrror(idesc->id_number, "MISSING '.'");
219 1.1 cgd proto.d_ino = idesc->id_number;
220 1.1 cgd proto.d_namlen = 1;
221 1.1 cgd (void)strcpy(proto.d_name, ".");
222 1.1 cgd entrysize = DIRSIZ(&proto);
223 1.1 cgd if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
224 1.1 cgd pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
225 1.1 cgd dirp->d_name);
226 1.1 cgd } else if (dirp->d_reclen < entrysize) {
227 1.1 cgd pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
228 1.1 cgd } else if (dirp->d_reclen < 2 * entrysize) {
229 1.1 cgd proto.d_reclen = dirp->d_reclen;
230 1.1 cgd bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
231 1.1 cgd if (reply("FIX") == 1)
232 1.1 cgd ret |= ALTERED;
233 1.1 cgd } else {
234 1.1 cgd n = dirp->d_reclen - entrysize;
235 1.1 cgd proto.d_reclen = entrysize;
236 1.1 cgd bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
237 1.1 cgd idesc->id_entryno++;
238 1.1 cgd lncntp[dirp->d_ino]--;
239 1.1 cgd dirp = (struct direct *)((char *)(dirp) + entrysize);
240 1.1 cgd bzero((char *)dirp, (size_t)n);
241 1.1 cgd dirp->d_reclen = n;
242 1.1 cgd if (reply("FIX") == 1)
243 1.1 cgd ret |= ALTERED;
244 1.1 cgd }
245 1.1 cgd chk1:
246 1.1 cgd if (idesc->id_entryno > 1)
247 1.1 cgd goto chk2;
248 1.1 cgd inp = getinoinfo(idesc->id_number);
249 1.1 cgd proto.d_ino = inp->i_parent;
250 1.1 cgd proto.d_namlen = 2;
251 1.1 cgd (void)strcpy(proto.d_name, "..");
252 1.1 cgd entrysize = DIRSIZ(&proto);
253 1.1 cgd if (idesc->id_entryno == 0) {
254 1.1 cgd n = DIRSIZ(dirp);
255 1.1 cgd if (dirp->d_reclen < n + entrysize)
256 1.1 cgd goto chk2;
257 1.1 cgd proto.d_reclen = dirp->d_reclen - n;
258 1.1 cgd dirp->d_reclen = n;
259 1.1 cgd idesc->id_entryno++;
260 1.1 cgd lncntp[dirp->d_ino]--;
261 1.1 cgd dirp = (struct direct *)((char *)(dirp) + n);
262 1.1 cgd bzero((char *)dirp, (size_t)proto.d_reclen);
263 1.1 cgd dirp->d_reclen = proto.d_reclen;
264 1.1 cgd }
265 1.1 cgd if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
266 1.1 cgd inp->i_dotdot = dirp->d_ino;
267 1.1 cgd goto chk2;
268 1.1 cgd }
269 1.1 cgd if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
270 1.1 cgd fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
271 1.1 cgd pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
272 1.1 cgd dirp->d_name);
273 1.1 cgd inp->i_dotdot = (ino_t)-1;
274 1.1 cgd } else if (dirp->d_reclen < entrysize) {
275 1.1 cgd fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
276 1.1 cgd pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
277 1.1 cgd inp->i_dotdot = (ino_t)-1;
278 1.1 cgd } else if (inp->i_parent != 0) {
279 1.1 cgd /*
280 1.1 cgd * We know the parent, so fix now.
281 1.1 cgd */
282 1.1 cgd inp->i_dotdot = inp->i_parent;
283 1.1 cgd fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
284 1.1 cgd proto.d_reclen = dirp->d_reclen;
285 1.1 cgd bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
286 1.1 cgd if (reply("FIX") == 1)
287 1.1 cgd ret |= ALTERED;
288 1.1 cgd }
289 1.1 cgd idesc->id_entryno++;
290 1.1 cgd if (dirp->d_ino != 0)
291 1.1 cgd lncntp[dirp->d_ino]--;
292 1.1 cgd return (ret|KEEPON);
293 1.1 cgd chk2:
294 1.1 cgd if (dirp->d_ino == 0)
295 1.1 cgd return (ret|KEEPON);
296 1.1 cgd if (dirp->d_namlen <= 2 &&
297 1.1 cgd dirp->d_name[0] == '.' &&
298 1.1 cgd idesc->id_entryno >= 2) {
299 1.1 cgd if (dirp->d_namlen == 1) {
300 1.1 cgd direrror(idesc->id_number, "EXTRA '.' ENTRY");
301 1.1 cgd dirp->d_ino = 0;
302 1.1 cgd if (reply("FIX") == 1)
303 1.1 cgd ret |= ALTERED;
304 1.1 cgd return (KEEPON | ret);
305 1.1 cgd }
306 1.1 cgd if (dirp->d_name[1] == '.') {
307 1.1 cgd direrror(idesc->id_number, "EXTRA '..' ENTRY");
308 1.1 cgd dirp->d_ino = 0;
309 1.1 cgd if (reply("FIX") == 1)
310 1.1 cgd ret |= ALTERED;
311 1.1 cgd return (KEEPON | ret);
312 1.1 cgd }
313 1.1 cgd }
314 1.1 cgd idesc->id_entryno++;
315 1.1 cgd n = 0;
316 1.1 cgd if (dirp->d_ino > maxino) {
317 1.1 cgd fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
318 1.1 cgd n = reply("REMOVE");
319 1.1 cgd } else {
320 1.1 cgd again:
321 1.1 cgd switch (statemap[dirp->d_ino]) {
322 1.1 cgd case USTATE:
323 1.1 cgd if (idesc->id_entryno <= 2)
324 1.1 cgd break;
325 1.1 cgd fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
326 1.1 cgd n = reply("REMOVE");
327 1.1 cgd break;
328 1.1 cgd
329 1.1 cgd case DCLEAR:
330 1.1 cgd case FCLEAR:
331 1.1 cgd if (idesc->id_entryno <= 2)
332 1.1 cgd break;
333 1.1 cgd if (statemap[dirp->d_ino] == DCLEAR)
334 1.1 cgd errmsg = "ZERO LENGTH DIRECTORY";
335 1.1 cgd else
336 1.1 cgd errmsg = "DUP/BAD";
337 1.1 cgd fileerror(idesc->id_number, dirp->d_ino, errmsg);
338 1.1 cgd if ((n = reply("REMOVE")) == 1)
339 1.1 cgd break;
340 1.1 cgd dp = ginode(dirp->d_ino);
341 1.1 cgd statemap[dirp->d_ino] =
342 1.1 cgd (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
343 1.1 cgd lncntp[dirp->d_ino] = dp->di_nlink;
344 1.1 cgd goto again;
345 1.1 cgd
346 1.1 cgd case DSTATE:
347 1.1 cgd if (statemap[idesc->id_number] == DFOUND)
348 1.1 cgd statemap[dirp->d_ino] = DFOUND;
349 1.1 cgd /* fall through */
350 1.1 cgd
351 1.1 cgd case DFOUND:
352 1.1 cgd inp = getinoinfo(dirp->d_ino);
353 1.1 cgd if (inp->i_parent != 0 && idesc->id_entryno > 2) {
354 1.1 cgd getpathname(pathbuf, idesc->id_number,
355 1.1 cgd idesc->id_number);
356 1.1 cgd getpathname(namebuf, dirp->d_ino, dirp->d_ino);
357 1.1 cgd pwarn("%s %s %s\n", pathbuf,
358 1.1 cgd "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
359 1.1 cgd namebuf);
360 1.1 cgd if (preen)
361 1.1 cgd printf(" (IGNORED)\n");
362 1.1 cgd else if ((n = reply("REMOVE")) == 1)
363 1.1 cgd break;
364 1.1 cgd }
365 1.1 cgd if (idesc->id_entryno > 2)
366 1.1 cgd inp->i_parent = idesc->id_number;
367 1.1 cgd /* fall through */
368 1.1 cgd
369 1.1 cgd case FSTATE:
370 1.1 cgd lncntp[dirp->d_ino]--;
371 1.1 cgd break;
372 1.1 cgd
373 1.1 cgd default:
374 1.1 cgd errexit("BAD STATE %d FOR INODE I=%d",
375 1.1 cgd statemap[dirp->d_ino], dirp->d_ino);
376 1.1 cgd }
377 1.1 cgd }
378 1.1 cgd if (n == 0)
379 1.1 cgd return (ret|KEEPON);
380 1.1 cgd dirp->d_ino = 0;
381 1.1 cgd return (ret|KEEPON|ALTERED);
382 1.1 cgd }
383 1.1 cgd
384 1.1 cgd /*
385 1.1 cgd * Routine to sort disk blocks.
386 1.1 cgd */
387 1.1 cgd blksort(inpp1, inpp2)
388 1.1 cgd struct inoinfo **inpp1, **inpp2;
389 1.1 cgd {
390 1.1 cgd
391 1.1 cgd return ((*inpp1)->i_blks[0] - (*inpp2)->i_blks[0]);
392 1.1 cgd }
393