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