xref: /openbsd-src/sys/ufs/ext2fs/ext2fs_inode.c (revision 129d5b5396d7607c3886066cdb47039db5bf0d41)
1*129d5b53Sbeck /*	$OpenBSD: ext2fs_inode.c,v 1.68 2024/07/13 14:37:56 beck Exp $	*/
21414b0faSart /*	$NetBSD: ext2fs_inode.c,v 1.24 2001/06/19 12:59:18 wiz Exp $	*/
35ac2d602Sdownsj 
45ac2d602Sdownsj /*
51f3ff51cSdownsj  * Copyright (c) 1997 Manuel Bouyer.
65ac2d602Sdownsj  * Copyright (c) 1982, 1986, 1989, 1993
75ac2d602Sdownsj  *	The Regents of the University of California.  All rights reserved.
85ac2d602Sdownsj  *
95ac2d602Sdownsj  * Redistribution and use in source and binary forms, with or without
105ac2d602Sdownsj  * modification, are permitted provided that the following conditions
115ac2d602Sdownsj  * are met:
125ac2d602Sdownsj  * 1. Redistributions of source code must retain the above copyright
135ac2d602Sdownsj  *    notice, this list of conditions and the following disclaimer.
145ac2d602Sdownsj  * 2. Redistributions in binary form must reproduce the above copyright
155ac2d602Sdownsj  *    notice, this list of conditions and the following disclaimer in the
165ac2d602Sdownsj  *    documentation and/or other materials provided with the distribution.
1729295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
185ac2d602Sdownsj  *    may be used to endorse or promote products derived from this software
195ac2d602Sdownsj  *    without specific prior written permission.
205ac2d602Sdownsj  *
215ac2d602Sdownsj  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225ac2d602Sdownsj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235ac2d602Sdownsj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245ac2d602Sdownsj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255ac2d602Sdownsj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265ac2d602Sdownsj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275ac2d602Sdownsj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285ac2d602Sdownsj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295ac2d602Sdownsj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305ac2d602Sdownsj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315ac2d602Sdownsj  * SUCH DAMAGE.
325ac2d602Sdownsj  *
335ac2d602Sdownsj  *	@(#)ffs_inode.c	8.8 (Berkeley) 10/19/94
341f3ff51cSdownsj  * Modified for ext2fs by Manuel Bouyer.
355ac2d602Sdownsj  */
365ac2d602Sdownsj 
375ac2d602Sdownsj #include <sys/param.h>
385ac2d602Sdownsj #include <sys/systm.h>
395ac2d602Sdownsj #include <sys/mount.h>
405ac2d602Sdownsj #include <sys/proc.h>
415ac2d602Sdownsj #include <sys/buf.h>
425ac2d602Sdownsj #include <sys/vnode.h>
435ac2d602Sdownsj #include <sys/kernel.h>
445ac2d602Sdownsj #include <sys/malloc.h>
455ac2d602Sdownsj #include <sys/resourcevar.h>
465ac2d602Sdownsj 
475ac2d602Sdownsj #include <ufs/ufs/quota.h>
485ac2d602Sdownsj #include <ufs/ufs/inode.h>
495ac2d602Sdownsj #include <ufs/ufs/ufsmount.h>
505ac2d602Sdownsj #include <ufs/ufs/ufs_extern.h>
515ac2d602Sdownsj 
525ac2d602Sdownsj #include <ufs/ext2fs/ext2fs.h>
535ac2d602Sdownsj #include <ufs/ext2fs/ext2fs_extern.h>
545ac2d602Sdownsj 
551dfb1939Spedro static int ext2fs_indirtrunc(struct inode *, int32_t, int32_t,
561dfb1939Spedro 				int32_t, int, long *);
575ac2d602Sdownsj 
585ac2d602Sdownsj /*
594dcbbbb0Sniallo  * Get the size of an inode.
604dcbbbb0Sniallo  */
614dcbbbb0Sniallo u_int64_t
ext2fs_size(struct inode * ip)624dcbbbb0Sniallo ext2fs_size(struct inode *ip)
634dcbbbb0Sniallo {
644dcbbbb0Sniallo 	u_int64_t size = ip->i_e2fs_size;
654dcbbbb0Sniallo 
664dcbbbb0Sniallo 	if ((ip->i_e2fs_mode & IFMT) == IFREG)
6742cd3248Spelikan 		size |= (u_int64_t)ip->i_e2fs_size_hi << 32;
6839fbb38fSpedro 
6939fbb38fSpedro 	return (size);
704dcbbbb0Sniallo }
714dcbbbb0Sniallo 
724dcbbbb0Sniallo int
ext2fs_setsize(struct inode * ip,u_int64_t size)734dcbbbb0Sniallo ext2fs_setsize(struct inode *ip, u_int64_t size)
744dcbbbb0Sniallo {
754dcbbbb0Sniallo 	struct m_ext2fs *fs = ip->i_e2fs;
764dcbbbb0Sniallo 
77c7fd3f62Spelikan 	if (size <= fs->e2fs_maxfilesize) {
7842cd3248Spelikan 		/* If HUGE_FILEs are off, e2fs_maxfilesize will protect us. */
7942cd3248Spelikan 		if ((ip->i_e2fs_mode & IFMT) == IFREG || ip->i_e2fs_mode == 0)
8042cd3248Spelikan 			ip->i_e2fs_size_hi = size >> 32;
814dcbbbb0Sniallo 
824dcbbbb0Sniallo 		ip->i_e2fs_size = size;
8339fbb38fSpedro 		return (0);
844dcbbbb0Sniallo 	}
854dcbbbb0Sniallo 
8642cd3248Spelikan 	/* Linux automagically upgrades to REV1 here! */
8742cd3248Spelikan 	if (fs->e2fs.e2fs_rev <= E2FS_REV0)
8842cd3248Spelikan 		return (EFBIG);
8942cd3248Spelikan 
90a87a19d1Skevlo 	if (!(fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGE_FILE)) {
91a87a19d1Skevlo 		fs->e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGE_FILE;
9242cd3248Spelikan 		fs->e2fs_fmod = 1;
9342cd3248Spelikan 	}
9442cd3248Spelikan 	return (EFBIG);
9542cd3248Spelikan }
9642cd3248Spelikan 
974dcbbbb0Sniallo 
984dcbbbb0Sniallo /*
995ac2d602Sdownsj  * Last reference to an inode.  If necessary, write or delete it.
1005ac2d602Sdownsj  */
1015ac2d602Sdownsj int
ext2fs_inactive(void * v)1025f64cd9cSjasper ext2fs_inactive(void *v)
1035ac2d602Sdownsj {
10499bc9d31Sderaadt 	struct vop_inactive_args *ap = v;
1057f1901c5Sart 	struct vnode *vp = ap->a_vp;
1067f1901c5Sart 	struct inode *ip = VTOI(vp);
1075ac2d602Sdownsj 	struct timespec ts;
10822f18572Sart 	int error = 0;
1094df17c52Spedro #ifdef DIAGNOSTIC
1101414b0faSart 	extern int prtactive;
1115ac2d602Sdownsj 
1125ac2d602Sdownsj 	if (prtactive && vp->v_usecount != 0)
113bbc90cb2Sart 		vprint("ext2fs_inactive: pushing active", vp);
1144df17c52Spedro #endif
1154df17c52Spedro 
1165ac2d602Sdownsj 	/* Get rid of inodes related to stale file handles. */
117*129d5b53Sbeck 	if (ip->i_e2din == NULL || ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime)
11807feb63cScsapuntz 		goto out;
1195ac2d602Sdownsj 
1207f1901c5Sart 	error = 0;
1215ac2d602Sdownsj 	if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1224dcbbbb0Sniallo 		if (ext2fs_size(ip) != 0) {
1237f1901c5Sart 			error = ext2fs_truncate(ip, (off_t)0, 0, NOCRED);
1247f1901c5Sart 		}
125c2475275Stholo 		getnanotime(&ts);
1265ac2d602Sdownsj 		ip->i_e2fs_dtime = ts.tv_sec;
1275ac2d602Sdownsj 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
128b080ad39Scsapuntz 		ext2fs_inode_free(ip, ip->i_number, ip->i_e2fs_mode);
1295ac2d602Sdownsj 	}
1305ac2d602Sdownsj 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
131d4fc1c49Sguenther 		ext2fs_update(ip, 0);
1325ac2d602Sdownsj 	}
13307feb63cScsapuntz out:
13436bb23f1Svisa 	VOP_UNLOCK(vp);
1355ac2d602Sdownsj 	/*
1365ac2d602Sdownsj 	 * If we are done with the inode, reclaim it
1375ac2d602Sdownsj 	 * so that it can be reused immediately.
1385ac2d602Sdownsj 	 */
139*129d5b53Sbeck 	if (ip->i_e2din == NULL || ip->i_e2fs_dtime != 0)
140db7aa982Smpi 		vrecycle(vp, ap->a_p);
1415ac2d602Sdownsj 	return (error);
1425ac2d602Sdownsj }
1435ac2d602Sdownsj 
1445ac2d602Sdownsj 
1455ac2d602Sdownsj /*
1465ac2d602Sdownsj  * Update the access, modified, and inode change times as specified by the
1475ac2d602Sdownsj  * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
1485ac2d602Sdownsj  * used to specify that the inode needs to be updated but that the times have
1495ac2d602Sdownsj  * already been set. The access and modified times are taken from the second
1505ac2d602Sdownsj  * and third parameters; the inode change time is always taken from the current
1515ac2d602Sdownsj  * time. If waitfor is set, then wait for the disk write of the inode to
1525ac2d602Sdownsj  * complete.
1535ac2d602Sdownsj  */
1545ac2d602Sdownsj int
ext2fs_update(struct inode * ip,int waitfor)155d4fc1c49Sguenther ext2fs_update(struct inode *ip, int waitfor)
1565ac2d602Sdownsj {
157b080ad39Scsapuntz 	struct m_ext2fs *fs;
1585ac2d602Sdownsj 	struct buf *bp;
1595ac2d602Sdownsj 	int error;
1607f1901c5Sart 	caddr_t cp;
1615ac2d602Sdownsj 
162b080ad39Scsapuntz 	if (ITOV(ip)->v_mount->mnt_flag & MNT_RDONLY)
1635ac2d602Sdownsj 		return (0);
164d4fc1c49Sguenther 	EXT2FS_ITIMES(ip);
1655ac2d602Sdownsj 	if ((ip->i_flag & IN_MODIFIED) == 0)
1665ac2d602Sdownsj 		return (0);
1675ac2d602Sdownsj 	ip->i_flag &= ~IN_MODIFIED;
1685ac2d602Sdownsj 	fs = ip->i_e2fs;
1695ac2d602Sdownsj 	error = bread(ip->i_devvp,
1705ac2d602Sdownsj 			  fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
17193f62a9eStedu 			  (int)fs->e2fs_bsize, &bp);
1725ac2d602Sdownsj 	if (error) {
1735ac2d602Sdownsj 		brelse(bp);
1745ac2d602Sdownsj 		return (error);
1755ac2d602Sdownsj 	}
1767f1901c5Sart 	ip->i_flag &= ~(IN_MODIFIED);
1777f1901c5Sart 	cp = (caddr_t)bp->b_data +
17825e19cd8Stedu 	    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
179d23196ecSpedro 
180d23196ecSpedro 	/*
181d23196ecSpedro 	 * See note about 16-bit UID/GID limitation in ext2fs_vget(). Now
182d23196ecSpedro 	 * that we are about to write the inode, construct the split UID and
183d23196ecSpedro 	 * GID fields out of the two 32-bit fields we kept in memory.
184d23196ecSpedro 	 */
185d23196ecSpedro 	ip->i_e2fs_uid_low = (u_int16_t)ip->i_e2fs_uid;
186d23196ecSpedro 	ip->i_e2fs_gid_low = (u_int16_t)ip->i_e2fs_gid;
187d23196ecSpedro 	ip->i_e2fs_uid_high = ip->i_e2fs_uid >> 16;
188d23196ecSpedro 	ip->i_e2fs_gid_high = ip->i_e2fs_gid >> 16;
189d23196ecSpedro 
190d31d4a6eSpelikan 	e2fs_isave(fs, ip->i_e2din, (struct ext2fs_dinode *)cp);
191b080ad39Scsapuntz 	if (waitfor)
1925ac2d602Sdownsj 		return (bwrite(bp));
1935ac2d602Sdownsj 	else {
1945ac2d602Sdownsj 		bdwrite(bp);
1955ac2d602Sdownsj 		return (0);
1965ac2d602Sdownsj 	}
1975ac2d602Sdownsj }
1985ac2d602Sdownsj 
1995ac2d602Sdownsj #define	SINGLE	0	/* index of single indirect block */
2005ac2d602Sdownsj #define	DOUBLE	1	/* index of double indirect block */
2015ac2d602Sdownsj #define	TRIPLE	2	/* index of triple indirect block */
2025ac2d602Sdownsj /*
2035ac2d602Sdownsj  * Truncate the inode oip to at most length size, freeing the
2045ac2d602Sdownsj  * disk blocks.
2055ac2d602Sdownsj  */
2065ac2d602Sdownsj int
ext2fs_truncate(struct inode * oip,off_t length,int flags,struct ucred * cred)207b080ad39Scsapuntz ext2fs_truncate(struct inode *oip, off_t length, int flags, struct ucred *cred)
2085ac2d602Sdownsj {
209b080ad39Scsapuntz 	struct vnode *ovp = ITOV(oip);
2101dfb1939Spedro 	int32_t lastblock;
2111dfb1939Spedro 	int32_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
2121dfb1939Spedro 	int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
2137f1901c5Sart 	struct m_ext2fs *fs;
2141414b0faSart 	struct buf *bp;
2155ac2d602Sdownsj 	int offset, size, level;
2165ac2d602Sdownsj 	long count, nblocks, vflags, blocksreleased = 0;
2177f1901c5Sart 	int i;
2181414b0faSart 	int aflags, error, allerror;
2195ac2d602Sdownsj 	off_t osize;
2205ac2d602Sdownsj 
2215ac2d602Sdownsj 	if (length < 0)
2225ac2d602Sdownsj 		return (EINVAL);
2235ac2d602Sdownsj 
224d8539d7cScsapuntz 	if (ovp->v_type != VREG &&
225d8539d7cScsapuntz 	    ovp->v_type != VDIR &&
226d8539d7cScsapuntz 	    ovp->v_type != VLNK)
227f93f0c68Scsapuntz 		return (0);
228d8539d7cScsapuntz 
2293484f39eSnatano 	if (ovp->v_type == VLNK && ext2fs_size(oip) < EXT2_MAXSYMLINKLEN) {
2305ac2d602Sdownsj #ifdef DIAGNOSTIC
2315ac2d602Sdownsj 		if (length != 0)
2325ac2d602Sdownsj 			panic("ext2fs_truncate: partial truncate of symlink");
2335ac2d602Sdownsj #endif
2340f5c6c8bStedu 		memset(&oip->i_e2din->e2di_shortlink, 0, ext2fs_size(oip));
2354dcbbbb0Sniallo 		(void)ext2fs_setsize(oip, 0);
2365ac2d602Sdownsj 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
237d4fc1c49Sguenther 		return (ext2fs_update(oip, 1));
2385ac2d602Sdownsj 	}
2394dcbbbb0Sniallo 
2404dcbbbb0Sniallo 	if (ext2fs_size(oip) == length) {
2415ac2d602Sdownsj 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
242d4fc1c49Sguenther 		return (ext2fs_update(oip, 0));
2435ac2d602Sdownsj 	}
2445ac2d602Sdownsj 	fs = oip->i_e2fs;
2454dcbbbb0Sniallo 	osize = ext2fs_size(oip);
2465ac2d602Sdownsj 	/*
2475ac2d602Sdownsj 	 * Lengthen the size of the file. We must ensure that the
2485ac2d602Sdownsj 	 * last byte of the file is allocated. Since the smallest
2495ac2d602Sdownsj 	 * value of osize is 0, length will be at least 1.
2505ac2d602Sdownsj 	 */
2515ac2d602Sdownsj 	if (osize < length) {
2525ac2d602Sdownsj #if 0 /* XXX */
2535ac2d602Sdownsj 		if (length > fs->fs_maxfilesize)
2545ac2d602Sdownsj 			return (EFBIG);
2555ac2d602Sdownsj #endif
2561414b0faSart 		offset = blkoff(fs, length - 1);
2571414b0faSart 		lbn = lblkno(fs, length - 1);
2581414b0faSart 		aflags = B_CLRBUF;
2591414b0faSart 		if (flags & IO_SYNC)
2601414b0faSart 			aflags |= B_SYNC;
2611414b0faSart 		error = ext2fs_buf_alloc(oip, lbn, offset + 1, cred, &bp,
2621414b0faSart 		    aflags);
2631414b0faSart 		if (error)
2641414b0faSart 			return (error);
2654dcbbbb0Sniallo 		(void)ext2fs_setsize(oip, length);
2661414b0faSart 		uvm_vnp_setsize(ovp, length);
2671414b0faSart 		uvm_vnp_uncache(ovp);
2681414b0faSart 		if (aflags & B_SYNC)
2691414b0faSart 			bwrite(bp);
2701414b0faSart 		else
2711414b0faSart 			bawrite(bp);
2725ac2d602Sdownsj 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
273d4fc1c49Sguenther 		return (ext2fs_update(oip, 1));
2745ac2d602Sdownsj 	}
2755ac2d602Sdownsj 	/*
2765ac2d602Sdownsj 	 * Shorten the size of the file. If the file is not being
277b66b9ef8Sjsg 	 * truncated to a block boundary, the contents of the
2785ac2d602Sdownsj 	 * partial block following the end of the file must be
2797f1901c5Sart 	 * zero'ed in case it ever become accessible again because
2805ac2d602Sdownsj 	 * of subsequent file growth.
2815ac2d602Sdownsj 	 */
2825ac2d602Sdownsj 	offset = blkoff(fs, length);
2831414b0faSart 	if (offset == 0) {
2844dcbbbb0Sniallo 		(void)ext2fs_setsize(oip, length);
2851414b0faSart 	} else {
2861414b0faSart 		lbn = lblkno(fs, length);
2871414b0faSart 		aflags = B_CLRBUF;
2881414b0faSart 		if (flags & IO_SYNC)
2891414b0faSart 			aflags |= B_SYNC;
2901414b0faSart 		error = ext2fs_buf_alloc(oip, lbn, offset, cred, &bp,
2911414b0faSart 		    aflags);
2921414b0faSart 		if (error)
2931414b0faSart 			return (error);
2944dcbbbb0Sniallo 		(void)ext2fs_setsize(oip, length);
2951414b0faSart 		size = fs->e2fs_bsize;
2965af79db2Sart 		uvm_vnp_setsize(ovp, length);
2971414b0faSart 		uvm_vnp_uncache(ovp);
2980f5c6c8bStedu 		memset(bp->b_data + offset, 0, size - offset);
299f43037e0Spedro 		bp->b_bcount = size;
3001414b0faSart 		if (aflags & B_SYNC)
3011414b0faSart 			bwrite(bp);
3021414b0faSart 		else
3031414b0faSart 			bawrite(bp);
3041414b0faSart 	}
3055ac2d602Sdownsj 	/*
3065ac2d602Sdownsj 	 * Calculate index into inode's block list of
3075ac2d602Sdownsj 	 * last direct and indirect blocks (if any)
3085ac2d602Sdownsj 	 * which we want to keep.  Lastblock is -1 when
3095ac2d602Sdownsj 	 * the file is truncated to 0.
3105ac2d602Sdownsj 	 */
3115ac2d602Sdownsj 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
3125ac2d602Sdownsj 	lastiblock[SINGLE] = lastblock - NDADDR;
3135ac2d602Sdownsj 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
3145ac2d602Sdownsj 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
3155ac2d602Sdownsj 	nblocks = btodb(fs->e2fs_bsize);
3165ac2d602Sdownsj 	/*
3175ac2d602Sdownsj 	 * Update file and block pointers on disk before we start freeing
3185ac2d602Sdownsj 	 * blocks.  If we crash before free'ing blocks below, the blocks
3195ac2d602Sdownsj 	 * will be returned to the free list.  lastiblock values are also
3205ac2d602Sdownsj 	 * normalized to -1 for calls to ext2fs_indirtrunc below.
3215ac2d602Sdownsj 	 */
3220f5c6c8bStedu 	memcpy(oldblks, &oip->i_e2fs_blocks[0], sizeof(oldblks));
3235ac2d602Sdownsj 	for (level = TRIPLE; level >= SINGLE; level--)
3245ac2d602Sdownsj 		if (lastiblock[level] < 0) {
3255ac2d602Sdownsj 			oip->i_e2fs_blocks[NDADDR + level] = 0;
3265ac2d602Sdownsj 			lastiblock[level] = -1;
3275ac2d602Sdownsj 		}
3285ac2d602Sdownsj 	for (i = NDADDR - 1; i > lastblock; i--)
3295ac2d602Sdownsj 		oip->i_e2fs_blocks[i] = 0;
3305ac2d602Sdownsj 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
331d4fc1c49Sguenther 	if ((error = ext2fs_update(oip, 1)) != 0)
3325ac2d602Sdownsj 		allerror = error;
3335ac2d602Sdownsj 	/*
3345ac2d602Sdownsj 	 * Having written the new inode to disk, save its new configuration
3355ac2d602Sdownsj 	 * and put back the old block pointers long enough to process them.
3365ac2d602Sdownsj 	 * Note that we save the new block configuration so we can check it
3375ac2d602Sdownsj 	 * when we are done.
3385ac2d602Sdownsj 	 */
3390f5c6c8bStedu 	memcpy(newblks, &oip->i_e2fs_blocks[0], sizeof(newblks));
3400f5c6c8bStedu 	memcpy(&oip->i_e2fs_blocks[0], oldblks, sizeof(oldblks));
3414dcbbbb0Sniallo 	(void)ext2fs_setsize(oip, osize);
3425ac2d602Sdownsj 	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
343a8d7c3beScheloha 	allerror = vinvalbuf(ovp, vflags, cred, curproc, 0, INFSLP);
3445ac2d602Sdownsj 
3455ac2d602Sdownsj 	/*
3465ac2d602Sdownsj 	 * Indirect blocks first.
3475ac2d602Sdownsj 	 */
3485ac2d602Sdownsj 	indir_lbn[SINGLE] = -NDADDR;
3495ac2d602Sdownsj 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
3505ac2d602Sdownsj 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
3515ac2d602Sdownsj 	for (level = TRIPLE; level >= SINGLE; level--) {
352f7dbefaaSpelikan 		bn = letoh32(oip->i_e2fs_blocks[NDADDR + level]);
3535ac2d602Sdownsj 		if (bn != 0) {
3545ac2d602Sdownsj 			error = ext2fs_indirtrunc(oip, indir_lbn[level],
3555ac2d602Sdownsj 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
3565ac2d602Sdownsj 			if (error)
3575ac2d602Sdownsj 				allerror = error;
3585ac2d602Sdownsj 			blocksreleased += count;
3595ac2d602Sdownsj 			if (lastiblock[level] < 0) {
3605ac2d602Sdownsj 				oip->i_e2fs_blocks[NDADDR + level] = 0;
3615ac2d602Sdownsj 				ext2fs_blkfree(oip, bn);
3625ac2d602Sdownsj 				blocksreleased += nblocks;
3635ac2d602Sdownsj 			}
3645ac2d602Sdownsj 		}
3655ac2d602Sdownsj 		if (lastiblock[level] >= 0)
3665ac2d602Sdownsj 			goto done;
3675ac2d602Sdownsj 	}
3685ac2d602Sdownsj 
3695ac2d602Sdownsj 	/*
3705ac2d602Sdownsj 	 * All whole direct blocks or frags.
3715ac2d602Sdownsj 	 */
3725ac2d602Sdownsj 	for (i = NDADDR - 1; i > lastblock; i--) {
373f7dbefaaSpelikan 		bn = letoh32(oip->i_e2fs_blocks[i]);
3745ac2d602Sdownsj 		if (bn == 0)
3755ac2d602Sdownsj 			continue;
3765ac2d602Sdownsj 		oip->i_e2fs_blocks[i] = 0;
3775ac2d602Sdownsj 		ext2fs_blkfree(oip, bn);
3785ac2d602Sdownsj 		blocksreleased += btodb(fs->e2fs_bsize);
3795ac2d602Sdownsj 	}
3805ac2d602Sdownsj 
3815ac2d602Sdownsj done:
3825ac2d602Sdownsj #ifdef DIAGNOSTIC
3835ac2d602Sdownsj 	for (level = SINGLE; level <= TRIPLE; level++)
3847f1901c5Sart 		if (newblks[NDADDR + level] !=
3857f1901c5Sart 		    oip->i_e2fs_blocks[NDADDR + level])
3867f1901c5Sart 			panic("ext2fs_truncate1");
3875ac2d602Sdownsj 	for (i = 0; i < NDADDR; i++)
3885ac2d602Sdownsj 		if (newblks[i] != oip->i_e2fs_blocks[i])
3897f1901c5Sart 			panic("ext2fs_truncate2");
3904b515238Svisa 	if (length == 0) {
3914b515238Svisa 		int s;
3924b515238Svisa 
3934b515238Svisa 		s = splbio();
3944b515238Svisa 		if (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
3954b515238Svisa 		    !LIST_EMPTY(&ovp->v_dirtyblkhd))
3967f1901c5Sart 			panic("ext2fs_truncate3");
3974b515238Svisa 		splx(s);
3984b515238Svisa 	}
3995ac2d602Sdownsj #endif /* DIAGNOSTIC */
4005ac2d602Sdownsj 	/*
4015ac2d602Sdownsj 	 * Put back the real size.
4025ac2d602Sdownsj 	 */
4034dcbbbb0Sniallo 	(void)ext2fs_setsize(oip, length);
404ae3afa17Skrw 	if (blocksreleased >= oip->i_e2fs_nblock)
4055ac2d602Sdownsj 		oip->i_e2fs_nblock = 0;
406ae3afa17Skrw 	else
407ae3afa17Skrw 		oip->i_e2fs_nblock -= blocksreleased;
4085ac2d602Sdownsj 	oip->i_flag |= IN_CHANGE;
4095ac2d602Sdownsj 	return (allerror);
4105ac2d602Sdownsj }
4115ac2d602Sdownsj 
4125ac2d602Sdownsj /*
4135ac2d602Sdownsj  * Release blocks associated with the inode ip and stored in the indirect
4145ac2d602Sdownsj  * block bn.  Blocks are free'd in LIFO order up to (but not including)
4155ac2d602Sdownsj  * lastbn.  If level is greater than SINGLE, the block is an indirect block
4165ac2d602Sdownsj  * and recursive calls to indirtrunc must be used to cleanse other indirect
4175ac2d602Sdownsj  * blocks.
4185ac2d602Sdownsj  *
4195ac2d602Sdownsj  * NB: triple indirect blocks are untested.
4205ac2d602Sdownsj  */
4215ac2d602Sdownsj static int
ext2fs_indirtrunc(struct inode * ip,int32_t lbn,int32_t dbn,int32_t lastbn,int level,long * countp)4225f64cd9cSjasper ext2fs_indirtrunc(struct inode *ip, int32_t lbn, int32_t dbn, int32_t lastbn, int level, long *countp)
4235ac2d602Sdownsj {
4247f1901c5Sart 	int i;
4255ac2d602Sdownsj 	struct buf *bp;
4267f1901c5Sart 	struct m_ext2fs *fs = ip->i_e2fs;
4271dfb1939Spedro 	int32_t *bap;
4285ac2d602Sdownsj 	struct vnode *vp;
4291dfb1939Spedro 	int32_t *copy = NULL, nb, nlbn, last;
4305ac2d602Sdownsj 	long blkcount, factor;
4315ac2d602Sdownsj 	int nblocks, blocksreleased = 0;
4325ac2d602Sdownsj 	int error = 0, allerror = 0;
4335ac2d602Sdownsj 
4345ac2d602Sdownsj 	/*
4355ac2d602Sdownsj 	 * Calculate index in current block of last
4365ac2d602Sdownsj 	 * block to be kept.  -1 indicates the entire
4375ac2d602Sdownsj 	 * block so we need not calculate the index.
4385ac2d602Sdownsj 	 */
4395ac2d602Sdownsj 	factor = 1;
4405ac2d602Sdownsj 	for (i = SINGLE; i < level; i++)
4415ac2d602Sdownsj 		factor *= NINDIR(fs);
4425ac2d602Sdownsj 	last = lastbn;
4435ac2d602Sdownsj 	if (lastbn > 0)
4445ac2d602Sdownsj 		last /= factor;
4455ac2d602Sdownsj 	nblocks = btodb(fs->e2fs_bsize);
4465ac2d602Sdownsj 	/*
4475ac2d602Sdownsj 	 * Get buffer of block pointers, zero those entries corresponding
4485ac2d602Sdownsj 	 * to blocks to be free'd, and update on disk copy first.  Since
4495ac2d602Sdownsj 	 * double(triple) indirect before single(double) indirect, calls
4505ac2d602Sdownsj 	 * to bmap on these blocks will fail.  However, we already have
4515ac2d602Sdownsj 	 * the on disk address, so we have to set the b_blkno field
4525ac2d602Sdownsj 	 * explicitly instead of letting bread do everything for us.
4535ac2d602Sdownsj 	 */
4545ac2d602Sdownsj 	vp = ITOV(ip);
455570df5c4Scheloha 	bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, INFSLP);
4560674f943Smickey 	if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
4578f15e6a4Sguenther 		curproc->p_ru.ru_inblock++;		/* pay for read */
458c10f2b41Sderaadt 		bcstats.pendingreads++;
459c10f2b41Sderaadt 		bcstats.numreads++;
4605ac2d602Sdownsj 		bp->b_flags |= B_READ;
4615ac2d602Sdownsj 		if (bp->b_bcount > bp->b_bufsize)
4625ac2d602Sdownsj 			panic("ext2fs_indirtrunc: bad buffer size");
4635ac2d602Sdownsj 		bp->b_blkno = dbn;
464f1993be3Svisa 		VOP_STRATEGY(bp->b_vp, bp);
4655ac2d602Sdownsj 		error = biowait(bp);
4665ac2d602Sdownsj 	}
4675ac2d602Sdownsj 	if (error) {
4685ac2d602Sdownsj 		brelse(bp);
4695ac2d602Sdownsj 		*countp = 0;
4705ac2d602Sdownsj 		return (error);
4715ac2d602Sdownsj 	}
4725ac2d602Sdownsj 
4731dfb1939Spedro 	bap = (int32_t *)bp->b_data;
4747f1901c5Sart 	if (lastbn >= 0) {
4756a2ae821Schl 		copy = malloc(fs->e2fs_bsize, M_TEMP, M_WAITOK);
4760f5c6c8bStedu 		memcpy(copy, bap, fs->e2fs_bsize);
4770f5c6c8bStedu 		memset(&bap[last + 1], 0,
4780f5c6c8bStedu 		    (NINDIR(fs) - (last + 1)) * sizeof(u_int32_t));
4795ac2d602Sdownsj 		error = bwrite(bp);
4805ac2d602Sdownsj 		if (error)
4815ac2d602Sdownsj 			allerror = error;
4825ac2d602Sdownsj 		bap = copy;
4835ac2d602Sdownsj 	}
4845ac2d602Sdownsj 
4855ac2d602Sdownsj 	/*
4865ac2d602Sdownsj 	 * Recursively free totally unused blocks.
4875ac2d602Sdownsj 	 */
4885ac2d602Sdownsj 	for (i = NINDIR(fs) - 1,
4895ac2d602Sdownsj 		nlbn = lbn + 1 - i * factor; i > last;
4905ac2d602Sdownsj 		i--, nlbn += factor) {
491f7dbefaaSpelikan 		nb = letoh32(bap[i]);
4925ac2d602Sdownsj 		if (nb == 0)
4935ac2d602Sdownsj 			continue;
4945ac2d602Sdownsj 		if (level > SINGLE) {
4955ac2d602Sdownsj 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
4961dfb1939Spedro 						   (int32_t)-1, level - 1,
4975ac2d602Sdownsj 						   &blkcount);
4985ac2d602Sdownsj 			if (error)
4995ac2d602Sdownsj 				allerror = error;
5005ac2d602Sdownsj 			blocksreleased += blkcount;
5015ac2d602Sdownsj 		}
5025ac2d602Sdownsj 		ext2fs_blkfree(ip, nb);
5035ac2d602Sdownsj 		blocksreleased += nblocks;
5045ac2d602Sdownsj 	}
5055ac2d602Sdownsj 
5065ac2d602Sdownsj 	/*
5075ac2d602Sdownsj 	 * Recursively free last partial block.
5085ac2d602Sdownsj 	 */
5095ac2d602Sdownsj 	if (level > SINGLE && lastbn >= 0) {
5105ac2d602Sdownsj 		last = lastbn % factor;
511f7dbefaaSpelikan 		nb = letoh32(bap[i]);
5125ac2d602Sdownsj 		if (nb != 0) {
5135ac2d602Sdownsj 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
5145ac2d602Sdownsj 						   last, level - 1, &blkcount);
5155ac2d602Sdownsj 			if (error)
5165ac2d602Sdownsj 				allerror = error;
5175ac2d602Sdownsj 			blocksreleased += blkcount;
5185ac2d602Sdownsj 		}
5195ac2d602Sdownsj 	}
5205ac2d602Sdownsj 
5215ac2d602Sdownsj 	if (copy != NULL) {
522c9a9cb3fSpelikan 		free(copy, M_TEMP, fs->e2fs_bsize);
5235ac2d602Sdownsj 	} else {
5245ac2d602Sdownsj 		bp->b_flags |= B_INVAL;
5255ac2d602Sdownsj 		brelse(bp);
5265ac2d602Sdownsj 	}
5275ac2d602Sdownsj 
5285ac2d602Sdownsj 	*countp = blocksreleased;
5295ac2d602Sdownsj 	return (allerror);
5305ac2d602Sdownsj }
531