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