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