Home | History | Annotate | Line # | Download | only in lfs
lfs_inode.c revision 1.61
      1 /*	$NetBSD: lfs_inode.c,v 1.61 2002/12/28 14:39:09 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 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.61 2002/12/28 14:39:09 yamt 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 *, ufs_daddr_t, ufs_daddr_t,
    105 			   ufs_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%x (seg %d)\n", dbtofsb(fs, bp->b_blkno),
    139 	       dtosn(fs, dbtofsb(fs, bp->b_blkno)));
    140 
    141 	return NULL;
    142 }
    143 
    144 int
    145 lfs_update(void *v)
    146 {
    147 	struct vop_update_args /* {
    148 				  struct vnode *a_vp;
    149 				  struct timespec *a_access;
    150 				  struct timespec *a_modify;
    151 				  int a_flags;
    152 				  } */ *ap = v;
    153 	struct inode *ip;
    154 	struct vnode *vp = ap->a_vp;
    155 	struct timespec ts;
    156 	struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
    157 	int s;
    158 
    159 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    160 		return (0);
    161 	ip = VTOI(vp);
    162 
    163 	/*
    164 	 * If we are called from vinvalbuf, and the file's blocks have
    165 	 * already been scheduled for writing, but the writes have not
    166 	 * yet completed, lfs_vflush will not be called, and vinvalbuf
    167 	 * will cause a panic.  So, we must wait until any pending write
    168 	 * for our inode completes, if we are called with UPDATE_WAIT set.
    169 	 */
    170 	s = splbio();
    171 	while ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT &&
    172 	    WRITEINPROG(vp)) {
    173 #ifdef DEBUG_LFS
    174 		printf("lfs_update: sleeping on inode %d (in-progress)\n",
    175 		       ip->i_number);
    176 #endif
    177 		tsleep(vp, (PRIBIO+1), "lfs_update", 0);
    178 	}
    179 	splx(s);
    180 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    181 	LFS_ITIMES(ip,
    182 		   ap->a_access ? ap->a_access : &ts,
    183 		   ap->a_modify ? ap->a_modify : &ts, &ts);
    184 	if ((ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING)) == 0) {
    185 		return (0);
    186 	}
    187 
    188 	/* If sync, push back the vnode and any dirty blocks it may have. */
    189 	if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT) {
    190 		/* Avoid flushing VDIROP. */
    191 		++fs->lfs_diropwait;
    192 		while (vp->v_flag & VDIROP) {
    193 #ifdef DEBUG_LFS
    194 			printf("lfs_update: sleeping on inode %d (dirops)\n",
    195 			       ip->i_number);
    196 			printf("lfs_update: vflags 0x%x, iflags 0x%x\n",
    197 				vp->v_flag, ip->i_flag);
    198 #endif
    199 			if (fs->lfs_dirops == 0)
    200 				lfs_flush_fs(fs, SEGM_SYNC);
    201 			else
    202 				tsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync",
    203 				       0);
    204 			/* XXX KS - by falling out here, are we writing the vn
    205 			twice? */
    206 		}
    207 		--fs->lfs_diropwait;
    208 		return lfs_vflush(vp);
    209         }
    210 	return 0;
    211 }
    212 
    213 #define	SINGLE	0	/* index of single indirect block */
    214 #define	DOUBLE	1	/* index of double indirect block */
    215 #define	TRIPLE	2	/* index of triple indirect block */
    216 /*
    217  * Truncate the inode oip to at most length size, freeing the
    218  * disk blocks.
    219  */
    220 /* VOP_BWRITE 1 + NIADDR + VOP_BALLOC == 2 + 2*NIADDR times */
    221 
    222 int
    223 lfs_truncate(void *v)
    224 {
    225 	struct vop_truncate_args /* {
    226 		struct vnode *a_vp;
    227 		off_t a_length;
    228 		int a_flags;
    229 		struct ucred *a_cred;
    230 		struct proc *a_p;
    231 	} */ *ap = v;
    232 	struct vnode *ovp = ap->a_vp;
    233 	ufs_daddr_t lastblock;
    234 	struct inode *oip;
    235 	ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
    236 	ufs_daddr_t newblks[NDADDR + NIADDR];
    237 	off_t length = ap->a_length;
    238 	struct lfs *fs;
    239 	struct buf *bp;
    240 	int offset, size, level;
    241 	long count, rcount, nblocks, blocksreleased = 0, real_released = 0;
    242 	int i;
    243 	int aflags, error, allerror = 0;
    244 	off_t osize;
    245 	long lastseg;
    246 	size_t bc;
    247 	int obufsize, odb;
    248 
    249 	if (length < 0)
    250 		return (EINVAL);
    251 	oip = VTOI(ovp);
    252 
    253 	/*
    254 	 * Just return and not update modification times.
    255 	 */
    256 	if (oip->i_ffs_size == length)
    257 		return (0);
    258 
    259 	if (ovp->v_type == VLNK &&
    260 	    (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
    261 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    262 	      oip->i_din.ffs_din.di_blocks == 0))) {
    263 #ifdef DIAGNOSTIC
    264 		if (length != 0)
    265 			panic("lfs_truncate: partial truncate of symlink");
    266 #endif
    267 		memset((char *)&oip->i_ffs_shortlink, 0, (u_int)oip->i_ffs_size);
    268 		oip->i_ffs_size = 0;
    269 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    270 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    271 	}
    272 	if (oip->i_ffs_size == length) {
    273 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    274 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    275 	}
    276 #ifdef QUOTA
    277 	if ((error = getinoquota(oip)) != 0)
    278 		return (error);
    279 #endif
    280 	fs = oip->i_lfs;
    281 	lfs_imtime(fs);
    282 	osize = oip->i_ffs_size;
    283 
    284 	/*
    285 	 * Lengthen the size of the file. We must ensure that the
    286 	 * last byte of the file is allocated. Since the smallest
    287 	 * value of osize is 0, length will be at least 1.
    288 	 */
    289 	if (osize < length) {
    290 		if (length > fs->lfs_maxfilesize)
    291 			return (EFBIG);
    292 		aflags = B_CLRBUF;
    293 		if (ap->a_flags & IO_SYNC)
    294 			aflags |= B_SYNC;
    295 		error = lfs_reserve(fs, ovp, NULL,
    296 		    btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
    297 		if (error)
    298 			return (error);
    299 		error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
    300 		lfs_reserve(fs, ovp, NULL,
    301 		    -btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
    302 		if (error)
    303 			return (error);
    304 		oip->i_ffs_size = length;
    305 		uvm_vnp_setsize(ovp, length);
    306 		(void) VOP_BWRITE(bp);
    307 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    308 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    309 	}
    310 
    311 	if ((error = lfs_reserve(fs, ovp, NULL,
    312 	    btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift))) != 0)
    313 		return (error);
    314 	/*
    315 	 * Make sure no writes to this inode can happen while we're
    316 	 * truncating.  Otherwise, blocks which are accounted for on the
    317 	 * inode *and* which have been created for cleaning can coexist,
    318 	 * and cause an overcounting.
    319 	 */
    320 #ifdef LFS_FRAGSIZE_SEGLOCK
    321 	lfs_seglock(fs, SEGM_PROT);
    322 #else
    323 	lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    324 #endif
    325 
    326 	/*
    327 	 * Shorten the size of the file. If the file is not being
    328 	 * truncated to a block boundary, the contents of the
    329 	 * partial block following the end of the file must be
    330 	 * zero'ed in case it ever becomes accessible again because
    331 	 * of subsequent file growth. Directories however are not
    332 	 * zero'ed as they should grow back initialized to empty.
    333 	 */
    334 	offset = blkoff(fs, length);
    335 	lastseg = -1;
    336 	bc = 0;
    337 	if (offset == 0) {
    338 		oip->i_ffs_size = length;
    339 	} else {
    340 		lbn = lblkno(fs, length);
    341 		aflags = B_CLRBUF;
    342 		if (ap->a_flags & IO_SYNC)
    343 			aflags |= B_SYNC;
    344 		error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
    345 		if (error) {
    346 			lfs_reserve(fs, ovp, NULL,
    347 			    -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
    348 #ifdef LFS_FRAGSIZE_SEGLOCK
    349 			lfs_segunlock(fs);
    350 #else
    351 			lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    352 #endif
    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 	}
    369 	uvm_vnp_setsize(ovp, length);
    370 	/*
    371 	 * Calculate index into inode's block list of
    372 	 * last direct and indirect blocks (if any)
    373 	 * which we want to keep.  Lastblock is -1 when
    374 	 * the file is truncated to 0.
    375 	 */
    376 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1;
    377 	lastiblock[SINGLE] = lastblock - NDADDR;
    378 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    379 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    380 	nblocks = btofsb(fs, fs->lfs_bsize);
    381 	/*
    382 	 * Record changed file and block pointers before we start
    383 	 * freeing blocks.  lastiblock values are also normalized to -1
    384 	 * for calls to lfs_indirtrunc below.
    385 	 */
    386 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
    387 	for (level = TRIPLE; level >= SINGLE; level--)
    388 		if (lastiblock[level] < 0) {
    389 			newblks[NDADDR+level] = 0;
    390 			lastiblock[level] = -1;
    391 		}
    392 	for (i = NDADDR - 1; i > lastblock; i--)
    393 		newblks[i] = 0;
    394 
    395 	oip->i_ffs_size = osize;
    396 	error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0);
    397 	if (error && !allerror)
    398 		allerror = error;
    399 
    400 	/*
    401 	 * Indirect blocks first.
    402 	 */
    403 	indir_lbn[SINGLE] = -NDADDR;
    404 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    405 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    406 	for (level = TRIPLE; level >= SINGLE; level--) {
    407 		bn = oip->i_ffs_ib[level];
    408 		if (bn != 0) {
    409 			error = lfs_indirtrunc(oip, indir_lbn[level],
    410 					       bn, lastiblock[level],
    411 					       level, &count, &rcount,
    412 					       &lastseg, &bc, ap->a_p);
    413 			if (error)
    414 				allerror = error;
    415 			real_released += rcount;
    416 			blocksreleased += count;
    417 			if (lastiblock[level] < 0) {
    418 				if (oip->i_ffs_ib[level] > 0)
    419 					real_released += nblocks;
    420 				blocksreleased += nblocks;
    421 				oip->i_ffs_ib[level] = 0;
    422 				lfs_blkfree(fs, bn, fs->lfs_bsize, &lastseg, &bc);
    423 			}
    424 		}
    425 		if (lastiblock[level] >= 0)
    426 			goto done;
    427 	}
    428 
    429 	/*
    430 	 * All whole direct blocks or frags.
    431 	 */
    432 	for (i = NDADDR - 1; i > lastblock; i--) {
    433 		long bsize, obsize;
    434 
    435 		bn = oip->i_ffs_db[i];
    436 		if (bn == 0)
    437 			continue;
    438 		bsize = blksize(fs, oip, i);
    439 		if (oip->i_ffs_db[i] > 0) {
    440 			/* Check for fragment size changes */
    441 			obsize = oip->i_lfs_fragsize[i];
    442 			real_released += btofsb(fs, obsize);
    443 			oip->i_lfs_fragsize[i] = 0;
    444 		} else
    445 			obsize = 0;
    446 		blocksreleased += btofsb(fs, bsize);
    447 		oip->i_ffs_db[i] = 0;
    448 		lfs_blkfree(fs, bn, obsize, &lastseg, &bc);
    449 	}
    450 	if (lastblock < 0)
    451 		goto done;
    452 
    453 	/*
    454 	 * Finally, look for a change in size of the
    455 	 * last direct block; release any frags.
    456 	 */
    457 	bn = oip->i_ffs_db[lastblock];
    458 	if (bn != 0) {
    459 		long oldspace, newspace, olddspace;
    460 
    461 		/*
    462 		 * Calculate amount of space we're giving
    463 		 * back as old block size minus new block size.
    464 		 */
    465 		oldspace = blksize(fs, oip, lastblock);
    466 		olddspace = oip->i_lfs_fragsize[lastblock];
    467 
    468 		oip->i_ffs_size = length;
    469 		newspace = blksize(fs, oip, lastblock);
    470 		if (newspace == 0)
    471 			panic("itrunc: newspace");
    472 		if (oldspace - newspace > 0) {
    473 			blocksreleased += btofsb(fs, oldspace - newspace);
    474 		}
    475 #if 0
    476 		if (bn > 0 && olddspace - newspace > 0) {
    477 			/* No segment accounting here, just vnode */
    478 			real_released += btofsb(fs, olddspace - newspace);
    479 		}
    480 #endif
    481 	}
    482 
    483 done:
    484 	/* Finish segment accounting corrections */
    485 	lfs_update_seguse(fs, lastseg, bc);
    486 #ifdef DIAGNOSTIC
    487 	for (level = SINGLE; level <= TRIPLE; level++)
    488 		if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
    489 			panic("lfs itrunc1");
    490 	for (i = 0; i < NDADDR; i++)
    491 		if (newblks[i] != oip->i_ffs_db[i])
    492 			panic("lfs itrunc2");
    493 	if (length == 0 &&
    494 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    495 		panic("lfs itrunc3");
    496 #endif /* DIAGNOSTIC */
    497 	/*
    498 	 * Put back the real size.
    499 	 */
    500 	oip->i_ffs_size = length;
    501 	oip->i_lfs_effnblks -= blocksreleased;
    502 	oip->i_ffs_blocks -= real_released;
    503 	fs->lfs_bfree += blocksreleased;
    504 #ifdef DIAGNOSTIC
    505 	if (oip->i_ffs_size == 0 && oip->i_ffs_blocks != 0) {
    506 		printf("lfs_truncate: truncate to 0 but %d blocks on inode\n",
    507 		       oip->i_ffs_blocks);
    508 		panic("lfs_truncate: persistent blocks");
    509 	}
    510 #endif
    511 	oip->i_flag |= IN_CHANGE;
    512 #ifdef QUOTA
    513 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    514 #endif
    515 	lfs_reserve(fs, ovp, NULL,
    516 	    -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
    517 #ifdef LFS_FRAGSIZE_SEGLOCK
    518 	lfs_segunlock(fs);
    519 #else
    520 	lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    521 #endif
    522 	return (allerror);
    523 }
    524 
    525 /* Update segment usage information when removing a block. */
    526 static int
    527 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
    528 	    size_t *num)
    529 {
    530 	long seg;
    531 	int error = 0;
    532 
    533 	bsize = fragroundup(fs, bsize);
    534 	if (daddr > 0) {
    535 		if (*lastseg != (seg = dtosn(fs, daddr))) {
    536 			error = lfs_update_seguse(fs, *lastseg, *num);
    537 			*num = bsize;
    538 			*lastseg = seg;
    539 		} else
    540 			*num += bsize;
    541 	}
    542 	return error;
    543 }
    544 
    545 /* Finish the accounting updates for a segment. */
    546 static int
    547 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
    548 {
    549 	SEGUSE *sup;
    550 	struct buf *bp;
    551 	int error;
    552 
    553 	if (lastseg < 0 || num == 0)
    554 		return 0;
    555 
    556 	LFS_SEGENTRY(sup, fs, lastseg, bp);
    557 	if (num > sup->su_nbytes) {
    558 		printf("lfs_truncate: segment %ld short by %ld\n",
    559 		       lastseg, (long)num - sup->su_nbytes);
    560 		panic("lfs_truncate: negative bytes");
    561 		sup->su_nbytes = num;
    562 	}
    563 	sup->su_nbytes -= num;
    564 	error = LFS_BWRITE_LOG(bp); /* Ifile */
    565 	return error;
    566 }
    567 
    568 /*
    569  * Release blocks associated with the inode ip and stored in the indirect
    570  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    571  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    572  * and recursive calls to indirtrunc must be used to cleanse other indirect
    573  * blocks.
    574  *
    575  * NB: triple indirect blocks are untested.
    576  */
    577 static int
    578 lfs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, daddr_t dbn,
    579 	       ufs_daddr_t lastbn, int level, long *countp,
    580 	       long *rcountp, long *lastsegp, size_t *bcp, struct proc *p)
    581 {
    582 	int i;
    583 	struct buf *bp;
    584 	struct lfs *fs = ip->i_lfs;
    585 	ufs_daddr_t *bap;
    586 	struct vnode *vp;
    587 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
    588 	long blkcount, rblkcount, factor;
    589 	int nblocks, blocksreleased = 0, real_released = 0;
    590 	int error = 0, allerror = 0;
    591 
    592 	/*
    593 	 * Calculate index in current block of last
    594 	 * block to be kept.  -1 indicates the entire
    595 	 * block so we need not calculate the index.
    596 	 */
    597 	factor = 1;
    598 	for (i = SINGLE; i < level; i++)
    599 		factor *= NINDIR(fs);
    600 	last = lastbn;
    601 	if (lastbn > 0)
    602 		last /= factor;
    603 	nblocks = btofsb(fs, fs->lfs_bsize);
    604 	/*
    605 	 * Get buffer of block pointers, zero those entries corresponding
    606 	 * to blocks to be free'd, and update on disk copy first.  Since
    607 	 * double(triple) indirect before single(double) indirect, calls
    608 	 * to bmap on these blocks will fail.  However, we already have
    609 	 * the on disk address, so we have to set the b_blkno field
    610 	 * explicitly instead of letting bread do everything for us.
    611 	 */
    612 	vp = ITOV(ip);
    613 	bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
    614 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    615 		/* Braces must be here in case trace evaluates to nothing. */
    616 		trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
    617 	} else {
    618 		trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
    619 		p->p_stats->p_ru.ru_inblock++;	/* pay for read */
    620 		bp->b_flags |= B_READ;
    621 		if (bp->b_bcount > bp->b_bufsize)
    622 			panic("lfs_indirtrunc: bad buffer size");
    623 		bp->b_blkno = fsbtodb(fs, dbn);
    624 		VOP_STRATEGY(bp);
    625 		error = biowait(bp);
    626 	}
    627 	if (error) {
    628 		brelse(bp);
    629 		*countp = *rcountp = 0;
    630 		return (error);
    631 	}
    632 
    633 	bap = (ufs_daddr_t *)bp->b_data;
    634 	if (lastbn >= 0) {
    635 		MALLOC(copy, ufs_daddr_t *, fs->lfs_bsize, M_TEMP, M_WAITOK);
    636 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
    637 		memset((caddr_t)&bap[last + 1], 0,
    638 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
    639 		error = VOP_BWRITE(bp);
    640 		if (error)
    641 			allerror = error;
    642 		bap = copy;
    643 	}
    644 
    645 	/*
    646 	 * Recursively free totally unused blocks.
    647 	 */
    648 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    649 	    i--, nlbn += factor) {
    650 		nb = bap[i];
    651 		if (nb == 0)
    652 			continue;
    653 		if (level > SINGLE) {
    654 			error = lfs_indirtrunc(ip, nlbn, nb,
    655 					       (ufs_daddr_t)-1, level - 1,
    656 					       &blkcount, &rblkcount,
    657 					       lastsegp, bcp, p);
    658 			if (error)
    659 				allerror = error;
    660 			blocksreleased += blkcount;
    661 			real_released += rblkcount;
    662 		}
    663 		lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
    664 		if (bap[i] > 0)
    665 			real_released += nblocks;
    666 		blocksreleased += nblocks;
    667 	}
    668 
    669 	/*
    670 	 * Recursively free last partial block.
    671 	 */
    672 	if (level > SINGLE && lastbn >= 0) {
    673 		last = lastbn % factor;
    674 		nb = bap[i];
    675 		if (nb != 0) {
    676 			error = lfs_indirtrunc(ip, nlbn, nb,
    677 					       last, level - 1, &blkcount,
    678 					       &rblkcount, lastsegp, bcp, p);
    679 			if (error)
    680 				allerror = error;
    681 			real_released += rblkcount;
    682 			blocksreleased += blkcount;
    683 		}
    684 	}
    685 
    686 	if (copy != NULL) {
    687 		FREE(copy, M_TEMP);
    688 	} else {
    689 		if (bp->b_flags & B_DELWRI) {
    690 			LFS_UNLOCK_BUF(bp);
    691 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    692 			wakeup(&fs->lfs_avail);
    693 		}
    694 		bp->b_flags |= B_INVAL;
    695 		brelse(bp);
    696 	}
    697 
    698 	*countp = blocksreleased;
    699 	*rcountp = real_released;
    700 	return (allerror);
    701 }
    702 
    703 /*
    704  * Destroy any in core blocks past the truncation length.
    705  * Inlined from vtruncbuf, so that lfs_avail could be updated.
    706  */
    707 static int
    708 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo)
    709 {
    710 	struct buf *bp, *nbp;
    711 	int s, error;
    712 	struct lfs *fs;
    713 
    714 	fs = VTOI(vp)->i_lfs;
    715 	s = splbio();
    716 
    717 restart:
    718 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
    719 		nbp = LIST_NEXT(bp, b_vnbufs);
    720 		if (bp->b_lblkno < lbn)
    721 			continue;
    722 		if (bp->b_flags & B_BUSY) {
    723 			bp->b_flags |= B_WANTED;
    724 			error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
    725 			    "lfs_vtruncbuf", slptimeo);
    726 			if (error) {
    727 				splx(s);
    728 				return (error);
    729 			}
    730 			goto restart;
    731 		}
    732 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
    733 		if (bp->b_flags & B_DELWRI) {
    734 			bp->b_flags &= ~B_DELWRI;
    735 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    736 			wakeup(&fs->lfs_avail);
    737 		}
    738 		LFS_UNLOCK_BUF(bp);
    739 		brelse(bp);
    740 	}
    741 
    742 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    743 		nbp = LIST_NEXT(bp, b_vnbufs);
    744 		if (bp->b_lblkno < lbn)
    745 			continue;
    746 		if (bp->b_flags & B_BUSY) {
    747 			bp->b_flags |= B_WANTED;
    748 			error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
    749 			    "lfs_vtruncbuf", slptimeo);
    750 			if (error) {
    751 				splx(s);
    752 				return (error);
    753 			}
    754 			goto restart;
    755 		}
    756 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
    757 		if (bp->b_flags & B_DELWRI) {
    758 			bp->b_flags &= ~B_DELWRI;
    759 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    760 			wakeup(&fs->lfs_avail);
    761 		}
    762 		LFS_UNLOCK_BUF(bp);
    763 		brelse(bp);
    764 	}
    765 
    766 	splx(s);
    767 
    768 	return (0);
    769 }
    770 
    771