Home | History | Annotate | Line # | Download | only in fsck_ext2fs
inode.c revision 1.31
      1  1.31  christos /*	$NetBSD: inode.c,v 1.31 2010/02/04 23:55:42 christos Exp $	*/
      2   1.1    bouyer 
      3   1.1    bouyer /*
      4   1.1    bouyer  * Copyright (c) 1980, 1986, 1993
      5   1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
      6   1.1    bouyer  *
      7   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      8   1.1    bouyer  * modification, are permitted provided that the following conditions
      9   1.1    bouyer  * are met:
     10   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     11   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     12   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     15  1.11       agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.11       agc  *    may be used to endorse or promote products derived from this software
     17  1.11       agc  *    without specific prior written permission.
     18  1.11       agc  *
     19  1.11       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.11       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.11       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.11       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.11       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.11       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.11       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.11       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.11       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.11       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.11       agc  * SUCH DAMAGE.
     30  1.11       agc  */
     31  1.11       agc 
     32  1.11       agc /*
     33  1.11       agc  * Copyright (c) 1997 Manuel Bouyer.
     34  1.11       agc  *
     35  1.11       agc  * Redistribution and use in source and binary forms, with or without
     36  1.11       agc  * modification, are permitted provided that the following conditions
     37  1.11       agc  * are met:
     38  1.11       agc  * 1. Redistributions of source code must retain the above copyright
     39  1.11       agc  *    notice, this list of conditions and the following disclaimer.
     40  1.11       agc  * 2. Redistributions in binary form must reproduce the above copyright
     41  1.11       agc  *    notice, this list of conditions and the following disclaimer in the
     42  1.11       agc  *    documentation and/or other materials provided with the distribution.
     43   1.1    bouyer  *
     44  1.14    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     45  1.14    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     46  1.14    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     47  1.14    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     48  1.14    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     49  1.14    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     50  1.14    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     51  1.14    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     52  1.14    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     53  1.14    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     54   1.1    bouyer  */
     55   1.1    bouyer 
     56   1.2     lukem #include <sys/cdefs.h>
     57   1.1    bouyer #ifndef lint
     58   1.1    bouyer #if 0
     59   1.1    bouyer static char sccsid[] = "@(#)inode.c	8.5 (Berkeley) 2/8/95";
     60   1.1    bouyer #else
     61  1.31  christos __RCSID("$NetBSD: inode.c,v 1.31 2010/02/04 23:55:42 christos Exp $");
     62   1.1    bouyer #endif
     63   1.1    bouyer #endif /* not lint */
     64   1.1    bouyer 
     65   1.1    bouyer #include <sys/param.h>
     66   1.1    bouyer #include <sys/time.h>
     67   1.1    bouyer #include <ufs/ext2fs/ext2fs_dinode.h>
     68   1.1    bouyer #include <ufs/ext2fs/ext2fs_dir.h>
     69   1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     70   1.1    bouyer 
     71   1.1    bouyer #include <ufs/ufs/dinode.h> /* for IFMT & friends */
     72   1.1    bouyer #ifndef SMALL
     73   1.1    bouyer #include <pwd.h>
     74   1.1    bouyer #endif
     75   1.1    bouyer #include <stdio.h>
     76   1.1    bouyer #include <stdlib.h>
     77   1.1    bouyer #include <string.h>
     78   1.4    kleink #include <time.h>
     79   1.1    bouyer 
     80   1.1    bouyer #include "fsck.h"
     81   1.1    bouyer #include "fsutil.h"
     82   1.1    bouyer #include "extern.h"
     83   1.1    bouyer 
     84   1.3    bouyer /*
     85   1.3    bouyer  * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
     86   1.3    bouyer  * here.
     87   1.3    bouyer  */
     88   1.3    bouyer 
     89   1.3    bouyer #define fsck_ino_to_fsba(fs, x)                      \
     90   1.3    bouyer 	(fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables) + \
     91   1.3    bouyer 	(((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
     92   1.3    bouyer 
     93   1.3    bouyer 
     94   1.1    bouyer static ino_t startinum;
     95   1.1    bouyer 
     96  1.15   xtraeme static int iblock(struct inodesc *, long, u_int64_t);
     97   1.1    bouyer 
     98  1.17        ws static int setlarge(void);
     99  1.17        ws 
    100  1.17        ws static int
    101  1.17        ws setlarge(void)
    102  1.17        ws {
    103  1.17        ws 	if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
    104  1.17        ws 		pfatal("LARGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
    105  1.17        ws 		return 0;
    106  1.17        ws 	}
    107  1.17        ws 	if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE)) {
    108  1.17        ws 		if (preen)
    109  1.17        ws 			pwarn("SETTING LARGE FILE INDICATOR\n");
    110  1.17        ws 		else if (!reply("SET LARGE FILE INDICATOR"))
    111  1.17        ws 			return 0;
    112  1.17        ws 		sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGEFILE;
    113  1.17        ws 		sbdirty();
    114  1.17        ws 	}
    115  1.17        ws 	return 1;
    116  1.17        ws }
    117  1.17        ws 
    118  1.17        ws u_int64_t
    119  1.17        ws inosize(struct ext2fs_dinode *dp)
    120  1.17        ws {
    121  1.17        ws 	u_int64_t size = fs2h32(dp->e2di_size);
    122  1.17        ws 
    123  1.17        ws 	if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG)
    124  1.17        ws 		size |= (u_int64_t)fs2h32(dp->e2di_dacl) << 32;
    125  1.25   tsutsui 	if (size > INT32_MAX)
    126  1.17        ws 		(void)setlarge();
    127  1.17        ws 	return size;
    128  1.17        ws }
    129  1.17        ws 
    130  1.17        ws void
    131  1.17        ws inossize(struct ext2fs_dinode *dp, u_int64_t size)
    132  1.17        ws {
    133  1.17        ws 	if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG) {
    134  1.17        ws 		dp->e2di_dacl = h2fs32(size >> 32);
    135  1.25   tsutsui 		if (size > INT32_MAX)
    136  1.17        ws 			if (!setlarge())
    137  1.17        ws 				return;
    138  1.25   tsutsui 	} else if (size > INT32_MAX) {
    139  1.17        ws 		pfatal("TRYING TO SET FILESIZE TO %llu ON MODE %x FILE\n",
    140  1.17        ws 		    (unsigned long long)size, fs2h16(dp->e2di_mode) & IFMT);
    141  1.17        ws 		return;
    142  1.17        ws 	}
    143  1.17        ws 	dp->e2di_size = h2fs32(size);
    144  1.17        ws }
    145  1.17        ws 
    146   1.1    bouyer int
    147  1.15   xtraeme ckinode(struct ext2fs_dinode *dp, struct inodesc *idesc)
    148   1.1    bouyer {
    149   1.2     lukem 	u_int32_t *ap;
    150   1.2     lukem 	long ret, n, ndb;
    151   1.1    bouyer 	struct ext2fs_dinode dino;
    152   1.1    bouyer 	u_int64_t remsize, sizepb;
    153   1.1    bouyer 	mode_t mode;
    154   1.1    bouyer 	char pathbuf[MAXPATHLEN + 1];
    155   1.1    bouyer 
    156   1.1    bouyer 	if (idesc->id_fix != IGNORE)
    157   1.1    bouyer 		idesc->id_fix = DONTKNOW;
    158   1.1    bouyer 	idesc->id_entryno = 0;
    159  1.17        ws 	idesc->id_filesize = inosize(dp);
    160   1.3    bouyer 	mode = fs2h16(dp->e2di_mode) & IFMT;
    161   1.6    bouyer 	if (mode == IFBLK || mode == IFCHR || mode == IFIFO ||
    162  1.17        ws 	    (mode == IFLNK && (inosize(dp) < EXT2_MAXSYMLINKLEN)))
    163   1.1    bouyer 		return (KEEPON);
    164   1.1    bouyer 	dino = *dp;
    165  1.17        ws 	ndb = howmany(inosize(&dino), sblock.e2fs_bsize);
    166   1.1    bouyer 	for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[NDADDR];
    167  1.24   tsutsui 	    ap++,ndb--) {
    168   1.1    bouyer 		idesc->id_numfrags = 1;
    169   1.1    bouyer 		if (*ap == 0) {
    170   1.1    bouyer 			if (idesc->id_type == DATA && ndb > 0) {
    171   1.1    bouyer 				/* An empty block in a directory XXX */
    172  1.10    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    173  1.10    itojun 				    idesc->id_number, idesc->id_number);
    174   1.1    bouyer 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    175   1.1    bouyer 				    pathbuf);
    176   1.1    bouyer 				if (reply("ADJUST LENGTH") == 1) {
    177   1.1    bouyer 					dp = ginode(idesc->id_number);
    178  1.17        ws 					inossize(dp,
    179  1.17        ws 					    (ap - &dino.e2di_blocks[0]) *
    180   1.3    bouyer 					    sblock.e2fs_bsize);
    181   1.1    bouyer 					printf(
    182   1.1    bouyer 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    183   1.1    bouyer 					rerun = 1;
    184   1.1    bouyer 					inodirty();
    185   1.1    bouyer 				}
    186   1.1    bouyer 			}
    187   1.1    bouyer 			continue;
    188   1.1    bouyer 		}
    189   1.3    bouyer 		idesc->id_blkno = fs2h32(*ap);
    190   1.1    bouyer 		if (idesc->id_type == ADDR)
    191   1.1    bouyer 			ret = (*idesc->id_func)(idesc);
    192   1.1    bouyer 		else
    193   1.1    bouyer 			ret = dirscan(idesc);
    194   1.1    bouyer 		if (ret & STOP)
    195   1.1    bouyer 			return (ret);
    196   1.1    bouyer 	}
    197   1.1    bouyer 	idesc->id_numfrags = 1;
    198  1.17        ws 	remsize = inosize(&dino) - sblock.e2fs_bsize * NDADDR;
    199   1.1    bouyer 	sizepb = sblock.e2fs_bsize;
    200   1.1    bouyer 	for (ap = &dino.e2di_blocks[NDADDR], n = 1; n <= NIADDR; ap++, n++) {
    201   1.1    bouyer 		if (*ap) {
    202   1.3    bouyer 			idesc->id_blkno = fs2h32(*ap);
    203   1.1    bouyer 			ret = iblock(idesc, n, remsize);
    204   1.1    bouyer 			if (ret & STOP)
    205   1.1    bouyer 				return (ret);
    206   1.1    bouyer 		} else {
    207   1.1    bouyer 			if (idesc->id_type == DATA && remsize > 0) {
    208   1.1    bouyer 				/* An empty block in a directory XXX */
    209  1.10    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    210  1.10    itojun 				    idesc->id_number, idesc->id_number);
    211   1.1    bouyer 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    212   1.1    bouyer 				    pathbuf);
    213   1.1    bouyer 				if (reply("ADJUST LENGTH") == 1) {
    214   1.1    bouyer 					dp = ginode(idesc->id_number);
    215  1.17        ws 					inossize(dp, inosize(dp) - remsize);
    216   1.1    bouyer 					remsize = 0;
    217   1.1    bouyer 					printf(
    218   1.1    bouyer 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    219   1.1    bouyer 					rerun = 1;
    220   1.1    bouyer 					inodirty();
    221   1.1    bouyer 					break;
    222   1.1    bouyer 				}
    223   1.1    bouyer 			}
    224   1.1    bouyer 		}
    225   1.1    bouyer 		sizepb *= NINDIR(&sblock);
    226   1.1    bouyer 		remsize -= sizepb;
    227   1.1    bouyer 	}
    228   1.1    bouyer 	return (KEEPON);
    229   1.1    bouyer }
    230   1.1    bouyer 
    231   1.1    bouyer static int
    232  1.15   xtraeme iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
    233   1.1    bouyer {
    234   1.9      fvdl 	/* XXX ondisk32 */
    235   1.9      fvdl 	int32_t *ap;
    236   1.9      fvdl 	int32_t *aplim;
    237   1.2     lukem 	struct bufarea *bp;
    238  1.29     lukem 	int i, n, (*func)(struct inodesc *);
    239  1.29     lukem 	size_t nif;
    240   1.1    bouyer 	u_int64_t sizepb;
    241   1.1    bouyer 	char buf[BUFSIZ];
    242   1.1    bouyer 	char pathbuf[MAXPATHLEN + 1];
    243   1.1    bouyer 	struct ext2fs_dinode *dp;
    244   1.1    bouyer 
    245   1.1    bouyer 	if (idesc->id_type == ADDR) {
    246   1.1    bouyer 		func = idesc->id_func;
    247   1.1    bouyer 		if (((n = (*func)(idesc)) & KEEPON) == 0)
    248   1.1    bouyer 			return (n);
    249   1.1    bouyer 	} else
    250   1.1    bouyer 		func = dirscan;
    251   1.1    bouyer 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    252   1.1    bouyer 		return (SKIP);
    253   1.1    bouyer 	bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
    254   1.1    bouyer 	ilevel--;
    255   1.1    bouyer 	for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
    256   1.1    bouyer 		sizepb *= NINDIR(&sblock);
    257   1.1    bouyer 	if (isize > sizepb * NINDIR(&sblock))
    258   1.1    bouyer 		nif = NINDIR(&sblock);
    259   1.1    bouyer 	else
    260   1.1    bouyer 		nif = howmany(isize, sizepb);
    261   1.1    bouyer 	if (idesc->id_func == pass1check &&
    262   1.1    bouyer 		nif < NINDIR(&sblock)) {
    263   1.1    bouyer 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
    264   1.1    bouyer 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
    265   1.1    bouyer 			if (*ap == 0)
    266   1.1    bouyer 				continue;
    267   1.5   mycroft 			(void)snprintf(buf, sizeof(buf),
    268  1.20  christos 			    "PARTIALLY TRUNCATED INODE I=%llu",
    269  1.20  christos 			    (unsigned long long)idesc->id_number);
    270   1.1    bouyer 			if (dofix(idesc, buf)) {
    271   1.1    bouyer 				*ap = 0;
    272   1.1    bouyer 				dirty(bp);
    273   1.1    bouyer 			}
    274   1.1    bouyer 		}
    275   1.1    bouyer 		flush(fswritefd, bp);
    276   1.1    bouyer 	}
    277   1.1    bouyer 	aplim = &bp->b_un.b_indir[nif];
    278   1.1    bouyer 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
    279   1.1    bouyer 		if (*ap) {
    280   1.3    bouyer 			idesc->id_blkno = fs2h32(*ap);
    281   1.1    bouyer 			if (ilevel == 0)
    282   1.1    bouyer 				n = (*func)(idesc);
    283   1.1    bouyer 			else
    284   1.1    bouyer 				n = iblock(idesc, ilevel, isize);
    285   1.1    bouyer 			if (n & STOP) {
    286   1.1    bouyer 				bp->b_flags &= ~B_INUSE;
    287   1.1    bouyer 				return (n);
    288   1.1    bouyer 			}
    289   1.1    bouyer 		} else {
    290   1.1    bouyer 			if (idesc->id_type == DATA && isize > 0) {
    291   1.1    bouyer 				/* An empty block in a directory XXX */
    292  1.10    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    293  1.10    itojun 				    idesc->id_number, idesc->id_number);
    294   1.1    bouyer 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    295   1.1    bouyer 				    pathbuf);
    296   1.1    bouyer 				if (reply("ADJUST LENGTH") == 1) {
    297   1.1    bouyer 					dp = ginode(idesc->id_number);
    298  1.17        ws 					inossize(dp, inosize(dp) - isize);
    299   1.1    bouyer 					isize = 0;
    300   1.1    bouyer 					printf(
    301   1.1    bouyer 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    302   1.1    bouyer 					rerun = 1;
    303   1.1    bouyer 					inodirty();
    304   1.1    bouyer 					bp->b_flags &= ~B_INUSE;
    305   1.1    bouyer 					return(STOP);
    306   1.1    bouyer 				}
    307   1.1    bouyer 			}
    308   1.1    bouyer 		}
    309   1.1    bouyer 		isize -= sizepb;
    310   1.1    bouyer 	}
    311   1.1    bouyer 	bp->b_flags &= ~B_INUSE;
    312   1.1    bouyer 	return (KEEPON);
    313   1.1    bouyer }
    314   1.1    bouyer 
    315   1.1    bouyer /*
    316   1.1    bouyer  * Check that a block in a legal block number.
    317   1.1    bouyer  * Return 0 if in range, 1 if out of range.
    318   1.1    bouyer  */
    319   1.1    bouyer int
    320  1.15   xtraeme chkrange(daddr_t blk, int cnt)
    321   1.1    bouyer {
    322   1.8    bouyer 	int c, overh;
    323   1.1    bouyer 
    324  1.24   tsutsui 	if ((unsigned int)(blk + cnt) > maxfsblock)
    325   1.1    bouyer 		return (1);
    326   1.1    bouyer 	c = dtog(&sblock, blk);
    327   1.8    bouyer 	overh = cgoverhead(c);
    328   1.8    bouyer 	if (blk < sblock.e2fs.e2fs_bpg * c + overh +
    329   1.8    bouyer 	    sblock.e2fs.e2fs_first_dblock) {
    330   1.8    bouyer 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh +
    331   1.8    bouyer 		    sblock.e2fs.e2fs_first_dblock) {
    332   1.1    bouyer 			if (debug) {
    333   1.9      fvdl 				printf("blk %lld < cgdmin %d;",
    334   1.9      fvdl 				    (long long)blk,
    335   1.9      fvdl 				    sblock.e2fs.e2fs_bpg * c + overh +
    336   1.8    bouyer 				    sblock.e2fs.e2fs_first_dblock);
    337   1.9      fvdl 				printf(" blk + cnt %lld > cgsbase %d\n",
    338   1.9      fvdl 				    (long long)(blk + cnt),
    339   1.9      fvdl 				    sblock.e2fs.e2fs_bpg * c +
    340   1.8    bouyer 				    overh + sblock.e2fs.e2fs_first_dblock);
    341   1.1    bouyer 			}
    342   1.1    bouyer 			return (1);
    343   1.1    bouyer 		}
    344   1.1    bouyer 	} else {
    345   1.8    bouyer 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh +
    346   1.8    bouyer 		    sblock.e2fs.e2fs_first_dblock) {
    347   1.1    bouyer 			if (debug)  {
    348   1.9      fvdl 				printf("blk %lld >= cgdmin %d;",
    349   1.9      fvdl 				    (long long)blk,
    350   1.9      fvdl 				    sblock.e2fs.e2fs_bpg * c + overh +
    351   1.8    bouyer 				    sblock.e2fs.e2fs_first_dblock);
    352   1.9      fvdl 				printf(" blk + cnt %lld > cgdmax %d\n",
    353   1.9      fvdl 				    (long long)(blk+cnt),
    354   1.9      fvdl 				    sblock.e2fs.e2fs_bpg * (c + 1) +
    355   1.8    bouyer 				    overh + sblock.e2fs.e2fs_first_dblock);
    356   1.1    bouyer 			}
    357   1.1    bouyer 			return (1);
    358   1.1    bouyer 		}
    359   1.1    bouyer 	}
    360   1.1    bouyer 	return (0);
    361   1.1    bouyer }
    362   1.1    bouyer 
    363   1.1    bouyer /*
    364   1.1    bouyer  * General purpose interface for reading inodes.
    365   1.1    bouyer  */
    366   1.1    bouyer struct ext2fs_dinode *
    367  1.15   xtraeme ginode(ino_t inumber)
    368   1.1    bouyer {
    369   1.1    bouyer 	daddr_t iblk;
    370  1.28   tsutsui 	struct ext2fs_dinode *dp;
    371   1.1    bouyer 
    372  1.21   tsutsui 	if ((inumber < EXT2_FIRSTINO &&
    373  1.21   tsutsui 	     inumber != EXT2_ROOTINO &&
    374  1.21   tsutsui 	     !(inumber == EXT2_RESIZEINO &&
    375  1.21   tsutsui 	       (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0))
    376   1.1    bouyer 		|| inumber > maxino)
    377  1.22     lukem 		errexit("bad inode number %llu to ginode",
    378  1.20  christos 		    (unsigned long long)inumber);
    379   1.1    bouyer 	if (startinum == 0 ||
    380   1.1    bouyer 	    inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
    381   1.3    bouyer 		iblk = fsck_ino_to_fsba(&sblock, inumber);
    382   1.1    bouyer 		if (pbp != 0)
    383   1.1    bouyer 			pbp->b_flags &= ~B_INUSE;
    384   1.1    bouyer 		pbp = getdatablk(iblk, sblock.e2fs_bsize);
    385  1.28   tsutsui 		startinum =
    386  1.28   tsutsui 		    ((inumber - 1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
    387   1.1    bouyer 	}
    388  1.28   tsutsui 	dp = (struct ext2fs_dinode *)(pbp->b_un.b_buf +
    389  1.28   tsutsui 	    EXT2_DINODE_SIZE(&sblock) * ino_to_fsbo(&sblock, inumber));
    390  1.28   tsutsui 
    391  1.28   tsutsui 	return dp;
    392   1.1    bouyer }
    393   1.1    bouyer 
    394   1.1    bouyer /*
    395   1.1    bouyer  * Special purpose version of ginode used to optimize first pass
    396   1.1    bouyer  * over all the inodes in numerical order.
    397   1.1    bouyer  */
    398   1.1    bouyer ino_t nextino, lastinum;
    399   1.1    bouyer long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    400  1.28   tsutsui char *inodebuf;
    401   1.1    bouyer 
    402   1.1    bouyer struct ext2fs_dinode *
    403  1.15   xtraeme getnextinode(ino_t inumber)
    404   1.1    bouyer {
    405   1.1    bouyer 	long size;
    406   1.1    bouyer 	daddr_t dblk;
    407  1.28   tsutsui 	struct ext2fs_dinode *dp;
    408  1.28   tsutsui 	static char *bp;
    409   1.1    bouyer 
    410   1.1    bouyer 	if (inumber != nextino++ || inumber > maxino)
    411  1.22     lukem 		errexit("bad inode number %llu to nextinode",
    412  1.20  christos 		    (unsigned long long)inumber);
    413   1.1    bouyer 	if (inumber >= lastinum) {
    414   1.1    bouyer 		readcnt++;
    415   1.3    bouyer 		dblk = fsbtodb(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
    416   1.1    bouyer 		if (readcnt % readpercg == 0) {
    417   1.1    bouyer 			size = partialsize;
    418   1.1    bouyer 			lastinum += partialcnt;
    419   1.1    bouyer 		} else {
    420   1.1    bouyer 			size = inobufsize;
    421   1.1    bouyer 			lastinum += fullcnt;
    422   1.1    bouyer 		}
    423  1.28   tsutsui 		(void)bread(fsreadfd, inodebuf, dblk, size);
    424  1.28   tsutsui 		bp = inodebuf;
    425   1.1    bouyer 	}
    426  1.28   tsutsui 	dp = (struct ext2fs_dinode *)bp;
    427  1.28   tsutsui 	bp += EXT2_DINODE_SIZE(&sblock);
    428  1.28   tsutsui 
    429  1.28   tsutsui 	return dp;
    430   1.1    bouyer }
    431   1.1    bouyer 
    432   1.1    bouyer void
    433  1.15   xtraeme resetinodebuf(void)
    434   1.1    bouyer {
    435   1.1    bouyer 
    436   1.1    bouyer 	startinum = 0;
    437   1.1    bouyer 	nextino = 1;
    438   1.1    bouyer 	lastinum = 1;
    439   1.1    bouyer 	readcnt = 0;
    440   1.1    bouyer 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    441  1.28   tsutsui 	fullcnt = inobufsize / EXT2_DINODE_SIZE(&sblock);
    442   1.1    bouyer 	readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
    443   1.1    bouyer 	partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
    444  1.28   tsutsui 	partialsize = partialcnt * EXT2_DINODE_SIZE(&sblock);
    445   1.1    bouyer 	if (partialcnt != 0) {
    446   1.1    bouyer 		readpercg++;
    447   1.1    bouyer 	} else {
    448   1.1    bouyer 		partialcnt = fullcnt;
    449   1.1    bouyer 		partialsize = inobufsize;
    450   1.1    bouyer 	}
    451   1.1    bouyer 	if (inodebuf == NULL &&
    452  1.24   tsutsui 	    (inodebuf = malloc((unsigned int)inobufsize)) == NULL)
    453  1.22     lukem 		errexit("Cannot allocate space for inode buffer");
    454   1.1    bouyer 	while (nextino < EXT2_ROOTINO)
    455   1.1    bouyer 		(void)getnextinode(nextino);
    456   1.1    bouyer }
    457   1.1    bouyer 
    458   1.1    bouyer void
    459  1.15   xtraeme freeinodebuf(void)
    460   1.1    bouyer {
    461   1.1    bouyer 
    462   1.1    bouyer 	if (inodebuf != NULL)
    463  1.24   tsutsui 		free(inodebuf);
    464   1.1    bouyer 	inodebuf = NULL;
    465   1.1    bouyer }
    466   1.1    bouyer 
    467   1.1    bouyer /*
    468   1.1    bouyer  * Routines to maintain information about directory inodes.
    469   1.1    bouyer  * This is built during the first pass and used during the
    470   1.1    bouyer  * second and third passes.
    471   1.1    bouyer  *
    472   1.1    bouyer  * Enter inodes into the cache.
    473   1.1    bouyer  */
    474   1.1    bouyer void
    475  1.15   xtraeme cacheino(struct ext2fs_dinode *dp, ino_t inumber)
    476   1.1    bouyer {
    477   1.2     lukem 	struct inoinfo *inp;
    478   1.1    bouyer 	struct inoinfo **inpp;
    479   1.1    bouyer 	unsigned int blks;
    480   1.1    bouyer 
    481  1.17        ws 	blks = howmany(inosize(dp), sblock.e2fs_bsize);
    482   1.1    bouyer 	if (blks > NDADDR)
    483   1.1    bouyer 		blks = NDADDR + NIADDR;
    484   1.9      fvdl 	/* XXX ondisk32 */
    485  1.24   tsutsui 	inp = malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
    486   1.1    bouyer 	if (inp == NULL)
    487   1.1    bouyer 		return;
    488   1.1    bouyer 	inpp = &inphead[inumber % numdirs];
    489   1.1    bouyer 	inp->i_nexthash = *inpp;
    490   1.1    bouyer 	*inpp = inp;
    491   1.1    bouyer 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
    492   1.1    bouyer 	if (inumber == EXT2_ROOTINO)
    493   1.1    bouyer 		inp->i_parent = EXT2_ROOTINO;
    494   1.1    bouyer 	else
    495   1.1    bouyer 		inp->i_parent = (ino_t)0;
    496   1.1    bouyer 	inp->i_dotdot = (ino_t)0;
    497   1.1    bouyer 	inp->i_number = inumber;
    498  1.17        ws 	inp->i_isize = inosize(dp);
    499   1.9      fvdl 	/* XXX ondisk32 */
    500   1.9      fvdl 	inp->i_numblks = blks * sizeof(int32_t);
    501   1.1    bouyer 	memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
    502   1.1    bouyer 	if (inplast == listmax) {
    503   1.1    bouyer 		listmax += 100;
    504   1.1    bouyer 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
    505  1.24   tsutsui 		    (unsigned int)listmax * sizeof(struct inoinfo *));
    506   1.1    bouyer 		if (inpsort == NULL)
    507  1.22     lukem 			errexit("cannot increase directory list");
    508   1.1    bouyer 	}
    509   1.1    bouyer 	inpsort[inplast++] = inp;
    510   1.1    bouyer }
    511   1.1    bouyer 
    512   1.1    bouyer /*
    513   1.1    bouyer  * Look up an inode cache structure.
    514   1.1    bouyer  */
    515   1.1    bouyer struct inoinfo *
    516  1.15   xtraeme getinoinfo(ino_t inumber)
    517   1.1    bouyer {
    518   1.2     lukem 	struct inoinfo *inp;
    519   1.1    bouyer 
    520   1.1    bouyer 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    521   1.1    bouyer 		if (inp->i_number != inumber)
    522   1.1    bouyer 			continue;
    523   1.1    bouyer 		return (inp);
    524   1.1    bouyer 	}
    525  1.22     lukem 	errexit("cannot find inode %llu", (unsigned long long)inumber);
    526   1.1    bouyer 	return ((struct inoinfo *)0);
    527   1.1    bouyer }
    528   1.1    bouyer 
    529   1.1    bouyer /*
    530   1.1    bouyer  * Clean up all the inode cache structure.
    531   1.1    bouyer  */
    532   1.1    bouyer void
    533  1.15   xtraeme inocleanup(void)
    534   1.1    bouyer {
    535   1.2     lukem 	struct inoinfo **inpp;
    536   1.1    bouyer 
    537   1.1    bouyer 	if (inphead == NULL)
    538   1.1    bouyer 		return;
    539   1.1    bouyer 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    540  1.24   tsutsui 		free(*inpp);
    541  1.24   tsutsui 	free(inphead);
    542  1.24   tsutsui 	free(inpsort);
    543   1.1    bouyer 	inphead = inpsort = NULL;
    544   1.1    bouyer }
    545   1.1    bouyer 
    546   1.1    bouyer void
    547  1.15   xtraeme inodirty(void)
    548   1.1    bouyer {
    549   1.1    bouyer 
    550   1.1    bouyer 	dirty(pbp);
    551   1.1    bouyer }
    552   1.1    bouyer 
    553   1.1    bouyer void
    554  1.18  christos clri(struct inodesc *idesc, const char *type, int flag)
    555   1.1    bouyer {
    556   1.2     lukem 	struct ext2fs_dinode *dp;
    557   1.1    bouyer 
    558   1.1    bouyer 	dp = ginode(idesc->id_number);
    559   1.1    bouyer 	if (flag == 1) {
    560   1.1    bouyer 		pwarn("%s %s", type,
    561  1.26   tsutsui 		    (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
    562   1.1    bouyer 		pinode(idesc->id_number);
    563   1.1    bouyer 	}
    564   1.1    bouyer 	if (preen || reply("CLEAR") == 1) {
    565   1.1    bouyer 		if (preen)
    566   1.1    bouyer 			printf(" (CLEARED)\n");
    567   1.1    bouyer 		n_files--;
    568   1.1    bouyer 		(void)ckinode(dp, idesc);
    569   1.1    bouyer 		clearinode(dp);
    570   1.1    bouyer 		statemap[idesc->id_number] = USTATE;
    571   1.1    bouyer 		inodirty();
    572   1.1    bouyer 	}
    573   1.1    bouyer }
    574   1.1    bouyer 
    575   1.1    bouyer int
    576  1.15   xtraeme findname(struct inodesc *idesc)
    577   1.1    bouyer {
    578   1.2     lukem 	struct ext2fs_direct *dirp = idesc->id_dirp;
    579   1.7    bouyer 	u_int16_t namlen = dirp->e2d_namlen;
    580  1.18  christos 	/* from utilities.c namebuf[] variable */
    581  1.18  christos 	char *buf = __UNCONST(idesc->id_name);
    582  1.18  christos 	if (namlen > MAXPATHLEN) {
    583  1.18  christos 		/* XXX: Prevent overflow but don't fix */
    584  1.18  christos 		namlen = MAXPATHLEN;
    585  1.18  christos 	}
    586   1.1    bouyer 
    587   1.3    bouyer 	if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
    588   1.1    bouyer 		return (KEEPON);
    589  1.18  christos 	(void)memcpy(buf, dirp->e2d_name, (size_t)namlen);
    590  1.18  christos 	buf[namlen] = '\0';
    591   1.1    bouyer 	return (STOP|FOUND);
    592   1.1    bouyer }
    593   1.1    bouyer 
    594   1.1    bouyer int
    595  1.15   xtraeme findino(struct inodesc *idesc)
    596   1.1    bouyer {
    597   1.2     lukem 	struct ext2fs_direct *dirp = idesc->id_dirp;
    598   1.3    bouyer 	u_int32_t ino = fs2h32(dirp->e2d_ino);
    599   1.1    bouyer 
    600   1.3    bouyer 	if (ino == 0)
    601   1.1    bouyer 		return (KEEPON);
    602   1.1    bouyer 	if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
    603   1.3    bouyer 	    (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
    604   1.3    bouyer 		&& ino <= maxino) {
    605   1.3    bouyer 		idesc->id_parent = ino;
    606   1.1    bouyer 		return (STOP|FOUND);
    607   1.1    bouyer 	}
    608   1.1    bouyer 	return (KEEPON);
    609   1.1    bouyer }
    610   1.1    bouyer 
    611   1.1    bouyer void
    612  1.15   xtraeme pinode(ino_t ino)
    613   1.1    bouyer {
    614   1.2     lukem 	struct ext2fs_dinode *dp;
    615   1.1    bouyer 	struct passwd *pw;
    616  1.26   tsutsui 	uid_t uid;
    617   1.1    bouyer 
    618  1.20  christos 	printf(" I=%llu ", (unsigned long long)ino);
    619   1.1    bouyer 	if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
    620   1.1    bouyer 		return;
    621   1.1    bouyer 	dp = ginode(ino);
    622  1.26   tsutsui 	uid = fs2h16(dp->e2di_uid);
    623  1.27   tsutsui 	if (sblock.e2fs.e2fs_rev > E2FS_REV0)
    624  1.27   tsutsui 		uid |= fs2h16(dp->e2di_uid_high) << 16;
    625   1.1    bouyer 	printf(" OWNER=");
    626   1.1    bouyer #ifndef SMALL
    627  1.26   tsutsui 	if (Uflag && (pw = getpwuid(uid)) != 0)
    628   1.1    bouyer 		printf("%s ", pw->pw_name);
    629   1.1    bouyer 	else
    630   1.1    bouyer #endif
    631  1.26   tsutsui 		printf("%u ", (unsigned int)uid);
    632   1.3    bouyer 	printf("MODE=%o\n", fs2h16(dp->e2di_mode));
    633   1.1    bouyer 	if (preen)
    634   1.1    bouyer 		printf("%s: ", cdevname());
    635  1.17        ws 	printf("SIZE=%llu ", (long long)inosize(dp));
    636  1.31  christos 	printf("MTIME=%s ", print_mtime(fs2h32(dp->e2di_mtime)));
    637   1.1    bouyer }
    638   1.1    bouyer 
    639   1.1    bouyer void
    640  1.19  christos blkerror(ino_t ino, const char *type, daddr_t blk)
    641   1.1    bouyer {
    642   1.1    bouyer 
    643  1.20  christos 	pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
    644   1.1    bouyer 	printf("\n");
    645   1.1    bouyer 	switch (statemap[ino]) {
    646   1.1    bouyer 
    647   1.1    bouyer 	case FSTATE:
    648   1.1    bouyer 		statemap[ino] = FCLEAR;
    649   1.1    bouyer 		return;
    650   1.1    bouyer 
    651   1.1    bouyer 	case DSTATE:
    652   1.1    bouyer 		statemap[ino] = DCLEAR;
    653   1.1    bouyer 		return;
    654   1.1    bouyer 
    655   1.1    bouyer 	case FCLEAR:
    656   1.1    bouyer 	case DCLEAR:
    657   1.1    bouyer 		return;
    658   1.1    bouyer 
    659   1.1    bouyer 	default:
    660  1.22     lukem 		errexit("BAD STATE %d TO BLKERR", statemap[ino]);
    661   1.1    bouyer 		/* NOTREACHED */
    662   1.1    bouyer 	}
    663   1.1    bouyer }
    664   1.1    bouyer 
    665   1.1    bouyer /*
    666   1.1    bouyer  * allocate an unused inode
    667   1.1    bouyer  */
    668   1.1    bouyer ino_t
    669  1.15   xtraeme allocino(ino_t request, int type)
    670   1.1    bouyer {
    671   1.2     lukem 	ino_t ino;
    672   1.2     lukem 	struct ext2fs_dinode *dp;
    673   1.1    bouyer 	time_t t;
    674   1.1    bouyer 
    675   1.1    bouyer 	if (request == 0)
    676   1.1    bouyer 		request = EXT2_ROOTINO;
    677   1.1    bouyer 	else if (statemap[request] != USTATE)
    678   1.1    bouyer 		return (0);
    679   1.1    bouyer 	for (ino = request; ino < maxino; ino++) {
    680   1.1    bouyer 		if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
    681   1.1    bouyer 			continue;
    682   1.1    bouyer 		if (statemap[ino] == USTATE)
    683   1.1    bouyer 			break;
    684   1.1    bouyer 	}
    685   1.1    bouyer 	if (ino == maxino)
    686   1.1    bouyer 		return (0);
    687   1.1    bouyer 	switch (type & IFMT) {
    688   1.1    bouyer 	case IFDIR:
    689   1.1    bouyer 		statemap[ino] = DSTATE;
    690   1.1    bouyer 		break;
    691   1.1    bouyer 	case IFREG:
    692   1.1    bouyer 	case IFLNK:
    693   1.1    bouyer 		statemap[ino] = FSTATE;
    694   1.1    bouyer 		break;
    695   1.1    bouyer 	default:
    696   1.1    bouyer 		return (0);
    697   1.1    bouyer 	}
    698   1.1    bouyer 	dp = ginode(ino);
    699   1.3    bouyer 	dp->e2di_blocks[0] = h2fs32(allocblk());
    700   1.1    bouyer 	if (dp->e2di_blocks[0] == 0) {
    701   1.1    bouyer 		statemap[ino] = USTATE;
    702   1.1    bouyer 		return (0);
    703   1.1    bouyer 	}
    704   1.3    bouyer 	dp->e2di_mode = h2fs16(type);
    705   1.1    bouyer 	(void)time(&t);
    706   1.3    bouyer 	dp->e2di_atime = h2fs32(t);
    707   1.1    bouyer 	dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
    708   1.1    bouyer 	dp->e2di_dtime = 0;
    709  1.17        ws 	inossize(dp, sblock.e2fs_bsize);
    710   1.3    bouyer 	dp->e2di_nblock = h2fs32(btodb(sblock.e2fs_bsize));
    711   1.1    bouyer 	n_files++;
    712   1.1    bouyer 	inodirty();
    713   1.8    bouyer 	typemap[ino] = E2IFTODT(type);
    714   1.1    bouyer 	return (ino);
    715   1.1    bouyer }
    716   1.1    bouyer 
    717   1.1    bouyer /*
    718   1.1    bouyer  * deallocate an inode
    719   1.1    bouyer  */
    720   1.1    bouyer void
    721  1.15   xtraeme freeino(ino_t ino)
    722   1.1    bouyer {
    723   1.1    bouyer 	struct inodesc idesc;
    724   1.1    bouyer 	struct ext2fs_dinode *dp;
    725   1.1    bouyer 
    726   1.1    bouyer 	memset(&idesc, 0, sizeof(struct inodesc));
    727   1.1    bouyer 	idesc.id_type = ADDR;
    728   1.1    bouyer 	idesc.id_func = pass4check;
    729   1.1    bouyer 	idesc.id_number = ino;
    730   1.1    bouyer 	dp = ginode(ino);
    731   1.1    bouyer 	(void)ckinode(dp, &idesc);
    732   1.1    bouyer 	clearinode(dp);
    733   1.1    bouyer 	inodirty();
    734   1.1    bouyer 	statemap[ino] = USTATE;
    735   1.1    bouyer 	n_files--;
    736   1.1    bouyer }
    737