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