aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

pass2.c (15714B)


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