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