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