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