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