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