Merge from vendor branch GCC:
[dragonfly.git] / sbin / fsck / 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  * @(#)pass2.c  8.9 (Berkeley) 4/28/95
34  * $FreeBSD: src/sbin/fsck/pass2.c,v 1.10.2.2 2001/11/24 15:14:59 iedowse Exp $
35  * $DragonFly: src/sbin/fsck/pass2.c,v 1.7 2004/12/18 21:43:38 swildner Exp $
36  */
37
38 #include <sys/param.h>
39
40 #include <vfs/ufs/dinode.h>
41 #include <vfs/ufs/dir.h>
42
43 #include <err.h>
44 #include <string.h>
45
46 #include "fsck.h"
47
48 #define MINDIRSIZE      (sizeof (struct dirtemplate))
49
50 static int blksort(const void *, const void *);
51 static int pass2check(struct inodesc *);
52
53 void
54 pass2(void)
55 {
56         register struct dinode *dp;
57         register struct inoinfo **inpp, *inp;
58         struct inoinfo **inpend;
59         struct inodesc curino;
60         struct dinode dino;
61         char pathbuf[MAXPATHLEN + 1];
62
63         switch (inoinfo(ROOTINO)->ino_state) {
64
65         case USTATE:
66                 pfatal("ROOT INODE UNALLOCATED");
67                 if (reply("ALLOCATE") == 0) {
68                         ckfini(0);
69                         exit(EEXIT);
70                 }
71                 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
72                         errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
73                 break;
74
75         case DCLEAR:
76                 pfatal("DUPS/BAD IN ROOT INODE");
77                 if (reply("REALLOCATE")) {
78                         freeino(ROOTINO);
79                         if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
80                                 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
81                         break;
82                 }
83                 if (reply("CONTINUE") == 0) {
84                         ckfini(0);
85                         exit(EEXIT);
86                 }
87                 break;
88
89         case FSTATE:
90         case FCLEAR:
91                 pfatal("ROOT INODE NOT DIRECTORY");
92                 if (reply("REALLOCATE")) {
93                         freeino(ROOTINO);
94                         if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
95                                 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
96                         break;
97                 }
98                 if (reply("FIX") == 0) {
99                         ckfini(0);
100                         exit(EEXIT);
101                 }
102                 dp = ginode(ROOTINO);
103                 dp->di_mode &= ~IFMT;
104                 dp->di_mode |= IFDIR;
105                 inodirty();
106                 break;
107
108         case DSTATE:
109                 break;
110
111         default:
112                 errx(EEXIT, "BAD STATE %d FOR ROOT INODE",
113                     inoinfo(ROOTINO)->ino_state);
114         }
115         inoinfo(ROOTINO)->ino_state = DFOUND;
116         if (newinofmt) {
117                 inoinfo(WINO)->ino_state = FSTATE;
118                 inoinfo(WINO)->ino_type = DT_WHT;
119         }
120         /*
121          * Sort the directory list into disk block order.
122          */
123         qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
124         /*
125          * Check the integrity of each directory.
126          */
127         memset(&curino, 0, sizeof(struct inodesc));
128         curino.id_type = DATA;
129         curino.id_func = pass2check;
130         dp = &dino;
131         inpend = &inpsort[inplast];
132         for (inpp = inpsort; inpp < inpend; inpp++) {
133                 if (got_siginfo) {
134                         printf("%s: phase 2: dir %d of %ld (%d%%)\n", cdevname,
135                             inpp - inpsort, inplast, (int)((inpp - inpsort) * 100 /
136                             inplast));
137                         got_siginfo = 0;
138                 }
139                 inp = *inpp;
140                 if (inp->i_isize == 0)
141                         continue;
142                 if (inp->i_isize < MINDIRSIZE) {
143                         direrror(inp->i_number, "DIRECTORY TOO SHORT");
144                         inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
145                         if (reply("FIX") == 1) {
146                                 dp = ginode(inp->i_number);
147                                 dp->di_size = inp->i_isize;
148                                 inodirty();
149                                 dp = &dino;
150                         }
151                 } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
152                         getpathname(pathbuf, inp->i_number, inp->i_number);
153                         if (usedsoftdep)
154                                 pfatal("%s %s: LENGTH %d NOT MULTIPLE OF %d",
155                                         "DIRECTORY", pathbuf, inp->i_isize,
156                                         DIRBLKSIZ);
157                         else
158                                 pwarn("%s %s: LENGTH %d NOT MULTIPLE OF %d",
159                                         "DIRECTORY", pathbuf, inp->i_isize,
160                                         DIRBLKSIZ);
161                         if (preen)
162                                 printf(" (ADJUSTED)\n");
163                         inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
164                         if (preen || reply("ADJUST") == 1) {
165                                 dp = ginode(inp->i_number);
166                                 dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
167                                 inodirty();
168                                 dp = &dino;
169                         }
170                 }
171                 memset(&dino, 0, sizeof(struct dinode));
172                 dino.di_mode = IFDIR;
173                 dp->di_size = inp->i_isize;
174                 memmove(&dp->di_db[0], &inp->i_blks[0], (size_t)inp->i_numblks);
175                 curino.id_number = inp->i_number;
176                 curino.id_parent = inp->i_parent;
177                 ckinode(dp, &curino);
178         }
179         /*
180          * Now that the parents of all directories have been found,
181          * make another pass to verify the value of `..'
182          */
183         for (inpp = inpsort; inpp < inpend; inpp++) {
184                 inp = *inpp;
185                 if (inp->i_parent == 0 || inp->i_isize == 0)
186                         continue;
187                 if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
188                     inoinfo(inp->i_number)->ino_state == DSTATE)
189                         inoinfo(inp->i_number)->ino_state = DFOUND;
190                 if (inp->i_dotdot == inp->i_parent ||
191                     inp->i_dotdot == (ino_t)-1)
192                         continue;
193                 if (inp->i_dotdot == 0) {
194                         inp->i_dotdot = inp->i_parent;
195                         fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
196                         if (reply("FIX") == 0)
197                                 continue;
198                         makeentry(inp->i_number, inp->i_parent, "..");
199                         inoinfo(inp->i_parent)->ino_linkcnt--;
200                         continue;
201                 }
202                 fileerror(inp->i_parent, inp->i_number,
203                     "BAD INODE NUMBER FOR '..'");
204                 if (reply("FIX") == 0)
205                         continue;
206                 inoinfo(inp->i_dotdot)->ino_linkcnt++;
207                 inoinfo(inp->i_parent)->ino_linkcnt--;
208                 inp->i_dotdot = inp->i_parent;
209                 changeino(inp->i_number, "..", inp->i_parent);
210         }
211         /*
212          * Mark all the directories that can be found from the root.
213          */
214         propagate();
215 }
216
217 static int
218 pass2check(struct inodesc *idesc)
219 {
220         register struct direct *dirp = idesc->id_dirp;
221         register struct inoinfo *inp;
222         int n, entrysize, ret = 0;
223         struct dinode *dp;
224         char *errmsg;
225         struct direct proto;
226         char namebuf[MAXPATHLEN + 1];
227         char pathbuf[MAXPATHLEN + 1];
228
229         /*
230          * If converting, set directory entry type.
231          */
232         if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) {
233                 dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
234                 ret |= ALTERED;
235         }
236         /*
237          * check for "."
238          */
239         if (idesc->id_entryno != 0)
240                 goto chk1;
241         if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
242                 if (dirp->d_ino != idesc->id_number) {
243                         direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
244                         dirp->d_ino = idesc->id_number;
245                         if (reply("FIX") == 1)
246                                 ret |= ALTERED;
247                 }
248                 if (newinofmt && dirp->d_type != DT_DIR) {
249                         direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
250                         dirp->d_type = DT_DIR;
251                         if (reply("FIX") == 1)
252                                 ret |= ALTERED;
253                 }
254                 goto chk1;
255         }
256         direrror(idesc->id_number, "MISSING '.'");
257         proto.d_ino = idesc->id_number;
258         if (newinofmt)
259                 proto.d_type = DT_DIR;
260         else
261                 proto.d_type = 0;
262         proto.d_namlen = 1;
263         strcpy(proto.d_name, ".");
264 #       if BYTE_ORDER == LITTLE_ENDIAN
265                 if (!newinofmt) {
266                         u_char tmp;
267
268                         tmp = proto.d_type;
269                         proto.d_type = proto.d_namlen;
270                         proto.d_namlen = tmp;
271                 }
272 #       endif
273         entrysize = DIRSIZ(0, &proto);
274         if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
275                 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
276                         dirp->d_name);
277         } else if (dirp->d_reclen < entrysize) {
278                 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
279         } else if (dirp->d_reclen < 2 * entrysize) {
280                 proto.d_reclen = dirp->d_reclen;
281                 memmove(dirp, &proto, (size_t)entrysize);
282                 if (reply("FIX") == 1)
283                         ret |= ALTERED;
284         } else {
285                 n = dirp->d_reclen - entrysize;
286                 proto.d_reclen = entrysize;
287                 memmove(dirp, &proto, (size_t)entrysize);
288                 idesc->id_entryno++;
289                 inoinfo(dirp->d_ino)->ino_linkcnt--;
290                 dirp = (struct direct *)((char *)(dirp) + entrysize);
291                 memset(dirp, 0, (size_t)n);
292                 dirp->d_reclen = n;
293                 if (reply("FIX") == 1)
294                         ret |= ALTERED;
295         }
296 chk1:
297         if (idesc->id_entryno > 1)
298                 goto chk2;
299         inp = getinoinfo(idesc->id_number);
300         proto.d_ino = inp->i_parent;
301         if (newinofmt)
302                 proto.d_type = DT_DIR;
303         else
304                 proto.d_type = 0;
305         proto.d_namlen = 2;
306         strcpy(proto.d_name, "..");
307 #       if BYTE_ORDER == LITTLE_ENDIAN
308                 if (!newinofmt) {
309                         u_char tmp;
310
311                         tmp = proto.d_type;
312                         proto.d_type = proto.d_namlen;
313                         proto.d_namlen = tmp;
314                 }
315 #       endif
316         entrysize = DIRSIZ(0, &proto);
317         if (idesc->id_entryno == 0) {
318                 n = DIRSIZ(0, dirp);
319                 if (dirp->d_reclen < n + entrysize)
320                         goto chk2;
321                 proto.d_reclen = dirp->d_reclen - n;
322                 dirp->d_reclen = n;
323                 idesc->id_entryno++;
324                 inoinfo(dirp->d_ino)->ino_linkcnt--;
325                 dirp = (struct direct *)((char *)(dirp) + n);
326                 memset(dirp, 0, (size_t)proto.d_reclen);
327                 dirp->d_reclen = proto.d_reclen;
328         }
329         if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
330                 inp->i_dotdot = dirp->d_ino;
331                 if (newinofmt && dirp->d_type != DT_DIR) {
332                         direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
333                         dirp->d_type = DT_DIR;
334                         if (reply("FIX") == 1)
335                                 ret |= ALTERED;
336                 }
337                 goto chk2;
338         }
339         if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
340                 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
341                 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
342                         dirp->d_name);
343                 inp->i_dotdot = (ino_t)-1;
344         } else if (dirp->d_reclen < entrysize) {
345                 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
346                 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
347                 inp->i_dotdot = (ino_t)-1;
348         } else if (inp->i_parent != 0) {
349                 /*
350                  * We know the parent, so fix now.
351                  */
352                 inp->i_dotdot = inp->i_parent;
353                 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
354                 proto.d_reclen = dirp->d_reclen;
355                 memmove(dirp, &proto, (size_t)entrysize);
356                 if (reply("FIX") == 1)
357                         ret |= ALTERED;
358         }
359         idesc->id_entryno++;
360         if (dirp->d_ino != 0)
361                 inoinfo(dirp->d_ino)->ino_linkcnt--;
362         return (ret|KEEPON);
363 chk2:
364         if (dirp->d_ino == 0)
365                 return (ret|KEEPON);
366         if (dirp->d_namlen <= 2 &&
367             dirp->d_name[0] == '.' &&
368             idesc->id_entryno >= 2) {
369                 if (dirp->d_namlen == 1) {
370                         direrror(idesc->id_number, "EXTRA '.' ENTRY");
371                         dirp->d_ino = 0;
372                         if (reply("FIX") == 1)
373                                 ret |= ALTERED;
374                         return (KEEPON | ret);
375                 }
376                 if (dirp->d_name[1] == '.') {
377                         direrror(idesc->id_number, "EXTRA '..' ENTRY");
378                         dirp->d_ino = 0;
379                         if (reply("FIX") == 1)
380                                 ret |= ALTERED;
381                         return (KEEPON | ret);
382                 }
383         }
384         idesc->id_entryno++;
385         n = 0;
386         if (dirp->d_ino > maxino) {
387                 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
388                 n = reply("REMOVE");
389         } else if (newinofmt &&
390                    ((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
391                     (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
392                 fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
393                 dirp->d_ino = WINO;
394                 dirp->d_type = DT_WHT;
395                 if (reply("FIX") == 1)
396                         ret |= ALTERED;
397         } else {
398 again:
399                 switch (inoinfo(dirp->d_ino)->ino_state) {
400                 case USTATE:
401                         if (idesc->id_entryno <= 2)
402                                 break;
403                         fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
404                         n = reply("REMOVE");
405                         break;
406
407                 case DCLEAR:
408                 case FCLEAR:
409                         if (idesc->id_entryno <= 2)
410                                 break;
411                         if (inoinfo(dirp->d_ino)->ino_state == FCLEAR)
412                                 errmsg = "DUP/BAD";
413                         else if (!preen && !usedsoftdep)
414                                 errmsg = "ZERO LENGTH DIRECTORY";
415                         else {
416                                 n = 1;
417                                 break;
418                         }
419                         fileerror(idesc->id_number, dirp->d_ino, errmsg);
420                         if ((n = reply("REMOVE")) == 1)
421                                 break;
422                         dp = ginode(dirp->d_ino);
423                         inoinfo(dirp->d_ino)->ino_state =
424                             (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
425                         inoinfo(dirp->d_ino)->ino_linkcnt = dp->di_nlink;
426                         goto again;
427
428                 case DSTATE:
429                         if (inoinfo(idesc->id_number)->ino_state == DFOUND)
430                                 inoinfo(dirp->d_ino)->ino_state = DFOUND;
431                         /* fall through */
432
433                 case DFOUND:
434                         inp = getinoinfo(dirp->d_ino);
435                         if (inp->i_parent != 0 && idesc->id_entryno > 2) {
436                                 getpathname(pathbuf, idesc->id_number,
437                                     idesc->id_number);
438                                 getpathname(namebuf, dirp->d_ino, dirp->d_ino);
439                                 pwarn("%s%s%s %s %s\n", pathbuf,
440                                     (strcmp(pathbuf, "/") == 0 ? "" : "/"),
441                                     dirp->d_name,
442                                     "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
443                                     namebuf);
444                                 if (preen) {
445                                         printf(" (REMOVED)\n");
446                                         n = 1;
447                                         break;
448                                 }
449                                 if ((n = reply("REMOVE")) == 1)
450                                         break;
451                         }
452                         if (idesc->id_entryno > 2)
453                                 inp->i_parent = idesc->id_number;
454                         /* fall through */
455
456                 case FSTATE:
457                         if (newinofmt &&
458                             dirp->d_type != inoinfo(dirp->d_ino)->ino_type) {
459                                 fileerror(idesc->id_number, dirp->d_ino,
460                                     "BAD TYPE VALUE");
461                                 dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
462                                 if (reply("FIX") == 1)
463                                         ret |= ALTERED;
464                         }
465                         inoinfo(dirp->d_ino)->ino_linkcnt--;
466                         break;
467
468                 default:
469                         errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
470                             inoinfo(dirp->d_ino)->ino_state, dirp->d_ino);
471                 }
472         }
473         if (n == 0)
474                 return (ret|KEEPON);
475         dirp->d_ino = 0;
476         return (ret|KEEPON|ALTERED);
477 }
478
479 /*
480  * Routine to sort disk blocks.
481  */
482 static int
483 blksort(const void *arg1, const void *arg2)
484 {
485
486         return ((*(struct inoinfo **)arg1)->i_blks[0] -
487                 (*(struct inoinfo **)arg2)->i_blks[0]);
488 }