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