aruu

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

inode.c (21881B)


      1 /*	$NetBSD: inode.c,v 1.37 2016/08/04 17:43:47 jdolecek 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[] = "@(#)inode.c	8.5 (Berkeley) 2/8/95";
     59 #else
     60 __RCSID("$NetBSD: inode.c,v 1.37 2016/08/04 17:43:47 jdolecek 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 #ifndef SMALL
     71 #include <pwd.h>
     72 #endif
     73 #include <stdio.h>
     74 #include <stdlib.h>
     75 #include <string.h>
     76 #include <time.h>
     77 
     78 #include "fsck.h"
     79 #include "fsutil.h"
     80 #include "extern.h"
     81 
     82 /*
     83  * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
     84  * here.
     85  */
     86 
     87 #define fsck_ino_to_fsba(fs, x)                                                                    \
     88   (fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables)                                        \
     89    + (((x) - 1) % (fs)->e2fs.e2fs_ipg) / (fs)->e2fs_ipb)
     90 
     91 static ino_t startinum;
     92 
     93 static int iblock(struct inodesc *, long, u_int64_t);
     94 
     95 static int setlarge(void);
     96 
     97 static int sethuge(void);
     98 
     99 static int
    100 setlarge(void)
    101 {
    102   if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
    103     pfatal("LARGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
    104     return 0;
    105   }
    106   if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE)) {
    107     if (preen)
    108       pwarn("SETTING LARGE FILE INDICATOR\n");
    109     else if (!reply("SET LARGE FILE INDICATOR"))
    110       return 0;
    111     sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGEFILE;
    112     sbdirty();
    113   }
    114   return 1;
    115 }
    116 
    117 static int
    118 sethuge(void)
    119 {
    120   if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
    121     pfatal("HUGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
    122     return 0;
    123   }
    124   if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_HUGE_FILE)) {
    125     if (preen)
    126       pwarn("SETTING HUGE FILE FEATURE\n");
    127     else if (!reply("SET HUGE FILE FEATURE"))
    128       return 0;
    129     sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_HUGE_FILE;
    130     sbdirty();
    131   }
    132   return 1;
    133 }
    134 
    135 u_int64_t
    136 inosize(struct ext2fs_dinode *dp)
    137 {
    138   u_int64_t size = fs2h32(dp->e2di_size);
    139 
    140   if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG)
    141     size |= (u_int64_t)fs2h32(dp->e2di_size_high) << 32;
    142   if (size > INT32_MAX)
    143     (void)setlarge();
    144   return size;
    145 }
    146 
    147 void
    148 inossize(struct ext2fs_dinode *dp, u_int64_t size)
    149 {
    150   if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG) {
    151     dp->e2di_size_high = h2fs32(size >> 32);
    152     if (size > INT32_MAX)
    153       if (!setlarge())
    154         return;
    155   } else if (size > INT32_MAX) {
    156     pfatal(
    157         "TRYING TO SET FILESIZE TO %llu ON MODE %x FILE\n",
    158         (unsigned long long)size,
    159         fs2h16(dp->e2di_mode) & IFMT
    160     );
    161     return;
    162   }
    163   dp->e2di_size = h2fs32(size);
    164 }
    165 
    166 int
    167 ckinode(struct ext2fs_dinode *dp, struct inodesc *idesc)
    168 {
    169   u_int32_t           *ap;
    170   long                 ret, n, ndb;
    171   struct ext2fs_dinode dino;
    172   u_int64_t            remsize, sizepb;
    173   mode_t               mode;
    174   char                 pathbuf[MAXPATHLEN + 1];
    175 
    176   if (idesc->id_fix != IGNORE)
    177     idesc->id_fix = DONTKNOW;
    178   idesc->id_entryno  = 0;
    179   idesc->id_filesize = inosize(dp);
    180   mode               = fs2h16(dp->e2di_mode) & IFMT;
    181   if (mode == IFBLK || mode == IFCHR || mode == IFIFO
    182       || (mode == IFLNK && (inosize(dp) < EXT2_MAXSYMLINKLEN)))
    183     return (KEEPON);
    184   dino = *dp;
    185   ndb  = howmany(inosize(&dino), sblock.e2fs_bsize);
    186   for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[EXT2FS_NDADDR]; ap++, ndb--) {
    187     idesc->id_numfrags = 1;
    188     if (*ap == 0) {
    189       if (idesc->id_type == DATA && ndb > 0) {
    190         /* An empty block in a directory XXX */
    191         getpathname(pathbuf, sizeof(pathbuf), idesc->id_number, idesc->id_number);
    192         pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", pathbuf);
    193         if (reply("ADJUST LENGTH") == 1) {
    194           dp = ginode(idesc->id_number);
    195           inossize(dp, (ap - &dino.e2di_blocks[0]) * sblock.e2fs_bsize);
    196           printf("YOU MUST RERUN FSCK AFTERWARDS\n");
    197           rerun = 1;
    198           inodirty();
    199         }
    200       }
    201       continue;
    202     }
    203     idesc->id_blkno = fs2h32(*ap);
    204     if (idesc->id_type == ADDR)
    205       ret = (*idesc->id_func)(idesc);
    206     else
    207       ret = dirscan(idesc);
    208     if (ret & STOP)
    209       return (ret);
    210   }
    211   idesc->id_numfrags = 1;
    212   remsize            = inosize(&dino) - sblock.e2fs_bsize * EXT2FS_NDADDR;
    213   sizepb             = sblock.e2fs_bsize;
    214   for (ap = &dino.e2di_blocks[EXT2FS_NDADDR], n = 1; n <= EXT2FS_NIADDR; ap++, n++) {
    215     if (*ap) {
    216       idesc->id_blkno = fs2h32(*ap);
    217       ret             = iblock(idesc, n, remsize);
    218       if (ret & STOP)
    219         return (ret);
    220     } else {
    221       if (idesc->id_type == DATA && remsize > 0) {
    222         /* An empty block in a directory XXX */
    223         getpathname(pathbuf, sizeof(pathbuf), idesc->id_number, idesc->id_number);
    224         pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", pathbuf);
    225         if (reply("ADJUST LENGTH") == 1) {
    226           dp = ginode(idesc->id_number);
    227           inossize(dp, inosize(dp) - remsize);
    228           remsize = 0;
    229           printf("YOU MUST RERUN FSCK AFTERWARDS\n");
    230           rerun = 1;
    231           inodirty();
    232           break;
    233         }
    234       }
    235     }
    236     sizepb *= EXT2_NINDIR(&sblock);
    237     remsize -= sizepb;
    238   }
    239   return (KEEPON);
    240 }
    241 
    242 static int
    243 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
    244 {
    245   /* XXX ondisk32 */
    246   int32_t              *ap;
    247   int32_t              *aplim;
    248   struct bufarea       *bp;
    249   int                   i, n, (*func)(struct inodesc *);
    250   size_t                nif;
    251   u_int64_t             sizepb;
    252   char                  buf[BUFSIZ];
    253   char                  pathbuf[MAXPATHLEN + 1];
    254   struct ext2fs_dinode *dp;
    255 
    256   if (idesc->id_type == ADDR) {
    257     func = idesc->id_func;
    258     if (((n = (*func)(idesc)) & KEEPON) == 0)
    259       return (n);
    260   } else
    261     func = dirscan;
    262   if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    263     return (SKIP);
    264   bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
    265   ilevel--;
    266   for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
    267     sizepb *= EXT2_NINDIR(&sblock);
    268   if (isize > sizepb * EXT2_NINDIR(&sblock))
    269     nif = EXT2_NINDIR(&sblock);
    270   else
    271     nif = howmany(isize, sizepb);
    272   if (idesc->id_func == pass1check && nif < EXT2_NINDIR(&sblock)) {
    273     aplim = &bp->b_un.b_indir[EXT2_NINDIR(&sblock)];
    274     for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
    275       if (*ap == 0)
    276         continue;
    277       (void)snprintf(
    278           buf, sizeof(buf), "PARTIALLY TRUNCATED INODE I=%llu", (unsigned long long)idesc->id_number
    279       );
    280       if (dofix(idesc, buf)) {
    281         *ap = 0;
    282         dirty(bp);
    283       }
    284     }
    285     flush(fswritefd, bp);
    286   }
    287   aplim = &bp->b_un.b_indir[nif];
    288   for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
    289     if (*ap) {
    290       idesc->id_blkno = fs2h32(*ap);
    291       if (ilevel == 0)
    292         n = (*func)(idesc);
    293       else
    294         n = iblock(idesc, ilevel, isize);
    295       if (n & STOP) {
    296         bp->b_flags &= ~B_INUSE;
    297         return (n);
    298       }
    299     } else {
    300       if (idesc->id_type == DATA && isize > 0) {
    301         /* An empty block in a directory XXX */
    302         getpathname(pathbuf, sizeof(pathbuf), idesc->id_number, idesc->id_number);
    303         pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", pathbuf);
    304         if (reply("ADJUST LENGTH") == 1) {
    305           dp = ginode(idesc->id_number);
    306           inossize(dp, inosize(dp) - isize);
    307           isize = 0;
    308           printf("YOU MUST RERUN FSCK AFTERWARDS\n");
    309           rerun = 1;
    310           inodirty();
    311           bp->b_flags &= ~B_INUSE;
    312           return (STOP);
    313         }
    314       }
    315     }
    316     isize -= sizepb;
    317   }
    318   bp->b_flags &= ~B_INUSE;
    319   return (KEEPON);
    320 }
    321 
    322 /*
    323  * Check that a block in a legal block number.
    324  * Return 0 if in range, 1 if out of range.
    325  */
    326 int
    327 chkrange(daddr_t blk, int cnt)
    328 {
    329   int c, overh;
    330 
    331   if ((unsigned int)(blk + cnt) > maxfsblock)
    332     return (1);
    333   c     = dtog(&sblock, blk);
    334   overh = cgoverhead(c);
    335   if (blk < sblock.e2fs.e2fs_bpg * c + overh + sblock.e2fs.e2fs_first_dblock) {
    336     if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh + sblock.e2fs.e2fs_first_dblock) {
    337       if (debug) {
    338         printf(
    339             "blk %lld < cgdmin %d;",
    340             (long long)blk,
    341             sblock.e2fs.e2fs_bpg * c + overh + sblock.e2fs.e2fs_first_dblock
    342         );
    343         printf(
    344             " blk + cnt %lld > cgsbase %d\n",
    345             (long long)(blk + cnt),
    346             sblock.e2fs.e2fs_bpg * c + overh + sblock.e2fs.e2fs_first_dblock
    347         );
    348       }
    349       return (1);
    350     }
    351   } else {
    352     if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh + sblock.e2fs.e2fs_first_dblock) {
    353       if (debug) {
    354         printf(
    355             "blk %lld >= cgdmin %d;",
    356             (long long)blk,
    357             sblock.e2fs.e2fs_bpg * c + overh + sblock.e2fs.e2fs_first_dblock
    358         );
    359         printf(
    360             " blk + cnt %lld > cgdmax %d\n",
    361             (long long)(blk + cnt),
    362             sblock.e2fs.e2fs_bpg * (c + 1) + overh + sblock.e2fs.e2fs_first_dblock
    363         );
    364       }
    365       return (1);
    366     }
    367   }
    368   return (0);
    369 }
    370 
    371 /*
    372  * General purpose interface for reading inodes.
    373  */
    374 struct ext2fs_dinode *
    375 ginode(ino_t inumber)
    376 {
    377   daddr_t               iblk;
    378   struct ext2fs_dinode *dp;
    379 
    380   if ((inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO
    381        && !(
    382            inumber == EXT2_RESIZEINO
    383            && (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0
    384        ))
    385       || inumber > maxino)
    386     errexit("bad inode number %llu to ginode", (unsigned long long)inumber);
    387   if (startinum == 0 || inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
    388     iblk = fsck_ino_to_fsba(&sblock, inumber);
    389     if (pbp != 0)
    390       pbp->b_flags &= ~B_INUSE;
    391     pbp       = getdatablk(iblk, sblock.e2fs_bsize);
    392     startinum = ((inumber - 1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
    393   }
    394   dp = (struct ext2fs_dinode *)(pbp->b_un.b_buf
    395                                 + EXT2_DINODE_SIZE(&sblock) * ino_to_fsbo(&sblock, inumber));
    396 
    397   return dp;
    398 }
    399 
    400 /*
    401  * Special purpose version of ginode used to optimize first pass
    402  * over all the inodes in numerical order.
    403  */
    404 ino_t nextino, lastinum;
    405 long  readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    406 char *inodebuf;
    407 
    408 struct ext2fs_dinode *
    409 getnextinode(ino_t inumber)
    410 {
    411   long                  size;
    412   daddr_t               dblk;
    413   struct ext2fs_dinode *dp;
    414   static char          *bp;
    415 
    416   if (inumber != nextino++ || inumber > maxino)
    417     errexit("bad inode number %llu to nextinode", (unsigned long long)inumber);
    418   if (inumber >= lastinum) {
    419     readcnt++;
    420     dblk = EXT2_FSBTODB(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
    421     if (readcnt % readpercg == 0) {
    422       size = partialsize;
    423       lastinum += partialcnt;
    424     } else {
    425       size = inobufsize;
    426       lastinum += fullcnt;
    427     }
    428     (void)bread(fsreadfd, inodebuf, dblk, size);
    429     bp = inodebuf;
    430   }
    431   dp = (struct ext2fs_dinode *)bp;
    432   bp += EXT2_DINODE_SIZE(&sblock);
    433 
    434   return dp;
    435 }
    436 
    437 void
    438 resetinodebuf(void)
    439 {
    440   startinum   = 0;
    441   nextino     = 1;
    442   lastinum    = 1;
    443   readcnt     = 0;
    444   inobufsize  = ext2_blkroundup(&sblock, INOBUFSIZE);
    445   fullcnt     = inobufsize / EXT2_DINODE_SIZE(&sblock);
    446   readpercg   = sblock.e2fs.e2fs_ipg / fullcnt;
    447   partialcnt  = sblock.e2fs.e2fs_ipg % fullcnt;
    448   partialsize = partialcnt * EXT2_DINODE_SIZE(&sblock);
    449   if (partialcnt != 0) {
    450     readpercg++;
    451   } else {
    452     partialcnt  = fullcnt;
    453     partialsize = inobufsize;
    454   }
    455   if (inodebuf == NULL && (inodebuf = malloc((unsigned int)inobufsize)) == NULL)
    456     errexit("Cannot allocate space for inode buffer");
    457   while (nextino < EXT2_ROOTINO)
    458     (void)getnextinode(nextino);
    459 }
    460 
    461 void
    462 freeinodebuf(void)
    463 {
    464   if (inodebuf != NULL)
    465     free(inodebuf);
    466   inodebuf = NULL;
    467 }
    468 
    469 /*
    470  * Routines to maintain information about directory inodes.
    471  * This is built during the first pass and used during the
    472  * second and third passes.
    473  *
    474  * Enter inodes into the cache.
    475  */
    476 void
    477 cacheino(struct ext2fs_dinode *dp, ino_t inumber)
    478 {
    479   struct inoinfo  *inp;
    480   struct inoinfo **inpp;
    481   unsigned int     blks;
    482 
    483   blks = howmany(inosize(dp), sblock.e2fs_bsize);
    484   if (blks > EXT2FS_NDADDR)
    485     blks = EXT2FS_NDADDR + EXT2FS_NIADDR;
    486   /* XXX ondisk32 */
    487   inp = malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
    488   if (inp == NULL)
    489     return;
    490   inpp            = &inphead[inumber % numdirs];
    491   inp->i_nexthash = *inpp;
    492   *inpp           = inp;
    493   inp->i_child = inp->i_sibling = inp->i_parentp = 0;
    494   if (inumber == EXT2_ROOTINO)
    495     inp->i_parent = EXT2_ROOTINO;
    496   else
    497     inp->i_parent = (ino_t)0;
    498   inp->i_dotdot = (ino_t)0;
    499   inp->i_number = inumber;
    500   inp->i_isize  = inosize(dp);
    501   /* XXX ondisk32 */
    502   inp->i_numblks = blks * sizeof(int32_t);
    503   memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
    504   if (inplast == listmax) {
    505     listmax += 100;
    506     inpsort = (struct inoinfo **)realloc(
    507         (char *)inpsort, (unsigned int)listmax * sizeof(struct inoinfo *)
    508     );
    509     if (inpsort == NULL)
    510       errexit("cannot increase directory list");
    511   }
    512   inpsort[inplast++] = inp;
    513 }
    514 
    515 /*
    516  * Look up an inode cache structure.
    517  */
    518 struct inoinfo *
    519 getinoinfo(ino_t inumber)
    520 {
    521   struct inoinfo *inp;
    522 
    523   for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    524     if (inp->i_number != inumber)
    525       continue;
    526     return (inp);
    527   }
    528   errexit("cannot find inode %llu", (unsigned long long)inumber);
    529   return ((struct inoinfo *)0);
    530 }
    531 
    532 /*
    533  * Clean up all the inode cache structure.
    534  */
    535 void
    536 inocleanup(void)
    537 {
    538   struct inoinfo **inpp;
    539 
    540   if (inphead == NULL)
    541     return;
    542   for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    543     free(*inpp);
    544   free(inphead);
    545   free(inpsort);
    546   inphead = inpsort = NULL;
    547 }
    548 
    549 void
    550 inodirty(void)
    551 {
    552   dirty(pbp);
    553 }
    554 
    555 void
    556 clri(struct inodesc *idesc, const char *type, int flag)
    557 {
    558   struct ext2fs_dinode *dp;
    559 
    560   dp = ginode(idesc->id_number);
    561   if (flag == 1) {
    562     pwarn("%s %s", type, (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
    563     pinode(idesc->id_number);
    564   }
    565   if (preen || reply("CLEAR") == 1) {
    566     if (preen)
    567       printf(" (CLEARED)\n");
    568     n_files--;
    569     (void)ckinode(dp, idesc);
    570     clearinode(dp);
    571     statemap[idesc->id_number] = USTATE;
    572     inodirty();
    573   }
    574 }
    575 
    576 int
    577 findname(struct inodesc *idesc)
    578 {
    579   struct ext2fs_direct *dirp   = idesc->id_dirp;
    580   u_int16_t             namlen = dirp->e2d_namlen;
    581   /* from utilities.c namebuf[] variable */
    582   char *buf = __UNCONST(idesc->id_name);
    583   if (namlen > MAXPATHLEN) {
    584     /* XXX: Prevent overflow but don't fix */
    585     namlen = MAXPATHLEN;
    586   }
    587 
    588   if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
    589     return (KEEPON);
    590   (void)memcpy(buf, dirp->e2d_name, (size_t)namlen);
    591   buf[namlen] = '\0';
    592   return (STOP | FOUND);
    593 }
    594 
    595 int
    596 findino(struct inodesc *idesc)
    597 {
    598   struct ext2fs_direct *dirp = idesc->id_dirp;
    599   u_int32_t             ino  = fs2h32(dirp->e2d_ino);
    600 
    601   if (ino == 0)
    602     return (KEEPON);
    603   if (strcmp(dirp->e2d_name, idesc->id_name) == 0 && (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
    604       && ino <= maxino) {
    605     idesc->id_parent = ino;
    606     return (STOP | FOUND);
    607   }
    608   return (KEEPON);
    609 }
    610 
    611 void
    612 pinode(ino_t ino)
    613 {
    614   struct ext2fs_dinode *dp;
    615   struct passwd        *pw;
    616   uid_t                 uid;
    617 
    618   printf(" I=%llu ", (unsigned long long)ino);
    619   if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
    620     return;
    621   dp  = ginode(ino);
    622   uid = fs2h16(dp->e2di_uid);
    623   if (sblock.e2fs.e2fs_rev > E2FS_REV0)
    624     uid |= fs2h16(dp->e2di_uid_high) << 16;
    625   printf(" OWNER=");
    626 #ifndef SMALL
    627   if (Uflag && (pw = getpwuid(uid)) != 0)
    628     printf("%s ", pw->pw_name);
    629   else
    630 #endif
    631     printf("%u ", (unsigned int)uid);
    632   printf("MODE=%o\n", fs2h16(dp->e2di_mode));
    633   if (preen)
    634     printf("%s: ", cdevname());
    635   printf("SIZE=%llu ", (long long)inosize(dp));
    636   printf("MTIME=%s ", print_mtime(fs2h32(dp->e2di_mtime)));
    637 }
    638 
    639 void
    640 blkerror(ino_t ino, const char *type, daddr_t blk)
    641 {
    642   pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
    643   printf("\n");
    644   switch (statemap[ino]) {
    645     case FSTATE:
    646       statemap[ino] = FCLEAR;
    647       return;
    648 
    649     case DSTATE:
    650       statemap[ino] = DCLEAR;
    651       return;
    652 
    653     case FCLEAR:
    654     case DCLEAR:
    655       return;
    656 
    657     default:
    658       errexit("BAD STATE %d TO BLKERR", statemap[ino]);
    659       /* NOTREACHED */
    660   }
    661 }
    662 
    663 /*
    664  * allocate an unused inode
    665  */
    666 ino_t
    667 allocino(ino_t request, int type)
    668 {
    669   ino_t                 ino;
    670   struct ext2fs_dinode *dp;
    671   time_t                t;
    672 
    673   if (request == 0)
    674     request = EXT2_ROOTINO;
    675   else if (statemap[request] != USTATE)
    676     return (0);
    677   for (ino = request; ino < maxino; ino++) {
    678     if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
    679       continue;
    680     if (statemap[ino] == USTATE)
    681       break;
    682   }
    683   if (ino == maxino)
    684     return (0);
    685   switch (type & IFMT) {
    686     case IFDIR:
    687       statemap[ino] = DSTATE;
    688       break;
    689     case IFREG:
    690     case IFLNK:
    691       statemap[ino] = FSTATE;
    692       break;
    693     default:
    694       return (0);
    695   }
    696   dp                 = ginode(ino);
    697   dp->e2di_blocks[0] = h2fs32(allocblk());
    698   if (dp->e2di_blocks[0] == 0) {
    699     statemap[ino] = USTATE;
    700     return (0);
    701   }
    702   dp->e2di_mode = h2fs16(type);
    703   (void)time(&t);
    704   dp->e2di_atime = h2fs32(t);
    705   dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
    706   dp->e2di_dtime                  = 0;
    707   inossize(dp, sblock.e2fs_bsize);
    708   inosnblock(dp, btodb(sblock.e2fs_bsize));
    709   n_files++;
    710   inodirty();
    711   typemap[ino] = E2IFTODT(type);
    712   return (ino);
    713 }
    714 
    715 /*
    716  * deallocate an inode
    717  */
    718 void
    719 freeino(ino_t ino)
    720 {
    721   struct inodesc        idesc;
    722   struct ext2fs_dinode *dp;
    723 
    724   memset(&idesc, 0, sizeof(struct inodesc));
    725   idesc.id_type   = ADDR;
    726   idesc.id_func   = pass4check;
    727   idesc.id_number = ino;
    728   dp              = ginode(ino);
    729   (void)ckinode(dp, &idesc);
    730   clearinode(dp);
    731   inodirty();
    732   statemap[ino] = USTATE;
    733   n_files--;
    734 }
    735 
    736 uint64_t
    737 inonblock(struct ext2fs_dinode *dp)
    738 {
    739   uint64_t nblock;
    740 
    741   /* XXX check for EXT2_HUGE_FILE without EXT2F_ROCOMPAT_HUGE_FILE? */
    742 
    743   nblock = fs2h32(dp->e2di_nblock);
    744 
    745   if ((sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_HUGE_FILE)) {
    746     nblock |= (uint64_t)fs2h16(dp->e2di_nblock_high) << 32;
    747     if (fs2h32(dp->e2di_flags) & EXT2_HUGE_FILE) {
    748       nblock = EXT2_FSBTODB(&sblock, nblock);
    749     }
    750   }
    751 
    752   return nblock;
    753 }
    754 
    755 void
    756 inosnblock(struct ext2fs_dinode *dp, uint64_t nblock)
    757 {
    758   uint32_t flags;
    759 
    760   flags = fs2h32(dp->e2di_flags);
    761 
    762   if (nblock <= 0xffffffffULL) {
    763     flags &= ~EXT2_HUGE_FILE;
    764     dp->e2di_flags  = h2fs32(flags);
    765     dp->e2di_nblock = h2fs32(nblock);
    766     return;
    767   }
    768 
    769   sethuge();
    770 
    771   if (nblock <= 0xffffffffffffULL) {
    772     flags &= ~EXT2_HUGE_FILE;
    773     dp->e2di_flags       = h2fs32(flags);
    774     dp->e2di_nblock      = h2fs32(nblock);
    775     dp->e2di_nblock_high = h2fs16((nblock >> 32));
    776     return;
    777   }
    778 
    779   if (EXT2_DBTOFSB(&sblock, nblock) <= 0xffffffffffffULL) {
    780     flags |= EXT2_HUGE_FILE;
    781     dp->e2di_flags       = h2fs32(flags);
    782     dp->e2di_nblock      = h2fs32(EXT2_DBTOFSB(&sblock, nblock));
    783     dp->e2di_nblock_high = h2fs16((EXT2_DBTOFSB(&sblock, nblock) >> 32));
    784     return;
    785   }
    786 
    787   pfatal("trying to set nblocks higher than representable");
    788 
    789   return;
    790 }