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