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