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