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