Home | History | Annotate | Line # | Download | only in fsck_ext2fs
inode.c revision 1.2
      1  1.2   lukem /*	$NetBSD: inode.c,v 1.2 1997/09/14 14:27:24 lukem 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.2   lukem __RCSID("$NetBSD: inode.c,v 1.2 1997/09/14 14:27:24 lukem 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.1  bouyer 
     60  1.1  bouyer #include "fsck.h"
     61  1.1  bouyer #include "fsutil.h"
     62  1.1  bouyer #include "extern.h"
     63  1.1  bouyer 
     64  1.1  bouyer static ino_t startinum;
     65  1.1  bouyer 
     66  1.1  bouyer static int iblock __P((struct inodesc *, long, u_int64_t));
     67  1.1  bouyer 
     68  1.1  bouyer int
     69  1.1  bouyer ckinode(dp, idesc)
     70  1.1  bouyer 	struct ext2fs_dinode *dp;
     71  1.2   lukem 	struct inodesc *idesc;
     72  1.1  bouyer {
     73  1.2   lukem 	u_int32_t *ap;
     74  1.2   lukem 	long ret, n, ndb;
     75  1.1  bouyer 	struct ext2fs_dinode dino;
     76  1.1  bouyer 	u_int64_t remsize, sizepb;
     77  1.1  bouyer 	mode_t mode;
     78  1.1  bouyer 	char pathbuf[MAXPATHLEN + 1];
     79  1.1  bouyer 
     80  1.1  bouyer 	if (idesc->id_fix != IGNORE)
     81  1.1  bouyer 		idesc->id_fix = DONTKNOW;
     82  1.1  bouyer 	idesc->id_entryno = 0;
     83  1.1  bouyer 	idesc->id_filesize = dp->e2di_size;
     84  1.1  bouyer 	mode = dp->e2di_mode & IFMT;
     85  1.1  bouyer 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
     86  1.1  bouyer 	    (dp->e2di_size < EXT2_MAXSYMLINKLEN)))
     87  1.1  bouyer 		return (KEEPON);
     88  1.1  bouyer 	dino = *dp;
     89  1.1  bouyer 	ndb = howmany(dino.e2di_size, sblock.e2fs_bsize);
     90  1.1  bouyer 	for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[NDADDR];
     91  1.1  bouyer 																ap++,ndb--) {
     92  1.1  bouyer 		idesc->id_numfrags = 1;
     93  1.1  bouyer 		if (*ap == 0) {
     94  1.1  bouyer 			if (idesc->id_type == DATA && ndb > 0) {
     95  1.1  bouyer 				/* An empty block in a directory XXX */
     96  1.1  bouyer 				getpathname(pathbuf, idesc->id_number,
     97  1.1  bouyer 				    idesc->id_number);
     98  1.1  bouyer 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
     99  1.1  bouyer 				    pathbuf);
    100  1.1  bouyer 				if (reply("ADJUST LENGTH") == 1) {
    101  1.1  bouyer 					dp = ginode(idesc->id_number);
    102  1.1  bouyer 					dp->e2di_size = (ap - &dino.e2di_blocks[0]) *
    103  1.1  bouyer 					    sblock.e2fs_bsize;
    104  1.1  bouyer 					printf(
    105  1.1  bouyer 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    106  1.1  bouyer 					rerun = 1;
    107  1.1  bouyer 					inodirty();
    108  1.1  bouyer 				}
    109  1.1  bouyer 			}
    110  1.1  bouyer 			continue;
    111  1.1  bouyer 		}
    112  1.1  bouyer 		idesc->id_blkno = *ap;
    113  1.1  bouyer 		if (idesc->id_type == ADDR)
    114  1.1  bouyer 			ret = (*idesc->id_func)(idesc);
    115  1.1  bouyer 		else
    116  1.1  bouyer 			ret = dirscan(idesc);
    117  1.1  bouyer 		if (ret & STOP)
    118  1.1  bouyer 			return (ret);
    119  1.1  bouyer 	}
    120  1.1  bouyer 	idesc->id_numfrags = 1;
    121  1.1  bouyer 	remsize = dino.e2di_size - sblock.e2fs_bsize * NDADDR;
    122  1.1  bouyer 	sizepb = sblock.e2fs_bsize;
    123  1.1  bouyer 	for (ap = &dino.e2di_blocks[NDADDR], n = 1; n <= NIADDR; ap++, n++) {
    124  1.1  bouyer 		if (*ap) {
    125  1.1  bouyer 			idesc->id_blkno = *ap;
    126  1.1  bouyer 			ret = iblock(idesc, n, remsize);
    127  1.1  bouyer 			if (ret & STOP)
    128  1.1  bouyer 				return (ret);
    129  1.1  bouyer 		} else {
    130  1.1  bouyer 			if (idesc->id_type == DATA && remsize > 0) {
    131  1.1  bouyer 				/* An empty block in a directory XXX */
    132  1.1  bouyer 				getpathname(pathbuf, idesc->id_number,
    133  1.1  bouyer 				    idesc->id_number);
    134  1.1  bouyer 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    135  1.1  bouyer 				    pathbuf);
    136  1.1  bouyer 				if (reply("ADJUST LENGTH") == 1) {
    137  1.1  bouyer 					dp = ginode(idesc->id_number);
    138  1.1  bouyer 					dp->e2di_size -= remsize;
    139  1.1  bouyer 					remsize = 0;
    140  1.1  bouyer 					printf(
    141  1.1  bouyer 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    142  1.1  bouyer 					rerun = 1;
    143  1.1  bouyer 					inodirty();
    144  1.1  bouyer 					break;
    145  1.1  bouyer 				}
    146  1.1  bouyer 			}
    147  1.1  bouyer 		}
    148  1.1  bouyer 		sizepb *= NINDIR(&sblock);
    149  1.1  bouyer 		remsize -= sizepb;
    150  1.1  bouyer 	}
    151  1.1  bouyer 	return (KEEPON);
    152  1.1  bouyer }
    153  1.1  bouyer 
    154  1.1  bouyer static int
    155  1.1  bouyer iblock(idesc, ilevel, isize)
    156  1.1  bouyer 	struct inodesc *idesc;
    157  1.1  bouyer 	long ilevel;
    158  1.1  bouyer 	u_int64_t isize;
    159  1.1  bouyer {
    160  1.2   lukem 	daddr_t *ap;
    161  1.2   lukem 	daddr_t *aplim;
    162  1.2   lukem 	struct bufarea *bp;
    163  1.1  bouyer 	int i, n, (*func) __P((struct inodesc *)), nif;
    164  1.1  bouyer 	u_int64_t sizepb;
    165  1.1  bouyer 	char buf[BUFSIZ];
    166  1.1  bouyer 	char pathbuf[MAXPATHLEN + 1];
    167  1.1  bouyer 	struct ext2fs_dinode *dp;
    168  1.1  bouyer 
    169  1.1  bouyer 	if (idesc->id_type == ADDR) {
    170  1.1  bouyer 		func = idesc->id_func;
    171  1.1  bouyer 		if (((n = (*func)(idesc)) & KEEPON) == 0)
    172  1.1  bouyer 			return (n);
    173  1.1  bouyer 	} else
    174  1.1  bouyer 		func = dirscan;
    175  1.1  bouyer 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    176  1.1  bouyer 		return (SKIP);
    177  1.1  bouyer 	bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
    178  1.1  bouyer 	ilevel--;
    179  1.1  bouyer 	for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
    180  1.1  bouyer 		sizepb *= NINDIR(&sblock);
    181  1.1  bouyer 	if (isize > sizepb * NINDIR(&sblock))
    182  1.1  bouyer 		nif = NINDIR(&sblock);
    183  1.1  bouyer 	else
    184  1.1  bouyer 		nif = howmany(isize, sizepb);
    185  1.1  bouyer 	if (idesc->id_func == pass1check &&
    186  1.1  bouyer 		nif < NINDIR(&sblock)) {
    187  1.1  bouyer 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
    188  1.1  bouyer 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
    189  1.1  bouyer 			if (*ap == 0)
    190  1.1  bouyer 				continue;
    191  1.1  bouyer 			(void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%u",
    192  1.1  bouyer 				idesc->id_number);
    193  1.1  bouyer 			if (dofix(idesc, buf)) {
    194  1.1  bouyer 				*ap = 0;
    195  1.1  bouyer 				dirty(bp);
    196  1.1  bouyer 			}
    197  1.1  bouyer 		}
    198  1.1  bouyer 		flush(fswritefd, bp);
    199  1.1  bouyer 	}
    200  1.1  bouyer 	aplim = &bp->b_un.b_indir[nif];
    201  1.1  bouyer 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
    202  1.1  bouyer 		if (*ap) {
    203  1.1  bouyer 			idesc->id_blkno = *ap;
    204  1.1  bouyer 			if (ilevel == 0)
    205  1.1  bouyer 				n = (*func)(idesc);
    206  1.1  bouyer 			else
    207  1.1  bouyer 				n = iblock(idesc, ilevel, isize);
    208  1.1  bouyer 			if (n & STOP) {
    209  1.1  bouyer 				bp->b_flags &= ~B_INUSE;
    210  1.1  bouyer 				return (n);
    211  1.1  bouyer 			}
    212  1.1  bouyer 		} else {
    213  1.1  bouyer 			if (idesc->id_type == DATA && isize > 0) {
    214  1.1  bouyer 				/* An empty block in a directory XXX */
    215  1.1  bouyer 				getpathname(pathbuf, idesc->id_number,
    216  1.1  bouyer 				    idesc->id_number);
    217  1.1  bouyer 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    218  1.1  bouyer 				    pathbuf);
    219  1.1  bouyer 				if (reply("ADJUST LENGTH") == 1) {
    220  1.1  bouyer 					dp = ginode(idesc->id_number);
    221  1.1  bouyer 					dp->e2di_size -= isize;
    222  1.1  bouyer 					isize = 0;
    223  1.1  bouyer 					printf(
    224  1.1  bouyer 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    225  1.1  bouyer 					rerun = 1;
    226  1.1  bouyer 					inodirty();
    227  1.1  bouyer 					bp->b_flags &= ~B_INUSE;
    228  1.1  bouyer 					return(STOP);
    229  1.1  bouyer 				}
    230  1.1  bouyer 			}
    231  1.1  bouyer 		}
    232  1.1  bouyer 		isize -= sizepb;
    233  1.1  bouyer 	}
    234  1.1  bouyer 	bp->b_flags &= ~B_INUSE;
    235  1.1  bouyer 	return (KEEPON);
    236  1.1  bouyer }
    237  1.1  bouyer 
    238  1.1  bouyer /*
    239  1.1  bouyer  * Check that a block in a legal block number.
    240  1.1  bouyer  * Return 0 if in range, 1 if out of range.
    241  1.1  bouyer  */
    242  1.1  bouyer int
    243  1.1  bouyer chkrange(blk, cnt)
    244  1.1  bouyer 	daddr_t blk;
    245  1.1  bouyer 	int cnt;
    246  1.1  bouyer {
    247  1.2   lukem 	int c;
    248  1.1  bouyer 
    249  1.1  bouyer 	if ((unsigned)(blk + cnt) > maxfsblock)
    250  1.1  bouyer 		return (1);
    251  1.1  bouyer 	c = dtog(&sblock, blk);
    252  1.1  bouyer 	if (blk < sblock.e2fs.e2fs_bpg * c + cgoverhead +
    253  1.1  bouyer 											sblock.e2fs.e2fs_first_dblock) {
    254  1.1  bouyer 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + cgoverhead +
    255  1.1  bouyer 											sblock.e2fs.e2fs_first_dblock) {
    256  1.1  bouyer 			if (debug) {
    257  1.1  bouyer 				printf("blk %d < cgdmin %d;",
    258  1.1  bouyer 				    blk, sblock.e2fs.e2fs_bpg * c + cgoverhead +
    259  1.1  bouyer 											sblock.e2fs.e2fs_first_dblock);
    260  1.1  bouyer 				printf(" blk + cnt %d > cgsbase %d\n",
    261  1.1  bouyer 				    blk + cnt, sblock.e2fs.e2fs_bpg * c + cgoverhead +
    262  1.1  bouyer 											sblock.e2fs.e2fs_first_dblock);
    263  1.1  bouyer 			}
    264  1.1  bouyer 			return (1);
    265  1.1  bouyer 		}
    266  1.1  bouyer 	} else {
    267  1.1  bouyer 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + cgoverhead +
    268  1.1  bouyer 											sblock.e2fs.e2fs_first_dblock) {
    269  1.1  bouyer 			if (debug)  {
    270  1.1  bouyer 				printf("blk %d >= cgdmin %d;",
    271  1.1  bouyer 				    blk, sblock.e2fs.e2fs_bpg * c + cgoverhead +
    272  1.1  bouyer 											sblock.e2fs.e2fs_first_dblock);
    273  1.1  bouyer 				printf(" blk + cnt %d > cgdmax %d\n",
    274  1.1  bouyer 				    blk+cnt, sblock.e2fs.e2fs_bpg * (c + 1) + cgoverhead +
    275  1.1  bouyer 											sblock.e2fs.e2fs_first_dblock);
    276  1.1  bouyer 			}
    277  1.1  bouyer 			return (1);
    278  1.1  bouyer 		}
    279  1.1  bouyer 	}
    280  1.1  bouyer 	return (0);
    281  1.1  bouyer }
    282  1.1  bouyer 
    283  1.1  bouyer /*
    284  1.1  bouyer  * General purpose interface for reading inodes.
    285  1.1  bouyer  */
    286  1.1  bouyer struct ext2fs_dinode *
    287  1.1  bouyer ginode(inumber)
    288  1.1  bouyer 	ino_t inumber;
    289  1.1  bouyer {
    290  1.1  bouyer 	daddr_t iblk;
    291  1.1  bouyer 
    292  1.1  bouyer 	if ((inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
    293  1.1  bouyer 		|| inumber > maxino)
    294  1.1  bouyer 		errexit("bad inode number %d to ginode\n", inumber);
    295  1.1  bouyer 	if (startinum == 0 ||
    296  1.1  bouyer 	    inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
    297  1.1  bouyer 		iblk = ino_to_fsba(&sblock, inumber);
    298  1.1  bouyer 		if (pbp != 0)
    299  1.1  bouyer 			pbp->b_flags &= ~B_INUSE;
    300  1.1  bouyer 		pbp = getdatablk(iblk, sblock.e2fs_bsize);
    301  1.1  bouyer 		startinum = ((inumber -1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
    302  1.1  bouyer 	}
    303  1.1  bouyer 	return (&pbp->b_un.b_dinode[(inumber-1) % sblock.e2fs_ipb]);
    304  1.1  bouyer }
    305  1.1  bouyer 
    306  1.1  bouyer /*
    307  1.1  bouyer  * Special purpose version of ginode used to optimize first pass
    308  1.1  bouyer  * over all the inodes in numerical order.
    309  1.1  bouyer  */
    310  1.1  bouyer ino_t nextino, lastinum;
    311  1.1  bouyer long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    312  1.1  bouyer struct ext2fs_dinode *inodebuf;
    313  1.1  bouyer 
    314  1.1  bouyer struct ext2fs_dinode *
    315  1.1  bouyer getnextinode(inumber)
    316  1.1  bouyer 	ino_t inumber;
    317  1.1  bouyer {
    318  1.1  bouyer 	long size;
    319  1.1  bouyer 	daddr_t dblk;
    320  1.1  bouyer 	static struct ext2fs_dinode *dp;
    321  1.1  bouyer 
    322  1.1  bouyer 	if (inumber != nextino++ || inumber > maxino)
    323  1.1  bouyer 		errexit("bad inode number %d to nextinode\n", inumber);
    324  1.1  bouyer 	if (inumber >= lastinum) {
    325  1.1  bouyer 		readcnt++;
    326  1.1  bouyer 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    327  1.1  bouyer 		if (readcnt % readpercg == 0) {
    328  1.1  bouyer 			size = partialsize;
    329  1.1  bouyer 			lastinum += partialcnt;
    330  1.1  bouyer 		} else {
    331  1.1  bouyer 			size = inobufsize;
    332  1.1  bouyer 			lastinum += fullcnt;
    333  1.1  bouyer 		}
    334  1.1  bouyer 		(void)bread(fsreadfd, (char *)inodebuf, dblk, size);
    335  1.1  bouyer 		dp = inodebuf;
    336  1.1  bouyer 	}
    337  1.1  bouyer 	return (dp++);
    338  1.1  bouyer }
    339  1.1  bouyer 
    340  1.1  bouyer void
    341  1.1  bouyer resetinodebuf()
    342  1.1  bouyer {
    343  1.1  bouyer 
    344  1.1  bouyer 	startinum = 0;
    345  1.1  bouyer 	nextino = 1;
    346  1.1  bouyer 	lastinum = 1;
    347  1.1  bouyer 	readcnt = 0;
    348  1.1  bouyer 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    349  1.1  bouyer 	fullcnt = inobufsize / sizeof(struct ext2fs_dinode);
    350  1.1  bouyer 	readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
    351  1.1  bouyer 	partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
    352  1.1  bouyer 	partialsize = partialcnt * sizeof(struct ext2fs_dinode);
    353  1.1  bouyer 	if (partialcnt != 0) {
    354  1.1  bouyer 		readpercg++;
    355  1.1  bouyer 	} else {
    356  1.1  bouyer 		partialcnt = fullcnt;
    357  1.1  bouyer 		partialsize = inobufsize;
    358  1.1  bouyer 	}
    359  1.1  bouyer 	if (inodebuf == NULL &&
    360  1.1  bouyer 	    (inodebuf = (struct ext2fs_dinode *)malloc((unsigned)inobufsize)) ==
    361  1.1  bouyer 		NULL)
    362  1.1  bouyer 		errexit("Cannot allocate space for inode buffer\n");
    363  1.1  bouyer 	while (nextino < EXT2_ROOTINO)
    364  1.1  bouyer 		(void)getnextinode(nextino);
    365  1.1  bouyer }
    366  1.1  bouyer 
    367  1.1  bouyer void
    368  1.1  bouyer freeinodebuf()
    369  1.1  bouyer {
    370  1.1  bouyer 
    371  1.1  bouyer 	if (inodebuf != NULL)
    372  1.1  bouyer 		free((char *)inodebuf);
    373  1.1  bouyer 	inodebuf = NULL;
    374  1.1  bouyer }
    375  1.1  bouyer 
    376  1.1  bouyer /*
    377  1.1  bouyer  * Routines to maintain information about directory inodes.
    378  1.1  bouyer  * This is built during the first pass and used during the
    379  1.1  bouyer  * second and third passes.
    380  1.1  bouyer  *
    381  1.1  bouyer  * Enter inodes into the cache.
    382  1.1  bouyer  */
    383  1.1  bouyer void
    384  1.1  bouyer cacheino(dp, inumber)
    385  1.2   lukem 	struct ext2fs_dinode *dp;
    386  1.1  bouyer 	ino_t inumber;
    387  1.1  bouyer {
    388  1.2   lukem 	struct inoinfo *inp;
    389  1.1  bouyer 	struct inoinfo **inpp;
    390  1.1  bouyer 	unsigned int blks;
    391  1.1  bouyer 
    392  1.1  bouyer 	blks = howmany(dp->e2di_size, sblock.e2fs_bsize);
    393  1.1  bouyer 	if (blks > NDADDR)
    394  1.1  bouyer 		blks = NDADDR + NIADDR;
    395  1.1  bouyer 	inp = (struct inoinfo *)
    396  1.1  bouyer 		malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr_t));
    397  1.1  bouyer 	if (inp == NULL)
    398  1.1  bouyer 		return;
    399  1.1  bouyer 	inpp = &inphead[inumber % numdirs];
    400  1.1  bouyer 	inp->i_nexthash = *inpp;
    401  1.1  bouyer 	*inpp = inp;
    402  1.1  bouyer 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
    403  1.1  bouyer 	if (inumber == EXT2_ROOTINO)
    404  1.1  bouyer 		inp->i_parent = EXT2_ROOTINO;
    405  1.1  bouyer 	else
    406  1.1  bouyer 		inp->i_parent = (ino_t)0;
    407  1.1  bouyer 	inp->i_dotdot = (ino_t)0;
    408  1.1  bouyer 	inp->i_number = inumber;
    409  1.1  bouyer 	inp->i_isize = dp->e2di_size;
    410  1.1  bouyer 	inp->i_numblks = blks * sizeof(daddr_t);
    411  1.1  bouyer 	memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
    412  1.1  bouyer 	if (inplast == listmax) {
    413  1.1  bouyer 		listmax += 100;
    414  1.1  bouyer 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
    415  1.1  bouyer 		    (unsigned)listmax * sizeof(struct inoinfo *));
    416  1.1  bouyer 		if (inpsort == NULL)
    417  1.1  bouyer 			errexit("cannot increase directory list\n");
    418  1.1  bouyer 	}
    419  1.1  bouyer 	inpsort[inplast++] = inp;
    420  1.1  bouyer }
    421  1.1  bouyer 
    422  1.1  bouyer /*
    423  1.1  bouyer  * Look up an inode cache structure.
    424  1.1  bouyer  */
    425  1.1  bouyer struct inoinfo *
    426  1.1  bouyer getinoinfo(inumber)
    427  1.1  bouyer 	ino_t inumber;
    428  1.1  bouyer {
    429  1.2   lukem 	struct inoinfo *inp;
    430  1.1  bouyer 
    431  1.1  bouyer 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    432  1.1  bouyer 		if (inp->i_number != inumber)
    433  1.1  bouyer 			continue;
    434  1.1  bouyer 		return (inp);
    435  1.1  bouyer 	}
    436  1.1  bouyer 	errexit("cannot find inode %d\n", inumber);
    437  1.1  bouyer 	return ((struct inoinfo *)0);
    438  1.1  bouyer }
    439  1.1  bouyer 
    440  1.1  bouyer /*
    441  1.1  bouyer  * Clean up all the inode cache structure.
    442  1.1  bouyer  */
    443  1.1  bouyer void
    444  1.1  bouyer inocleanup()
    445  1.1  bouyer {
    446  1.2   lukem 	struct inoinfo **inpp;
    447  1.1  bouyer 
    448  1.1  bouyer 	if (inphead == NULL)
    449  1.1  bouyer 		return;
    450  1.1  bouyer 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    451  1.1  bouyer 		free((char *)(*inpp));
    452  1.1  bouyer 	free((char *)inphead);
    453  1.1  bouyer 	free((char *)inpsort);
    454  1.1  bouyer 	inphead = inpsort = NULL;
    455  1.1  bouyer }
    456  1.1  bouyer 
    457  1.1  bouyer void
    458  1.1  bouyer inodirty()
    459  1.1  bouyer {
    460  1.1  bouyer 
    461  1.1  bouyer 	dirty(pbp);
    462  1.1  bouyer }
    463  1.1  bouyer 
    464  1.1  bouyer void
    465  1.1  bouyer clri(idesc, type, flag)
    466  1.2   lukem 	struct inodesc *idesc;
    467  1.1  bouyer 	char *type;
    468  1.1  bouyer 	int flag;
    469  1.1  bouyer {
    470  1.2   lukem 	struct ext2fs_dinode *dp;
    471  1.1  bouyer 
    472  1.1  bouyer 	dp = ginode(idesc->id_number);
    473  1.1  bouyer 	if (flag == 1) {
    474  1.1  bouyer 		pwarn("%s %s", type,
    475  1.1  bouyer 		    (dp->e2di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
    476  1.1  bouyer 		pinode(idesc->id_number);
    477  1.1  bouyer 	}
    478  1.1  bouyer 	if (preen || reply("CLEAR") == 1) {
    479  1.1  bouyer 		if (preen)
    480  1.1  bouyer 			printf(" (CLEARED)\n");
    481  1.1  bouyer 		n_files--;
    482  1.1  bouyer 		(void)ckinode(dp, idesc);
    483  1.1  bouyer 		clearinode(dp);
    484  1.1  bouyer 		statemap[idesc->id_number] = USTATE;
    485  1.1  bouyer 		inodirty();
    486  1.1  bouyer 	}
    487  1.1  bouyer }
    488  1.1  bouyer 
    489  1.1  bouyer int
    490  1.1  bouyer findname(idesc)
    491  1.1  bouyer 	struct inodesc *idesc;
    492  1.1  bouyer {
    493  1.2   lukem 	struct ext2fs_direct *dirp = idesc->id_dirp;
    494  1.1  bouyer 
    495  1.1  bouyer 	if (dirp->e2d_ino != idesc->id_parent)
    496  1.1  bouyer 		return (KEEPON);
    497  1.1  bouyer 	memcpy(idesc->id_name, dirp->e2d_name, (size_t)dirp->e2d_namlen);
    498  1.1  bouyer 	idesc->id_name[dirp->e2d_namlen] = '\0';
    499  1.1  bouyer 	return (STOP|FOUND);
    500  1.1  bouyer }
    501  1.1  bouyer 
    502  1.1  bouyer int
    503  1.1  bouyer findino(idesc)
    504  1.1  bouyer 	struct inodesc *idesc;
    505  1.1  bouyer {
    506  1.2   lukem 	struct ext2fs_direct *dirp = idesc->id_dirp;
    507  1.1  bouyer 
    508  1.1  bouyer 	if (dirp->e2d_ino == 0)
    509  1.1  bouyer 		return (KEEPON);
    510  1.1  bouyer 	if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
    511  1.1  bouyer 	    (dirp->e2d_ino == EXT2_ROOTINO || dirp->e2d_ino >= EXT2_FIRSTINO)
    512  1.1  bouyer 		&& dirp->e2d_ino <= maxino) {
    513  1.1  bouyer 		idesc->id_parent = dirp->e2d_ino;
    514  1.1  bouyer 		return (STOP|FOUND);
    515  1.1  bouyer 	}
    516  1.1  bouyer 	return (KEEPON);
    517  1.1  bouyer }
    518  1.1  bouyer 
    519  1.1  bouyer void
    520  1.1  bouyer pinode(ino)
    521  1.1  bouyer 	ino_t ino;
    522  1.1  bouyer {
    523  1.2   lukem 	struct ext2fs_dinode *dp;
    524  1.2   lukem 	char *p;
    525  1.1  bouyer 	struct passwd *pw;
    526  1.1  bouyer 	time_t t;
    527  1.1  bouyer 
    528  1.1  bouyer 	printf(" I=%u ", ino);
    529  1.1  bouyer 	if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
    530  1.1  bouyer 		return;
    531  1.1  bouyer 	dp = ginode(ino);
    532  1.1  bouyer 	printf(" OWNER=");
    533  1.1  bouyer #ifndef SMALL
    534  1.1  bouyer 	if ((pw = getpwuid((int)dp->e2di_uid)) != 0)
    535  1.1  bouyer 		printf("%s ", pw->pw_name);
    536  1.1  bouyer 	else
    537  1.1  bouyer #endif
    538  1.1  bouyer 		printf("%u ", (unsigned)dp->e2di_uid);
    539  1.1  bouyer 	printf("MODE=%o\n", dp->e2di_mode);
    540  1.1  bouyer 	if (preen)
    541  1.1  bouyer 		printf("%s: ", cdevname());
    542  1.1  bouyer 	printf("SIZE=%u ", dp->e2di_size);
    543  1.1  bouyer 	t = dp->e2di_mtime;
    544  1.1  bouyer 	p = ctime(&t);
    545  1.1  bouyer 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
    546  1.1  bouyer }
    547  1.1  bouyer 
    548  1.1  bouyer void
    549  1.1  bouyer blkerror(ino, type, blk)
    550  1.1  bouyer 	ino_t ino;
    551  1.1  bouyer 	char *type;
    552  1.1  bouyer 	daddr_t blk;
    553  1.1  bouyer {
    554  1.1  bouyer 
    555  1.1  bouyer 	pfatal("%d %s I=%u", blk, type, ino);
    556  1.1  bouyer 	printf("\n");
    557  1.1  bouyer 	switch (statemap[ino]) {
    558  1.1  bouyer 
    559  1.1  bouyer 	case FSTATE:
    560  1.1  bouyer 		statemap[ino] = FCLEAR;
    561  1.1  bouyer 		return;
    562  1.1  bouyer 
    563  1.1  bouyer 	case DSTATE:
    564  1.1  bouyer 		statemap[ino] = DCLEAR;
    565  1.1  bouyer 		return;
    566  1.1  bouyer 
    567  1.1  bouyer 	case FCLEAR:
    568  1.1  bouyer 	case DCLEAR:
    569  1.1  bouyer 		return;
    570  1.1  bouyer 
    571  1.1  bouyer 	default:
    572  1.1  bouyer 		errexit("BAD STATE %d TO BLKERR\n", statemap[ino]);
    573  1.1  bouyer 		/* NOTREACHED */
    574  1.1  bouyer 	}
    575  1.1  bouyer }
    576  1.1  bouyer 
    577  1.1  bouyer /*
    578  1.1  bouyer  * allocate an unused inode
    579  1.1  bouyer  */
    580  1.1  bouyer ino_t
    581  1.1  bouyer allocino(request, type)
    582  1.1  bouyer 	ino_t request;
    583  1.1  bouyer 	int type;
    584  1.1  bouyer {
    585  1.2   lukem 	ino_t ino;
    586  1.2   lukem 	struct ext2fs_dinode *dp;
    587  1.1  bouyer 	time_t t;
    588  1.1  bouyer 
    589  1.1  bouyer 	if (request == 0)
    590  1.1  bouyer 		request = EXT2_ROOTINO;
    591  1.1  bouyer 	else if (statemap[request] != USTATE)
    592  1.1  bouyer 		return (0);
    593  1.1  bouyer 	for (ino = request; ino < maxino; ino++) {
    594  1.1  bouyer 		if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
    595  1.1  bouyer 			continue;
    596  1.1  bouyer 		if (statemap[ino] == USTATE)
    597  1.1  bouyer 			break;
    598  1.1  bouyer 	}
    599  1.1  bouyer 	if (ino == maxino)
    600  1.1  bouyer 		return (0);
    601  1.1  bouyer 	switch (type & IFMT) {
    602  1.1  bouyer 	case IFDIR:
    603  1.1  bouyer 		statemap[ino] = DSTATE;
    604  1.1  bouyer 		break;
    605  1.1  bouyer 	case IFREG:
    606  1.1  bouyer 	case IFLNK:
    607  1.1  bouyer 		statemap[ino] = FSTATE;
    608  1.1  bouyer 		break;
    609  1.1  bouyer 	default:
    610  1.1  bouyer 		return (0);
    611  1.1  bouyer 	}
    612  1.1  bouyer 	dp = ginode(ino);
    613  1.1  bouyer 	dp->e2di_blocks[0] = allocblk();
    614  1.1  bouyer 	if (dp->e2di_blocks[0] == 0) {
    615  1.1  bouyer 		statemap[ino] = USTATE;
    616  1.1  bouyer 		return (0);
    617  1.1  bouyer 	}
    618  1.1  bouyer 	dp->e2di_mode = type;
    619  1.1  bouyer 	(void)time(&t);
    620  1.1  bouyer 	dp->e2di_atime = t;
    621  1.1  bouyer 	dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
    622  1.1  bouyer 	dp->e2di_dtime = 0;
    623  1.1  bouyer 	dp->e2di_size = sblock.e2fs_bsize;
    624  1.1  bouyer 	dp->e2di_nblock = btodb(sblock.e2fs_bsize);
    625  1.1  bouyer 	n_files++;
    626  1.1  bouyer 	inodirty();
    627  1.1  bouyer 	return (ino);
    628  1.1  bouyer }
    629  1.1  bouyer 
    630  1.1  bouyer /*
    631  1.1  bouyer  * deallocate an inode
    632  1.1  bouyer  */
    633  1.1  bouyer void
    634  1.1  bouyer freeino(ino)
    635  1.1  bouyer 	ino_t ino;
    636  1.1  bouyer {
    637  1.1  bouyer 	struct inodesc idesc;
    638  1.1  bouyer 	struct ext2fs_dinode *dp;
    639  1.1  bouyer 
    640  1.1  bouyer 	memset(&idesc, 0, sizeof(struct inodesc));
    641  1.1  bouyer 	idesc.id_type = ADDR;
    642  1.1  bouyer 	idesc.id_func = pass4check;
    643  1.1  bouyer 	idesc.id_number = ino;
    644  1.1  bouyer 	dp = ginode(ino);
    645  1.1  bouyer 	(void)ckinode(dp, &idesc);
    646  1.1  bouyer 	clearinode(dp);
    647  1.1  bouyer 	inodirty();
    648  1.1  bouyer 	statemap[ino] = USTATE;
    649  1.1  bouyer 	n_files--;
    650  1.1  bouyer }
    651