386BSD 0.1 development
[unix-history] / usr / src / sbin / fsck / pass2.c
index 4823557..70bc605 100644 (file)
@@ -2,21 +2,37 @@
  * Copyright (c) 1980, 1986 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980, 1986 The Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)pass2.c    5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)pass2.c    5.17 (Berkeley) 12/28/90";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -25,7 +41,8 @@ static char sccsid[] = "@(#)pass2.c   5.10 (Berkeley) %G%";
 #define KERNEL
 #include <ufs/dir.h>
 #undef KERNEL
 #define KERNEL
 #include <ufs/dir.h>
 #undef KERNEL
-#include <strings.h>
+#include <stdlib.h>
+#include <string.h>
 #include "fsck.h"
 
 #define MINDIRSIZE     (sizeof (struct dirtemplate))
 #include "fsck.h"
 
 #define MINDIRSIZE     (sizeof (struct dirtemplate))
@@ -90,28 +107,30 @@ pass2()
        /*
         * Sort the directory list into disk block order.
         */
        /*
         * Sort the directory list into disk block order.
         */
-       qsort((char *)inpsort, (int)inplast, sizeof *inpsort, blksort);
+       qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
        /*
         * Check the integrity of each directory.
         */
        bzero((char *)&curino, sizeof(struct inodesc));
        curino.id_type = DATA;
        curino.id_func = pass2check;
        /*
         * Check the integrity of each directory.
         */
        bzero((char *)&curino, sizeof(struct inodesc));
        curino.id_type = DATA;
        curino.id_func = pass2check;
+       dino.di_mode = IFDIR;
        dp = &dino;
        inpend = &inpsort[inplast];
        for (inpp = inpsort; inpp < inpend; inpp++) {
                inp = *inpp;
        dp = &dino;
        inpend = &inpsort[inplast];
        for (inpp = inpsort; inpp < inpend; inpp++) {
                inp = *inpp;
+               if (inp->i_isize == 0)
+                       continue;
                if (inp->i_isize < MINDIRSIZE) {
                        direrror(inp->i_number, "DIRECTORY TOO SHORT");
                if (inp->i_isize < MINDIRSIZE) {
                        direrror(inp->i_number, "DIRECTORY TOO SHORT");
-                       inp->i_isize = MINDIRSIZE;
+                       inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
                        if (reply("FIX") == 1) {
                                dp = ginode(inp->i_number);
                        if (reply("FIX") == 1) {
                                dp = ginode(inp->i_number);
-                               dp->di_size = MINDIRSIZE;
+                               dp->di_size = inp->i_isize;
                                inodirty();
                                dp = &dino;
                        }
                                inodirty();
                                dp = &dino;
                        }
-               }
-               if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
+               } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
                        getpathname(pathbuf, inp->i_number, inp->i_number);
                        pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
                                pathbuf, inp->i_isize, DIRBLKSIZ);
                        getpathname(pathbuf, inp->i_number, inp->i_number);
                        pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
                                pathbuf, inp->i_isize, DIRBLKSIZ);
@@ -127,7 +146,7 @@ pass2()
                }
                dp->di_size = inp->i_isize;
                bcopy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
                }
                dp->di_size = inp->i_isize;
                bcopy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
-                       (int)inp->i_numblks);
+                       (size_t)inp->i_numblks);
                curino.id_number = inp->i_number;
                curino.id_parent = inp->i_parent;
                (void)ckinode(dp, &curino);
                curino.id_number = inp->i_number;
                curino.id_parent = inp->i_parent;
                (void)ckinode(dp, &curino);
@@ -138,7 +157,7 @@ pass2()
         */
        for (inpp = inpsort; inpp < inpend; inpp++) {
                inp = *inpp;
         */
        for (inpp = inpsort; inpp < inpend; inpp++) {
                inp = *inpp;
-               if (inp->i_parent == 0)
+               if (inp->i_parent == 0 || inp->i_isize == 0)
                        continue;
                if (statemap[inp->i_parent] == DFOUND &&
                    statemap[inp->i_number] == DSTATE)
                        continue;
                if (statemap[inp->i_parent] == DFOUND &&
                    statemap[inp->i_number] == DSTATE)
@@ -151,7 +170,7 @@ pass2()
                        fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
                        if (reply("FIX") == 0)
                                continue;
                        fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
                        if (reply("FIX") == 0)
                                continue;
-                       makeentry(inp->i_number, inp->i_parent, "..");
+                       (void)makeentry(inp->i_number, inp->i_parent, "..");
                        lncntp[inp->i_parent]--;
                        continue;
                }
                        lncntp[inp->i_parent]--;
                        continue;
                }
@@ -177,6 +196,7 @@ pass2check(idesc)
        register struct inoinfo *inp;
        int n, entrysize, ret = 0;
        struct dinode *dp;
        register struct inoinfo *inp;
        int n, entrysize, ret = 0;
        struct dinode *dp;
