Home | History | Annotate | Line # | Download | only in fsck_ffs
inode.c revision 1.7
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1980, 1986 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.5  mycroft /*static char sccsid[] = "from: @(#)inode.c	5.18 (Berkeley) 3/19/91";*/
     36  1.7      cgd static char rcsid[] = "$Id: inode.c,v 1.7 1994/04/25 18:28:25 cgd Exp $";
     37  1.1      cgd #endif /* not lint */
     38  1.1      cgd 
     39  1.1      cgd #include <sys/param.h>
     40  1.7      cgd #include <sys/time.h>
     41  1.1      cgd #include <ufs/dinode.h>
     42  1.1      cgd #include <ufs/fs.h>
     43  1.1      cgd #include <ufs/dir.h>
     44  1.6      cgd #ifndef SMALL
     45  1.1      cgd #include <pwd.h>
     46  1.6      cgd #endif
     47  1.1      cgd #include <stdlib.h>
     48  1.1      cgd #include <string.h>
     49  1.1      cgd #include "fsck.h"
     50  1.1      cgd 
     51  1.1      cgd static ino_t startinum;
     52  1.1      cgd 
     53  1.1      cgd ckinode(dp, idesc)
     54  1.1      cgd 	struct dinode *dp;
     55  1.1      cgd 	register struct inodesc *idesc;
     56  1.1      cgd {
     57  1.1      cgd 	register daddr_t *ap;
     58  1.1      cgd 	long ret, n, ndb, offset;
     59  1.1      cgd 	struct dinode dino;
     60  1.1      cgd 
     61  1.1      cgd 	if (idesc->id_fix != IGNORE)
     62  1.1      cgd 		idesc->id_fix = DONTKNOW;
     63  1.1      cgd 	idesc->id_entryno = 0;
     64  1.1      cgd 	idesc->id_filesize = dp->di_size;
     65  1.4  mycroft 	if ((dp->di_mode & IFMT) == IFBLK || (dp->di_mode & IFMT) == IFCHR ||
     66  1.4  mycroft 		DFASTLINK(*dp))
     67  1.1      cgd 		return (KEEPON);
     68  1.1      cgd 	dino = *dp;
     69  1.1      cgd 	ndb = howmany(dino.di_size, sblock.fs_bsize);
     70  1.1      cgd 	for (ap = &dino.di_db[0]; ap < &dino.di_db[NDADDR]; ap++) {
     71  1.1      cgd 		if (--ndb == 0 && (offset = blkoff(&sblock, dino.di_size)) != 0)
     72  1.1      cgd 			idesc->id_numfrags =
     73  1.1      cgd 				numfrags(&sblock, fragroundup(&sblock, offset));
     74  1.1      cgd 		else
     75  1.1      cgd 			idesc->id_numfrags = sblock.fs_frag;
     76  1.1      cgd 		if (*ap == 0)
     77  1.1      cgd 			continue;
     78  1.1      cgd 		idesc->id_blkno = *ap;
     79  1.1      cgd 		if (idesc->id_type == ADDR)
     80  1.1      cgd 			ret = (*idesc->id_func)(idesc);
     81  1.1      cgd 		else
     82  1.1      cgd 			ret = dirscan(idesc);
     83  1.1      cgd 		if (ret & STOP)
     84  1.1      cgd 			return (ret);
     85  1.1      cgd 	}
     86  1.1      cgd 	idesc->id_numfrags = sblock.fs_frag;
     87  1.1      cgd 	for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
     88  1.1      cgd 		if (*ap) {
     89  1.1      cgd 			idesc->id_blkno = *ap;
     90  1.1      cgd 			ret = iblock(idesc, n,
     91  1.1      cgd 				dino.di_size - sblock.fs_bsize * NDADDR);
     92  1.1      cgd 			if (ret & STOP)
     93  1.1      cgd 				return (ret);
     94  1.1      cgd 		}
     95  1.1      cgd 	}
     96  1.1      cgd 	return (KEEPON);
     97  1.1      cgd }
     98  1.1      cgd 
     99  1.1      cgd iblock(idesc, ilevel, isize)
    100  1.1      cgd 	struct inodesc *idesc;
    101  1.1      cgd 	register long ilevel;
    102  1.1      cgd 	u_long isize;
    103  1.1      cgd {
    104  1.1      cgd 	register daddr_t *ap;
    105  1.1      cgd 	register daddr_t *aplim;
    106  1.1      cgd 	int i, n, (*func)(), nif, sizepb;
    107  1.1      cgd 	register struct bufarea *bp;
    108  1.1      cgd 	char buf[BUFSIZ];
    109  1.1      cgd 	extern int dirscan(), pass1check();
    110  1.1      cgd 
    111  1.1      cgd 	if (idesc->id_type == ADDR) {
    112  1.1      cgd 		func = idesc->id_func;
    113  1.1      cgd 		if (((n = (*func)(idesc)) & KEEPON) == 0)
    114  1.1      cgd 			return (n);
    115  1.1      cgd 	} else
    116  1.1      cgd 		func = dirscan;
    117  1.1      cgd 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    118  1.1      cgd 		return (SKIP);
    119  1.1      cgd 	bp = getdatablk(idesc->id_blkno, sblock.fs_bsize);
    120  1.1      cgd 	ilevel--;
    121  1.1      cgd 	for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++)
    122  1.1      cgd 		sizepb *= NINDIR(&sblock);
    123  1.1      cgd 	nif = isize / sizepb + 1;
    124  1.1      cgd 	if (nif > NINDIR(&sblock))
    125  1.1      cgd 		nif = NINDIR(&sblock);
    126  1.1      cgd 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
    127  1.1      cgd 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
    128  1.1      cgd 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
    129  1.1      cgd 			if (*ap == 0)
    130  1.1      cgd 				continue;
    131  1.1      cgd 			(void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
    132  1.1      cgd 				idesc->id_number);
    133  1.1      cgd 			if (dofix(idesc, buf)) {
    134  1.1      cgd 				*ap = 0;
    135  1.1      cgd 				dirty(bp);
    136  1.1      cgd 			}
    137  1.1      cgd 		}
    138  1.1      cgd 		flush(fswritefd, bp);
    139  1.1      cgd 	}
    140  1.1      cgd 	aplim = &bp->b_un.b_indir[nif];
    141  1.1      cgd 	for (ap = bp->b_un.b_indir, i = 1; ap < aplim; ap++, i++) {
    142  1.1      cgd 		if (*ap) {
    143  1.1      cgd 			idesc->id_blkno = *ap;
    144  1.1      cgd 			if (ilevel > 0)
    145  1.1      cgd 				n = iblock(idesc, ilevel, isize - i * sizepb);
    146  1.1      cgd 			else
    147  1.1      cgd 				n = (*func)(idesc);
    148  1.1      cgd 			if (n & STOP) {
    149  1.1      cgd 				bp->b_flags &= ~B_INUSE;
    150  1.1      cgd 				return (n);
    151  1.1      cgd 			}
    152  1.1      cgd 		}
    153  1.1      cgd 	}
    154  1.1      cgd 	bp->b_flags &= ~B_INUSE;
    155  1.1      cgd 	return (KEEPON);
    156  1.1      cgd }
    157  1.1      cgd 
    158  1.1      cgd /*
    159  1.1      cgd  * Check that a block in a legal block number.
    160  1.1      cgd  * Return 0 if in range, 1 if out of range.
    161  1.1      cgd  */
    162  1.1      cgd chkrange(blk, cnt)
    163  1.1      cgd 	daddr_t blk;
    164  1.1      cgd 	int cnt;
    165  1.1      cgd {
    166  1.1      cgd 	register int c;
    167  1.1      cgd 
    168  1.1      cgd 	if ((unsigned)(blk + cnt) > maxfsblock)
    169  1.1      cgd 		return (1);
    170  1.1      cgd 	c = dtog(&sblock, blk);
    171  1.1      cgd 	if (blk < cgdmin(&sblock, c)) {
    172  1.1      cgd 		if ((blk + cnt) > cgsblock(&sblock, c)) {
    173  1.1      cgd 			if (debug) {
    174  1.1      cgd 				printf("blk %ld < cgdmin %ld;",
    175  1.1      cgd 				    blk, cgdmin(&sblock, c));
    176  1.1      cgd 				printf(" blk + cnt %ld > cgsbase %ld\n",
    177  1.1      cgd 				    blk + cnt, cgsblock(&sblock, c));
    178  1.1      cgd 			}
    179  1.1      cgd 			return (1);
    180  1.1      cgd 		}
    181  1.1      cgd 	} else {
    182  1.1      cgd 		if ((blk + cnt) > cgbase(&sblock, c+1)) {
    183  1.1      cgd 			if (debug)  {
    184  1.1      cgd 				printf("blk %ld >= cgdmin %ld;",
    185  1.1      cgd 				    blk, cgdmin(&sblock, c));
    186  1.1      cgd 				printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
    187  1.1      cgd 				    blk+cnt, sblock.fs_fpg);
    188  1.1      cgd 			}
    189  1.1      cgd 			return (1);
    190  1.1      cgd 		}
    191  1.1      cgd 	}
    192  1.1      cgd 	return (0);
    193  1.1      cgd }
    194  1.1      cgd 
    195  1.1      cgd /*
    196  1.1      cgd  * General purpose interface for reading inodes.
    197  1.1      cgd  */
    198  1.1      cgd struct dinode *
    199  1.1      cgd ginode(inumber)
    200  1.1      cgd 	ino_t inumber;
    201  1.1      cgd {
    202  1.1      cgd 	daddr_t iblk;
    203  1.1      cgd 
    204  1.1      cgd 	if (inumber < ROOTINO || inumber > maxino)
    205  1.1      cgd 		errexit("bad inode number %d to ginode\n", inumber);
    206  1.1      cgd 	if (startinum == 0 ||
    207  1.1      cgd 	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
    208  1.1      cgd 		iblk = itod(&sblock, inumber);
    209  1.1      cgd 		if (pbp != 0)
    210  1.1      cgd 			pbp->b_flags &= ~B_INUSE;
    211  1.1      cgd 		pbp = getdatablk(iblk, sblock.fs_bsize);
    212  1.1      cgd 		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
    213  1.1      cgd 	}
    214  1.1      cgd 	return (&pbp->b_un.b_dinode[inumber % INOPB(&sblock)]);
    215  1.1      cgd }
    216  1.1      cgd 
    217  1.1      cgd /*
    218  1.1      cgd  * Special purpose version of ginode used to optimize first pass
    219  1.1      cgd  * over all the inodes in numerical order.
    220  1.1      cgd  */
    221  1.1      cgd ino_t nextino, lastinum;
    222  1.1      cgd long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    223  1.1      cgd struct dinode *inodebuf;
    224  1.1      cgd 
    225  1.1      cgd struct dinode *
    226  1.1      cgd getnextinode(inumber)
    227  1.1      cgd 	ino_t inumber;
    228  1.1      cgd {
    229  1.1      cgd 	long size;
    230  1.1      cgd 	daddr_t dblk;
    231  1.1      cgd 	static struct dinode *dp;
    232  1.1      cgd 
    233  1.1      cgd 	if (inumber != nextino++ || inumber > maxino)
    234  1.1      cgd 		errexit("bad inode number %d to nextinode\n", inumber);
    235  1.1      cgd 	if (inumber >= lastinum) {
    236  1.1      cgd 		readcnt++;
    237  1.1      cgd 		dblk = fsbtodb(&sblock, itod(&sblock, lastinum));
    238  1.1      cgd 		if (readcnt % readpercg == 0) {
    239  1.1      cgd 			size = partialsize;
    240  1.1      cgd 			lastinum += partialcnt;
    241  1.1      cgd 		} else {
    242  1.1      cgd 			size = inobufsize;
    243  1.1      cgd 			lastinum += fullcnt;
    244  1.1      cgd 		}
    245  1.1      cgd 		(void)bread(fsreadfd, (char *)inodebuf, dblk, size); /* ??? */
    246  1.1      cgd 		dp = inodebuf;
    247  1.1      cgd 	}
    248  1.1      cgd 	return (dp++);
    249  1.1      cgd }
    250  1.1      cgd 
    251  1.1      cgd resetinodebuf()
    252  1.1      cgd {
    253  1.1      cgd 
    254  1.1      cgd 	startinum = 0;
    255  1.1      cgd 	nextino = 0;
    256  1.1      cgd 	lastinum = 0;
    257  1.1      cgd 	readcnt = 0;
    258  1.1      cgd 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    259  1.1      cgd 	fullcnt = inobufsize / sizeof(struct dinode);
    260  1.1      cgd 	readpercg = sblock.fs_ipg / fullcnt;
    261  1.1      cgd 	partialcnt = sblock.fs_ipg % fullcnt;
    262  1.1      cgd 	partialsize = partialcnt * sizeof(struct dinode);
    263  1.1      cgd 	if (partialcnt != 0) {
    264  1.1      cgd 		readpercg++;
    265  1.1      cgd 	} else {
    266  1.1      cgd 		partialcnt = fullcnt;
    267  1.1      cgd 		partialsize = inobufsize;
    268  1.1      cgd 	}
    269  1.1      cgd 	if (inodebuf == NULL &&
    270  1.1      cgd 	    (inodebuf = (struct dinode *)malloc((unsigned)inobufsize)) == NULL)
    271  1.1      cgd 		errexit("Cannot allocate space for inode buffer\n");
    272  1.1      cgd 	while (nextino < ROOTINO)
    273  1.1      cgd 		(void)getnextinode(nextino);
    274  1.1      cgd }
    275  1.1      cgd 
    276  1.1      cgd freeinodebuf()
    277  1.1      cgd {
    278  1.1      cgd 
    279  1.1      cgd 	if (inodebuf != NULL)
    280  1.1      cgd 		free((char *)inodebuf);
    281  1.1      cgd 	inodebuf = NULL;
    282  1.1      cgd }
    283  1.1      cgd 
    284  1.1      cgd /*
    285  1.1      cgd  * Routines to maintain information about directory inodes.
    286  1.1      cgd  * This is built during the first pass and used during the
    287  1.1      cgd  * second and third passes.
    288  1.1      cgd  *
    289  1.1      cgd  * Enter inodes into the cache.
    290  1.1      cgd  */
    291  1.1      cgd cacheino(dp, inumber)
    292  1.1      cgd 	register struct dinode *dp;
    293  1.1      cgd 	ino_t inumber;
    294  1.1      cgd {
    295  1.1      cgd 	register struct inoinfo *inp;
    296  1.1      cgd 	struct inoinfo **inpp;
    297  1.1      cgd 	unsigned int blks;
    298  1.1      cgd 
    299  1.1      cgd 	blks = howmany(dp->di_size, sblock.fs_bsize);
    300  1.1      cgd 	if (blks > NDADDR)
    301  1.1      cgd 		blks = NDADDR + NIADDR;
    302  1.1      cgd 	inp = (struct inoinfo *)
    303  1.1      cgd 		malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr_t));
    304  1.1      cgd 	if (inp == NULL)
    305  1.1      cgd 		return;
    306  1.1      cgd 	inpp = &inphead[inumber % numdirs];
    307  1.1      cgd 	inp->i_nexthash = *inpp;
    308  1.1      cgd 	*inpp = inp;
    309  1.1      cgd 	inp->i_parent = (ino_t)0;
    310  1.1      cgd 	inp->i_dotdot = (ino_t)0;
    311  1.1      cgd 	inp->i_number = inumber;
    312  1.1      cgd 	inp->i_isize = dp->di_size;
    313  1.1      cgd 	inp->i_numblks = blks * sizeof(daddr_t);
    314  1.1      cgd 	bcopy((char *)&dp->di_db[0], (char *)&inp->i_blks[0],
    315  1.1      cgd 	    (size_t)inp->i_numblks);
    316  1.1      cgd 	if (inplast == listmax) {
    317  1.1      cgd 		listmax += 100;
    318  1.1      cgd 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
    319  1.1      cgd 		    (unsigned)listmax * sizeof(struct inoinfo *));
    320  1.1      cgd 		if (inpsort == NULL)
    321  1.1      cgd 			errexit("cannot increase directory list");
    322  1.1      cgd 	}
    323  1.1      cgd 	inpsort[inplast++] = inp;
    324  1.1      cgd }
    325  1.1      cgd 
    326  1.1      cgd /*
    327  1.1      cgd  * Look up an inode cache structure.
    328  1.1      cgd  */
    329  1.1      cgd struct inoinfo *
    330  1.1      cgd getinoinfo(inumber)
    331  1.1      cgd 	ino_t inumber;
    332  1.1      cgd {
    333  1.1      cgd 	register struct inoinfo *inp;
    334  1.1      cgd 
    335  1.1      cgd 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    336  1.1      cgd 		if (inp->i_number != inumber)
    337  1.1      cgd 			continue;
    338  1.1      cgd 		return (inp);
    339  1.1      cgd 	}
    340  1.1      cgd 	errexit("cannot find inode %d\n", inumber);
    341  1.1      cgd 	return ((struct inoinfo *)0);
    342  1.1      cgd }
    343  1.1      cgd 
    344  1.1      cgd /*
    345  1.1      cgd  * Clean up all the inode cache structure.
    346  1.1      cgd  */
    347  1.1      cgd inocleanup()
    348  1.1      cgd {
    349  1.1      cgd 	register struct inoinfo **inpp;
    350  1.1      cgd 
    351  1.1      cgd 	if (inphead == NULL)
    352  1.1      cgd 		return;
    353  1.1      cgd 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    354  1.1      cgd 		free((char *)(*inpp));
    355  1.1      cgd 	free((char *)inphead);
    356  1.1      cgd 	free((char *)inpsort);
    357  1.1      cgd 	inphead = inpsort = NULL;
    358  1.1      cgd }
    359  1.1      cgd 
    360  1.1      cgd inodirty()
    361  1.1      cgd {
    362  1.1      cgd 
    363  1.1      cgd 	dirty(pbp);
    364  1.1      cgd }
    365  1.1      cgd 
    366  1.1      cgd clri(idesc, type, flag)
    367  1.1      cgd 	register struct inodesc *idesc;
    368  1.1      cgd 	char *type;
    369  1.1      cgd 	int flag;
    370  1.1      cgd {
    371  1.1      cgd 	register struct dinode *dp;
    372  1.1      cgd 
    373  1.1      cgd 	dp = ginode(idesc->id_number);
    374  1.1      cgd 	if (flag == 1) {
    375  1.1      cgd 		pwarn("%s %s", type,
    376  1.1      cgd 		    (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
    377  1.1      cgd 		pinode(idesc->id_number);
    378  1.1      cgd 	}
    379  1.1      cgd 	if (preen || reply("CLEAR") == 1) {
    380  1.1      cgd 		if (preen)
    381  1.1      cgd 			printf(" (CLEARED)\n");
    382  1.1      cgd 		n_files--;
    383  1.1      cgd 		(void)ckinode(dp, idesc);
    384  1.1      cgd 		clearinode(dp);
    385  1.1      cgd 		statemap[idesc->id_number] = USTATE;
    386  1.1      cgd 		inodirty();
    387  1.1      cgd 	}
    388  1.1      cgd }
    389  1.1      cgd 
    390  1.1      cgd findname(idesc)
    391  1.1      cgd 	struct inodesc *idesc;
    392  1.1      cgd {
    393  1.1      cgd 	register struct direct *dirp = idesc->id_dirp;
    394  1.1      cgd 
    395  1.1      cgd 	if (dirp->d_ino != idesc->id_parent)
    396  1.1      cgd 		return (KEEPON);
    397  1.1      cgd 	bcopy(dirp->d_name, idesc->id_name, (size_t)dirp->d_namlen + 1);
    398  1.1      cgd 	return (STOP|FOUND);
    399  1.1      cgd }
    400  1.1      cgd 
    401  1.1      cgd findino(idesc)
    402  1.1      cgd 	struct inodesc *idesc;
    403  1.1      cgd {
    404  1.1      cgd 	register struct direct *dirp = idesc->id_dirp;
    405  1.1      cgd 
    406  1.1      cgd 	if (dirp->d_ino == 0)
    407  1.1      cgd 		return (KEEPON);
    408  1.1      cgd 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    409  1.1      cgd 	    dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
    410  1.1      cgd 		idesc->id_parent = dirp->d_ino;
    411  1.1      cgd 		return (STOP|FOUND);
    412  1.1      cgd 	}
    413  1.1      cgd 	return (KEEPON);
    414  1.1      cgd }
    415  1.1      cgd 
    416  1.1      cgd pinode(ino)
    417  1.1      cgd 	ino_t ino;
    418  1.1      cgd {
    419  1.1      cgd 	register struct dinode *dp;
    420  1.1      cgd 	register char *p;
    421  1.1      cgd 	struct passwd *pw;
    422  1.1      cgd 	char *ctime();
    423  1.1      cgd 
    424  1.1      cgd 	printf(" I=%lu ", ino);
    425  1.1      cgd 	if (ino < ROOTINO || ino > maxino)
    426  1.1      cgd 		return;
    427  1.1      cgd 	dp = ginode(ino);
    428  1.1      cgd 	printf(" OWNER=");
    429  1.6      cgd #ifndef SMALL
    430  1.1      cgd 	if ((pw = getpwuid((int)dp->di_uid)) != 0)
    431  1.1      cgd 		printf("%s ", pw->pw_name);
    432  1.1      cgd 	else
    433  1.6      cgd #endif
    434  1.1      cgd 		printf("%u ", (unsigned)dp->di_uid);
    435  1.1      cgd 	printf("MODE=%o\n", dp->di_mode);
    436  1.1      cgd 	if (preen)
    437  1.1      cgd 		printf("%s: ", devname);
    438  1.1      cgd 	printf("SIZE=%lu ", dp->di_size);
    439  1.1      cgd 	p = ctime(&dp->di_mtime);
    440  1.1      cgd 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
    441  1.1      cgd }
    442  1.1      cgd 
    443  1.1      cgd blkerror(ino, type, blk)
    444  1.1      cgd 	ino_t ino;
    445  1.1      cgd 	char *type;
    446  1.1      cgd 	daddr_t blk;
    447  1.1      cgd {
    448  1.1      cgd 
    449  1.1      cgd 	pfatal("%ld %s I=%lu", blk, type, ino);
    450  1.1      cgd 	printf("\n");
    451  1.1      cgd 	switch (statemap[ino]) {
    452  1.1      cgd 
    453  1.1      cgd 	case FSTATE:
    454  1.1      cgd 		statemap[ino] = FCLEAR;
    455  1.1      cgd 		return;
    456  1.1      cgd 
    457  1.1      cgd 	case DSTATE:
    458  1.1      cgd 		statemap[ino] = DCLEAR;
    459  1.1      cgd 		return;
    460  1.1      cgd 
    461  1.1      cgd 	case FCLEAR:
    462  1.1      cgd 	case DCLEAR:
    463  1.1      cgd 		return;
    464  1.1      cgd 
    465  1.1      cgd 	default:
    466  1.1      cgd 		errexit("BAD STATE %d TO BLKERR", statemap[ino]);
    467  1.1      cgd 		/* NOTREACHED */
    468  1.1      cgd 	}
    469  1.1      cgd }
    470  1.1      cgd 
    471  1.1      cgd /*
    472  1.1      cgd  * allocate an unused inode
    473  1.1      cgd  */
    474  1.1      cgd ino_t
    475  1.1      cgd allocino(request, type)
    476  1.1      cgd 	ino_t request;
    477  1.1      cgd 	int type;
    478  1.1      cgd {
    479  1.1      cgd 	register ino_t ino;
    480  1.1      cgd 	register struct dinode *dp;
    481  1.1      cgd 
    482  1.1      cgd 	if (request == 0)
    483  1.1      cgd 		request = ROOTINO;
    484  1.1      cgd 	else if (statemap[request] != USTATE)
    485  1.1      cgd 		return (0);
    486  1.1      cgd 	for (ino = request; ino < maxino; ino++)
    487  1.1      cgd 		if (statemap[ino] == USTATE)
    488  1.1      cgd 			break;
    489  1.1      cgd 	if (ino == maxino)
    490  1.1      cgd 		return (0);
    491  1.1      cgd 	switch (type & IFMT) {
    492  1.1      cgd 	case IFDIR:
    493  1.1      cgd 		statemap[ino] = DSTATE;
    494  1.1      cgd 		break;
    495  1.1      cgd 	case IFREG:
    496  1.1      cgd 	case IFLNK:
    497  1.1      cgd 		statemap[ino] = FSTATE;
    498  1.1      cgd 		break;
    499  1.1      cgd 	default:
    500  1.1      cgd 		return (0);
    501  1.1      cgd 	}
    502  1.1      cgd 	dp = ginode(ino);
    503  1.1      cgd 	dp->di_db[0] = allocblk((long)1);
    504  1.1      cgd 	if (dp->di_db[0] == 0) {
    505  1.1      cgd 		statemap[ino] = USTATE;
    506  1.1      cgd 		return (0);
    507  1.1      cgd 	}
    508  1.1      cgd 	dp->di_mode = type;
    509  1.7      cgd 	(void)time(&dp->di_atime.ts_sec);
    510  1.7      cgd 	dp->di_atime.ts_nsec = 0;
    511  1.1      cgd 	dp->di_mtime = dp->di_ctime = dp->di_atime;
    512  1.1      cgd 	dp->di_size = sblock.fs_fsize;
    513  1.1      cgd 	dp->di_blocks = btodb(sblock.fs_fsize);
    514  1.1      cgd 	n_files++;
    515  1.1      cgd 	inodirty();
    516  1.1      cgd 	return (ino);
    517  1.1      cgd }
    518  1.1      cgd 
    519  1.1      cgd /*
    520  1.1      cgd  * deallocate an inode
    521  1.1      cgd  */
    522  1.1      cgd freeino(ino)
    523  1.1      cgd 	ino_t ino;
    524  1.1      cgd {
    525  1.1      cgd 	struct inodesc idesc;
    526  1.1      cgd 	extern int pass4check();
    527  1.1      cgd 	struct dinode *dp;
    528  1.1      cgd 
    529  1.1      cgd 	bzero((char *)&idesc, sizeof(struct inodesc));
    530  1.1      cgd 	idesc.id_type = ADDR;
    531  1.1      cgd 	idesc.id_func = pass4check;
    532  1.1      cgd 	idesc.id_number = ino;
    533  1.1      cgd 	dp = ginode(ino);
    534  1.1      cgd 	(void)ckinode(dp, &idesc);
    535  1.1      cgd 	clearinode(dp);
    536  1.1      cgd 	inodirty();
    537  1.1      cgd 	statemap[ino] = USTATE;
    538  1.1      cgd 	n_files--;
    539  1.1      cgd }
    540