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