Home | History | Annotate | Line # | Download | only in fsck_lfs
      1  1.70     joerg /* $NetBSD: inode.c,v 1.70 2020/04/03 19:36:33 joerg 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.45  dholland #define vnode uvnode
     68   1.1  perseant #include <ufs/lfs/lfs.h>
     69  1.57  dholland #include <ufs/lfs/lfs_accessors.h>
     70  1.50  dholland #include <ufs/lfs/lfs_inode.h>
     71  1.16  perseant #undef vnode
     72  1.16  perseant 
     73  1.16  perseant #include <err.h>
     74   1.1  perseant #ifndef SMALL
     75   1.1  perseant #include <pwd.h>
     76   1.1  perseant #endif
     77   1.1  perseant #include <stdio.h>
     78   1.1  perseant #include <stdlib.h>
     79   1.1  perseant #include <string.h>
     80  1.36  christos #include <util.h>
     81   1.1  perseant 
     82  1.16  perseant #include "bufcache.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 static int iblock(struct inodesc *, long, u_int64_t);
     90  1.16  perseant int blksreqd(struct lfs *, int);
     91  1.16  perseant int lfs_maxino(void);
     92   1.1  perseant 
     93   1.1  perseant /*
     94  1.16  perseant  * Get a dinode of a given inum.
     95  1.16  perseant  * XXX combine this function with vget.
     96   1.1  perseant  */
     97  1.59  dholland union lfs_dinode *
     98  1.16  perseant ginode(ino_t ino)
     99   1.1  perseant {
    100  1.16  perseant 	struct uvnode *vp;
    101  1.16  perseant 	struct ubuf *bp;
    102  1.11  perseant 	IFILE *ifp;
    103  1.58  dholland 	daddr_t daddr;
    104  1.58  dholland 	unsigned segno;
    105   1.1  perseant 
    106  1.16  perseant 	vp = vget(fs, ino);
    107  1.16  perseant 	if (vp == NULL)
    108   1.6  perseant 		return NULL;
    109   1.5  perseant 
    110  1.16  perseant 	if (din_table[ino] == 0x0) {
    111  1.16  perseant 		LFS_IENTRY(ifp, fs, ino, bp);
    112  1.58  dholland 		daddr = lfs_if_getdaddr(fs, ifp);
    113  1.58  dholland 		segno = lfs_dtosn(fs, daddr);
    114  1.58  dholland 		din_table[ino] = daddr;
    115  1.59  dholland 		seg_table[segno].su_nbytes += DINOSIZE(fs);
    116  1.37        ad 		brelse(bp, 0);
    117   1.6  perseant 	}
    118  1.60  dholland 	return VTOI(vp)->i_din;
    119   1.1  perseant }
    120   1.1  perseant 
    121   1.1  perseant /*
    122  1.16  perseant  * Check validity of held blocks in an inode, recursing through all blocks.
    123   1.1  perseant  */
    124   1.1  perseant int
    125  1.59  dholland ckinode(union lfs_dinode *dp, struct inodesc *idesc)
    126   1.6  perseant {
    127  1.62  dholland 	daddr_t lbn, pbn;
    128  1.16  perseant 	long ret, n, ndb, offset;
    129  1.59  dholland 	union lfs_dinode dino;
    130  1.16  perseant 	u_int64_t remsize, sizepb;
    131  1.16  perseant 	mode_t mode;
    132  1.16  perseant 	char pathbuf[MAXPATHLEN + 1];
    133  1.16  perseant 	struct uvnode *vp, *thisvp;
    134   1.6  perseant 
    135   1.6  perseant 	if (idesc->id_fix != IGNORE)
    136   1.6  perseant 		idesc->id_fix = DONTKNOW;
    137   1.6  perseant 	idesc->id_entryno = 0;
    138  1.59  dholland 	idesc->id_filesize = lfs_dino_getsize(fs, dp);
    139  1.59  dholland 	mode = lfs_dino_getmode(fs, dp) & LFS_IFMT;
    140  1.47  dholland 	if (mode == LFS_IFBLK || mode == LFS_IFCHR ||
    141  1.59  dholland 	    (mode == LFS_IFLNK && (lfs_dino_getsize(fs, dp) < lfs_sb_getmaxsymlinklen(fs) ||
    142  1.56  dholland 		    (lfs_sb_getmaxsymlinklen(fs) == 0 &&
    143  1.59  dholland 			lfs_dino_getblocks(fs, dp) == 0))))
    144   1.6  perseant 		return (KEEPON);
    145  1.59  dholland 	/* XXX is this safe if we're 32-bit? */
    146   1.6  perseant 	dino = *dp;
    147  1.59  dholland 	ndb = howmany(lfs_dino_getsize(fs, &dino), lfs_sb_getbsize(fs));
    148   1.6  perseant 
    149  1.18      yamt 	thisvp = vget(fs, idesc->id_number);
    150  1.44  dholland 	for (lbn = 0; lbn < ULFS_NDADDR; lbn++) {
    151  1.59  dholland 		pbn = lfs_dino_getdb(fs, &dino, lbn);
    152  1.16  perseant 		if (thisvp)
    153   1.6  perseant 			idesc->id_numfrags =
    154  1.51  christos 				lfs_numfrags(fs, VTOI(thisvp)->i_lfs_fragsize[lbn]);
    155  1.16  perseant 		else {
    156  1.59  dholland 			if (--ndb == 0 && (offset = lfs_blkoff(fs, lfs_dino_getsize(fs, &dino))) != 0) {
    157  1.16  perseant 				idesc->id_numfrags =
    158  1.51  christos 			    	lfs_numfrags(fs, lfs_fragroundup(fs, offset));
    159  1.16  perseant 			} else
    160  1.55  dholland 				idesc->id_numfrags = lfs_sb_getfrag(fs);
    161  1.16  perseant 		}
    162  1.59  dholland 		if (pbn == 0) {
    163   1.6  perseant 			if (idesc->id_type == DATA && ndb >= 0) {
    164   1.6  perseant 				/* An empty block in a directory XXX */
    165  1.19    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    166  1.19    itojun 				    idesc->id_number, idesc->id_number);
    167  1.32  perseant 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [1]",
    168  1.32  perseant 				    pathbuf, (long long)idesc->id_number);
    169   1.6  perseant 				if (reply("ADJUST LENGTH") == 1) {
    170  1.16  perseant 					vp = vget(fs, idesc->id_number);
    171  1.16  perseant 					dp = VTOD(vp);
    172  1.59  dholland 					lfs_dino_setsize(fs, dp,
    173  1.59  dholland 					    lbn * lfs_sb_getbsize(fs));
    174   1.6  perseant 					printf(
    175  1.16  perseant 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    176   1.6  perseant 					rerun = 1;
    177  1.16  perseant 					inodirty(VTOI(vp));
    178  1.32  perseant 				} else
    179  1.32  perseant 					break;
    180   1.6  perseant 			}
    181   1.6  perseant 			continue;
    182   1.6  perseant 		}
    183  1.59  dholland 		idesc->id_blkno = pbn;
    184  1.59  dholland 		idesc->id_lblkno = lbn;
    185   1.6  perseant 		if (idesc->id_type == ADDR) {
    186  1.16  perseant 			ret = (*idesc->id_func) (idesc);
    187   1.6  perseant 		} else
    188   1.6  perseant 			ret = dirscan(idesc);
    189   1.6  perseant 		if (ret & STOP)
    190   1.6  perseant 			return (ret);
    191   1.6  perseant 	}
    192  1.55  dholland 	idesc->id_numfrags = lfs_sb_getfrag(fs);
    193  1.59  dholland 	remsize = lfs_dino_getsize(fs, &dino) - lfs_sb_getbsize(fs) * ULFS_NDADDR;
    194  1.55  dholland 	sizepb = lfs_sb_getbsize(fs);
    195  1.59  dholland 	for (n = 1; n <= ULFS_NIADDR; n++) {
    196  1.59  dholland 		pbn = lfs_dino_getib(fs, &dino, n-1);
    197  1.59  dholland 		if (pbn) {
    198  1.59  dholland 			idesc->id_blkno = pbn;
    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.32  perseant 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [2]",
    208  1.32  perseant 				    pathbuf, (long long)idesc->id_number);
    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.59  dholland 					lfs_dino_setsize(fs, dp,
    213  1.59  dholland 					    lfs_dino_getsize(fs, dp) - remsize);
    214   1.6  perseant 					remsize = 0;
    215   1.6  perseant 					printf(
    216  1.16  perseant 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    217   1.6  perseant 					rerun = 1;
    218  1.16  perseant 					inodirty(VTOI(vp));
    219   1.6  perseant 					break;
    220  1.32  perseant 				} else
    221  1.32  perseant 					break;
    222   1.6  perseant 			}
    223   1.6  perseant 		}
    224  1.51  christos 		sizepb *= LFS_NINDIR(fs);
    225   1.6  perseant 		remsize -= sizepb;
    226   1.6  perseant 	}
    227   1.6  perseant 	return (KEEPON);
    228   1.1  perseant }
    229   1.1  perseant 
    230   1.1  perseant static int
    231  1.16  perseant iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
    232   1.6  perseant {
    233  1.62  dholland 	unsigned j, maxindir;
    234  1.62  dholland 	daddr_t found;
    235  1.16  perseant 	struct ubuf *bp;
    236  1.16  perseant 	int i, n, (*func) (struct inodesc *), nif;
    237  1.16  perseant 	u_int64_t sizepb;
    238  1.16  perseant 	char pathbuf[MAXPATHLEN + 1], buf[BUFSIZ];
    239  1.16  perseant 	struct uvnode *devvp, *vp;
    240  1.16  perseant 	int diddirty = 0;
    241   1.6  perseant 
    242   1.6  perseant 	if (idesc->id_type == ADDR) {
    243   1.6  perseant 		func = idesc->id_func;
    244  1.16  perseant 		n = (*func) (idesc);
    245   1.6  perseant 		if ((n & KEEPON) == 0)
    246   1.6  perseant 			return (n);
    247   1.6  perseant 	} else
    248   1.6  perseant 		func = dirscan;
    249  1.42   mlelstv 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    250   1.6  perseant 		return (SKIP);
    251  1.16  perseant 
    252  1.26  perseant 	devvp = fs->lfs_devvp;
    253  1.55  dholland 	bread(devvp, LFS_FSBTODB(fs, idesc->id_blkno), lfs_sb_getbsize(fs),
    254  1.53    chopps 	    0, &bp);
    255   1.6  perseant 	ilevel--;
    256  1.55  dholland 	for (sizepb = lfs_sb_getbsize(fs), i = 0; i < ilevel; i++)
    257  1.51  christos 		sizepb *= LFS_NINDIR(fs);
    258  1.51  christos 	if (isize > sizepb * LFS_NINDIR(fs))
    259  1.51  christos 		nif = LFS_NINDIR(fs);
    260   1.6  perseant 	else
    261   1.6  perseant 		nif = howmany(isize, sizepb);
    262  1.51  christos 	if (idesc->id_func == pass1check && nif < LFS_NINDIR(fs)) {
    263  1.62  dholland 		maxindir = LFS_NINDIR(fs);
    264  1.62  dholland 		for (j = nif; j < maxindir; j++) {
    265  1.62  dholland 			found = lfs_iblock_get(fs, bp->b_data, j);
    266  1.62  dholland 			if (found == 0)
    267   1.6  perseant 				continue;
    268  1.52  dholland 			(void)snprintf(buf, sizeof(buf),
    269  1.52  dholland 			    "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.62  dholland 				lfs_iblock_set(fs, bp->b_data, j, 0);
    273  1.16  perseant 				++diddirty;
    274   1.6  perseant 			}
    275   1.6  perseant 		}
    276   1.6  perseant 	}
    277  1.62  dholland 	maxindir = nif;
    278  1.62  dholland 	for (j = 0; j < maxindir; j++) {
    279  1.62  dholland 		found = lfs_iblock_get(fs, bp->b_data, j);
    280  1.62  dholland 		if (found) {
    281  1.62  dholland 			idesc->id_blkno = found;
    282  1.22      yamt 			if (ilevel == 0) {
    283  1.22      yamt 				/*
    284  1.51  christos 				 * dirscan needs lfs_lblkno.
    285  1.22      yamt 				 */
    286  1.22      yamt 				idesc->id_lblkno++;
    287  1.16  perseant 				n = (*func) (idesc);
    288  1.22      yamt 			} else {
    289   1.6  perseant 				n = iblock(idesc, ilevel, isize);
    290  1.22      yamt 			}
    291   1.6  perseant 			if (n & STOP) {
    292  1.16  perseant 				if (diddirty)
    293  1.16  perseant 					VOP_BWRITE(bp);
    294  1.16  perseant 				else
    295  1.37        ad 					brelse(bp, 0);
    296   1.6  perseant 				return (n);
    297   1.6  perseant 			}
    298   1.6  perseant 		} else {
    299   1.6  perseant 			if (idesc->id_type == DATA && isize > 0) {
    300   1.6  perseant 				/* An empty block in a directory XXX */
    301  1.19    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    302  1.19    itojun 				    idesc->id_number, idesc->id_number);
    303  1.32  perseant 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [3]",
    304  1.32  perseant 				    pathbuf, (long long)idesc->id_number);
    305   1.6  perseant 				if (reply("ADJUST LENGTH") == 1) {
    306  1.16  perseant 					vp = vget(fs, idesc->id_number);
    307  1.61  dholland 					lfs_dino_setsize(fs, VTOI(vp)->i_din,
    308  1.61  dholland 					    lfs_dino_getsize(fs,
    309  1.61  dholland 							     VTOI(vp)->i_din)
    310  1.61  dholland 					    - isize);
    311   1.6  perseant 					isize = 0;
    312   1.6  perseant 					printf(
    313  1.16  perseant 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    314   1.6  perseant 					rerun = 1;
    315  1.16  perseant 					inodirty(VTOI(vp));
    316  1.16  perseant 					if (diddirty)
    317  1.16  perseant 						VOP_BWRITE(bp);
    318  1.16  perseant 					else
    319  1.37        ad 						brelse(bp, 0);
    320   1.6  perseant 					return (STOP);
    321   1.6  perseant 				}
    322   1.6  perseant 			}
    323   1.6  perseant 		}
    324   1.6  perseant 		isize -= sizepb;
    325   1.6  perseant 	}
    326  1.16  perseant 	if (diddirty)
    327  1.16  perseant 		VOP_BWRITE(bp);
    328  1.16  perseant 	else
    329  1.37        ad 		brelse(bp, 0);
    330   1.6  perseant 	return (KEEPON);
    331   1.1  perseant }
    332  1.27  perseant 
    333   1.1  perseant /*
    334   1.1  perseant  * Check that a block in a legal block number.
    335   1.1  perseant  * Return 0 if in range, 1 if out of range.
    336   1.1  perseant  */
    337   1.1  perseant int
    338   1.6  perseant chkrange(daddr_t blk, int cnt)
    339   1.6  perseant {
    340  1.51  christos 	if (blk < lfs_sntod(fs, 0)) {
    341  1.11  perseant 		return (1);
    342  1.11  perseant 	}
    343  1.11  perseant 	if (blk > maxfsblock) {
    344  1.11  perseant 		return (1);
    345  1.11  perseant 	}
    346  1.51  christos 	if (blk + cnt < lfs_sntod(fs, 0)) {
    347   1.7  perseant 		return (1);
    348   1.7  perseant 	}
    349  1.11  perseant 	if (blk + cnt > maxfsblock) {
    350   1.6  perseant 		return (1);
    351   1.6  perseant 	}
    352   1.6  perseant 	return (0);
    353   1.1  perseant }
    354  1.27  perseant 
    355   1.1  perseant /*
    356   1.1  perseant  * Routines to maintain information about directory inodes.
    357   1.1  perseant  * This is built during the first pass and used during the
    358   1.1  perseant  * second and third passes.
    359   1.1  perseant  *
    360   1.1  perseant  * Enter inodes into the cache.
    361   1.1  perseant  */
    362   1.1  perseant void
    363  1.59  dholland cacheino(union lfs_dinode *dp, ino_t inumber)
    364   1.6  perseant {
    365  1.16  perseant 	struct inoinfo *inp;
    366  1.21    itojun 	struct inoinfo **inpp, **ninpsort;
    367  1.59  dholland 	unsigned int blks, i;
    368   1.6  perseant 
    369  1.59  dholland 	blks = howmany(lfs_dino_getsize(fs, dp), lfs_sb_getbsize(fs));
    370  1.44  dholland 	if (blks > ULFS_NDADDR)
    371  1.44  dholland 		blks = ULFS_NDADDR + ULFS_NIADDR;
    372  1.62  dholland 	inp = emalloc(sizeof(*inp) + (blks - 1) * sizeof(inp->i_blks[0]));
    373   1.6  perseant 	inpp = &inphead[inumber % numdirs];
    374   1.6  perseant 	inp->i_nexthash = *inpp;
    375   1.6  perseant 	*inpp = inp;
    376   1.6  perseant 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
    377  1.44  dholland 	if (inumber == ULFS_ROOTINO)
    378  1.44  dholland 		inp->i_parent = ULFS_ROOTINO;
    379   1.6  perseant 	else
    380  1.16  perseant 		inp->i_parent = (ino_t) 0;
    381  1.16  perseant 	inp->i_dotdot = (ino_t) 0;
    382   1.6  perseant 	inp->i_number = inumber;
    383  1.59  dholland 	inp->i_isize = lfs_dino_getsize(fs, dp);
    384  1.16  perseant 
    385  1.62  dholland 	inp->i_numblks = blks * sizeof(inp->i_blks[0]);
    386  1.59  dholland 	for (i=0; i<blks && i<ULFS_NDADDR; i++) {
    387  1.59  dholland 		inp->i_blks[i] = lfs_dino_getdb(fs, dp, i);
    388  1.59  dholland 	}
    389  1.59  dholland 	for (; i<blks; i++) {
    390  1.59  dholland 		inp->i_blks[i] = lfs_dino_getib(fs, dp, i - ULFS_NDADDR);
    391  1.59  dholland 	}
    392   1.6  perseant 	if (inplast == listmax) {
    393  1.36  christos 		ninpsort = erealloc(inpsort,
    394  1.36  christos 		    (listmax + 100) * sizeof(struct inoinfo *));
    395  1.21    itojun 		inpsort = ninpsort;
    396   1.6  perseant 		listmax += 100;
    397   1.6  perseant 	}
    398   1.6  perseant 	inpsort[inplast++] = inp;
    399   1.1  perseant }
    400   1.1  perseant 
    401   1.1  perseant /*
    402   1.1  perseant  * Look up an inode cache structure.
    403   1.1  perseant  */
    404   1.1  perseant struct inoinfo *
    405   1.6  perseant getinoinfo(ino_t inumber)
    406   1.1  perseant {
    407  1.25     perry 	struct inoinfo *inp;
    408   1.1  perseant 
    409   1.6  perseant 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    410   1.6  perseant 		if (inp->i_number != inumber)
    411   1.6  perseant 			continue;
    412   1.6  perseant 		return (inp);
    413   1.6  perseant 	}
    414  1.54  christos 	err(EEXIT, "cannot find inode %llu", (unsigned long long)inumber);
    415  1.16  perseant 	return ((struct inoinfo *) 0);
    416   1.1  perseant }
    417   1.1  perseant 
    418   1.1  perseant /*
    419   1.1  perseant  * Clean up all the inode cache structure.
    420   1.1  perseant  */
    421   1.1  perseant void
    422  1.25     perry inocleanup(void)
    423   1.1  perseant {
    424  1.25     perry 	struct inoinfo **inpp;
    425   1.1  perseant 
    426   1.6  perseant 	if (inphead == NULL)
    427   1.6  perseant 		return;
    428   1.6  perseant 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    429  1.16  perseant 		free((char *) (*inpp));
    430  1.16  perseant 	free((char *) inphead);
    431  1.16  perseant 	free((char *) inpsort);
    432   1.6  perseant 	inphead = inpsort = NULL;
    433   1.1  perseant }
    434   1.1  perseant 
    435   1.1  perseant void
    436  1.16  perseant inodirty(struct inode *ip)
    437   1.1  perseant {
    438  1.69  pgoyette 	ip->i_state |= IN_MODIFIED;
    439   1.1  perseant }
    440   1.1  perseant 
    441   1.1  perseant void
    442  1.28  christos clri(struct inodesc * idesc, const char *type, int flag)
    443   1.6  perseant {
    444  1.16  perseant 	struct uvnode *vp;
    445   1.6  perseant 
    446  1.16  perseant 	vp = vget(fs, idesc->id_number);
    447  1.35  perseant 	if (flag & 0x1) {
    448   1.6  perseant 		pwarn("%s %s", type,
    449  1.61  dholland 		      (lfs_dino_getmode(fs, VTOI(vp)->i_din) & LFS_IFMT) == LFS_IFDIR ? "DIR" : "FILE");
    450   1.6  perseant 		pinode(idesc->id_number);
    451   1.6  perseant 	}
    452  1.35  perseant 	if ((flag & 0x2) || preen || reply("CLEAR") == 1) {
    453  1.16  perseant 		if (preen && flag != 2)
    454   1.6  perseant 			printf(" (CLEARED)\n");
    455   1.6  perseant 		n_files--;
    456  1.16  perseant 		(void) ckinode(VTOD(vp), idesc);
    457  1.24      yamt 		clearinode(idesc->id_number);
    458   1.6  perseant 		statemap[idesc->id_number] = USTATE;
    459  1.24      yamt 		vnode_destroy(vp);
    460  1.35  perseant 		return;
    461  1.24      yamt 	}
    462  1.35  perseant 	return;
    463  1.24      yamt }
    464   1.6  perseant 
    465  1.24      yamt void
    466  1.24      yamt clearinode(ino_t inumber)
    467  1.24      yamt {
    468  1.24      yamt 	struct ubuf *bp;
    469  1.24      yamt 	IFILE *ifp;
    470  1.24      yamt 	daddr_t daddr;
    471  1.24      yamt 
    472  1.24      yamt 	/* Send cleared inode to the free list */
    473  1.24      yamt 
    474  1.24      yamt 	LFS_IENTRY(ifp, fs, inumber, bp);
    475  1.58  dholland 	daddr = lfs_if_getdaddr(fs, ifp);
    476  1.33  perseant 	if (daddr == LFS_UNUSED_DADDR) {
    477  1.37        ad 		brelse(bp, 0);
    478  1.33  perseant 		return;
    479  1.33  perseant 	}
    480  1.58  dholland 	lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    481  1.58  dholland 	lfs_if_setnextfree(fs, ifp, lfs_sb_getfreehd(fs));
    482  1.55  dholland 	lfs_sb_setfreehd(fs, inumber);
    483  1.24      yamt 	sbdirty();
    484  1.24      yamt 	VOP_BWRITE(bp);
    485   1.6  perseant 
    486  1.24      yamt 	/*
    487  1.24      yamt 	 * update segment usage.
    488  1.24      yamt 	 */
    489  1.24      yamt 	if (daddr != LFS_UNUSED_DADDR) {
    490  1.24      yamt 		SEGUSE *sup;
    491  1.51  christos 		u_int32_t oldsn = lfs_dtosn(fs, daddr);
    492  1.24      yamt 
    493  1.59  dholland 		seg_table[oldsn].su_nbytes -= DINOSIZE(fs);
    494  1.24      yamt 		LFS_SEGENTRY(sup, fs, oldsn, bp);
    495  1.59  dholland 		sup->su_nbytes -= DINOSIZE(fs);
    496  1.24      yamt 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);	/* Ifile */
    497   1.6  perseant 	}
    498   1.1  perseant }
    499   1.1  perseant 
    500   1.1  perseant int
    501  1.16  perseant findname(struct inodesc * idesc)
    502   1.1  perseant {
    503  1.68  dholland 	LFS_DIRHEADER *dirp = idesc->id_dirp;
    504  1.28  christos 	size_t len;
    505  1.28  christos 	char *buf;
    506   1.1  perseant 
    507  1.64  dholland 	if (lfs_dir_getino(fs, dirp) != idesc->id_parent)
    508   1.6  perseant 		return (KEEPON);
    509  1.63  dholland 	len = lfs_dir_getnamlen(fs, dirp) + 1;
    510  1.63  dholland 	if (len > MAXPATHLEN) {
    511  1.28  christos 		/* Truncate it but don't overflow the buffer */
    512  1.63  dholland 		/* XXX: this case doesn't null-terminate the result */
    513  1.28  christos 		len = MAXPATHLEN;
    514  1.28  christos 	}
    515  1.28  christos 	/* this is namebuf with utils.h */
    516  1.28  christos 	buf = __UNCONST(idesc->id_name);
    517  1.66  dholland 	(void)memcpy(buf, lfs_dir_nameptr(fs, dirp), len);
    518   1.6  perseant 	return (STOP | FOUND);
    519   1.1  perseant }
    520   1.1  perseant 
    521   1.1  perseant int
    522  1.16  perseant findino(struct inodesc * idesc)
    523   1.1  perseant {
    524  1.68  dholland 	LFS_DIRHEADER *dirp = idesc->id_dirp;
    525  1.64  dholland 	ino_t ino;
    526   1.1  perseant 
    527  1.64  dholland 	ino = lfs_dir_getino(fs, dirp);
    528  1.64  dholland 	if (ino == 0)
    529   1.6  perseant 		return (KEEPON);
    530  1.66  dholland 	if (strcmp(lfs_dir_nameptr(fs, dirp), idesc->id_name) == 0 &&
    531  1.64  dholland 	    ino >= ULFS_ROOTINO && ino < maxino) {
    532  1.64  dholland 		idesc->id_parent = ino;
    533   1.6  perseant 		return (STOP | FOUND);
    534   1.6  perseant 	}
    535   1.6  perseant 	return (KEEPON);
    536   1.1  perseant }
    537   1.1  perseant 
    538   1.1  perseant void
    539   1.6  perseant pinode(ino_t ino)
    540   1.1  perseant {
    541  1.59  dholland 	union lfs_dinode *dp;
    542  1.16  perseant 	struct passwd *pw;
    543   1.6  perseant 
    544  1.30  christos 	printf(" I=%llu ", (unsigned long long)ino);
    545  1.44  dholland 	if (ino < ULFS_ROOTINO || ino >= maxino)
    546   1.6  perseant 		return;
    547   1.6  perseant 	dp = ginode(ino);
    548   1.6  perseant 	if (dp) {
    549   1.6  perseant 		printf(" OWNER=");
    550   1.1  perseant #ifndef SMALL
    551  1.59  dholland 		if (Uflag && (pw = getpwuid(lfs_dino_getuid(fs, dp))) != 0)
    552   1.6  perseant 			printf("%s ", pw->pw_name);
    553   1.6  perseant 		else
    554   1.1  perseant #endif
    555  1.59  dholland 			printf("%u ", (unsigned)lfs_dino_getuid(fs, dp));
    556  1.59  dholland 		printf("MODE=%o\n", lfs_dino_getmode(fs, dp));
    557   1.6  perseant 		if (preen)
    558   1.6  perseant 			printf("%s: ", cdevname());
    559  1.59  dholland 		printf("SIZE=%ju ", (uintmax_t) lfs_dino_getsize(fs, dp));
    560  1.59  dholland 		printf("MTIME=%s ", print_mtime(lfs_dino_getmtime(fs, dp)));
    561   1.6  perseant 	}
    562   1.1  perseant }
    563   1.1  perseant 
    564   1.1  perseant void
    565  1.29  christos blkerror(ino_t ino, const char *type, daddr_t blk)
    566   1.6  perseant {
    567   1.6  perseant 
    568  1.30  christos 	pfatal("%lld %s I=%llu", (long long) blk, type,
    569  1.30  christos 	    (unsigned long long)ino);
    570   1.6  perseant 	printf("\n");
    571   1.6  perseant 	if (exitonfail)
    572   1.6  perseant 		exit(1);
    573   1.6  perseant 	switch (statemap[ino]) {
    574   1.6  perseant 
    575   1.6  perseant 	case FSTATE:
    576   1.6  perseant 		statemap[ino] = FCLEAR;
    577   1.6  perseant 		return;
    578   1.6  perseant 
    579   1.6  perseant 	case DSTATE:
    580   1.6  perseant 		statemap[ino] = DCLEAR;
    581   1.6  perseant 		return;
    582   1.6  perseant 
    583   1.6  perseant 	case FCLEAR:
    584   1.6  perseant 	case DCLEAR:
    585   1.6  perseant 		return;
    586   1.6  perseant 
    587   1.6  perseant 	default:
    588  1.54  christos 		err(EEXIT, "BAD STATE %d TO BLKERR", statemap[ino]);
    589   1.6  perseant 		/* NOTREACHED */
    590   1.6  perseant 	}
    591   1.1  perseant }
    592  1.27  perseant 
    593   1.1  perseant /*
    594   1.1  perseant  * allocate an unused inode
    595   1.1  perseant  */
    596   1.1  perseant ino_t
    597   1.6  perseant allocino(ino_t request, int type)
    598   1.6  perseant {
    599  1.16  perseant 	ino_t ino;
    600  1.59  dholland 	union lfs_dinode *dp;
    601  1.16  perseant 	time_t t;
    602  1.16  perseant 	struct uvnode *vp;
    603  1.16  perseant 	struct ubuf *bp;
    604   1.6  perseant 
    605   1.6  perseant 	if (request == 0)
    606  1.44  dholland 		request = ULFS_ROOTINO;
    607   1.6  perseant 	else if (statemap[request] != USTATE)
    608   1.6  perseant 		return (0);
    609   1.6  perseant 	for (ino = request; ino < maxino; ino++)
    610   1.6  perseant 		if (statemap[ino] == USTATE)
    611   1.6  perseant 			break;
    612   1.6  perseant 	if (ino == maxino)
    613  1.32  perseant 		extend_ifile(fs);
    614  1.32  perseant 
    615  1.47  dholland 	switch (type & LFS_IFMT) {
    616  1.47  dholland 	case LFS_IFDIR:
    617   1.6  perseant 		statemap[ino] = DSTATE;
    618   1.6  perseant 		break;
    619  1.47  dholland 	case LFS_IFREG:
    620  1.47  dholland 	case LFS_IFLNK:
    621   1.6  perseant 		statemap[ino] = FSTATE;
    622   1.6  perseant 		break;
    623   1.6  perseant 	default:
    624   1.6  perseant 		return (0);
    625   1.6  perseant 	}
    626  1.27  perseant         vp = lfs_valloc(fs, ino);
    627  1.27  perseant 	if (vp == NULL)
    628  1.27  perseant 		return (0);
    629  1.60  dholland 	dp = VTOI(vp)->i_din;
    630  1.55  dholland 	bp = getblk(vp, 0, lfs_sb_getfsize(fs));
    631  1.16  perseant 	VOP_BWRITE(bp);
    632  1.59  dholland 	lfs_dino_setmode(fs, dp, type);
    633  1.16  perseant 	(void) time(&t);
    634  1.59  dholland 	lfs_dino_setatime(fs, dp, t);
    635  1.59  dholland 	lfs_dino_setctime(fs, dp, t);
    636  1.59  dholland 	lfs_dino_setmtime(fs, dp, t);
    637  1.59  dholland 	lfs_dino_setsize(fs, dp, lfs_sb_getfsize(fs));
    638  1.59  dholland 	lfs_dino_setblocks(fs, dp, lfs_btofsb(fs, lfs_sb_getfsize(fs)));
    639   1.6  perseant 	n_files++;
    640  1.16  perseant 	inodirty(VTOI(vp));
    641  1.49  dholland 	typemap[ino] = LFS_IFTODT(type);
    642   1.6  perseant 	return (ino);
    643   1.1  perseant }
    644  1.27  perseant 
    645   1.1  perseant /*
    646   1.1  perseant  * deallocate an inode
    647   1.1  perseant  */
    648   1.1  perseant void
    649   1.6  perseant freeino(ino_t ino)
    650   1.1  perseant {
    651  1.16  perseant 	struct inodesc idesc;
    652  1.16  perseant 	struct uvnode *vp;
    653   1.1  perseant 
    654   1.6  perseant 	memset(&idesc, 0, sizeof(struct inodesc));
    655   1.6  perseant 	idesc.id_type = ADDR;
    656   1.6  perseant 	idesc.id_func = pass4check;
    657   1.6  perseant 	idesc.id_number = ino;
    658  1.16  perseant 	vp = vget(fs, ino);
    659  1.16  perseant 	(void) ckinode(VTOD(vp), &idesc);
    660  1.24      yamt 	clearinode(ino);
    661   1.6  perseant 	statemap[ino] = USTATE;
    662  1.24      yamt 	vnode_destroy(vp);
    663   1.5  perseant 
    664   1.6  perseant 	n_files--;
    665   1.1  perseant }
    666