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