Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_inode.c revision 1.35
      1  1.35       agc /*	$NetBSD: ext2fs_inode.c,v 1.35 2003/08/07 16:34:26 agc Exp $	*/
      2   1.1    bouyer 
      3   1.1    bouyer /*
      4   1.1    bouyer  * Copyright (c) 1982, 1986, 1989, 1993
      5   1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
      6   1.1    bouyer  *
      7   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      8   1.1    bouyer  * modification, are permitted provided that the following conditions
      9   1.1    bouyer  * are met:
     10   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     11   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     12   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     15  1.35       agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.35       agc  *    may be used to endorse or promote products derived from this software
     17  1.35       agc  *    without specific prior written permission.
     18  1.35       agc  *
     19  1.35       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.35       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.35       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.35       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.35       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.35       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.35       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.35       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.35       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.35       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.35       agc  * SUCH DAMAGE.
     30  1.35       agc  *
     31  1.35       agc  *	@(#)ffs_inode.c	8.8 (Berkeley) 10/19/94
     32  1.35       agc  * Modified for ext2fs by Manuel Bouyer.
     33  1.35       agc  */
     34  1.35       agc 
     35  1.35       agc /*
     36  1.35       agc  * Copyright (c) 1997 Manuel Bouyer.
     37  1.35       agc  *
     38  1.35       agc  * Redistribution and use in source and binary forms, with or without
     39  1.35       agc  * modification, are permitted provided that the following conditions
     40  1.35       agc  * are met:
     41  1.35       agc  * 1. Redistributions of source code must retain the above copyright
     42  1.35       agc  *    notice, this list of conditions and the following disclaimer.
     43  1.35       agc  * 2. Redistributions in binary form must reproduce the above copyright
     44  1.35       agc  *    notice, this list of conditions and the following disclaimer in the
     45  1.35       agc  *    documentation and/or other materials provided with the distribution.
     46   1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     47   1.1    bouyer  *    must display the following acknowledgement:
     48   1.1    bouyer  *	This product includes software developed by the University of
     49   1.1    bouyer  *	California, Berkeley and its contributors.
     50   1.1    bouyer  * 4. Neither the name of the University nor the names of its contributors
     51   1.1    bouyer  *    may be used to endorse or promote products derived from this software
     52   1.1    bouyer  *    without specific prior written permission.
     53   1.1    bouyer  *
     54   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.1    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.1    bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.1    bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.1    bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.1    bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.1    bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.1    bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.1    bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.1    bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.1    bouyer  * SUCH DAMAGE.
     65   1.1    bouyer  *
     66   1.1    bouyer  *	@(#)ffs_inode.c	8.8 (Berkeley) 10/19/94
     67   1.1    bouyer  * Modified for ext2fs by Manuel Bouyer.
     68   1.1    bouyer  */
     69  1.27     lukem 
     70  1.27     lukem #include <sys/cdefs.h>
     71  1.35       agc __KERNEL_RCSID(0, "$NetBSD: ext2fs_inode.c,v 1.35 2003/08/07 16:34:26 agc Exp $");
     72   1.5       mrg 
     73   1.1    bouyer #include <sys/param.h>
     74   1.1    bouyer #include <sys/systm.h>
     75   1.1    bouyer #include <sys/mount.h>
     76   1.1    bouyer #include <sys/proc.h>
     77   1.1    bouyer #include <sys/file.h>
     78   1.1    bouyer #include <sys/buf.h>
     79   1.1    bouyer #include <sys/vnode.h>
     80   1.1    bouyer #include <sys/kernel.h>
     81   1.1    bouyer #include <sys/malloc.h>
     82   1.1    bouyer #include <sys/trace.h>
     83   1.1    bouyer #include <sys/resourcevar.h>
     84   1.1    bouyer 
     85   1.1    bouyer #include <ufs/ufs/inode.h>
     86   1.1    bouyer #include <ufs/ufs/ufsmount.h>
     87   1.1    bouyer #include <ufs/ufs/ufs_extern.h>
     88   1.1    bouyer 
     89   1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     90   1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     91   1.1    bouyer 
     92  1.22   tsutsui extern int prtactive;
     93  1.22   tsutsui 
     94  1.29      fvdl static int ext2fs_indirtrunc __P((struct inode *, daddr_t, daddr_t,
     95  1.29      fvdl 				  daddr_t, int, long *));
     96   1.1    bouyer 
     97   1.1    bouyer /*
     98   1.1    bouyer  * Last reference to an inode.  If necessary, write or delete it.
     99   1.1    bouyer  */
    100   1.1    bouyer int
    101   1.1    bouyer ext2fs_inactive(v)
    102   1.1    bouyer 	void *v;
    103   1.1    bouyer {
    104   1.1    bouyer 	struct vop_inactive_args /* {
    105   1.1    bouyer 		struct vnode *a_vp;
    106  1.34      fvdl 		struct proc *a_p;
    107   1.1    bouyer 	} */ *ap = v;
    108   1.6      fvdl 	struct vnode *vp = ap->a_vp;
    109   1.6      fvdl 	struct inode *ip = VTOI(vp);
    110  1.34      fvdl 	struct proc *p = ap->a_p;
    111   1.1    bouyer 	struct timespec ts;
    112   1.6      fvdl 	int error = 0;
    113   1.1    bouyer 
    114   1.1    bouyer 	if (prtactive && vp->v_usecount != 0)
    115   1.7     mikel 		vprint("ext2fs_inactive: pushing active", vp);
    116   1.1    bouyer 	/* Get rid of inodes related to stale file handles. */
    117   1.6      fvdl 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
    118   1.6      fvdl 		goto out;
    119   1.1    bouyer 
    120   1.1    bouyer 	error = 0;
    121   1.1    bouyer 	if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
    122  1.23       chs 		if (ip->i_e2fs_size != 0) {
    123  1.23       chs 			error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL);
    124  1.23       chs 		}
    125   1.1    bouyer 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    126   1.1    bouyer 		ip->i_e2fs_dtime = ts.tv_sec;
    127   1.1    bouyer 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    128   1.1    bouyer 		VOP_VFREE(vp, ip->i_number, ip->i_e2fs_mode);
    129   1.1    bouyer 	}
    130  1.18   mycroft 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED))
    131  1.12   mycroft 		VOP_UPDATE(vp, NULL, NULL, 0);
    132   1.6      fvdl out:
    133   1.6      fvdl 	VOP_UNLOCK(vp, 0);
    134   1.1    bouyer 	/*
    135   1.1    bouyer 	 * If we are done with the inode, reclaim it
    136   1.1    bouyer 	 * so that it can be reused immediately.
    137   1.1    bouyer 	 */
    138   1.6      fvdl 	if (ip->i_e2fs_dtime != 0)
    139  1.34      fvdl 		vrecycle(vp, NULL, p);
    140   1.1    bouyer 	return (error);
    141   1.1    bouyer }
    142   1.1    bouyer 
    143   1.1    bouyer 
    144   1.1    bouyer /*
    145   1.1    bouyer  * Update the access, modified, and inode change times as specified by the
    146   1.1    bouyer  * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
    147   1.1    bouyer  * used to specify that the inode needs to be updated but that the times have
    148   1.1    bouyer  * already been set. The access and modified times are taken from the second
    149   1.1    bouyer  * and third parameters; the inode change time is always taken from the current
    150  1.15  perseant  * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk
    151  1.15  perseant  * write of the inode to complete.
    152   1.1    bouyer  */
    153   1.1    bouyer int
    154   1.1    bouyer ext2fs_update(v)
    155   1.1    bouyer 	void *v;
    156   1.1    bouyer {
    157   1.1    bouyer 	struct vop_update_args /* {
    158   1.1    bouyer 		struct vnode *a_vp;
    159   1.1    bouyer 		struct timespec *a_access;
    160   1.1    bouyer 		struct timespec *a_modify;
    161  1.15  perseant 		int a_flags;
    162   1.1    bouyer 	} */ *ap = v;
    163  1.14  augustss 	struct m_ext2fs *fs;
    164   1.1    bouyer 	struct buf *bp;
    165   1.1    bouyer 	struct inode *ip;
    166   1.1    bouyer 	int error;
    167   1.1    bouyer 	struct timespec ts;
    168  1.10   thorpej 	caddr_t cp;
    169  1.18   mycroft 	int flags;
    170   1.1    bouyer 
    171   1.1    bouyer 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
    172   1.1    bouyer 		return (0);
    173   1.1    bouyer 	ip = VTOI(ap->a_vp);
    174   1.1    bouyer 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    175  1.11   mycroft 	EXT2FS_ITIMES(ip,
    176  1.11   mycroft 	    ap->a_access ? ap->a_access : &ts,
    177  1.11   mycroft 	    ap->a_modify ? ap->a_modify : &ts, &ts);
    178  1.18   mycroft 	flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
    179  1.18   mycroft 	if (flags == 0)
    180   1.1    bouyer 		return (0);
    181   1.1    bouyer 	fs = ip->i_e2fs;
    182  1.18   mycroft 
    183   1.1    bouyer 	error = bread(ip->i_devvp,
    184   1.1    bouyer 			  fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    185   1.1    bouyer 			  (int)fs->e2fs_bsize, NOCRED, &bp);
    186   1.1    bouyer 	if (error) {
    187   1.1    bouyer 		brelse(bp);
    188   1.1    bouyer 		return (error);
    189   1.1    bouyer 	}
    190  1.19   mycroft 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    191  1.10   thorpej 	cp = (caddr_t)bp->b_data +
    192  1.10   thorpej 	    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
    193  1.32        he 	e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
    194  1.19   mycroft 	if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
    195  1.18   mycroft 	    (flags & IN_MODIFIED) != 0 &&
    196  1.15  perseant 	    (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
    197   1.1    bouyer 		return (bwrite(bp));
    198   1.1    bouyer 	else {
    199   1.1    bouyer 		bdwrite(bp);
    200   1.1    bouyer 		return (0);
    201   1.1    bouyer 	}
    202   1.1    bouyer }
    203   1.1    bouyer 
    204   1.1    bouyer #define	SINGLE	0	/* index of single indirect block */
    205   1.1    bouyer #define	DOUBLE	1	/* index of double indirect block */
    206   1.1    bouyer #define	TRIPLE	2	/* index of triple indirect block */
    207   1.1    bouyer /*
    208   1.1    bouyer  * Truncate the inode oip to at most length size, freeing the
    209   1.1    bouyer  * disk blocks.
    210   1.1    bouyer  */
    211   1.1    bouyer int
    212   1.1    bouyer ext2fs_truncate(v)
    213   1.1    bouyer 	void *v;
    214   1.1    bouyer {
    215   1.1    bouyer 	struct vop_truncate_args /* {
    216   1.1    bouyer 		struct vnode *a_vp;
    217   1.1    bouyer 		off_t a_length;
    218   1.1    bouyer 		int a_flags;
    219   1.1    bouyer 		struct ucred *a_cred;
    220   1.1    bouyer 		struct proc *a_p;
    221   1.1    bouyer 	} */ *ap = v;
    222  1.14  augustss 	struct vnode *ovp = ap->a_vp;
    223  1.29      fvdl 	daddr_t lastblock;
    224  1.14  augustss 	struct inode *oip;
    225  1.29      fvdl 	daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    226  1.30      fvdl 	/* XXX ondisk32 */
    227  1.30      fvdl 	int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
    228   1.1    bouyer 	off_t length = ap->a_length;
    229  1.14  augustss 	struct m_ext2fs *fs;
    230   1.1    bouyer 	int offset, size, level;
    231  1.16   mycroft 	long count, nblocks, blocksreleased = 0;
    232  1.14  augustss 	int i;
    233  1.21       chs 	int error, allerror = 0;
    234   1.1    bouyer 	off_t osize;
    235   1.1    bouyer 
    236   1.1    bouyer 	if (length < 0)
    237   1.1    bouyer 		return (EINVAL);
    238   1.1    bouyer 
    239   1.1    bouyer 	oip = VTOI(ovp);
    240   1.1    bouyer 	if (ovp->v_type == VLNK &&
    241   1.1    bouyer 		(oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen ||
    242   1.1    bouyer 		 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    243   1.1    bouyer 		  oip->i_e2fs_nblock == 0))) {
    244   1.1    bouyer #ifdef DIAGNOSTIC
    245   1.1    bouyer 		if (length != 0)
    246   1.1    bouyer 			panic("ext2fs_truncate: partial truncate of symlink");
    247   1.1    bouyer #endif
    248  1.31      fvdl 		memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0,
    249   1.1    bouyer 			(u_int)oip->i_e2fs_size);
    250   1.1    bouyer 		oip->i_e2fs_size = 0;
    251   1.1    bouyer 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    252  1.15  perseant 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
    253   1.1    bouyer 	}
    254   1.1    bouyer 	if (oip->i_e2fs_size == length) {
    255   1.1    bouyer 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    256  1.12   mycroft 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    257   1.1    bouyer 	}
    258   1.1    bouyer 	fs = oip->i_e2fs;
    259   1.1    bouyer 	osize = oip->i_e2fs_size;
    260   1.1    bouyer 	/*
    261   1.1    bouyer 	 * Lengthen the size of the file. We must ensure that the
    262   1.1    bouyer 	 * last byte of the file is allocated. Since the smallest
    263   1.1    bouyer 	 * value of osize is 0, length will be at least 1.
    264   1.1    bouyer 	 */
    265   1.1    bouyer 	if (osize < length) {
    266   1.1    bouyer #if 0 /* XXX */
    267   1.1    bouyer 		if (length > fs->fs_maxfilesize)
    268   1.1    bouyer 			return (EFBIG);
    269   1.1    bouyer #endif
    270  1.28  jdolecek 		ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    271  1.21       chs 		    ap->a_flags & IO_SYNC ? B_SYNC : 0);
    272   1.1    bouyer 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    273  1.21       chs 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
    274   1.1    bouyer 	}
    275   1.1    bouyer 	/*
    276   1.1    bouyer 	 * Shorten the size of the file. If the file is not being
    277   1.1    bouyer 	 * truncated to a block boundry, the contents of the
    278   1.1    bouyer 	 * partial block following the end of the file must be
    279  1.24       wiz 	 * zero'ed in case it ever become accessible again because
    280   1.1    bouyer 	 * of subsequent file growth.
    281   1.1    bouyer 	 */
    282   1.1    bouyer 	offset = blkoff(fs, length);
    283  1.21       chs 	if (offset != 0) {
    284   1.1    bouyer 		size = fs->e2fs_bsize;
    285  1.21       chs 
    286  1.21       chs 		/* XXXUBC we should handle more than just VREG */
    287  1.21       chs 		uvm_vnp_zerorange(ovp, length, size - offset);
    288   1.1    bouyer 	}
    289  1.21       chs 	oip->i_e2fs_size = length;
    290   1.6      fvdl 	uvm_vnp_setsize(ovp, length);
    291  1.13       mrg 
    292   1.1    bouyer 	/*
    293   1.1    bouyer 	 * Calculate index into inode's block list of
    294   1.1    bouyer 	 * last direct and indirect blocks (if any)
    295   1.1    bouyer 	 * which we want to keep.  Lastblock is -1 when
    296   1.1    bouyer 	 * the file is truncated to 0.
    297   1.1    bouyer 	 */
    298   1.1    bouyer 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
    299   1.1    bouyer 	lastiblock[SINGLE] = lastblock - NDADDR;
    300   1.1    bouyer 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    301   1.1    bouyer 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    302   1.1    bouyer 	nblocks = btodb(fs->e2fs_bsize);
    303   1.1    bouyer 	/*
    304   1.1    bouyer 	 * Update file and block pointers on disk before we start freeing
    305   1.1    bouyer 	 * blocks.  If we crash before free'ing blocks below, the blocks
    306   1.1    bouyer 	 * will be returned to the free list.  lastiblock values are also
    307   1.1    bouyer 	 * normalized to -1 for calls to ext2fs_indirtrunc below.
    308   1.1    bouyer 	 */
    309   1.8     perry 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
    310   1.1    bouyer 	for (level = TRIPLE; level >= SINGLE; level--)
    311   1.1    bouyer 		if (lastiblock[level] < 0) {
    312   1.1    bouyer 			oip->i_e2fs_blocks[NDADDR + level] = 0;
    313   1.1    bouyer 			lastiblock[level] = -1;
    314   1.1    bouyer 		}
    315   1.1    bouyer 	for (i = NDADDR - 1; i > lastblock; i--)
    316   1.1    bouyer 		oip->i_e2fs_blocks[i] = 0;
    317   1.1    bouyer 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    318  1.16   mycroft 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    319  1.16   mycroft 	if (error && !allerror)
    320   1.1    bouyer 		allerror = error;
    321  1.16   mycroft 
    322   1.1    bouyer 	/*
    323   1.1    bouyer 	 * Having written the new inode to disk, save its new configuration
    324   1.1    bouyer 	 * and put back the old block pointers long enough to process them.
    325   1.1    bouyer 	 * Note that we save the new block configuration so we can check it
    326   1.1    bouyer 	 * when we are done.
    327   1.1    bouyer 	 */
    328  1.21       chs 
    329   1.8     perry 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
    330   1.8     perry 	memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
    331   1.1    bouyer 	oip->i_e2fs_size = osize;
    332  1.16   mycroft 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    333  1.16   mycroft 	if (error && !allerror)
    334  1.16   mycroft 		allerror = error;
    335   1.1    bouyer 
    336   1.1    bouyer 	/*
    337   1.1    bouyer 	 * Indirect blocks first.
    338   1.1    bouyer 	 */
    339   1.1    bouyer 	indir_lbn[SINGLE] = -NDADDR;
    340   1.1    bouyer 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
    341   1.1    bouyer 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    342   1.1    bouyer 	for (level = TRIPLE; level >= SINGLE; level--) {
    343  1.29      fvdl 		/* XXX ondisk32 */
    344   1.3    bouyer 		bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
    345   1.1    bouyer 		if (bn != 0) {
    346   1.1    bouyer 			error = ext2fs_indirtrunc(oip, indir_lbn[level],
    347  1.16   mycroft 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    348   1.1    bouyer 			if (error)
    349   1.1    bouyer 				allerror = error;
    350   1.1    bouyer 			blocksreleased += count;
    351   1.1    bouyer 			if (lastiblock[level] < 0) {
    352   1.1    bouyer 				oip->i_e2fs_blocks[NDADDR + level] = 0;
    353   1.1    bouyer 				ext2fs_blkfree(oip, bn);
    354   1.1    bouyer 				blocksreleased += nblocks;
    355   1.1    bouyer 			}
    356   1.1    bouyer 		}
    357   1.1    bouyer 		if (lastiblock[level] >= 0)
    358   1.1    bouyer 			goto done;
    359   1.1    bouyer 	}
    360   1.1    bouyer 
    361   1.1    bouyer 	/*
    362   1.1    bouyer 	 * All whole direct blocks or frags.
    363   1.1    bouyer 	 */
    364   1.1    bouyer 	for (i = NDADDR - 1; i > lastblock; i--) {
    365  1.29      fvdl 		/* XXX ondisk32 */
    366   1.3    bouyer 		bn = fs2h32(oip->i_e2fs_blocks[i]);
    367   1.1    bouyer 		if (bn == 0)
    368   1.1    bouyer 			continue;
    369   1.1    bouyer 		oip->i_e2fs_blocks[i] = 0;
    370   1.1    bouyer 		ext2fs_blkfree(oip, bn);
    371   1.1    bouyer 		blocksreleased += btodb(fs->e2fs_bsize);
    372   1.1    bouyer 	}
    373   1.1    bouyer 
    374   1.1    bouyer done:
    375   1.1    bouyer #ifdef DIAGNOSTIC
    376   1.1    bouyer 	for (level = SINGLE; level <= TRIPLE; level++)
    377  1.21       chs 		if (newblks[NDADDR + level] !=
    378  1.21       chs 		    oip->i_e2fs_blocks[NDADDR + level])
    379  1.21       chs 			panic("ext2fs_truncate1");
    380   1.1    bouyer 	for (i = 0; i < NDADDR; i++)
    381  1.16   mycroft 		if (newblks[i] != oip->i_e2fs_blocks[i])
    382  1.21       chs 			panic("ext2fs_truncate2");
    383   1.1    bouyer 	if (length == 0 &&
    384  1.21       chs 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
    385  1.21       chs 	     !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    386  1.21       chs 		panic("ext2fs_truncate3");
    387   1.1    bouyer #endif /* DIAGNOSTIC */
    388   1.1    bouyer 	/*
    389   1.1    bouyer 	 * Put back the real size.
    390   1.1    bouyer 	 */
    391   1.1    bouyer 	oip->i_e2fs_size = length;
    392   1.1    bouyer 	oip->i_e2fs_nblock -= blocksreleased;
    393   1.1    bouyer 	oip->i_flag |= IN_CHANGE;
    394   1.1    bouyer 	return (allerror);
    395   1.1    bouyer }
    396   1.1    bouyer 
    397   1.1    bouyer /*
    398   1.1    bouyer  * Release blocks associated with the inode ip and stored in the indirect
    399   1.1    bouyer  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    400   1.1    bouyer  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    401   1.1    bouyer  * and recursive calls to indirtrunc must be used to cleanse other indirect
    402   1.1    bouyer  * blocks.
    403   1.1    bouyer  *
    404   1.1    bouyer  * NB: triple indirect blocks are untested.
    405   1.1    bouyer  */
    406   1.1    bouyer static int
    407   1.1    bouyer ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    408  1.14  augustss 	struct inode *ip;
    409  1.29      fvdl 	daddr_t lbn, lastbn;
    410  1.29      fvdl 	daddr_t dbn;
    411   1.1    bouyer 	int level;
    412   1.1    bouyer 	long *countp;
    413   1.1    bouyer {
    414  1.14  augustss 	int i;
    415   1.1    bouyer 	struct buf *bp;
    416  1.14  augustss 	struct m_ext2fs *fs = ip->i_e2fs;
    417  1.29      fvdl 	int32_t *bap;	/* XXX ondisk32 */
    418   1.1    bouyer 	struct vnode *vp;
    419  1.29      fvdl 	daddr_t nb, nlbn, last;
    420  1.29      fvdl 	int32_t *copy = NULL;	/* XXX ondisk32 */
    421   1.1    bouyer 	long blkcount, factor;
    422   1.1    bouyer 	int nblocks, blocksreleased = 0;
    423   1.1    bouyer 	int error = 0, allerror = 0;
    424   1.1    bouyer 
    425   1.1    bouyer 	/*
    426   1.1    bouyer 	 * Calculate index in current block of last
    427   1.1    bouyer 	 * block to be kept.  -1 indicates the entire
    428   1.1    bouyer 	 * block so we need not calculate the index.
    429   1.1    bouyer 	 */
    430   1.1    bouyer 	factor = 1;
    431   1.1    bouyer 	for (i = SINGLE; i < level; i++)
    432   1.1    bouyer 		factor *= NINDIR(fs);
    433   1.1    bouyer 	last = lastbn;
    434   1.1    bouyer 	if (lastbn > 0)
    435   1.1    bouyer 		last /= factor;
    436   1.1    bouyer 	nblocks = btodb(fs->e2fs_bsize);
    437   1.1    bouyer 	/*
    438   1.1    bouyer 	 * Get buffer of block pointers, zero those entries corresponding
    439   1.1    bouyer 	 * to blocks to be free'd, and update on disk copy first.  Since
    440   1.1    bouyer 	 * double(triple) indirect before single(double) indirect, calls
    441   1.1    bouyer 	 * to bmap on these blocks will fail.  However, we already have
    442   1.1    bouyer 	 * the on disk address, so we have to set the b_blkno field
    443   1.1    bouyer 	 * explicitly instead of letting bread do everything for us.
    444   1.1    bouyer 	 */
    445   1.1    bouyer 	vp = ITOV(ip);
    446   1.1    bouyer 	bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
    447   1.1    bouyer 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    448   1.1    bouyer 		/* Braces must be here in case trace evaluates to nothing. */
    449   1.1    bouyer 		trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
    450   1.1    bouyer 	} else {
    451   1.1    bouyer 		trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
    452   1.1    bouyer 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    453   1.1    bouyer 		bp->b_flags |= B_READ;
    454   1.1    bouyer 		if (bp->b_bcount > bp->b_bufsize)
    455   1.1    bouyer 			panic("ext2fs_indirtrunc: bad buffer size");
    456   1.1    bouyer 		bp->b_blkno = dbn;
    457   1.1    bouyer 		VOP_STRATEGY(bp);
    458   1.1    bouyer 		error = biowait(bp);
    459   1.1    bouyer 	}
    460   1.1    bouyer 	if (error) {
    461   1.1    bouyer 		brelse(bp);
    462   1.1    bouyer 		*countp = 0;
    463   1.1    bouyer 		return (error);
    464   1.1    bouyer 	}
    465   1.1    bouyer 
    466  1.29      fvdl 	bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    467  1.17   mycroft 	if (lastbn >= 0) {
    468  1.29      fvdl 		/* XXX ondisk32 */
    469  1.29      fvdl 		MALLOC(copy, int32_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
    470   1.8     perry 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
    471   1.8     perry 		memset((caddr_t)&bap[last + 1], 0,
    472   1.1    bouyer 			(u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
    473   1.1    bouyer 		error = bwrite(bp);
    474   1.1    bouyer 		if (error)
    475   1.1    bouyer 			allerror = error;
    476   1.1    bouyer 		bap = copy;
    477   1.1    bouyer 	}
    478   1.1    bouyer 
    479   1.1    bouyer 	/*
    480   1.1    bouyer 	 * Recursively free totally unused blocks.
    481   1.1    bouyer 	 */
    482   1.1    bouyer 	for (i = NINDIR(fs) - 1,
    483   1.1    bouyer 		nlbn = lbn + 1 - i * factor; i > last;
    484   1.1    bouyer 		i--, nlbn += factor) {
    485  1.29      fvdl 		/* XXX ondisk32 */
    486   1.3    bouyer 		nb = fs2h32(bap[i]);
    487   1.1    bouyer 		if (nb == 0)
    488   1.1    bouyer 			continue;
    489   1.1    bouyer 		if (level > SINGLE) {
    490   1.1    bouyer 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    491  1.29      fvdl 						   (daddr_t)-1, level - 1,
    492   1.1    bouyer 						   &blkcount);
    493   1.1    bouyer 			if (error)
    494   1.1    bouyer 				allerror = error;
    495   1.1    bouyer 			blocksreleased += blkcount;
    496   1.1    bouyer 		}
    497   1.1    bouyer 		ext2fs_blkfree(ip, nb);
    498   1.1    bouyer 		blocksreleased += nblocks;
    499   1.1    bouyer 	}
    500   1.1    bouyer 
    501   1.1    bouyer 	/*
    502   1.1    bouyer 	 * Recursively free last partial block.
    503   1.1    bouyer 	 */
    504   1.1    bouyer 	if (level > SINGLE && lastbn >= 0) {
    505   1.1    bouyer 		last = lastbn % factor;
    506  1.29      fvdl 		/* XXX ondisk32 */
    507   1.3    bouyer 		nb = fs2h32(bap[i]);
    508   1.1    bouyer 		if (nb != 0) {
    509   1.1    bouyer 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    510   1.1    bouyer 						   last, level - 1, &blkcount);
    511   1.1    bouyer 			if (error)
    512   1.1    bouyer 				allerror = error;
    513   1.1    bouyer 			blocksreleased += blkcount;
    514   1.1    bouyer 		}
    515   1.1    bouyer 	}
    516   1.1    bouyer 
    517   1.1    bouyer 	if (copy != NULL) {
    518   1.1    bouyer 		FREE(copy, M_TEMP);
    519   1.1    bouyer 	} else {
    520   1.1    bouyer 		bp->b_flags |= B_INVAL;
    521   1.1    bouyer 		brelse(bp);
    522   1.1    bouyer 	}
    523   1.1    bouyer 
    524   1.1    bouyer 	*countp = blocksreleased;
    525   1.1    bouyer 	return (allerror);
    526   1.1    bouyer }
    527