Home | History | Annotate | Line # | Download | only in lfs
lfs_inode.c revision 1.64
      1 /*	$NetBSD: lfs_inode.c,v 1.64 2003/02/17 23:48:18 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1986, 1989, 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs_inode.c	8.9 (Berkeley) 5/8/95
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: lfs_inode.c,v 1.64 2003/02/17 23:48:18 perseant Exp $");
     75 
     76 #if defined(_KERNEL_OPT)
     77 #include "opt_quota.h"
     78 #endif
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/mount.h>
     83 #include <sys/proc.h>
     84 #include <sys/file.h>
     85 #include <sys/buf.h>
     86 #include <sys/vnode.h>
     87 #include <sys/kernel.h>
     88 #include <sys/malloc.h>
     89 #include <sys/trace.h>
     90 #include <sys/resourcevar.h>
     91 
     92 #include <ufs/ufs/quota.h>
     93 #include <ufs/ufs/inode.h>
     94 #include <ufs/ufs/ufsmount.h>
     95 #include <ufs/ufs/ufs_extern.h>
     96 
     97 #include <ufs/lfs/lfs.h>
     98 #include <ufs/lfs/lfs_extern.h>
     99 
    100 extern int locked_queue_count;
    101 extern long locked_queue_bytes;
    102 
    103 static int lfs_update_seguse(struct lfs *, long, size_t);
    104 static int lfs_indirtrunc (struct inode *, daddr_t, daddr_t,
    105 			   daddr_t, int, long *, long *, long *, size_t *,
    106 			   struct proc *);
    107 static int lfs_blkfree (struct lfs *, daddr_t, size_t, long *, size_t *);
    108 static int lfs_vtruncbuf(struct vnode *, daddr_t, int, int);
    109 
    110 /* Search a block for a specific dinode. */
    111 struct dinode *
    112 lfs_ifind(struct lfs *fs, ino_t ino, struct buf *bp)
    113 {
    114 	struct dinode *dip = (struct dinode *)bp->b_data;
    115 	struct dinode *ldip, *fin;
    116 
    117 #ifdef LFS_IFILE_FRAG_ADDRESSING
    118 	if (fs->lfs_version == 1)
    119 		fin = dip + INOPB(fs);
    120 	else
    121 		fin = dip + INOPF(fs);
    122 #else
    123 	fin = dip + INOPB(fs);
    124 #endif
    125 
    126 	/*
    127 	 * Read the inode block backwards, since later versions of the
    128 	 * inode will supercede earlier ones.  Though it is unlikely, it is
    129 	 * possible that the same inode will appear in the same inode block.
    130 	 */
    131 	for (ldip = fin - 1; ldip >= dip; --ldip)
    132 		if (ldip->di_inumber == ino)
    133 			return (ldip);
    134 
    135 	printf("searched %d entries\n", (int)(fin - dip));
    136 	printf("offset is 0x%x (seg %d)\n", fs->lfs_offset,
    137 	       dtosn(fs, fs->lfs_offset));
    138 	printf("block is 0x%llx (seg %lld)\n",
    139 	       (unsigned long long)dbtofsb(fs, bp->b_blkno),
    140 	       (long long)dtosn(fs, dbtofsb(fs, bp->b_blkno)));
    141 
    142 	return NULL;
    143 }
    144 
    145 int
    146 lfs_update(void *v)
    147 {
    148 	struct vop_update_args /* {
    149 				  struct vnode *a_vp;
    150 				  struct timespec *a_access;
    151 				  struct timespec *a_modify;
    152 				  int a_flags;
    153 				  } */ *ap = v;
    154 	struct inode *ip;
    155 	struct vnode *vp = ap->a_vp;
    156 	struct timespec ts;
    157 	struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
    158 	int s;
    159 
    160 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    161 		return (0);
    162 	ip = VTOI(vp);
    163 
    164 	/*
    165 	 * If we are called from vinvalbuf, and the file's blocks have
    166 	 * already been scheduled for writing, but the writes have not
    167 	 * yet completed, lfs_vflush will not be called, and vinvalbuf
    168 	 * will cause a panic.  So, we must wait until any pending write
    169 	 * for our inode completes, if we are called with UPDATE_WAIT set.
    170 	 */
    171 	s = splbio();
    172 	while ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT &&
    173 	    WRITEINPROG(vp)) {
    174 #ifdef DEBUG_LFS
    175 		printf("lfs_update: sleeping on inode %d (in-progress)\n",
    176 		       ip->i_number);
    177 #endif
    178 		tsleep(vp, (PRIBIO+1), "lfs_update", 0);
    179 	}
    180 	splx(s);
    181 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    182 	LFS_ITIMES(ip,
    183 		   ap->a_access ? ap->a_access : &ts,
    184 		   ap->a_modify ? ap->a_modify : &ts, &ts);
    185 	if ((ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING)) == 0) {
    186 		return (0);
    187 	}
    188 
    189 	/* If sync, push back the vnode and any dirty blocks it may have. */
    190 	if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT) {
    191 		/* Avoid flushing VDIROP. */
    192 		++fs->lfs_diropwait;
    193 		while (vp->v_flag & VDIROP) {
    194 #ifdef DEBUG_LFS
    195 			printf("lfs_update: sleeping on inode %d (dirops)\n",
    196 			       ip->i_number);
    197 			printf("lfs_update: vflags 0x%x, iflags 0x%x\n",
    198 				vp->v_flag, ip->i_flag);
    199 #endif
    200 			if (fs->lfs_dirops == 0)
    201 				lfs_flush_fs(fs, SEGM_SYNC);
    202 			else
    203 				tsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync",
    204 				       0);
    205 			/* XXX KS - by falling out here, are we writing the vn
    206 			twice? */
    207 		}
    208 		--fs->lfs_diropwait;
    209 		return lfs_vflush(vp);
    210         }
    211 	return 0;
    212 }
    213 
    214 #define	SINGLE	0	/* index of single indirect block */
    215 #define	DOUBLE	1	/* index of double indirect block */
    216 #define	TRIPLE	2	/* index of triple indirect block */
    217 /*
    218  * Truncate the inode oip to at most length size, freeing the
    219  * disk blocks.
    220  */
    221 /* VOP_BWRITE 1 + NIADDR + VOP_BALLOC == 2 + 2*NIADDR times */
    222 
    223 int
    224 lfs_truncate(void *v)
    225 {
    226 	struct vop_truncate_args /* {
    227 		struct vnode *a_vp;
    228 		off_t a_length;
    229 		int a_flags;
    230 		struct ucred *a_cred;
    231 		struct proc *a_p;
    232 	} */ *ap = v;
    233 	struct vnode *ovp = ap->a_vp;
    234 #ifdef LFS_UBC
    235 	struct genfs_node *gp = VTOG(ovp);
    236 #endif
    237 	daddr_t lastblock;
    238 	struct inode *oip;
    239 	daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
    240 	/* XXX ondisk32 */
    241 	int32_t newblks[NDADDR + NIADDR];
    242 	off_t length = ap->a_length;
    243 	struct lfs *fs;
    244 	struct buf *bp;
    245 	int offset, size, level;
    246 	long count, rcount, nblocks, blocksreleased = 0, real_released = 0;
    247 	int i;
    248 	int aflags, error, allerror = 0;
    249 	off_t osize;
    250 	long lastseg;
    251 	size_t bc;
    252 	int obufsize, odb;
    253 	int usepc, needunlock;
    254 
    255 	if (length < 0)
    256 		return (EINVAL);
    257 	oip = VTOI(ovp);
    258 
    259 	/*
    260 	 * Just return and not update modification times.
    261 	 */
    262 	if (oip->i_ffs_size == length)
    263 		return (0);
    264 
    265 	if (ovp->v_type == VLNK &&
    266 	    (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
    267 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    268 	      oip->i_din.ffs_din.di_blocks == 0))) {
    269 #ifdef DIAGNOSTIC
    270 		if (length != 0)
    271 			panic("lfs_truncate: partial truncate of symlink");
    272 #endif
    273 		memset((char *)&oip->i_ffs_shortlink, 0, (u_int)oip->i_ffs_size);
    274 		oip->i_ffs_size = 0;
    275 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    276 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    277 	}
    278 	if (oip->i_ffs_size == length) {
    279 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    280 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    281 	}
    282 #ifdef QUOTA
    283 	if ((error = getinoquota(oip)) != 0)
    284 		return (error);
    285 #endif
    286 	fs = oip->i_lfs;
    287 	lfs_imtime(fs);
    288 	osize = oip->i_ffs_size;
    289 	needunlock = usepc = 0;
    290 #ifdef LFS_UBC
    291 	usepc = (ovp->v_type == VREG && osize > length && ovp != fs->lfs_ivnode);
    292 #endif
    293 
    294 	/*
    295 	 * Lengthen the size of the file. We must ensure that the
    296 	 * last byte of the file is allocated. Since the smallest
    297 	 * value of osize is 0, length will be at least 1.
    298 	 */
    299 	if (osize < length) {
    300 		if (length > fs->lfs_maxfilesize)
    301 			return (EFBIG);
    302 		aflags = B_CLRBUF;
    303 		if (ap->a_flags & IO_SYNC)
    304 			aflags |= B_SYNC;
    305 		error = lfs_reserve(fs, ovp, NULL,
    306 		    btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
    307 		if (error)
    308 			return (error);
    309 		error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
    310 		lfs_reserve(fs, ovp, NULL,
    311 		    -btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
    312 		if (error)
    313 			return (error);
    314 		oip->i_ffs_size = length;
    315 		uvm_vnp_setsize(ovp, length);
    316 		(void) VOP_BWRITE(bp);
    317 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    318 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    319 	}
    320 
    321 	if ((error = lfs_reserve(fs, ovp, NULL,
    322 	    btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift))) != 0)
    323 		return (error);
    324 
    325 	/*
    326 	 * Shorten the size of the file. If the file is not being
    327 	 * truncated to a block boundary, the contents of the
    328 	 * partial block following the end of the file must be
    329 	 * zero'ed in case it ever becomes accessible again because
    330 	 * of subsequent file growth. Directories however are not
    331 	 * zero'ed as they should grow back initialized to empty.
    332 	 */
    333 	offset = blkoff(fs, length);
    334 	lastseg = -1;
    335 	bc = 0;
    336 	if (offset == 0) {
    337 		oip->i_ffs_size = length;
    338 	} else
    339 #ifdef LFS_UBC
    340 	if (!usepc)
    341 #endif
    342 	{
    343 		lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    344 		lbn = lblkno(fs, length);
    345 		aflags = B_CLRBUF;
    346 		if (ap->a_flags & IO_SYNC)
    347 			aflags |= B_SYNC;
    348 		error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
    349 		if (error) {
    350 			lfs_reserve(fs, ovp, NULL,
    351 			    -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
    352 			lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    353 			return (error);
    354 		}
    355 		obufsize = bp->b_bufsize;
    356 		odb = btofsb(fs, bp->b_bcount);
    357 		oip->i_ffs_size = length;
    358 		size = blksize(fs, oip, lbn);
    359 		if (ovp->v_type != VDIR)
    360 			memset((char *)bp->b_data + offset, 0,
    361 			       (u_int)(size - offset));
    362 		allocbuf(bp, size);
    363 		if ((bp->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
    364 			locked_queue_bytes -= obufsize - bp->b_bufsize;
    365 		if (bp->b_flags & B_DELWRI)
    366 			fs->lfs_avail += odb - btofsb(fs, size);
    367 		(void) VOP_BWRITE(bp);
    368 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    369 	}
    370 #ifdef LFS_UBC
    371         /*
    372          * When truncating a regular file down to a non-block-aligned size,
    373          * we must zero the part of last block which is past the new EOF.
    374          * We must synchronously flush the zeroed pages to disk
    375          * since the new pages will be invalidated as soon as we
    376          * inform the VM system of the new, smaller size.
    377          * We must do this before acquiring the GLOCK, since fetching
    378          * the pages will acquire the GLOCK internally.
    379          * So there is a window where another thread could see a whole
    380          * zeroed page past EOF, but that's life.
    381          */
    382 
    383         else { /* vp->v_type == VREG && length < osize && offset != 0 */
    384                 voff_t eoz;
    385 
    386 		aflags = ap->a_flags & IO_SYNC ? B_SYNC : 0;
    387                 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    388 			aflags);
    389                 if (error) {
    390                         return error;
    391                 }
    392                 size = blksize(fs, oip, lblkno(fs, length));
    393                 eoz = MIN(lblktosize(fs, lblkno(fs, length)) + size, osize);
    394                 uvm_vnp_zerorange(ovp, length, eoz - length);
    395                 simple_lock(&ovp->v_interlock);
    396                 error = VOP_PUTPAGES(ovp, trunc_page(length), round_page(eoz),
    397                     PGO_CLEANIT | PGO_DEACTIVATE | PGO_SYNCIO);
    398                 if (error) {
    399                         return error;
    400                 }
    401         }
    402 
    403         lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    404 #endif
    405 
    406 	oip->i_ffs_size = length;
    407 	uvm_vnp_setsize(ovp, length);
    408 	/*
    409 	 * Calculate index into inode's block list of
    410 	 * last direct and indirect blocks (if any)
    411 	 * which we want to keep.  Lastblock is -1 when
    412 	 * the file is truncated to 0.
    413 	 */
    414 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1;
    415 	lastiblock[SINGLE] = lastblock - NDADDR;
    416 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    417 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    418 	nblocks = btofsb(fs, fs->lfs_bsize);
    419 	/*
    420 	 * Record changed file and block pointers before we start
    421 	 * freeing blocks.  lastiblock values are also normalized to -1
    422 	 * for calls to lfs_indirtrunc below.
    423 	 */
    424 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
    425 	for (level = TRIPLE; level >= SINGLE; level--)
    426 		if (lastiblock[level] < 0) {
    427 			newblks[NDADDR+level] = 0;
    428 			lastiblock[level] = -1;
    429 		}
    430 	for (i = NDADDR - 1; i > lastblock; i--)
    431 		newblks[i] = 0;
    432 
    433 	oip->i_ffs_size = osize;
    434 	error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0);
    435 	if (error && !allerror)
    436 		allerror = error;
    437 
    438 	/*
    439 	 * Indirect blocks first.
    440 	 */
    441 	indir_lbn[SINGLE] = -NDADDR;
    442 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    443 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    444 	for (level = TRIPLE; level >= SINGLE; level--) {
    445 		bn = oip->i_ffs_ib[level];
    446 		if (bn != 0) {
    447 			error = lfs_indirtrunc(oip, indir_lbn[level],
    448 					       bn, lastiblock[level],
    449 					       level, &count, &rcount,
    450 					       &lastseg, &bc, ap->a_p);
    451 			if (error)
    452 				allerror = error;
    453 			real_released += rcount;
    454 			blocksreleased += count;
    455 			if (lastiblock[level] < 0) {
    456 				if (oip->i_ffs_ib[level] > 0)
    457 					real_released += nblocks;
    458 				blocksreleased += nblocks;
    459 				oip->i_ffs_ib[level] = 0;
    460 				lfs_blkfree(fs, bn, fs->lfs_bsize, &lastseg, &bc);
    461 			}
    462 		}
    463 		if (lastiblock[level] >= 0)
    464 			goto done;
    465 	}
    466 
    467 	if (!usepc) {
    468 		lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    469 		needunlock = 1;
    470 	}
    471 	/*
    472 	 * All whole direct blocks or frags.
    473 	 */
    474 	for (i = NDADDR - 1; i > lastblock; i--) {
    475 		long bsize, obsize;
    476 
    477 		bn = oip->i_ffs_db[i];
    478 		if (bn == 0)
    479 			continue;
    480 		bsize = blksize(fs, oip, i);
    481 		if (oip->i_ffs_db[i] > 0) {
    482 			/* Check for fragment size changes */
    483 			obsize = oip->i_lfs_fragsize[i];
    484 			real_released += btofsb(fs, obsize);
    485 			oip->i_lfs_fragsize[i] = 0;
    486 		} else
    487 			obsize = 0;
    488 		blocksreleased += btofsb(fs, bsize);
    489 		oip->i_ffs_db[i] = 0;
    490 		lfs_blkfree(fs, bn, obsize, &lastseg, &bc);
    491 	}
    492 	if (lastblock < 0)
    493 		goto done;
    494 
    495 	/*
    496 	 * Finally, look for a change in size of the
    497 	 * last direct block; release any frags.
    498 	 */
    499 	bn = oip->i_ffs_db[lastblock];
    500 	if (bn != 0) {
    501 		long oldspace, newspace, olddspace;
    502 
    503 		/*
    504 		 * Calculate amount of space we're giving
    505 		 * back as old block size minus new block size.
    506 		 */
    507 		oldspace = blksize(fs, oip, lastblock);
    508 		olddspace = oip->i_lfs_fragsize[lastblock];
    509 
    510 		oip->i_ffs_size = length;
    511 		newspace = blksize(fs, oip, lastblock);
    512 		if (newspace == 0)
    513 			panic("itrunc: newspace");
    514 		if (oldspace - newspace > 0) {
    515 			blocksreleased += btofsb(fs, oldspace - newspace);
    516 		}
    517 #if 0
    518 		if (bn > 0 && olddspace - newspace > 0) {
    519 			/* No segment accounting here, just vnode */
    520 			real_released += btofsb(fs, olddspace - newspace);
    521 		}
    522 #endif
    523 	}
    524 
    525 done:
    526 	/* Finish segment accounting corrections */
    527 	lfs_update_seguse(fs, lastseg, bc);
    528 #ifdef DIAGNOSTIC
    529 	for (level = SINGLE; level <= TRIPLE; level++)
    530 		if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
    531 			panic("lfs itrunc1");
    532 	for (i = 0; i < NDADDR; i++)
    533 		if (newblks[i] != oip->i_ffs_db[i])
    534 			panic("lfs itrunc2");
    535 	if (length == 0 &&
    536 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    537 		panic("lfs itrunc3");
    538 #endif /* DIAGNOSTIC */
    539 	/*
    540 	 * Put back the real size.
    541 	 */
    542 	oip->i_ffs_size = length;
    543 	oip->i_lfs_effnblks -= blocksreleased;
    544 	oip->i_ffs_blocks -= real_released;
    545 	fs->lfs_bfree += blocksreleased;
    546 #ifdef DIAGNOSTIC
    547 	if (oip->i_ffs_size == 0 && oip->i_ffs_blocks != 0) {
    548 		printf("lfs_truncate: truncate to 0 but %d blocks on inode\n",
    549 		       oip->i_ffs_blocks);
    550 		panic("lfs_truncate: persistent blocks");
    551 	}
    552 #endif
    553 	oip->i_flag |= IN_CHANGE;
    554 #ifdef QUOTA
    555 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    556 #endif
    557 	lfs_reserve(fs, ovp, NULL,
    558 	    -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
    559 	if (needunlock)
    560 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    561 #ifdef LFS_UBC
    562 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    563 #endif
    564 	return (allerror);
    565 }
    566 
    567 /* Update segment usage information when removing a block. */
    568 static int
    569 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
    570 	    size_t *num)
    571 {
    572 	long seg;
    573 	int error = 0;
    574 
    575 	bsize = fragroundup(fs, bsize);
    576 	if (daddr > 0) {
    577 		if (*lastseg != (seg = dtosn(fs, daddr))) {
    578 			error = lfs_update_seguse(fs, *lastseg, *num);
    579 			*num = bsize;
    580 			*lastseg = seg;
    581 		} else
    582 			*num += bsize;
    583 	}
    584 	return error;
    585 }
    586 
    587 /* Finish the accounting updates for a segment. */
    588 static int
    589 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
    590 {
    591 	SEGUSE *sup;
    592 	struct buf *bp;
    593 
    594 	if (lastseg < 0 || num == 0)
    595 		return 0;
    596 
    597 	LFS_SEGENTRY(sup, fs, lastseg, bp);
    598 	if (num > sup->su_nbytes) {
    599 		printf("lfs_truncate: segment %ld short by %ld\n",
    600 		       lastseg, (long)num - sup->su_nbytes);
    601 		panic("lfs_truncate: negative bytes");
    602 		sup->su_nbytes = num;
    603 	}
    604 	sup->su_nbytes -= num;
    605 	LFS_WRITESEGENTRY(sup, fs, lastseg, bp);
    606 
    607 	return 0;
    608 }
    609 
    610 /*
    611  * Release blocks associated with the inode ip and stored in the indirect
    612  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    613  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    614  * and recursive calls to indirtrunc must be used to cleanse other indirect
    615  * blocks.
    616  *
    617  * NB: triple indirect blocks are untested.
    618  */
    619 static int
    620 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
    621 	       daddr_t lastbn, int level, long *countp,
    622 	       long *rcountp, long *lastsegp, size_t *bcp, struct proc *p)
    623 {
    624 	int i;
    625 	struct buf *bp;
    626 	struct lfs *fs = ip->i_lfs;
    627 	int32_t *bap;	/* XXX ondisk32 */
    628 	struct vnode *vp;
    629 	daddr_t nb, nlbn, last;
    630 	int32_t *copy = NULL;	/* XXX ondisk32 */
    631 	long blkcount, rblkcount, factor;
    632 	int nblocks, blocksreleased = 0, real_released = 0;
    633 	int error = 0, allerror = 0;
    634 
    635 	/*
    636 	 * Calculate index in current block of last
    637 	 * block to be kept.  -1 indicates the entire
    638 	 * block so we need not calculate the index.
    639 	 */
    640 	factor = 1;
    641 	for (i = SINGLE; i < level; i++)
    642 		factor *= NINDIR(fs);
    643 	last = lastbn;
    644 	if (lastbn > 0)
    645 		last /= factor;
    646 	nblocks = btofsb(fs, fs->lfs_bsize);
    647 	/*
    648 	 * Get buffer of block pointers, zero those entries corresponding
    649 	 * to blocks to be free'd, and update on disk copy first.  Since
    650 	 * double(triple) indirect before single(double) indirect, calls
    651 	 * to bmap on these blocks will fail.  However, we already have
    652 	 * the on disk address, so we have to set the b_blkno field
    653 	 * explicitly instead of letting bread do everything for us.
    654 	 */
    655 	vp = ITOV(ip);
    656 	bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
    657 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    658 		/* Braces must be here in case trace evaluates to nothing. */
    659 		trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
    660 	} else {
    661 		trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
    662 		p->p_stats->p_ru.ru_inblock++;	/* pay for read */
    663 		bp->b_flags |= B_READ;
    664 		if (bp->b_bcount > bp->b_bufsize)
    665 			panic("lfs_indirtrunc: bad buffer size");
    666 		bp->b_blkno = fsbtodb(fs, dbn);
    667 		VOP_STRATEGY(bp);
    668 		error = biowait(bp);
    669 	}
    670 	if (error) {
    671 		brelse(bp);
    672 		*countp = *rcountp = 0;
    673 		return (error);
    674 	}
    675 
    676 	bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    677 	if (lastbn >= 0) {
    678 		MALLOC(copy, int32_t *, fs->lfs_bsize, M_TEMP, M_WAITOK);
    679 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
    680 		memset((caddr_t)&bap[last + 1], 0,
    681 		/* XXX ondisk32 */
    682 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t));
    683 		error = VOP_BWRITE(bp);
    684 		if (error)
    685 			allerror = error;
    686 		bap = copy;
    687 	}
    688 
    689 	/*
    690 	 * Recursively free totally unused blocks.
    691 	 */
    692 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    693 	    i--, nlbn += factor) {
    694 		nb = bap[i];
    695 		if (nb == 0)
    696 			continue;
    697 		if (level > SINGLE) {
    698 			error = lfs_indirtrunc(ip, nlbn, nb,
    699 					       (daddr_t)-1, level - 1,
    700 					       &blkcount, &rblkcount,
    701 					       lastsegp, bcp, p);
    702 			if (error)
    703 				allerror = error;
    704 			blocksreleased += blkcount;
    705 			real_released += rblkcount;
    706 		}
    707 		lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
    708 		if (bap[i] > 0)
    709 			real_released += nblocks;
    710 		blocksreleased += nblocks;
    711 	}
    712 
    713 	/*
    714 	 * Recursively free last partial block.
    715 	 */
    716 	if (level > SINGLE && lastbn >= 0) {
    717 		last = lastbn % factor;
    718 		nb = bap[i];
    719 		if (nb != 0) {
    720 			error = lfs_indirtrunc(ip, nlbn, nb,
    721 					       last, level - 1, &blkcount,
    722 					       &rblkcount, lastsegp, bcp, p);
    723 			if (error)
    724 				allerror = error;
    725 			real_released += rblkcount;
    726 			blocksreleased += blkcount;
    727 		}
    728 	}
    729 
    730 	if (copy != NULL) {
    731 		FREE(copy, M_TEMP);
    732 	} else {
    733 		if (bp->b_flags & B_DELWRI) {
    734 			LFS_UNLOCK_BUF(bp);
    735 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    736 			wakeup(&fs->lfs_avail);
    737 		}
    738 		bp->b_flags |= B_INVAL;
    739 		brelse(bp);
    740 	}
    741 
    742 	*countp = blocksreleased;
    743 	*rcountp = real_released;
    744 	return (allerror);
    745 }
    746 
    747 /*
    748  * Destroy any in core blocks past the truncation length.
    749  * Inlined from vtruncbuf, so that lfs_avail could be updated.
    750  * We take the fraglock to prevent cleaning from occurring while we are
    751  * invalidating blocks.
    752  */
    753 static int
    754 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo)
    755 {
    756 	struct buf *bp, *nbp;
    757 	int s, error;
    758 	struct lfs *fs;
    759 	voff_t off;
    760 
    761 	off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
    762 	simple_lock(&vp->v_interlock);
    763 	error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
    764 	if (error) {
    765 		return error;
    766 	}
    767 
    768 	fs = VTOI(vp)->i_lfs;
    769 	s = splbio();
    770 
    771 	lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    772 restart:
    773 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
    774 		nbp = LIST_NEXT(bp, b_vnbufs);
    775 		if (bp->b_lblkno < lbn)
    776 			continue;
    777 		if (bp->b_flags & B_BUSY) {
    778 			bp->b_flags |= B_WANTED;
    779 			error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
    780 			    "lfs_vtruncbuf", slptimeo);
    781 			if (error) {
    782 				splx(s);
    783 				lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    784 				return (error);
    785 			}
    786 			goto restart;
    787 		}
    788 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
    789 		if (bp->b_flags & B_DELWRI) {
    790 			bp->b_flags &= ~B_DELWRI;
    791 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    792 			wakeup(&fs->lfs_avail);
    793 		}
    794 		LFS_UNLOCK_BUF(bp);
    795 		brelse(bp);
    796 	}
    797 
    798 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    799 		nbp = LIST_NEXT(bp, b_vnbufs);
    800 		if (bp->b_lblkno < lbn)
    801 			continue;
    802 		if (bp->b_flags & B_BUSY) {
    803 			bp->b_flags |= B_WANTED;
    804 			error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
    805 			    "lfs_vtruncbuf", slptimeo);
    806 			if (error) {
    807 				splx(s);
    808 				lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    809 				return (error);
    810 			}
    811 			goto restart;
    812 		}
    813 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
    814 		if (bp->b_flags & B_DELWRI) {
    815 			bp->b_flags &= ~B_DELWRI;
    816 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    817 			wakeup(&fs->lfs_avail);
    818 		}
    819 		LFS_UNLOCK_BUF(bp);
    820 		brelse(bp);
    821 	}
    822 
    823 	splx(s);
    824 	lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    825 
    826 	return (0);
    827 }
    828 
    829