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