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