+       char *errmsg;
        struct direct proto;
        char namebuf[MAXPATHLEN + 1];
        char pathbuf[MAXPATHLEN + 1];
        struct direct proto;
        char namebuf[MAXPATHLEN + 1];
        char pathbuf[MAXPATHLEN + 1];
@@ -207,17 +227,17 @@ pass2check(idesc)
                pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
        } else if (dirp->d_reclen < 2 * entrysize) {
                proto.d_reclen = dirp->d_reclen;
                pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
        } else if (dirp->d_reclen < 2 * entrysize) {
                proto.d_reclen = dirp->d_reclen;
-               bcopy((char *)&proto, (char *)dirp, entrysize);
+               bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
                if (reply("FIX") == 1)
                        ret |= ALTERED;
        } else {
                n = dirp->d_reclen - entrysize;
                proto.d_reclen = entrysize;
                if (reply("FIX") == 1)
                        ret |= ALTERED;
        } else {
                n = dirp->d_reclen - entrysize;
                proto.d_reclen = entrysize;
-               bcopy((char *)&proto, (char *)dirp, entrysize);
+               bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
                idesc->id_entryno++;
                lncntp[dirp->d_ino]--;
                dirp = (struct direct *)((char *)(dirp) + entrysize);
                idesc->id_entryno++;
                lncntp[dirp->d_ino]--;
                dirp = (struct direct *)((char *)(dirp) + entrysize);
-               bzero((char *)dirp, n);
+               bzero((char *)dirp, (size_t)n);
                dirp->d_reclen = n;
                if (reply("FIX") == 1)
                        ret |= ALTERED;
                dirp->d_reclen = n;
                if (reply("FIX") == 1)
                        ret |= ALTERED;
@@ -239,7 +259,7 @@ chk1:
                idesc->id_entryno++;
                lncntp[dirp->d_ino]--;
                dirp = (struct direct *)((char *)(dirp) + n);
                idesc->id_entryno++;
                lncntp[dirp->d_ino]--;
                dirp = (struct direct *)((char *)(dirp) + n);
-               bzero((char *)dirp, (int)proto.d_reclen);
+               bzero((char *)dirp, (size_t)proto.d_reclen);
                dirp->d_reclen = proto.d_reclen;
        }
        if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
                dirp->d_reclen = proto.d_reclen;
        }
        if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
@@ -262,7 +282,7 @@ chk1:
                inp->i_dotdot = inp->i_parent;
                fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
                proto.d_reclen = dirp->d_reclen;
                inp->i_dotdot = inp->i_parent;
                fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
                proto.d_reclen = dirp->d_reclen;
-               bcopy((char *)&proto, (char *)dirp, entrysize);
+               bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
                if (reply("FIX") == 1)
                        ret |= ALTERED;
        }
                if (reply("FIX") == 1)
                        ret |= ALTERED;
        }
@@ -293,7 +313,7 @@ chk2:
        }
        idesc->id_entryno++;
        n = 0;
        }
        idesc->id_entryno++;
        n = 0;
-       if (dirp->d_ino > maxino || dirp->d_ino <= 0) {
+       if (dirp->d_ino > maxino) {
                fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
                n = reply("REMOVE");
        } else {
                fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
                n = reply("REMOVE");
        } else {
@@ -310,7 +330,11 @@ again:
                case FCLEAR:
                        if (idesc->id_entryno <= 2)
                                break;
                case FCLEAR:
                        if (idesc->id_entryno <= 2)
                                break;
-                       fileerror(idesc->id_number, dirp->d_ino, "DUP/BAD");
+                       if (statemap[dirp->d_ino] == DCLEAR)
+                               errmsg = "ZERO LENGTH DIRECTORY";
+                       else
+                               errmsg = "DUP/BAD";
+                       fileerror(idesc->id_number, dirp->d_ino, errmsg);
                        if ((n = reply("REMOVE")) == 1)
                                break;
                        dp = ginode(dirp->d_ino);
                        if ((n = reply("REMOVE")) == 1)
                                break;
                        dp = ginode(dirp->d_ino);
@@ -326,13 +350,6 @@ again:
 
                case DFOUND:
                        inp = getinoinfo(dirp->d_ino);
 
                case DFOUND:
                        inp = getinoinfo(dirp->d_ino);
-                       if (inp->i_isize == 0) {
-                               direrror(dirp->d_ino, "ZERO LENGTH DIRECTORY");
-                               if ((n = reply("REMOVE")) == 1) {
-                                       statemap[dirp->d_ino] = DCLEAR;
-                                       break;
-                               }
-                       }
                        if (inp->i_parent != 0 && idesc->id_entryno > 2) {
                                getpathname(pathbuf, idesc->id_number,
                                    idesc->id_number);
                        if (inp->i_parent != 0 && idesc->id_entryno > 2) {
                                getpathname(pathbuf, idesc->id_number,
                                    idesc->id_number);