Home | History | Annotate | Line # | Download | only in lfs
lfs_inode.c revision 1.142
      1 /*	$NetBSD: lfs_inode.c,v 1.142 2015/08/02 18:12:41 dholland Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Copyright (c) 1986, 1989, 1991, 1993
     33  *	The Regents of the University of California.  All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. Neither the name of the University nor the names of its contributors
     44  *    may be used to endorse or promote products derived from this software
     45  *    without specific prior written permission.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  * SUCH DAMAGE.
     58  *
     59  *	@(#)lfs_inode.c	8.9 (Berkeley) 5/8/95
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: lfs_inode.c,v 1.142 2015/08/02 18:12:41 dholland Exp $");
     64 
     65 #if defined(_KERNEL_OPT)
     66 #include "opt_quota.h"
     67 #endif
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/mount.h>
     72 #include <sys/malloc.h>
     73 #include <sys/proc.h>
     74 #include <sys/file.h>
     75 #include <sys/buf.h>
     76 #include <sys/vnode.h>
     77 #include <sys/kernel.h>
     78 #include <sys/trace.h>
     79 #include <sys/resourcevar.h>
     80 #include <sys/kauth.h>
     81 
     82 #include <ufs/lfs/ulfs_quotacommon.h>
     83 #include <ufs/lfs/ulfs_inode.h>
     84 #include <ufs/lfs/ulfsmount.h>
     85 #include <ufs/lfs/ulfs_extern.h>
     86 
     87 #include <ufs/lfs/lfs.h>
     88 #include <ufs/lfs/lfs_accessors.h>
     89 #include <ufs/lfs/lfs_extern.h>
     90 #include <ufs/lfs/lfs_kernel.h>
     91 
     92 static int lfs_update_seguse(struct lfs *, struct inode *ip, long, size_t);
     93 static int lfs_indirtrunc(struct inode *, daddr_t, daddr_t,
     94 			  daddr_t, int, daddr_t *, daddr_t *,
     95 			  long *, size_t *);
     96 static int lfs_blkfree (struct lfs *, struct inode *, daddr_t, size_t, long *, size_t *);
     97 static int lfs_vtruncbuf(struct vnode *, daddr_t, bool, int);
     98 
     99 /* Search a block for a specific dinode. */
    100 struct ulfs1_dinode *
    101 lfs_ifind(struct lfs *fs, ino_t ino, struct buf *bp)
    102 {
    103 	struct ulfs1_dinode *dip = (struct ulfs1_dinode *)bp->b_data;
    104 	struct ulfs1_dinode *ldip, *fin;
    105 
    106 	ASSERT_NO_SEGLOCK(fs);
    107 	/*
    108 	 * Read the inode block backwards, since later versions of the
    109 	 * inode will supercede earlier ones.  Though it is unlikely, it is
    110 	 * possible that the same inode will appear in the same inode block.
    111 	 */
    112 	fin = dip + LFS_INOPB(fs);
    113 	for (ldip = fin - 1; ldip >= dip; --ldip)
    114 		if (ldip->di_inumber == ino)
    115 			return (ldip);
    116 
    117 	printf("searched %d entries\n", (int)(fin - dip));
    118 	printf("offset is 0x%jx (seg %d)\n", (uintmax_t)lfs_sb_getoffset(fs),
    119 	       lfs_dtosn(fs, lfs_sb_getoffset(fs)));
    120 	printf("block is 0x%jx (seg %d)\n",
    121 	       (uintmax_t)LFS_DBTOFSB(fs, bp->b_blkno),
    122 	       lfs_dtosn(fs, LFS_DBTOFSB(fs, bp->b_blkno)));
    123 
    124 	return NULL;
    125 }
    126 
    127 int
    128 lfs_update(struct vnode *vp, const struct timespec *acc,
    129     const struct timespec *mod, int updflags)
    130 {
    131 	struct inode *ip;
    132 	struct lfs *fs = VFSTOULFS(vp->v_mount)->um_lfs;
    133 	int flags;
    134 
    135 	ASSERT_NO_SEGLOCK(fs);
    136 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    137 		return (0);
    138 	ip = VTOI(vp);
    139 
    140 	/*
    141 	 * If we are called from vinvalbuf, and the file's blocks have
    142 	 * already been scheduled for writing, but the writes have not
    143 	 * yet completed, lfs_vflush will not be called, and vinvalbuf
    144 	 * will cause a panic.	So, we must wait until any pending write
    145 	 * for our inode completes, if we are called with UPDATE_WAIT set.
    146 	 */
    147 	mutex_enter(vp->v_interlock);
    148 	while ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT &&
    149 	    WRITEINPROG(vp)) {
    150 		DLOG((DLOG_SEG, "lfs_update: sleeping on ino %d"
    151 		      " (in progress)\n", ip->i_number));
    152 		cv_wait(&vp->v_cv, vp->v_interlock);
    153 	}
    154 	mutex_exit(vp->v_interlock);
    155 	LFS_ITIMES(ip, acc, mod, NULL);
    156 	if (updflags & UPDATE_CLOSE)
    157 		flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING);
    158 	else
    159 		flags = ip->i_flag & (IN_MODIFIED | IN_CLEANING);
    160 	if (flags == 0)
    161 		return (0);
    162 
    163 	/* If sync, push back the vnode and any dirty blocks it may have. */
    164 	if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT) {
    165 		/* Avoid flushing VU_DIROP. */
    166 		mutex_enter(&lfs_lock);
    167 		++fs->lfs_diropwait;
    168 		while (vp->v_uflag & VU_DIROP) {
    169 			DLOG((DLOG_DIROP, "lfs_update: sleeping on inode %d"
    170 			      " (dirops)\n", ip->i_number));
    171 			DLOG((DLOG_DIROP, "lfs_update: vflags 0x%x, iflags"
    172 			      " 0x%x\n",
    173 			      vp->v_iflag | vp->v_vflag | vp->v_uflag,
    174 			      ip->i_flag));
    175 			if (fs->lfs_dirops == 0)
    176 				lfs_flush_fs(fs, SEGM_SYNC);
    177 			else
    178 				mtsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync",
    179 					0, &lfs_lock);
    180 			/* XXX KS - by falling out here, are we writing the vn
    181 			twice? */
    182 		}
    183 		--fs->lfs_diropwait;
    184 		mutex_exit(&lfs_lock);
    185 		return lfs_vflush(vp);
    186 	}
    187 	return 0;
    188 }
    189 
    190 #define	SINGLE	0	/* index of single indirect block */
    191 #define	DOUBLE	1	/* index of double indirect block */
    192 #define	TRIPLE	2	/* index of triple indirect block */
    193 /*
    194  * Truncate the inode oip to at most length size, freeing the
    195  * disk blocks.
    196  */
    197 /* VOP_BWRITE 1 + ULFS_NIADDR + lfs_balloc == 2 + 2*ULFS_NIADDR times */
    198 
    199 int
    200 lfs_truncate(struct vnode *ovp, off_t length, int ioflag, kauth_cred_t cred)
    201 {
    202 	daddr_t lastblock;
    203 	struct inode *oip = VTOI(ovp);
    204 	daddr_t bn, lbn, lastiblock[ULFS_NIADDR], indir_lbn[ULFS_NIADDR];
    205 	/* XXX ondisk32 */
    206 	int32_t newblks[ULFS_NDADDR + ULFS_NIADDR];
    207 	struct lfs *fs;
    208 	struct buf *bp;
    209 	int offset, size, level;
    210 	long count, rcount;
    211 	daddr_t blocksreleased = 0, real_released = 0;
    212 	int i, nblocks;
    213 	int aflags, error, allerror = 0;
    214 	off_t osize;
    215 	long lastseg;
    216 	size_t bc;
    217 	int obufsize, odb;
    218 	int usepc;
    219 
    220 	if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
    221 	    ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
    222 		KASSERT(oip->i_size == 0);
    223 		return 0;
    224 	}
    225 
    226 	if (length < 0)
    227 		return (EINVAL);
    228 
    229 	/*
    230 	 * Just return and not update modification times.
    231 	 */
    232 	if (oip->i_size == length) {
    233 		/* still do a uvm_vnp_setsize() as writesize may be larger */
    234 		uvm_vnp_setsize(ovp, length);
    235 		return (0);
    236 	}
    237 
    238 	fs = oip->i_lfs;
    239 
    240 	if (ovp->v_type == VLNK &&
    241 	    (oip->i_size < fs->um_maxsymlinklen ||
    242 	     (fs->um_maxsymlinklen == 0 &&
    243 	      oip->i_ffs1_blocks == 0))) {
    244 #ifdef DIAGNOSTIC
    245 		if (length != 0)
    246 			panic("lfs_truncate: partial truncate of symlink");
    247 #endif
    248 		memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size);
    249 		oip->i_size = oip->i_ffs1_size = 0;
    250 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    251 		return (lfs_update(ovp, NULL, NULL, 0));
    252 	}
    253 	if (oip->i_size == length) {
    254 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    255 		return (lfs_update(ovp, NULL, NULL, 0));
    256 	}
    257 	lfs_imtime(fs);
    258 	osize = oip->i_size;
    259 	usepc = (ovp->v_type == VREG && ovp != fs->lfs_ivnode);
    260 
    261 	ASSERT_NO_SEGLOCK(fs);
    262 	/*
    263 	 * Lengthen the size of the file. We must ensure that the
    264 	 * last byte of the file is allocated. Since the smallest
    265 	 * value of osize is 0, length will be at least 1.
    266 	 */
    267 	if (osize < length) {
    268 		if (length > fs->um_maxfilesize)
    269 			return (EFBIG);
    270 		aflags = B_CLRBUF;
    271 		if (ioflag & IO_SYNC)
    272 			aflags |= B_SYNC;
    273 		if (usepc) {
    274 			if (lfs_lblkno(fs, osize) < ULFS_NDADDR &&
    275 			    lfs_lblkno(fs, osize) != lfs_lblkno(fs, length) &&
    276 			    lfs_blkroundup(fs, osize) != osize) {
    277 				off_t eob;
    278 
    279 				eob = lfs_blkroundup(fs, osize);
    280 				uvm_vnp_setwritesize(ovp, eob);
    281 				error = ulfs_balloc_range(ovp, osize,
    282 				    eob - osize, cred, aflags);
    283 				if (error) {
    284 					(void) lfs_truncate(ovp, osize,
    285 						    ioflag & IO_SYNC, cred);
    286 					return error;
    287 				}
    288 				if (ioflag & IO_SYNC) {
    289 					mutex_enter(ovp->v_interlock);
    290 					VOP_PUTPAGES(ovp,
    291 					    trunc_page(osize & lfs_sb_getbmask(fs)),
    292 					    round_page(eob),
    293 					    PGO_CLEANIT | PGO_SYNCIO);
    294 				}
    295 			}
    296 			uvm_vnp_setwritesize(ovp, length);
    297 			error = ulfs_balloc_range(ovp, length - 1, 1, cred,
    298 						 aflags);
    299 			if (error) {
    300 				(void) lfs_truncate(ovp, osize,
    301 						    ioflag & IO_SYNC, cred);
    302 				return error;
    303 			}
    304 			uvm_vnp_setsize(ovp, length);
    305 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    306 			KASSERT(ovp->v_size == oip->i_size);
    307 			oip->i_lfs_hiblk = lfs_lblkno(fs, oip->i_size + lfs_sb_getbsize(fs) - 1) - 1;
    308 			return (lfs_update(ovp, NULL, NULL, 0));
    309 		} else {
    310 			error = lfs_reserve(fs, ovp, NULL,
    311 			    lfs_btofsb(fs, (ULFS_NIADDR + 2) << lfs_sb_getbshift(fs)));
    312 			if (error)
    313 				return (error);
    314 			error = lfs_balloc(ovp, length - 1, 1, cred,
    315 					   aflags, &bp);
    316 			lfs_reserve(fs, ovp, NULL,
    317 			    -lfs_btofsb(fs, (ULFS_NIADDR + 2) << lfs_sb_getbshift(fs)));
    318 			if (error)
    319 				return (error);
    320 			oip->i_ffs1_size = oip->i_size = length;
    321 			uvm_vnp_setsize(ovp, length);
    322 			(void) VOP_BWRITE(bp->b_vp, bp);
    323 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    324 			oip->i_lfs_hiblk = lfs_lblkno(fs, oip->i_size + lfs_sb_getbsize(fs) - 1) - 1;
    325 			return (lfs_update(ovp, NULL, NULL, 0));
    326 		}
    327 	}
    328 
    329 	if ((error = lfs_reserve(fs, ovp, NULL,
    330 	    lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)))) != 0)
    331 		return (error);
    332 
    333 	/*
    334 	 * Shorten the size of the file. If the file is not being
    335 	 * truncated to a block boundary, the contents of the
    336 	 * partial block following the end of the file must be
    337 	 * zero'ed in case it ever becomes accessible again because
    338 	 * of subsequent file growth. Directories however are not
    339 	 * zero'ed as they should grow back initialized to empty.
    340 	 */
    341 	offset = lfs_blkoff(fs, length);
    342 	lastseg = -1;
    343 	bc = 0;
    344 
    345 	if (ovp != fs->lfs_ivnode)
    346 		lfs_seglock(fs, SEGM_PROT);
    347 	if (offset == 0) {
    348 		oip->i_size = oip->i_ffs1_size = length;
    349 	} else if (!usepc) {
    350 		lbn = lfs_lblkno(fs, length);
    351 		aflags = B_CLRBUF;
    352 		if (ioflag & IO_SYNC)
    353 			aflags |= B_SYNC;
    354 		error = lfs_balloc(ovp, length - 1, 1, cred, aflags, &bp);
    355 		if (error) {
    356 			lfs_reserve(fs, ovp, NULL,
    357 			    -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)));
    358 			goto errout;
    359 		}
    360 		obufsize = bp->b_bufsize;
    361 		odb = lfs_btofsb(fs, bp->b_bcount);
    362 		oip->i_size = oip->i_ffs1_size = length;
    363 		size = lfs_blksize(fs, oip, lbn);
    364 		if (ovp->v_type != VDIR)
    365 			memset((char *)bp->b_data + offset, 0,
    366 			       (u_int)(size - offset));
    367 		allocbuf(bp, size, 1);
    368 		if ((bp->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) {
    369 			mutex_enter(&lfs_lock);
    370 			locked_queue_bytes -= obufsize - bp->b_bufsize;
    371 			mutex_exit(&lfs_lock);
    372 		}
    373 		if (bp->b_oflags & BO_DELWRI) {
    374 			lfs_sb_addavail(fs, odb - lfs_btofsb(fs, size));
    375 			/* XXX shouldn't this wake up on lfs_availsleep? */
    376 		}
    377 		(void) VOP_BWRITE(bp->b_vp, bp);
    378 	} else { /* vp->v_type == VREG && length < osize && offset != 0 */
    379 		/*
    380 		 * When truncating a regular file down to a non-block-aligned
    381 		 * size, we must zero the part of last block which is past
    382 		 * the new EOF.  We must synchronously flush the zeroed pages
    383 		 * to disk since the new pages will be invalidated as soon
    384 		 * as we inform the VM system of the new, smaller size.
    385 		 * We must do this before acquiring the GLOCK, since fetching
    386 		 * the pages will acquire the GLOCK internally.
    387 		 * So there is a window where another thread could see a whole
    388 		 * zeroed page past EOF, but that's life.
    389 		 */
    390 		daddr_t xlbn;
    391 		voff_t eoz;
    392 
    393 		aflags = ioflag & IO_SYNC ? B_SYNC : 0;
    394 		error = ulfs_balloc_range(ovp, length - 1, 1, cred, aflags);
    395 		if (error) {
    396 			lfs_reserve(fs, ovp, NULL,
    397 				    -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)));
    398 			goto errout;
    399 		}
    400 		xlbn = lfs_lblkno(fs, length);
    401 		size = lfs_blksize(fs, oip, xlbn);
    402 		eoz = MIN(lfs_lblktosize(fs, xlbn) + size, osize);
    403 		ubc_zerorange(&ovp->v_uobj, length, eoz - length,
    404 		    UBC_UNMAP_FLAG(ovp));
    405 		if (round_page(eoz) > round_page(length)) {
    406 			mutex_enter(ovp->v_interlock);
    407 			error = VOP_PUTPAGES(ovp, round_page(length),
    408 			    round_page(eoz),
    409 			    PGO_CLEANIT | PGO_DEACTIVATE |
    410 			    ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
    411 			if (error) {
    412 				lfs_reserve(fs, ovp, NULL,
    413 					    -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)));
    414 				goto errout;
    415 			}
    416 		}
    417 	}
    418 
    419 	genfs_node_wrlock(ovp);
    420 
    421 	oip->i_size = oip->i_ffs1_size = length;
    422 	uvm_vnp_setsize(ovp, length);
    423 
    424 	/*
    425 	 * Calculate index into inode's block list of
    426 	 * last direct and indirect blocks (if any)
    427 	 * which we want to keep.  Lastblock is -1 when
    428 	 * the file is truncated to 0.
    429 	 */
    430 	/* Avoid sign overflow - XXX assumes that off_t is a quad_t. */
    431 	if (length > QUAD_MAX - lfs_sb_getbsize(fs))
    432 		lastblock = lfs_lblkno(fs, QUAD_MAX - lfs_sb_getbsize(fs));
    433 	else
    434 		lastblock = lfs_lblkno(fs, length + lfs_sb_getbsize(fs) - 1) - 1;
    435 	lastiblock[SINGLE] = lastblock - ULFS_NDADDR;
    436 	lastiblock[DOUBLE] = lastiblock[SINGLE] - LFS_NINDIR(fs);
    437 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - LFS_NINDIR(fs) * LFS_NINDIR(fs);
    438 	nblocks = lfs_btofsb(fs, lfs_sb_getbsize(fs));
    439 	/*
    440 	 * Record changed file and block pointers before we start
    441 	 * freeing blocks.  lastiblock values are also normalized to -1
    442 	 * for calls to lfs_indirtrunc below.
    443 	 */
    444 	memcpy((void *)newblks, (void *)&oip->i_ffs1_db[0], sizeof newblks);
    445 	for (level = TRIPLE; level >= SINGLE; level--)
    446 		if (lastiblock[level] < 0) {
    447 			newblks[ULFS_NDADDR+level] = 0;
    448 			lastiblock[level] = -1;
    449 		}
    450 	for (i = ULFS_NDADDR - 1; i > lastblock; i--)
    451 		newblks[i] = 0;
    452 
    453 	oip->i_size = oip->i_ffs1_size = osize;
    454 	error = lfs_vtruncbuf(ovp, lastblock + 1, false, 0);
    455 	if (error && !allerror)
    456 		allerror = error;
    457 
    458 	/*
    459 	 * Indirect blocks first.
    460 	 */
    461 	indir_lbn[SINGLE] = -ULFS_NDADDR;
    462 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - LFS_NINDIR(fs) - 1;
    463 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - LFS_NINDIR(fs) * LFS_NINDIR(fs) - 1;
    464 	for (level = TRIPLE; level >= SINGLE; level--) {
    465 		bn = oip->i_ffs1_ib[level];
    466 		if (bn != 0) {
    467 			error = lfs_indirtrunc(oip, indir_lbn[level],
    468 					       bn, lastiblock[level],
    469 					       level, &count, &rcount,
    470 					       &lastseg, &bc);
    471 			if (error)
    472 				allerror = error;
    473 			real_released += rcount;
    474 			blocksreleased += count;
    475 			if (lastiblock[level] < 0) {
    476 				if (oip->i_ffs1_ib[level] > 0)
    477 					real_released += nblocks;
    478 				blocksreleased += nblocks;
    479 				oip->i_ffs1_ib[level] = 0;
    480 				lfs_blkfree(fs, oip, bn, lfs_sb_getbsize(fs),
    481 					    &lastseg, &bc);
    482         			lfs_deregister_block(ovp, bn);
    483 			}
    484 		}
    485 		if (lastiblock[level] >= 0)
    486 			goto done;
    487 	}
    488 
    489 	/*
    490 	 * All whole direct blocks or frags.
    491 	 */
    492 	for (i = ULFS_NDADDR - 1; i > lastblock; i--) {
    493 		long bsize, obsize;
    494 
    495 		bn = oip->i_ffs1_db[i];
    496 		if (bn == 0)
    497 			continue;
    498 		bsize = lfs_blksize(fs, oip, i);
    499 		if (oip->i_ffs1_db[i] > 0) {
    500 			/* Check for fragment size changes */
    501 			obsize = oip->i_lfs_fragsize[i];
    502 			real_released += lfs_btofsb(fs, obsize);
    503 			oip->i_lfs_fragsize[i] = 0;
    504 		} else
    505 			obsize = 0;
    506 		blocksreleased += lfs_btofsb(fs, bsize);
    507 		oip->i_ffs1_db[i] = 0;
    508 		lfs_blkfree(fs, oip, bn, obsize, &lastseg, &bc);
    509         	lfs_deregister_block(ovp, bn);
    510 	}
    511 	if (lastblock < 0)
    512 		goto done;
    513 
    514 	/*
    515 	 * Finally, look for a change in size of the
    516 	 * last direct block; release any frags.
    517 	 */
    518 	bn = oip->i_ffs1_db[lastblock];
    519 	if (bn != 0) {
    520 		long oldspace, newspace;
    521 #if 0
    522 		long olddspace;
    523 #endif
    524 
    525 		/*
    526 		 * Calculate amount of space we're giving
    527 		 * back as old block size minus new block size.
    528 		 */
    529 		oldspace = lfs_blksize(fs, oip, lastblock);
    530 #if 0
    531 		olddspace = oip->i_lfs_fragsize[lastblock];
    532 #endif
    533 
    534 		oip->i_size = oip->i_ffs1_size = length;
    535 		newspace = lfs_blksize(fs, oip, lastblock);
    536 		if (newspace == 0)
    537 			panic("itrunc: newspace");
    538 		if (oldspace - newspace > 0) {
    539 			blocksreleased += lfs_btofsb(fs, oldspace - newspace);
    540 		}
    541 #if 0
    542 		if (bn > 0 && olddspace - newspace > 0) {
    543 			/* No segment accounting here, just vnode */
    544 			real_released += lfs_btofsb(fs, olddspace - newspace);
    545 		}
    546 #endif
    547 	}
    548 
    549 done:
    550 	/* Finish segment accounting corrections */
    551 	lfs_update_seguse(fs, oip, lastseg, bc);
    552 #ifdef DIAGNOSTIC
    553 	for (level = SINGLE; level <= TRIPLE; level++)
    554 		if ((newblks[ULFS_NDADDR + level] == 0) !=
    555 		    ((oip->i_ffs1_ib[level]) == 0)) {
    556 			panic("lfs itrunc1");
    557 		}
    558 	for (i = 0; i < ULFS_NDADDR; i++)
    559 		if ((newblks[i] == 0) != (oip->i_ffs1_db[i] == 0)) {
    560 			panic("lfs itrunc2");
    561 		}
    562 	if (length == 0 &&
    563 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    564 		panic("lfs itrunc3");
    565 #endif /* DIAGNOSTIC */
    566 	/*
    567 	 * Put back the real size.
    568 	 */
    569 	oip->i_size = oip->i_ffs1_size = length;
    570 	oip->i_lfs_effnblks -= blocksreleased;
    571 	oip->i_ffs1_blocks -= real_released;
    572 	mutex_enter(&lfs_lock);
    573 	lfs_sb_addbfree(fs, blocksreleased);
    574 	mutex_exit(&lfs_lock);
    575 #ifdef DIAGNOSTIC
    576 	if (oip->i_size == 0 &&
    577 	    (oip->i_ffs1_blocks != 0 || oip->i_lfs_effnblks != 0)) {
    578 		printf("lfs_truncate: truncate to 0 but %d blks/%jd effblks\n",
    579 		       oip->i_ffs1_blocks, (intmax_t)oip->i_lfs_effnblks);
    580 		panic("lfs_truncate: persistent blocks");
    581 	}
    582 #endif
    583 
    584 	/*
    585 	 * If we truncated to zero, take us off the paging queue.
    586 	 */
    587 	mutex_enter(&lfs_lock);
    588 	if (oip->i_size == 0 && oip->i_flags & IN_PAGING) {
    589 		oip->i_flags &= ~IN_PAGING;
    590 		TAILQ_REMOVE(&fs->lfs_pchainhd, oip, i_lfs_pchain);
    591 	}
    592 	mutex_exit(&lfs_lock);
    593 
    594 	oip->i_flag |= IN_CHANGE;
    595 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    596 	(void) lfs_chkdq(oip, -blocksreleased, NOCRED, 0);
    597 #endif
    598 	lfs_reserve(fs, ovp, NULL,
    599 	    -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)));
    600 	genfs_node_unlock(ovp);
    601   errout:
    602 	oip->i_lfs_hiblk = lfs_lblkno(fs, oip->i_size + lfs_sb_getbsize(fs) - 1) - 1;
    603 	if (ovp != fs->lfs_ivnode)
    604 		lfs_segunlock(fs);
    605 	return (allerror ? allerror : error);
    606 }
    607 
    608 /* Update segment and avail usage information when removing a block. */
    609 static int
    610 lfs_blkfree(struct lfs *fs, struct inode *ip, daddr_t daddr,
    611 	    size_t bsize, long *lastseg, size_t *num)
    612 {
    613 	long seg;
    614 	int error = 0;
    615 
    616 	ASSERT_SEGLOCK(fs);
    617 	bsize = lfs_fragroundup(fs, bsize);
    618 	if (daddr > 0) {
    619 		if (*lastseg != (seg = lfs_dtosn(fs, daddr))) {
    620 			error = lfs_update_seguse(fs, ip, *lastseg, *num);
    621 			*num = bsize;
    622 			*lastseg = seg;
    623 		} else
    624 			*num += bsize;
    625 	}
    626 
    627 	return error;
    628 }
    629 
    630 /* Finish the accounting updates for a segment. */
    631 static int
    632 lfs_update_seguse(struct lfs *fs, struct inode *ip, long lastseg, size_t num)
    633 {
    634 	struct segdelta *sd;
    635 
    636 	ASSERT_SEGLOCK(fs);
    637 	if (lastseg < 0 || num == 0)
    638 		return 0;
    639 
    640 	LIST_FOREACH(sd, &ip->i_lfs_segdhd, list)
    641 		if (sd->segnum == lastseg)
    642 			break;
    643 	if (sd == NULL) {
    644 		sd = malloc(sizeof(*sd), M_SEGMENT, M_WAITOK);
    645 		sd->segnum = lastseg;
    646 		sd->num = 0;
    647 		LIST_INSERT_HEAD(&ip->i_lfs_segdhd, sd, list);
    648 	}
    649 	sd->num += num;
    650 
    651 	return 0;
    652 }
    653 
    654 static void
    655 lfs_finalize_seguse(struct lfs *fs, void *v)
    656 {
    657 	SEGUSE *sup;
    658 	struct buf *bp;
    659 	struct segdelta *sd;
    660 	LIST_HEAD(, segdelta) *hd = v;
    661 
    662 	ASSERT_SEGLOCK(fs);
    663 	while((sd = LIST_FIRST(hd)) != NULL) {
    664 		LIST_REMOVE(sd, list);
    665 		LFS_SEGENTRY(sup, fs, sd->segnum, bp);
    666 		if (sd->num > sup->su_nbytes) {
    667 			printf("lfs_finalize_seguse: segment %ld short by %ld\n",
    668 				sd->segnum, (long)(sd->num - sup->su_nbytes));
    669 			panic("lfs_finalize_seguse: negative bytes");
    670 			sup->su_nbytes = sd->num;
    671 		}
    672 		sup->su_nbytes -= sd->num;
    673 		LFS_WRITESEGENTRY(sup, fs, sd->segnum, bp);
    674 		free(sd, M_SEGMENT);
    675 	}
    676 }
    677 
    678 /* Finish the accounting updates for a segment. */
    679 void
    680 lfs_finalize_ino_seguse(struct lfs *fs, struct inode *ip)
    681 {
    682 	ASSERT_SEGLOCK(fs);
    683 	lfs_finalize_seguse(fs, &ip->i_lfs_segdhd);
    684 }
    685 
    686 /* Finish the accounting updates for a segment. */
    687 void
    688 lfs_finalize_fs_seguse(struct lfs *fs)
    689 {
    690 	ASSERT_SEGLOCK(fs);
    691 	lfs_finalize_seguse(fs, &fs->lfs_segdhd);
    692 }
    693 
    694 /*
    695  * Release blocks associated with the inode ip and stored in the indirect
    696  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    697  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    698  * and recursive calls to indirtrunc must be used to cleanse other indirect
    699  * blocks.
    700  *
    701  * NB: triple indirect blocks are untested.
    702  */
    703 static int
    704 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
    705 	       daddr_t lastbn, int level, daddr_t *countp,
    706 	       daddr_t *rcountp, long *lastsegp, size_t *bcp)
    707 {
    708 	int i;
    709 	struct buf *bp;
    710 	struct lfs *fs = ip->i_lfs;
    711 	int32_t *bap;	/* XXX ondisk32 */
    712 	struct vnode *vp;
    713 	daddr_t nb, nlbn, last;
    714 	int32_t *copy = NULL;	/* XXX ondisk32 */
    715 	daddr_t blkcount, rblkcount, factor;
    716 	int nblocks;
    717 	daddr_t blocksreleased = 0, real_released = 0;
    718 	int error = 0, allerror = 0;
    719 
    720 	ASSERT_SEGLOCK(fs);
    721 	/*
    722 	 * Calculate index in current block of last
    723 	 * block to be kept.  -1 indicates the entire
    724 	 * block so we need not calculate the index.
    725 	 */
    726 	factor = 1;
    727 	for (i = SINGLE; i < level; i++)
    728 		factor *= LFS_NINDIR(fs);
    729 	last = lastbn;
    730 	if (lastbn > 0)
    731 		last /= factor;
    732 	nblocks = lfs_btofsb(fs, lfs_sb_getbsize(fs));
    733 	/*
    734 	 * Get buffer of block pointers, zero those entries corresponding
    735 	 * to blocks to be free'd, and update on disk copy first.  Since
    736 	 * double(triple) indirect before single(double) indirect, calls
    737 	 * to bmap on these blocks will fail.  However, we already have
    738 	 * the on disk address, so we have to set the b_blkno field
    739 	 * explicitly instead of letting bread do everything for us.
    740 	 */
    741 	vp = ITOV(ip);
    742 	bp = getblk(vp, lbn, lfs_sb_getbsize(fs), 0, 0);
    743 	if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
    744 		/* Braces must be here in case trace evaluates to nothing. */
    745 		trace(TR_BREADHIT, pack(vp, lfs_sb_getbsize(fs)), lbn);
    746 	} else {
    747 		trace(TR_BREADMISS, pack(vp, lfs_sb_getbsize(fs)), lbn);
    748 		curlwp->l_ru.ru_inblock++; /* pay for read */
    749 		bp->b_flags |= B_READ;
    750 		if (bp->b_bcount > bp->b_bufsize)
    751 			panic("lfs_indirtrunc: bad buffer size");
    752 		bp->b_blkno = LFS_FSBTODB(fs, dbn);
    753 		VOP_STRATEGY(vp, bp);
    754 		error = biowait(bp);
    755 	}
    756 	if (error) {
    757 		brelse(bp, 0);
    758 		*countp = *rcountp = 0;
    759 		return (error);
    760 	}
    761 
    762 	bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    763 	if (lastbn >= 0) {
    764 		copy = lfs_malloc(fs, lfs_sb_getbsize(fs), LFS_NB_IBLOCK);
    765 		memcpy((void *)copy, (void *)bap, lfs_sb_getbsize(fs));
    766 		memset((void *)&bap[last + 1], 0,
    767 		/* XXX ondisk32 */
    768 		  (u_int)(LFS_NINDIR(fs) - (last + 1)) * sizeof (int32_t));
    769 		error = VOP_BWRITE(bp->b_vp, bp);
    770 		if (error)
    771 			allerror = error;
    772 		bap = copy;
    773 	}
    774 
    775 	/*
    776 	 * Recursively free totally unused blocks.
    777 	 */
    778 	for (i = LFS_NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    779 	    i--, nlbn += factor) {
    780 		nb = bap[i];
    781 		if (nb == 0)
    782 			continue;
    783 		if (level > SINGLE) {
    784 			error = lfs_indirtrunc(ip, nlbn, nb,
    785 					       (daddr_t)-1, level - 1,
    786 					       &blkcount, &rblkcount,
    787 					       lastsegp, bcp);
    788 			if (error)
    789 				allerror = error;
    790 			blocksreleased += blkcount;
    791 			real_released += rblkcount;
    792 		}
    793 		lfs_blkfree(fs, ip, nb, lfs_sb_getbsize(fs), lastsegp, bcp);
    794 		if (bap[i] > 0)
    795 			real_released += nblocks;
    796 		blocksreleased += nblocks;
    797 	}
    798 
    799 	/*
    800 	 * Recursively free last partial block.
    801 	 */
    802 	if (level > SINGLE && lastbn >= 0) {
    803 		last = lastbn % factor;
    804 		nb = bap[i];
    805 		if (nb != 0) {
    806 			error = lfs_indirtrunc(ip, nlbn, nb,
    807 					       last, level - 1, &blkcount,
    808 					       &rblkcount, lastsegp, bcp);
    809 			if (error)
    810 				allerror = error;
    811 			real_released += rblkcount;
    812 			blocksreleased += blkcount;
    813 		}
    814 	}
    815 
    816 	if (copy != NULL) {
    817 		lfs_free(fs, copy, LFS_NB_IBLOCK);
    818 	} else {
    819 		mutex_enter(&bufcache_lock);
    820 		if (bp->b_oflags & BO_DELWRI) {
    821 			LFS_UNLOCK_BUF(bp);
    822 			lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
    823 			wakeup(&fs->lfs_availsleep);
    824 		}
    825 		brelsel(bp, BC_INVAL);
    826 		mutex_exit(&bufcache_lock);
    827 	}
    828 
    829 	*countp = blocksreleased;
    830 	*rcountp = real_released;
    831 	return (allerror);
    832 }
    833 
    834 /*
    835  * Destroy any in core blocks past the truncation length.
    836  * Inlined from vtruncbuf, so that lfs_avail could be updated.
    837  * We take the seglock to prevent cleaning from occurring while we are
    838  * invalidating blocks.
    839  */
    840 static int
    841 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, bool catch, int slptimeo)
    842 {
    843 	struct buf *bp, *nbp;
    844 	int error;
    845 	struct lfs *fs;
    846 	voff_t off;
    847 
    848 	off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
    849 	mutex_enter(vp->v_interlock);
    850 	error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
    851 	if (error)
    852 		return error;
    853 
    854 	fs = VTOI(vp)->i_lfs;
    855 
    856 	ASSERT_SEGLOCK(fs);
    857 
    858 	mutex_enter(&bufcache_lock);
    859 restart:
    860 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
    861 		nbp = LIST_NEXT(bp, b_vnbufs);
    862 		if (bp->b_lblkno < lbn)
    863 			continue;
    864 		error = bbusy(bp, catch, slptimeo, NULL);
    865 		if (error == EPASSTHROUGH)
    866 			goto restart;
    867 		if (error != 0) {
    868 			mutex_exit(&bufcache_lock);
    869 			return (error);
    870 		}
    871 		mutex_enter(bp->b_objlock);
    872 		if (bp->b_oflags & BO_DELWRI) {
    873 			bp->b_oflags &= ~BO_DELWRI;
    874 			lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
    875 			wakeup(&fs->lfs_availsleep);
    876 		}
    877 		mutex_exit(bp->b_objlock);
    878 		LFS_UNLOCK_BUF(bp);
    879 		brelsel(bp, BC_INVAL | BC_VFLUSH);
    880 	}
    881 
    882 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    883 		nbp = LIST_NEXT(bp, b_vnbufs);
    884 		if (bp->b_lblkno < lbn)
    885 			continue;
    886 		error = bbusy(bp, catch, slptimeo, NULL);
    887 		if (error == EPASSTHROUGH)
    888 			goto restart;
    889 		if (error != 0) {
    890 			mutex_exit(&bufcache_lock);
    891 			return (error);
    892 		}
    893 		mutex_enter(bp->b_objlock);
    894 		if (bp->b_oflags & BO_DELWRI) {
    895 			bp->b_oflags &= ~BO_DELWRI;
    896 			lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
    897 			wakeup(&fs->lfs_availsleep);
    898 		}
    899 		mutex_exit(bp->b_objlock);
    900 		LFS_UNLOCK_BUF(bp);
    901 		brelsel(bp, BC_INVAL | BC_VFLUSH);
    902 	}
    903 	mutex_exit(&bufcache_lock);
    904 
    905 	return (0);
    906 }
    907 
    908