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