Home | History | Annotate | Line # | Download | only in fsck_ffs
inode.c revision 1.71.12.1
      1  1.71.12.1    bouyer /*	$NetBSD: inode.c,v 1.71.12.1 2017/04/21 16:53:13 bouyer Exp $	*/
      2       1.13       cgd 
      3        1.1       cgd /*
      4        1.9   mycroft  * Copyright (c) 1980, 1986, 1993
      5        1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.40       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  */
     31        1.1       cgd 
     32       1.24     lukem #include <sys/cdefs.h>
     33        1.1       cgd #ifndef lint
     34       1.13       cgd #if 0
     35       1.26     lukem static char sccsid[] = "@(#)inode.c	8.8 (Berkeley) 4/28/95";
     36       1.13       cgd #else
     37  1.71.12.1    bouyer __RCSID("$NetBSD: inode.c,v 1.71.12.1 2017/04/21 16:53:13 bouyer Exp $");
     38       1.13       cgd #endif
     39        1.1       cgd #endif /* not lint */
     40        1.1       cgd 
     41        1.1       cgd #include <sys/param.h>
     42        1.7       cgd #include <sys/time.h>
     43       1.64    bouyer #include <sys/stat.h>
     44       1.26     lukem 
     45        1.9   mycroft #include <ufs/ufs/dinode.h>
     46        1.9   mycroft #include <ufs/ufs/dir.h>
     47        1.9   mycroft #include <ufs/ffs/fs.h>
     48       1.27    bouyer #include <ufs/ffs/ffs_extern.h>
     49       1.32      fvdl #include <ufs/ufs/ufs_bswap.h>
     50       1.28    kleink 
     51        1.6       cgd #ifndef SMALL
     52       1.26     lukem #include <err.h>
     53        1.1       cgd #include <pwd.h>
     54        1.6       cgd #endif
     55       1.12       cgd #include <stdio.h>
     56        1.1       cgd #include <stdlib.h>
     57        1.1       cgd #include <string.h>
     58       1.28    kleink #include <time.h>
     59       1.12       cgd 
     60        1.1       cgd #include "fsck.h"
     61       1.22  christos #include "fsutil.h"
     62       1.12       cgd #include "extern.h"
     63        1.1       cgd 
     64        1.1       cgd static ino_t startinum;
     65        1.1       cgd 
     66       1.51   xtraeme static int iblock(struct inodesc *, long, u_int64_t);
     67  1.71.12.1    bouyer #ifndef NO_FFS_EI
     68       1.37      fvdl static void swap_dinode1(union dinode *, int);
     69       1.37      fvdl static void swap_dinode2(union dinode *, int);
     70  1.71.12.1    bouyer #endif
     71       1.12       cgd 
     72       1.12       cgd int
     73       1.50   xtraeme ckinode(union dinode *dp, struct inodesc *idesc)
     74        1.1       cgd {
     75       1.37      fvdl 	int ret, offset, i;
     76       1.37      fvdl 	union dinode dino;
     77       1.27    bouyer 	u_int64_t sizepb;
     78       1.27    bouyer 	int64_t remsize;
     79       1.37      fvdl 	daddr_t ndb;
     80        1.9   mycroft 	mode_t mode;
     81       1.23   thorpej 	char pathbuf[MAXPATHLEN + 1];
     82        1.1       cgd 
     83        1.1       cgd 	if (idesc->id_fix != IGNORE)
     84        1.1       cgd 		idesc->id_fix = DONTKNOW;
     85        1.1       cgd 	idesc->id_entryno = 0;
     86       1.37      fvdl 	idesc->id_filesize = iswap64(DIP(dp, size));
     87       1.37      fvdl 	mode = iswap16(DIP(dp, mode)) & IFMT;
     88        1.9   mycroft 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
     89       1.27    bouyer 	    (idesc->id_filesize < sblock->fs_maxsymlinklen ||
     90       1.35       dbj 	    (isappleufs && (idesc->id_filesize < APPLEUFS_MAXSYMLINKLEN)) ||
     91       1.37      fvdl 	     (sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))))
     92        1.1       cgd 		return (KEEPON);
     93       1.37      fvdl 	if (is_ufs2)
     94       1.37      fvdl 		dino.dp2 = dp->dp2;
     95       1.37      fvdl 	else
     96       1.37      fvdl 		dino.dp1 = dp->dp1;
     97       1.37      fvdl 	ndb = howmany(iswap64(DIP(&dino, size)), sblock->fs_bsize);
     98       1.65  dholland 	for (i = 0; i < UFS_NDADDR; i++) {
     99       1.27    bouyer 		if (--ndb == 0 &&
    100       1.66  dholland 		    (offset = ffs_blkoff(sblock, iswap64(DIP(&dino, size)))) != 0)
    101        1.1       cgd 			idesc->id_numfrags =
    102       1.68  dholland 				ffs_numfrags(sblock, ffs_fragroundup(sblock, offset));
    103        1.1       cgd 		else
    104       1.27    bouyer 			idesc->id_numfrags = sblock->fs_frag;
    105       1.37      fvdl 		if (DIP(&dino, db[i]) == 0) {
    106       1.23   thorpej 			if (idesc->id_type == DATA && ndb >= 0) {
    107       1.23   thorpej 				/* An empty block in a directory XXX */
    108       1.27    bouyer 				markclean = 0;
    109       1.39    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    110       1.39    itojun 				    idesc->id_number, idesc->id_number);
    111       1.53  christos 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    112       1.23   thorpej 				    pathbuf);
    113       1.23   thorpej 				if (reply("ADJUST LENGTH") == 1) {
    114       1.23   thorpej 					dp = ginode(idesc->id_number);
    115       1.57     skrll 					DIP_SET(dp, size, iswap64(i *
    116       1.57     skrll 					    sblock->fs_bsize));
    117       1.23   thorpej 					printf(
    118       1.23   thorpej 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    119       1.23   thorpej 					rerun = 1;
    120       1.23   thorpej 					inodirty();
    121       1.23   thorpej 				}
    122       1.23   thorpej 			}
    123        1.1       cgd 			continue;
    124       1.23   thorpej 		}
    125       1.37      fvdl 		if (is_ufs2)
    126       1.37      fvdl 			idesc->id_blkno = iswap64(dino.dp2.di_db[i]);
    127       1.37      fvdl 		else
    128       1.37      fvdl 			idesc->id_blkno = iswap32(dino.dp1.di_db[i]);
    129       1.47   hannken 		if (idesc->id_type != DATA)
    130        1.1       cgd 			ret = (*idesc->id_func)(idesc);
    131        1.1       cgd 		else
    132        1.1       cgd 			ret = dirscan(idesc);
    133        1.1       cgd 		if (ret & STOP)
    134        1.1       cgd 			return (ret);
    135        1.1       cgd 	}
    136       1.27    bouyer 	idesc->id_numfrags = sblock->fs_frag;
    137       1.65  dholland 	remsize = iswap64(DIP(&dino, size)) - sblock->fs_bsize * UFS_NDADDR;
    138       1.27    bouyer 	sizepb = sblock->fs_bsize;
    139       1.65  dholland 	for (i = 0; i < UFS_NIADDR; i++) {
    140       1.37      fvdl 		if (DIP(&dino, ib[i])) {
    141       1.37      fvdl 			if (is_ufs2)
    142       1.37      fvdl 				idesc->id_blkno = iswap64(dino.dp2.di_ib[i]);
    143       1.37      fvdl 			else
    144       1.37      fvdl 				idesc->id_blkno = iswap32(dino.dp1.di_ib[i]);
    145       1.37      fvdl 			ret = iblock(idesc, i + 1, remsize);
    146        1.1       cgd 			if (ret & STOP)
    147        1.1       cgd 				return (ret);
    148       1.23   thorpej 		} else {
    149       1.23   thorpej 			if (idesc->id_type == DATA && remsize > 0) {
    150       1.23   thorpej 				/* An empty block in a directory XXX */
    151       1.27    bouyer 				markclean = 0;
    152       1.39    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    153       1.39    itojun 				    idesc->id_number, idesc->id_number);
    154       1.23   thorpej 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    155       1.23   thorpej 				    pathbuf);
    156       1.23   thorpej 				if (reply("ADJUST LENGTH") == 1) {
    157       1.23   thorpej 					dp = ginode(idesc->id_number);
    158       1.57     skrll 					DIP_SET(dp, size,
    159       1.37      fvdl 					    iswap64(iswap64(DIP(dp, size))
    160       1.57     skrll 						- remsize));
    161       1.23   thorpej 					remsize = 0;
    162       1.23   thorpej 					printf(
    163       1.23   thorpej 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    164       1.23   thorpej 					rerun = 1;
    165       1.23   thorpej 					inodirty();
    166       1.23   thorpej 					break;
    167       1.23   thorpej 				}
    168       1.23   thorpej 			}
    169        1.1       cgd 		}
    170       1.66  dholland 		sizepb *= FFS_NINDIR(sblock);
    171        1.9   mycroft 		remsize -= sizepb;
    172        1.1       cgd 	}
    173        1.1       cgd 	return (KEEPON);
    174        1.1       cgd }
    175        1.1       cgd 
    176       1.21  christos static int
    177       1.50   xtraeme iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
    178        1.1       cgd {
    179       1.24     lukem 	struct bufarea *bp;
    180       1.50   xtraeme 	int i, n, (*func) (struct inodesc *), nif;
    181       1.19   mycroft 	u_int64_t sizepb;
    182        1.1       cgd 	char buf[BUFSIZ];
    183       1.23   thorpej 	char pathbuf[MAXPATHLEN + 1];
    184       1.37      fvdl 	union dinode *dp;
    185        1.1       cgd 
    186       1.47   hannken 	if (idesc->id_type != DATA) {
    187        1.1       cgd 		func = idesc->id_func;
    188        1.1       cgd 		if (((n = (*func)(idesc)) & KEEPON) == 0)
    189        1.1       cgd 			return (n);
    190        1.1       cgd 	} else
    191        1.1       cgd 		func = dirscan;
    192        1.1       cgd 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    193        1.1       cgd 		return (SKIP);
    194       1.27    bouyer 	bp = getdatablk(idesc->id_blkno, sblock->fs_bsize);
    195        1.1       cgd 	ilevel--;
    196       1.27    bouyer 	for (sizepb = sblock->fs_bsize, i = 0; i < ilevel; i++)
    197       1.66  dholland 		sizepb *= FFS_NINDIR(sblock);
    198       1.66  dholland 	if (howmany(isize, sizepb) > (size_t)FFS_NINDIR(sblock))
    199       1.66  dholland 		nif = FFS_NINDIR(sblock);
    200       1.18   mycroft 	else
    201       1.18   mycroft 		nif = howmany(isize, sizepb);
    202       1.27    bouyer 	if (do_blkswap) { /* swap byte order of the whole blk */
    203       1.37      fvdl 		if (is_ufs2) {
    204       1.37      fvdl 			for (i = 0; i < nif; i++)
    205       1.37      fvdl 				bp->b_un.b_indir2[i] =
    206       1.37      fvdl 				    bswap64(bp->b_un.b_indir2[i]);
    207       1.37      fvdl 		} else {
    208       1.37      fvdl 			for (i = 0; i < nif; i++)
    209       1.37      fvdl 				bp->b_un.b_indir1[i] =
    210       1.38      fvdl 				    bswap32(bp->b_un.b_indir1[i]);
    211       1.37      fvdl 		}
    212       1.27    bouyer 		dirty(bp);
    213       1.27    bouyer 		flush(fswritefd, bp);
    214       1.27    bouyer 	}
    215       1.66  dholland 	if (idesc->id_func == pass1check && nif < FFS_NINDIR(sblock)) {
    216       1.66  dholland 		for (i = nif; i < FFS_NINDIR(sblock); i++) {
    217       1.37      fvdl 			if (IBLK(bp, i) == 0)
    218        1.1       cgd 				continue;
    219       1.29   mycroft 			(void)snprintf(buf, sizeof(buf),
    220       1.56  christos 			    "PARTIALLY TRUNCATED INODE I=%llu",
    221       1.56  christos 			    (unsigned long long)idesc->id_number);
    222        1.1       cgd 			if (dofix(idesc, buf)) {
    223       1.57     skrll 				IBLK_SET(bp, i, 0);
    224        1.1       cgd 				dirty(bp);
    225       1.27    bouyer 			} else
    226       1.60    simonb 				markclean = 0;
    227        1.1       cgd 		}
    228        1.1       cgd 		flush(fswritefd, bp);
    229        1.1       cgd 	}
    230       1.37      fvdl 	for (i = 0; i < nif; i++) {
    231       1.37      fvdl 		if (IBLK(bp, i)) {
    232       1.37      fvdl 			if (is_ufs2)
    233       1.37      fvdl 				idesc->id_blkno = iswap64(bp->b_un.b_indir2[i]);
    234       1.37      fvdl 			else
    235       1.37      fvdl 				idesc->id_blkno = iswap32(bp->b_un.b_indir1[i]);
    236        1.9   mycroft 			if (ilevel == 0)
    237        1.9   mycroft 				n = (*func)(idesc);
    238        1.1       cgd 			else
    239        1.9   mycroft 				n = iblock(idesc, ilevel, isize);
    240        1.1       cgd 			if (n & STOP) {
    241        1.1       cgd 				bp->b_flags &= ~B_INUSE;
    242        1.1       cgd 				return (n);
    243       1.23   thorpej 			}
    244       1.23   thorpej 		} else {
    245       1.23   thorpej 			if (idesc->id_type == DATA && isize > 0) {
    246       1.23   thorpej 				/* An empty block in a directory XXX */
    247       1.60    simonb 				markclean = 0;
    248       1.39    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    249       1.39    itojun 				    idesc->id_number, idesc->id_number);
    250       1.23   thorpej 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    251       1.23   thorpej 				    pathbuf);
    252       1.23   thorpej 				if (reply("ADJUST LENGTH") == 1) {
    253       1.23   thorpej 					dp = ginode(idesc->id_number);
    254       1.57     skrll 					DIP_SET(dp, size,
    255       1.37      fvdl 					    iswap64(iswap64(DIP(dp, size))
    256       1.57     skrll 						- isize));
    257       1.23   thorpej 					isize = 0;
    258       1.23   thorpej 					printf(
    259       1.23   thorpej 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    260       1.23   thorpej 					rerun = 1;
    261       1.23   thorpej 					inodirty();
    262       1.23   thorpej 					bp->b_flags &= ~B_INUSE;
    263       1.23   thorpej 					return(STOP);
    264       1.23   thorpej 				}
    265        1.1       cgd 			}
    266        1.1       cgd 		}
    267        1.9   mycroft 		isize -= sizepb;
    268        1.1       cgd 	}
    269        1.1       cgd 	bp->b_flags &= ~B_INUSE;
    270        1.1       cgd 	return (KEEPON);
    271        1.1       cgd }
    272        1.1       cgd 
    273        1.1       cgd /*
    274        1.1       cgd  * Check that a block in a legal block number.
    275        1.1       cgd  * Return 0 if in range, 1 if out of range.
    276        1.1       cgd  */
    277       1.12       cgd int
    278       1.50   xtraeme chkrange(daddr_t blk, int cnt)
    279        1.1       cgd {
    280       1.24     lukem 	int c;
    281        1.1       cgd 
    282       1.46       dbj 	if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
    283       1.46       dbj 	    cnt - 1 > maxfsblock - blk)
    284        1.1       cgd 		return (1);
    285       1.37      fvdl 	if (cnt > sblock->fs_frag ||
    286       1.69  dholland 	    ffs_fragnum(sblock, blk) + cnt > sblock->fs_frag) {
    287       1.37      fvdl 		if (debug)
    288       1.37      fvdl 			printf("bad size: blk %lld, offset %d, size %d\n",
    289       1.69  dholland 			    (long long)blk, (int)ffs_fragnum(sblock, blk), cnt);
    290       1.37      fvdl 	}
    291       1.27    bouyer 	c = dtog(sblock, blk);
    292       1.27    bouyer 	if (blk < cgdmin(sblock, c)) {
    293       1.27    bouyer 		if ((blk + cnt) > cgsblock(sblock, c)) {
    294        1.1       cgd 			if (debug) {
    295       1.36      fvdl 				printf("blk %lld < cgdmin %lld;",
    296       1.36      fvdl 				    (long long)blk,
    297       1.36      fvdl 				    (long long)cgdmin(sblock, c));
    298       1.36      fvdl 				printf(" blk + cnt %lld > cgsbase %lld\n",
    299       1.36      fvdl 				    (long long)(blk + cnt),
    300       1.36      fvdl 				    (long long)cgsblock(sblock, c));
    301        1.1       cgd 			}
    302        1.1       cgd 			return (1);
    303        1.1       cgd 		}
    304        1.1       cgd 	} else {
    305       1.27    bouyer 		if ((blk + cnt) > cgbase(sblock, c+1)) {
    306        1.1       cgd 			if (debug)  {
    307       1.36      fvdl 				printf("blk %lld >= cgdmin %lld;",
    308       1.36      fvdl 				    (long long)blk,
    309       1.36      fvdl 				    (long long)cgdmin(sblock, c));
    310       1.36      fvdl 				printf(" blk + cnt %lld > sblock->fs_fpg %d\n",
    311       1.36      fvdl 				    (long long)(blk+cnt), sblock->fs_fpg);
    312        1.1       cgd 			}
    313        1.1       cgd 			return (1);
    314        1.1       cgd 		}
    315        1.1       cgd 	}
    316        1.1       cgd 	return (0);
    317        1.1       cgd }
    318        1.1       cgd 
    319        1.1       cgd /*
    320        1.1       cgd  * General purpose interface for reading inodes.
    321        1.1       cgd  */
    322       1.37      fvdl union dinode *
    323       1.50   xtraeme ginode(ino_t inumber)
    324        1.1       cgd {
    325       1.36      fvdl 	daddr_t iblk;
    326       1.30   thorpej 	int blkoff;
    327        1.1       cgd 
    328       1.65  dholland 	if (inumber < UFS_ROOTINO || inumber > maxino)
    329       1.59  christos 		errexit("bad inode number %llu to ginode",
    330       1.56  christos 		    (unsigned long long)inumber);
    331        1.1       cgd 	if (startinum == 0 ||
    332       1.66  dholland 	    inumber < startinum || inumber >= startinum + FFS_INOPB(sblock)) {
    333       1.27    bouyer 		iblk = ino_to_fsba(sblock, inumber);
    334        1.1       cgd 		if (pbp != 0)
    335        1.1       cgd 			pbp->b_flags &= ~B_INUSE;
    336       1.27    bouyer 		pbp = getdatablk(iblk, sblock->fs_bsize);
    337       1.66  dholland 		startinum = (inumber / FFS_INOPB(sblock)) * FFS_INOPB(sblock);
    338        1.1       cgd 	}
    339       1.37      fvdl 	if (is_ufs2) {
    340       1.66  dholland 		blkoff = (inumber % FFS_INOPB(sblock)) * DINODE2_SIZE;
    341       1.37      fvdl 		return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
    342       1.37      fvdl 	}
    343       1.66  dholland 	blkoff = (inumber % FFS_INOPB(sblock)) * DINODE1_SIZE;
    344       1.37      fvdl 	return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
    345       1.37      fvdl }
    346       1.37      fvdl 
    347  1.71.12.1    bouyer #ifndef NO_FFS_EI
    348       1.37      fvdl static void
    349       1.37      fvdl swap_dinode1(union dinode *dp, int n)
    350       1.37      fvdl {
    351       1.37      fvdl 	int i, j;
    352       1.37      fvdl 	struct ufs1_dinode *dp1;
    353       1.45       dbj 	int32_t maxsymlinklen = sblock->fs_maxsymlinklen;
    354       1.45       dbj 	if (isappleufs)
    355       1.45       dbj 		maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    356       1.37      fvdl 
    357       1.37      fvdl 	dp1 = (struct ufs1_dinode *)&dp->dp1;
    358       1.37      fvdl 	for (i = 0; i < n; i++, dp1++) {
    359       1.37      fvdl 		ffs_dinode1_swap(dp1, dp1);
    360       1.42       dbj 		if (((iswap16(dp1->di_mode) & IFMT) != IFLNK) ||
    361       1.44       dbj 		    doinglevel2 ||
    362       1.45       dbj 		    (maxsymlinklen < 0) ||
    363       1.62     lukem 		    (iswap64(dp1->di_size) > (uint64_t)maxsymlinklen)) {
    364       1.71    justin 			for (j = 0; j < UFS_NDADDR; j++)
    365       1.37      fvdl 			    dp1->di_db[j] = bswap32(dp1->di_db[j]);
    366       1.71    justin 			for (j = 0; j < UFS_NIADDR; j++)
    367       1.71    justin 			    dp1->di_ib[j] = bswap32(dp1->di_ib[j]);
    368       1.37      fvdl 		}
    369       1.37      fvdl 	}
    370       1.37      fvdl }
    371       1.37      fvdl 
    372       1.37      fvdl static void
    373       1.37      fvdl swap_dinode2(union dinode *dp, int n)
    374       1.37      fvdl {
    375       1.37      fvdl 	int i, j;
    376       1.37      fvdl 	struct ufs2_dinode *dp2;
    377       1.37      fvdl 
    378       1.37      fvdl 	dp2 = (struct ufs2_dinode *)&dp->dp2;
    379       1.37      fvdl 	for (i = 0; i < n; i++, dp2++) {
    380       1.37      fvdl 		ffs_dinode2_swap(dp2, dp2);
    381       1.37      fvdl 		if ((iswap16(dp2->di_mode) & IFMT) != IFLNK) {
    382       1.71    justin 			for (j = 0; j < UFS_NXADDR; j++)
    383       1.37      fvdl 				dp2->di_extb[j] = bswap64(dp2->di_extb[j]);
    384       1.71    justin 			for (j = 0; j < UFS_NDADDR; j++)
    385       1.71    justin 				dp2->di_db[j] = bswap64(dp2->di_db[j]);
    386       1.71    justin 			for (j = 0; j < UFS_NIADDR; j++)
    387       1.71    justin 				dp2->di_ib[j] = bswap64(dp2->di_ib[j]);
    388       1.37      fvdl 		}
    389       1.37      fvdl 	}
    390        1.1       cgd }
    391  1.71.12.1    bouyer #endif /* !NO_FFS_EI */
    392        1.1       cgd 
    393        1.1       cgd /*
    394        1.1       cgd  * Special purpose version of ginode used to optimize first pass
    395        1.1       cgd  * over all the inodes in numerical order.
    396        1.1       cgd  */
    397       1.37      fvdl ino_t nextino, lastinum, lastvalidinum;
    398        1.1       cgd long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    399       1.37      fvdl union dinode *inodebuf;
    400        1.1       cgd 
    401       1.37      fvdl union dinode *
    402       1.50   xtraeme getnextinode(ino_t inumber)
    403        1.1       cgd {
    404        1.1       cgd 	long size;
    405       1.36      fvdl 	daddr_t dblk;
    406       1.37      fvdl 	static union dinode *dp;
    407       1.37      fvdl 	union dinode *ret;
    408        1.1       cgd 
    409       1.52  christos 	if (inumber != nextino++ || inumber > lastvalidinum)
    410       1.59  christos 		errexit("bad inode number %llu to nextinode",
    411       1.56  christos 		    (unsigned long long)inumber);
    412       1.37      fvdl 
    413        1.1       cgd 	if (inumber >= lastinum) {
    414        1.1       cgd 		readcnt++;
    415       1.67  dholland 		dblk = FFS_FSBTODB(sblock, ino_to_fsba(sblock, lastinum));
    416        1.1       cgd 		if (readcnt % readpercg == 0) {
    417        1.1       cgd 			size = partialsize;
    418        1.1       cgd 			lastinum += partialcnt;
    419        1.1       cgd 		} else {
    420        1.1       cgd 			size = inobufsize;
    421        1.1       cgd 			lastinum += fullcnt;
    422        1.1       cgd 		}
    423       1.37      fvdl 		(void)bread(fsreadfd, (caddr_t)inodebuf, dblk, size);
    424       1.27    bouyer 		if (doswap) {
    425       1.37      fvdl 			if (is_ufs2)
    426       1.37      fvdl 				swap_dinode2(inodebuf, lastinum - inumber);
    427       1.37      fvdl 			else
    428       1.37      fvdl 				swap_dinode1(inodebuf, lastinum - inumber);
    429       1.27    bouyer 			bwrite(fswritefd, (char *)inodebuf, dblk, size);
    430       1.27    bouyer 		}
    431       1.37      fvdl 		dp = (union dinode *)inodebuf;
    432        1.1       cgd 	}
    433       1.37      fvdl 	ret = dp;
    434       1.37      fvdl 	dp = (union dinode *)
    435       1.37      fvdl 	    ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
    436       1.37      fvdl 	return ret;
    437        1.1       cgd }
    438        1.1       cgd 
    439       1.12       cgd void
    440       1.50   xtraeme setinodebuf(ino_t inum)
    441        1.1       cgd {
    442        1.1       cgd 
    443       1.37      fvdl 	if (inum % sblock->fs_ipg != 0)
    444       1.59  christos 		errexit("bad inode number %llu to setinodebuf",
    445       1.56  christos 		    (unsigned long long)inum);
    446       1.37      fvdl 
    447       1.37      fvdl 	lastvalidinum = inum + sblock->fs_ipg - 1;
    448        1.1       cgd 	startinum = 0;
    449       1.37      fvdl 	nextino = inum;
    450       1.37      fvdl 	lastinum = inum;
    451        1.1       cgd 	readcnt = 0;
    452       1.37      fvdl 	if (inodebuf != NULL)
    453       1.37      fvdl 		return;
    454       1.68  dholland 	inobufsize = ffs_blkroundup(sblock, INOBUFSIZE);
    455       1.37      fvdl 	fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    456       1.27    bouyer 	readpercg = sblock->fs_ipg / fullcnt;
    457       1.27    bouyer 	partialcnt = sblock->fs_ipg % fullcnt;
    458       1.37      fvdl 	partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    459        1.1       cgd 	if (partialcnt != 0) {
    460        1.1       cgd 		readpercg++;
    461        1.1       cgd 	} else {
    462        1.1       cgd 		partialcnt = fullcnt;
    463        1.1       cgd 		partialsize = inobufsize;
    464        1.1       cgd 	}
    465        1.1       cgd 	if (inodebuf == NULL &&
    466       1.37      fvdl 	    (inodebuf = malloc((unsigned)inobufsize)) == NULL)
    467       1.59  christos 		errexit("Cannot allocate space for inode buffer");
    468        1.1       cgd }
    469        1.1       cgd 
    470       1.12       cgd void
    471       1.50   xtraeme freeinodebuf(void)
    472        1.1       cgd {
    473        1.1       cgd 
    474        1.1       cgd 	if (inodebuf != NULL)
    475        1.1       cgd 		free((char *)inodebuf);
    476        1.1       cgd 	inodebuf = NULL;
    477        1.1       cgd }
    478        1.1       cgd 
    479        1.1       cgd /*
    480        1.1       cgd  * Routines to maintain information about directory inodes.
    481        1.1       cgd  * This is built during the first pass and used during the
    482        1.1       cgd  * second and third passes.
    483        1.1       cgd  *
    484        1.1       cgd  * Enter inodes into the cache.
    485        1.1       cgd  */
    486       1.12       cgd void
    487       1.50   xtraeme cacheino(union dinode *dp, ino_t inumber)
    488        1.1       cgd {
    489       1.24     lukem 	struct inoinfo *inp;
    490       1.41    itojun 	struct inoinfo **inpp, **ninpsort;
    491       1.62     lukem 	unsigned int i, blks, extra;
    492       1.37      fvdl 	int64_t size;
    493        1.1       cgd 
    494       1.37      fvdl 	size = iswap64(DIP(dp, size));
    495       1.37      fvdl 	blks = howmany(size, sblock->fs_bsize);
    496       1.65  dholland 	if (blks > UFS_NDADDR)
    497       1.65  dholland 		blks = UFS_NDADDR + UFS_NIADDR;
    498       1.58       chs 	if (blks > 0)
    499       1.58       chs 		extra = (blks - 1) * sizeof (int64_t);
    500       1.58       chs 	else
    501       1.58       chs 		extra = 0;
    502       1.58       chs 	inp = malloc(sizeof(*inp) + extra);
    503        1.1       cgd 	if (inp == NULL)
    504        1.1       cgd 		return;
    505       1.37      fvdl 	inpp = &inphead[inumber % dirhash];
    506        1.1       cgd 	inp->i_nexthash = *inpp;
    507        1.1       cgd 	*inpp = inp;
    508       1.48   mycroft 	inp->i_child = inp->i_sibling = 0;
    509       1.65  dholland 	if (inumber == UFS_ROOTINO)
    510       1.65  dholland 		inp->i_parent = UFS_ROOTINO;
    511       1.14   mycroft 	else
    512       1.14   mycroft 		inp->i_parent = (ino_t)0;
    513        1.1       cgd 	inp->i_dotdot = (ino_t)0;
    514        1.1       cgd 	inp->i_number = inumber;
    515       1.37      fvdl 	inp->i_isize = size;
    516       1.37      fvdl 	inp->i_numblks = blks;
    517       1.65  dholland 	for (i = 0; i < (blks < UFS_NDADDR ? blks : UFS_NDADDR); i++)
    518       1.37      fvdl 		inp->i_blks[i] = DIP(dp, db[i]);
    519       1.65  dholland 	if (blks > UFS_NDADDR)
    520       1.65  dholland 		for (i = 0; i < UFS_NIADDR; i++)
    521       1.65  dholland 			inp->i_blks[UFS_NDADDR + i] = DIP(dp, ib[i]);
    522        1.1       cgd 	if (inplast == listmax) {
    523       1.41    itojun 		ninpsort = (struct inoinfo **)realloc((char *)inpsort,
    524       1.41    itojun 		    (unsigned)(listmax + 100) * sizeof(struct inoinfo *));
    525        1.1       cgd 		if (inpsort == NULL)
    526       1.59  christos 			errexit("cannot increase directory list");
    527       1.41    itojun 		inpsort = ninpsort;
    528       1.41    itojun 		listmax += 100;
    529        1.1       cgd 	}
    530        1.1       cgd 	inpsort[inplast++] = inp;
    531        1.1       cgd }
    532        1.1       cgd 
    533        1.1       cgd /*
    534        1.1       cgd  * Look up an inode cache structure.
    535        1.1       cgd  */
    536        1.1       cgd struct inoinfo *
    537       1.50   xtraeme getinoinfo(ino_t inumber)
    538        1.1       cgd {
    539       1.24     lukem 	struct inoinfo *inp;
    540        1.1       cgd 
    541       1.37      fvdl 	for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
    542        1.1       cgd 		if (inp->i_number != inumber)
    543        1.1       cgd 			continue;
    544        1.1       cgd 		return (inp);
    545        1.1       cgd 	}
    546       1.59  christos 	errexit("cannot find inode %llu", (unsigned long long)inumber);
    547        1.1       cgd 	return ((struct inoinfo *)0);
    548        1.1       cgd }
    549        1.1       cgd 
    550        1.1       cgd /*
    551        1.1       cgd  * Clean up all the inode cache structure.
    552        1.1       cgd  */
    553       1.12       cgd void
    554       1.50   xtraeme inocleanup(void)
    555        1.1       cgd {
    556       1.24     lukem 	struct inoinfo **inpp;
    557        1.1       cgd 
    558        1.1       cgd 	if (inphead == NULL)
    559        1.1       cgd 		return;
    560        1.1       cgd 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    561        1.1       cgd 		free((char *)(*inpp));
    562        1.1       cgd 	free((char *)inphead);
    563        1.1       cgd 	free((char *)inpsort);
    564        1.1       cgd 	inphead = inpsort = NULL;
    565        1.1       cgd }
    566        1.9   mycroft 
    567       1.12       cgd void
    568       1.50   xtraeme inodirty(void)
    569        1.1       cgd {
    570        1.1       cgd 
    571        1.1       cgd 	dirty(pbp);
    572        1.1       cgd }
    573        1.1       cgd 
    574       1.12       cgd void
    575       1.55  christos clri(struct inodesc *idesc, const char *type, int flag)
    576        1.1       cgd {
    577       1.37      fvdl 	union dinode *dp;
    578        1.1       cgd 
    579        1.1       cgd 	dp = ginode(idesc->id_number);
    580        1.1       cgd 	if (flag == 1) {
    581        1.1       cgd 		pwarn("%s %s", type,
    582       1.37      fvdl 		    (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? "DIR" : "FILE");
    583        1.1       cgd 		pinode(idesc->id_number);
    584        1.1       cgd 	}
    585        1.1       cgd 	if (preen || reply("CLEAR") == 1) {
    586        1.1       cgd 		if (preen)
    587        1.1       cgd 			printf(" (CLEARED)\n");
    588        1.1       cgd 		n_files--;
    589       1.64    bouyer 		/*
    590       1.64    bouyer 		 * ckinode will call id_func (actually always pass4check)
    591       1.64    bouyer 		 * which will update the block count
    592       1.64    bouyer 		 */
    593       1.64    bouyer 		if (idesc->id_type != SNAP)
    594       1.64    bouyer 			update_uquot(idesc->id_number,
    595       1.64    bouyer 			    idesc->id_uid, idesc->id_gid, 0, -1);
    596        1.1       cgd 		(void)ckinode(dp, idesc);
    597        1.1       cgd 		clearinode(dp);
    598       1.37      fvdl 		inoinfo(idesc->id_number)->ino_state = USTATE;
    599        1.1       cgd 		inodirty();
    600       1.27    bouyer 	} else
    601       1.60    simonb 		markclean = 0;
    602        1.1       cgd }
    603        1.1       cgd 
    604       1.12       cgd int
    605       1.50   xtraeme findname(struct inodesc *idesc)
    606        1.1       cgd {
    607       1.24     lukem 	struct direct *dirp = idesc->id_dirp;
    608       1.55  christos 	size_t len;
    609       1.55  christos 	char *buf;
    610        1.1       cgd 
    611       1.37      fvdl 	if (iswap32(dirp->d_ino) != idesc->id_parent || idesc->id_entryno < 2) {
    612       1.37      fvdl 		idesc->id_entryno++;
    613        1.1       cgd 		return (KEEPON);
    614       1.37      fvdl 	}
    615       1.55  christos 	if ((len = dirp->d_namlen + 1) > MAXPATHLEN) {
    616       1.55  christos 		/* XXX: We don't fix but we ignore */
    617       1.55  christos 		len = MAXPATHLEN;
    618       1.55  christos 	}
    619       1.55  christos 	/* this is namebuf from utilities.c */
    620       1.55  christos 	buf = __UNCONST(idesc->id_name);
    621       1.55  christos 	(void)memcpy(buf, dirp->d_name, (size_t)dirp->d_namlen + 1);
    622        1.1       cgd 	return (STOP|FOUND);
    623        1.1       cgd }
    624        1.1       cgd 
    625       1.12       cgd int
    626       1.50   xtraeme findino(struct inodesc *idesc)
    627        1.1       cgd {
    628       1.24     lukem 	struct direct *dirp = idesc->id_dirp;
    629        1.1       cgd 
    630        1.1       cgd 	if (dirp->d_ino == 0)
    631        1.1       cgd 		return (KEEPON);
    632        1.1       cgd 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    633       1.65  dholland 	    iswap32(dirp->d_ino) >= UFS_ROOTINO && iswap32(dirp->d_ino) <= maxino) {
    634       1.27    bouyer 		idesc->id_parent = iswap32(dirp->d_ino);
    635        1.1       cgd 		return (STOP|FOUND);
    636        1.1       cgd 	}
    637        1.1       cgd 	return (KEEPON);
    638        1.1       cgd }
    639        1.1       cgd 
    640       1.37      fvdl int
    641       1.50   xtraeme clearentry(struct inodesc *idesc)
    642       1.37      fvdl {
    643       1.37      fvdl 	struct direct *dirp = idesc->id_dirp;
    644       1.37      fvdl 
    645       1.37      fvdl 	if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
    646       1.37      fvdl 		idesc->id_entryno++;
    647       1.37      fvdl 		return (KEEPON);
    648       1.37      fvdl 	}
    649       1.37      fvdl 	dirp->d_ino = 0;
    650       1.37      fvdl 	return (STOP|FOUND|ALTERED);
    651       1.37      fvdl }
    652       1.37      fvdl 
    653       1.12       cgd void
    654       1.50   xtraeme pinode(ino_t ino)
    655        1.1       cgd {
    656       1.37      fvdl 	union dinode *dp;
    657        1.1       cgd 	struct passwd *pw;
    658        1.1       cgd 
    659       1.56  christos 	printf(" I=%llu ", (unsigned long long)ino);
    660       1.65  dholland 	if (ino < UFS_ROOTINO || ino > maxino)
    661        1.1       cgd 		return;
    662        1.1       cgd 	dp = ginode(ino);
    663        1.1       cgd 	printf(" OWNER=");
    664        1.6       cgd #ifndef SMALL
    665       1.61  christos 	if (Uflag && (pw = getpwuid((int)iswap32(DIP(dp, uid)))) != 0)
    666        1.1       cgd 		printf("%s ", pw->pw_name);
    667        1.1       cgd 	else
    668        1.6       cgd #endif
    669       1.37      fvdl 		printf("%u ", (unsigned)iswap32(DIP(dp, uid)));
    670       1.37      fvdl 	printf("MODE=%o\n", iswap16(DIP(dp, mode)));
    671        1.1       cgd 	if (preen)
    672       1.21  christos 		printf("%s: ", cdevname());
    673       1.37      fvdl 	printf("SIZE=%llu ", (unsigned long long)iswap64(DIP(dp, size)));
    674       1.63  christos 	printf("MTIME=%s ", print_mtime(iswap32(DIP(dp, mtime))));
    675        1.1       cgd }
    676        1.1       cgd 
    677       1.12       cgd void
    678       1.55  christos blkerror(ino_t ino, const char *type, daddr_t blk)
    679        1.1       cgd {
    680       1.37      fvdl 	struct inostat *info;
    681        1.1       cgd 
    682       1.56  christos 	pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
    683        1.1       cgd 	printf("\n");
    684       1.37      fvdl 	info = inoinfo(ino);
    685       1.37      fvdl 	switch (info->ino_state) {
    686        1.1       cgd 
    687        1.1       cgd 	case FSTATE:
    688       1.37      fvdl 		info->ino_state = FCLEAR;
    689        1.1       cgd 		return;
    690        1.1       cgd 
    691        1.1       cgd 	case DSTATE:
    692       1.37      fvdl 		info->ino_state = DCLEAR;
    693        1.1       cgd 		return;
    694        1.1       cgd 
    695        1.1       cgd 	case FCLEAR:
    696        1.1       cgd 	case DCLEAR:
    697        1.1       cgd 		return;
    698        1.1       cgd 
    699        1.1       cgd 	default:
    700       1.59  christos 		errexit("BAD STATE %d TO BLKERR", info->ino_state);
    701        1.1       cgd 		/* NOTREACHED */
    702        1.1       cgd 	}
    703        1.1       cgd }
    704        1.1       cgd 
    705        1.1       cgd /*
    706        1.1       cgd  * allocate an unused inode
    707        1.1       cgd  */
    708        1.1       cgd ino_t
    709       1.50   xtraeme allocino(ino_t request, int type)
    710        1.1       cgd {
    711       1.24     lukem 	ino_t ino;
    712       1.37      fvdl 	union dinode *dp;
    713       1.37      fvdl 	struct ufs1_dinode *dp1;
    714       1.37      fvdl 	struct ufs2_dinode *dp2;
    715       1.16   thorpej 	time_t t;
    716       1.32      fvdl 	struct cg *cgp = cgrp;
    717       1.32      fvdl 	int cg;
    718       1.54     lukem 	struct inostat *info = NULL;
    719       1.64    bouyer 	int nfrags;
    720        1.1       cgd 
    721        1.1       cgd 	if (request == 0)
    722       1.65  dholland 		request = UFS_ROOTINO;
    723       1.37      fvdl 	else if (inoinfo(request)->ino_state != USTATE)
    724        1.1       cgd 		return (0);
    725       1.37      fvdl 	for (ino = request; ino < maxino; ino++) {
    726       1.37      fvdl 		info = inoinfo(ino);
    727       1.37      fvdl 		if (info->ino_state == USTATE)
    728        1.1       cgd 			break;
    729       1.37      fvdl 	}
    730        1.1       cgd 	if (ino == maxino)
    731        1.1       cgd 		return (0);
    732       1.32      fvdl 	cg = ino_to_cg(sblock, ino);
    733       1.49       dbj 	/* If necessary, extend the inoinfo array. grow exponentially */
    734       1.62     lukem 	if ((ino % sblock->fs_ipg) >= (uint64_t)inostathead[cg].il_numalloced) {
    735       1.49       dbj 		unsigned long newalloced, i;
    736       1.49       dbj 		newalloced = MIN(sblock->fs_ipg,
    737       1.49       dbj 			MAX(2 * inostathead[cg].il_numalloced, 10));
    738       1.49       dbj 		info = calloc(newalloced, sizeof(struct inostat));
    739       1.49       dbj 		if (info == NULL) {
    740       1.49       dbj 			pwarn("cannot alloc %lu bytes to extend inoinfo\n",
    741       1.49       dbj 				sizeof(struct inostat) * newalloced);
    742       1.49       dbj 			return 0;
    743       1.49       dbj 		}
    744       1.49       dbj 		memmove(info, inostathead[cg].il_stat,
    745       1.49       dbj 			inostathead[cg].il_numalloced * sizeof(*info));
    746       1.49       dbj 		for (i = inostathead[cg].il_numalloced; i < newalloced; i++) {
    747       1.49       dbj 			info[i].ino_state = USTATE;
    748       1.49       dbj 		}
    749       1.49       dbj 		if (inostathead[cg].il_numalloced)
    750       1.49       dbj 			free(inostathead[cg].il_stat);
    751       1.49       dbj 		inostathead[cg].il_stat = info;
    752       1.49       dbj 		inostathead[cg].il_numalloced = newalloced;
    753       1.49       dbj 		info = inoinfo(ino);
    754       1.49       dbj 	}
    755       1.32      fvdl 	getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
    756       1.32      fvdl 	memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    757       1.32      fvdl 	if ((doswap && !needswap) || (!doswap && needswap))
    758       1.37      fvdl 		ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
    759       1.32      fvdl 	if (!cg_chkmagic(cgp, 0))
    760       1.32      fvdl 		pfatal("CG %d: ALLOCINO: BAD MAGIC NUMBER\n", cg);
    761       1.32      fvdl 	if (doswap)
    762       1.32      fvdl 		cgdirty();
    763       1.32      fvdl 	setbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
    764       1.32      fvdl 	cgp->cg_cs.cs_nifree--;
    765       1.64    bouyer 	sblock->fs_cstotal.cs_nifree--;
    766       1.64    bouyer 	sblock->fs_cs(fs, cg).cs_nifree--;
    767       1.64    bouyer 	sbdirty();
    768        1.1       cgd 	switch (type & IFMT) {
    769        1.1       cgd 	case IFDIR:
    770       1.37      fvdl 		info->ino_state = DSTATE;
    771       1.32      fvdl 		cgp->cg_cs.cs_ndir++;
    772       1.64    bouyer 		nfrags = 1;
    773        1.1       cgd 		break;
    774        1.1       cgd 	case IFREG:
    775       1.64    bouyer 		info->ino_state = FSTATE;
    776       1.64    bouyer 		nfrags = sblock->fs_frag;
    777       1.64    bouyer 		break;
    778        1.1       cgd 	case IFLNK:
    779       1.37      fvdl 		info->ino_state = FSTATE;
    780       1.64    bouyer 		nfrags = 1;
    781        1.1       cgd 		break;
    782        1.1       cgd 	default:
    783        1.1       cgd 		return (0);
    784        1.1       cgd 	}
    785       1.32      fvdl 	cgdirty();
    786        1.1       cgd 	dp = ginode(ino);
    787       1.37      fvdl 	if (is_ufs2) {
    788       1.37      fvdl 		dp2 = &dp->dp2;
    789       1.64    bouyer 		dp2->di_db[0] = iswap64(allocblk(nfrags));
    790       1.37      fvdl 		if (dp2->di_db[0] == 0) {
    791       1.37      fvdl 			info->ino_state = USTATE;
    792       1.37      fvdl 			return (0);
    793       1.37      fvdl 		}
    794       1.37      fvdl 		dp2->di_mode = iswap16(type);
    795       1.37      fvdl 		dp2->di_flags = 0;
    796       1.37      fvdl 		(void)time(&t);
    797       1.37      fvdl 		dp2->di_atime = iswap64(t);
    798       1.37      fvdl 		dp2->di_mtime = dp2->di_ctime = dp2->di_atime;
    799       1.68  dholland 		dp2->di_size = iswap64(ffs_lfragtosize(sblock, nfrags));
    800       1.68  dholland 		dp2->di_blocks = iswap64(btodb(ffs_lfragtosize(sblock, nfrags)));
    801       1.37      fvdl 	} else {
    802       1.37      fvdl 		dp1 = &dp->dp1;
    803       1.64    bouyer 		dp1->di_db[0] = iswap32(allocblk(nfrags));
    804       1.37      fvdl 		if (dp1->di_db[0] == 0) {
    805       1.37      fvdl 			info->ino_state = USTATE;
    806       1.37      fvdl 			return (0);
    807       1.37      fvdl 		}
    808       1.37      fvdl 		dp1->di_mode = iswap16(type);
    809       1.37      fvdl 		dp1->di_flags = 0;
    810       1.37      fvdl 		(void)time(&t);
    811       1.37      fvdl 		dp1->di_atime = iswap32(t);
    812       1.37      fvdl 		dp1->di_mtime = dp1->di_ctime = dp1->di_atime;
    813       1.68  dholland 		dp1->di_size = iswap64(ffs_lfragtosize(sblock, nfrags));
    814       1.68  dholland 		dp1->di_blocks = iswap32(btodb(ffs_lfragtosize(sblock, nfrags)));
    815        1.1       cgd 	}
    816        1.1       cgd 	n_files++;
    817        1.1       cgd 	inodirty();
    818        1.9   mycroft 	if (newinofmt)
    819       1.37      fvdl 		info->ino_type = IFTODT(type);
    820        1.1       cgd 	return (ino);
    821        1.1       cgd }
    822        1.1       cgd 
    823        1.1       cgd /*
    824        1.1       cgd  * deallocate an inode
    825        1.1       cgd  */
    826       1.12       cgd void
    827       1.50   xtraeme freeino(ino_t ino)
    828        1.1       cgd {
    829        1.1       cgd 	struct inodesc idesc;
    830       1.37      fvdl 	union dinode *dp;
    831       1.64    bouyer 	struct cg *cgp = cgrp;
    832       1.64    bouyer 	int cg;
    833       1.64    bouyer 
    834       1.64    bouyer 	cg = ino_to_cg(sblock, ino);
    835       1.64    bouyer 	getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
    836       1.64    bouyer 	memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    837       1.64    bouyer 	if ((doswap && !needswap) || (!doswap && needswap))
    838       1.64    bouyer 		ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
    839       1.64    bouyer 	if (!cg_chkmagic(cgp, 0)) {
    840       1.64    bouyer 		pwarn("CG %d: FREEINO: BAD MAGIC NUMBER\n", cg);
    841       1.64    bouyer 		cgp = NULL;
    842       1.64    bouyer 	}
    843        1.1       cgd 
    844       1.11   mycroft 	memset(&idesc, 0, sizeof(struct inodesc));
    845        1.1       cgd 	idesc.id_func = pass4check;
    846        1.1       cgd 	idesc.id_number = ino;
    847        1.1       cgd 	dp = ginode(ino);
    848       1.64    bouyer 	idesc.id_uid = iswap32(DIP(dp, uid));
    849       1.64    bouyer 	idesc.id_gid = iswap32(DIP(dp, gid));
    850       1.64    bouyer 	if (iswap32(DIP(dp, flags)) & SF_SNAPSHOT)
    851       1.64    bouyer 		idesc.id_type = SNAP;
    852       1.64    bouyer 	else
    853       1.64    bouyer 		idesc.id_type = ADDR;
    854        1.1       cgd 	(void)ckinode(dp, &idesc);
    855        1.1       cgd 	clearinode(dp);
    856        1.1       cgd 	inodirty();
    857       1.37      fvdl 	inoinfo(ino)->ino_state = USTATE;
    858       1.64    bouyer 	if (idesc.id_type != SNAP)
    859       1.64    bouyer 		update_uquot(idesc.id_number,
    860       1.64    bouyer 		    idesc.id_uid, idesc.id_gid, 0, -1);
    861        1.1       cgd 	n_files--;
    862       1.64    bouyer 	if (cgp) {
    863       1.64    bouyer 		clrbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
    864       1.64    bouyer 		cgp->cg_cs.cs_nifree++;
    865       1.64    bouyer 		sblock->fs_cstotal.cs_nifree++;
    866       1.64    bouyer 		sblock->fs_cs(fs, cg).cs_nifree++;
    867       1.64    bouyer 		sbdirty();
    868       1.64    bouyer 		cgdirty();
    869       1.64    bouyer 	}
    870       1.64    bouyer }
    871       1.64    bouyer 
    872       1.64    bouyer /* read a data block from inode */
    873       1.64    bouyer ssize_t
    874       1.64    bouyer readblk(union dinode *dp, off_t offset, struct bufarea **bp)
    875       1.64    bouyer {
    876       1.68  dholland 	daddr_t blkno = ffs_lblkno(sblock, offset);
    877       1.64    bouyer 	daddr_t iblkno;
    878       1.64    bouyer 	int type = IFMT & iswap16(DIP(dp, mode));
    879       1.64    bouyer 	ssize_t filesize = iswap64(DIP(dp, size));
    880       1.64    bouyer 	int ilevel;
    881       1.64    bouyer 	daddr_t nblks;
    882       1.64    bouyer 	const daddr_t naddrperblk = sblock->fs_bsize /
    883       1.64    bouyer 	    (is_ufs2 ? sizeof(uint64_t) : sizeof(uint32_t));
    884       1.64    bouyer 	struct bufarea *ibp;
    885       1.64    bouyer 
    886       1.64    bouyer 	*bp = NULL;
    887       1.64    bouyer 	offset &= ~(sblock->fs_bsize - 1);
    888       1.64    bouyer 
    889       1.64    bouyer 	if (type != IFREG)
    890       1.64    bouyer 		return 0;
    891       1.64    bouyer 	if (offset >= filesize)
    892       1.64    bouyer 		return 0; /* short read */
    893       1.65  dholland 	if (blkno < UFS_NDADDR) {
    894       1.64    bouyer 		blkno = is_ufs2 ? iswap64(dp->dp2.di_db[blkno]) :
    895       1.64    bouyer 		    iswap32(dp->dp1.di_db[blkno]);
    896       1.64    bouyer 		if (blkno == 0)
    897       1.64    bouyer 			return 0;
    898       1.64    bouyer 		*bp = getdatablk(blkno, sblock->fs_bsize);
    899       1.64    bouyer 		return (bp != NULL) ? sblock->fs_bsize : 0;
    900       1.64    bouyer 	}
    901       1.65  dholland 	blkno -= UFS_NDADDR;
    902       1.64    bouyer 	/* find indir level */
    903       1.64    bouyer 	for (ilevel = 1, nblks = naddrperblk;
    904       1.65  dholland 	     ilevel <= UFS_NIADDR;
    905       1.64    bouyer 	     ilevel++, nblks *= naddrperblk) {
    906       1.64    bouyer 		if (blkno < nblks)
    907       1.64    bouyer 			break;
    908       1.64    bouyer 		else
    909       1.64    bouyer 			blkno -= nblks;
    910       1.64    bouyer 	}
    911       1.65  dholland 	if (ilevel > UFS_NIADDR)
    912       1.64    bouyer 		errexit("bad ofsset %" PRIu64 " to readblk", offset);
    913       1.64    bouyer 
    914       1.64    bouyer 	/* get the first indirect block */
    915       1.64    bouyer 	iblkno = is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
    916       1.64    bouyer 		    iswap32(dp->dp1.di_ib[ilevel - 1]);
    917       1.64    bouyer 	if (iblkno == 0)
    918       1.64    bouyer 		return 0;
    919       1.64    bouyer 	ibp = getdatablk(iblkno, sblock->fs_bsize);
    920       1.64    bouyer 	/* walk indirect blocks up to the data block */
    921       1.64    bouyer 	for (; ilevel >0 ; ilevel--) {
    922       1.64    bouyer 		nblks = nblks / naddrperblk;
    923       1.64    bouyer 		if (is_ufs2)
    924       1.64    bouyer 			iblkno = iswap64(ibp->b_un.b_indir2[blkno / nblks]);
    925       1.64    bouyer 		else
    926       1.64    bouyer 			iblkno = iswap32(ibp->b_un.b_indir1[blkno / nblks]);
    927       1.64    bouyer 		if (iblkno == 0)
    928       1.64    bouyer 			return 0;
    929       1.64    bouyer 		blkno = blkno % nblks;
    930       1.64    bouyer 		ibp->b_flags &= ~B_INUSE;
    931       1.64    bouyer 		ibp = getdatablk(iblkno, sblock->fs_bsize);
    932       1.64    bouyer 	}
    933       1.64    bouyer 	*bp = ibp;
    934       1.64    bouyer 	return sblock->fs_bsize;
    935       1.64    bouyer }
    936       1.64    bouyer 
    937       1.64    bouyer static struct bufarea * getnewblk(daddr_t *);
    938       1.64    bouyer static struct bufarea *
    939       1.64    bouyer getnewblk(daddr_t *blkno)
    940       1.64    bouyer {
    941       1.64    bouyer 	struct bufarea *bp;
    942       1.64    bouyer 	*blkno = allocblk(sblock->fs_frag);
    943       1.64    bouyer 	if (*blkno == 0)
    944       1.64    bouyer 		return NULL;
    945       1.64    bouyer 	bp = getdatablk(*blkno, sblock->fs_bsize);
    946       1.64    bouyer 	memset(bp->b_un.b_buf, 0, sblock->fs_bsize);
    947       1.64    bouyer 	return bp;
    948       1.64    bouyer }
    949       1.64    bouyer 
    950       1.64    bouyer /* expand given inode by one full fs block */
    951       1.64    bouyer struct bufarea *
    952       1.64    bouyer expandfile(union dinode *dp)
    953       1.64    bouyer {
    954       1.64    bouyer 	uint64_t filesize = iswap64(DIP(dp, size));
    955       1.64    bouyer 	daddr_t newblk, blkno, iblkno, nblks;
    956       1.64    bouyer 	daddr_t di_blocks;
    957       1.64    bouyer 	int ilevel;
    958       1.64    bouyer 	const daddr_t naddrperblk = sblock->fs_bsize /
    959       1.64    bouyer 	    (is_ufs2 ? sizeof(uint64_t) : sizeof(uint32_t));
    960       1.64    bouyer 	struct bufarea *ibp, *bp = NULL;
    961       1.64    bouyer 
    962       1.64    bouyer 	di_blocks = is_ufs2 ? iswap64(dp->dp2.di_blocks) :
    963       1.64    bouyer 	    iswap32(dp->dp1.di_blocks);
    964       1.64    bouyer 	/* compute location of new block */
    965       1.68  dholland 	blkno = ffs_lblkno(sblock, filesize);
    966       1.64    bouyer 
    967       1.65  dholland 	if (blkno < UFS_NDADDR) {
    968       1.64    bouyer 		/* easy way: allocate a direct block */
    969       1.64    bouyer 		if ((bp = getnewblk(&newblk)) == NULL) {
    970       1.64    bouyer 			return NULL;
    971       1.64    bouyer 		}
    972       1.64    bouyer 		di_blocks += btodb(sblock->fs_bsize);
    973       1.64    bouyer 
    974       1.64    bouyer 		if (is_ufs2) {
    975       1.64    bouyer 			dp->dp2.di_db[blkno] = iswap64(newblk);
    976       1.64    bouyer 		} else {
    977       1.64    bouyer 			dp->dp1.di_db[blkno] = iswap32(newblk);
    978       1.64    bouyer 		}
    979       1.64    bouyer 		goto out;
    980       1.64    bouyer 	}
    981       1.65  dholland 	blkno -= UFS_NDADDR;
    982       1.64    bouyer 	/* find indir level */
    983       1.64    bouyer 	for (ilevel = 1, nblks = naddrperblk;
    984       1.65  dholland 	     ilevel <= UFS_NIADDR;
    985       1.64    bouyer 	     ilevel++, nblks *= naddrperblk) {
    986       1.64    bouyer 		if (blkno < nblks)
    987       1.64    bouyer 			break;
    988       1.64    bouyer 		else
    989       1.64    bouyer 			blkno -= nblks;
    990       1.64    bouyer 	}
    991       1.65  dholland 	if (ilevel > UFS_NIADDR)
    992       1.64    bouyer 		errexit("bad filesize %" PRIu64 " to expandfile", filesize);
    993       1.64    bouyer 
    994       1.64    bouyer 	/* get the first indirect block, allocating if needed */
    995       1.64    bouyer 	if ((is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
    996       1.64    bouyer 		iswap32(dp->dp1.di_ib[ilevel - 1])) == 0) {
    997       1.64    bouyer 		if ((ibp = getnewblk(&newblk)) == NULL)
    998       1.64    bouyer 			return 0;
    999       1.64    bouyer 		di_blocks += btodb(sblock->fs_bsize);
   1000       1.64    bouyer 		if (is_ufs2)
   1001       1.64    bouyer 			dp->dp2.di_ib[ilevel - 1] = iswap64(newblk);
   1002       1.64    bouyer 		else
   1003       1.64    bouyer 			dp->dp1.di_ib[ilevel - 1] = iswap32(newblk);
   1004       1.64    bouyer 	} else {
   1005       1.64    bouyer 		ibp = getdatablk(is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
   1006       1.70    bouyer 		    iswap32(dp->dp1.di_ib[ilevel - 1]), sblock->fs_bsize);
   1007       1.64    bouyer 	}
   1008       1.64    bouyer 	/* walk indirect blocks up to the data block */
   1009       1.64    bouyer 	for (; ilevel >0 ; ilevel--) {
   1010       1.64    bouyer 		nblks = nblks / naddrperblk;
   1011       1.64    bouyer 		if (is_ufs2)
   1012       1.64    bouyer 			iblkno = iswap64(ibp->b_un.b_indir2[blkno / nblks]);
   1013       1.64    bouyer 		else
   1014       1.64    bouyer 			iblkno = iswap32(ibp->b_un.b_indir1[blkno / nblks]);
   1015       1.64    bouyer 		if (iblkno == 0) {
   1016       1.64    bouyer 			if ((bp = getnewblk(&newblk)) == NULL)
   1017       1.64    bouyer 				return NULL;
   1018       1.64    bouyer 			di_blocks += btodb(sblock->fs_bsize);
   1019       1.64    bouyer 			if (is_ufs2)
   1020       1.64    bouyer 				ibp->b_un.b_indir2[blkno / nblks] =
   1021       1.64    bouyer 				    iswap64(newblk);
   1022       1.64    bouyer 			else
   1023       1.64    bouyer 				ibp->b_un.b_indir1[blkno / nblks] =
   1024       1.64    bouyer 				    iswap32(newblk);
   1025       1.64    bouyer 			dirty(ibp);
   1026       1.64    bouyer 			ibp->b_flags &= ~B_INUSE;
   1027       1.64    bouyer 			ibp = bp;
   1028       1.64    bouyer 		} else {
   1029       1.64    bouyer 			ibp->b_flags &= ~B_INUSE;
   1030       1.64    bouyer 			ibp = getdatablk(iblkno, sblock->fs_bsize);
   1031       1.64    bouyer 			bp = NULL;
   1032       1.64    bouyer 		}
   1033       1.64    bouyer 		blkno = blkno % nblks;
   1034       1.64    bouyer 	}
   1035       1.64    bouyer 	if (bp == NULL) {
   1036       1.64    bouyer 		errexit("INTERNAL ERROR: "
   1037       1.64    bouyer 		    "expandfile() failed to allocate a new block\n");
   1038       1.64    bouyer 	}
   1039       1.64    bouyer 
   1040       1.64    bouyer out:
   1041       1.64    bouyer 	filesize += sblock->fs_bsize;
   1042       1.64    bouyer 	if (is_ufs2) {
   1043       1.64    bouyer 		dp->dp2.di_size = iswap64(filesize);
   1044       1.64    bouyer 		dp->dp2.di_blocks = iswap64(di_blocks);
   1045       1.64    bouyer 	} else {
   1046       1.64    bouyer 		dp->dp1.di_size = iswap64(filesize);
   1047       1.64    bouyer 		dp->dp1.di_blocks = iswap32(di_blocks);
   1048       1.64    bouyer 	}
   1049       1.64    bouyer 	inodirty();
   1050       1.64    bouyer 	return bp;
   1051        1.1       cgd }
   1052