Home | History | Annotate | Line # | Download | only in lfs
lfs_inode.c revision 1.97.2.1
      1 /*	$NetBSD: lfs_inode.c,v 1.97.2.1 2005/10/20 03:00:30 yamt 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.97.2.1 2005/10/20 03:00:30 yamt 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/proc.h>
     80 #include <sys/file.h>
     81 #include <sys/buf.h>
     82 #include <sys/vnode.h>
     83 #include <sys/kernel.h>
     84 #include <sys/trace.h>
     85 #include <sys/resourcevar.h>
     86 
     87 #include <ufs/ufs/quota.h>
     88 #include <ufs/ufs/inode.h>
     89 #include <ufs/ufs/ufsmount.h>
     90 #include <ufs/ufs/ufs_extern.h>
     91 
     92 #include <ufs/lfs/lfs.h>
     93 #include <ufs/lfs/lfs_extern.h>
     94 
     95 static int lfs_update_seguse(struct lfs *, long, size_t);
     96 static int lfs_indirtrunc (struct inode *, daddr_t, daddr_t,
     97 			   daddr_t, int, long *, long *, long *, size_t *,
     98 			   struct proc *);
     99 static int lfs_blkfree (struct lfs *, daddr_t, size_t, long *, size_t *);
    100 static int lfs_vtruncbuf(struct vnode *, daddr_t, int, int);
    101 
    102 /* Search a block for a specific dinode. */
    103 struct ufs1_dinode *
    104 lfs_ifind(struct lfs *fs, ino_t ino, struct buf *bp)
    105 {
    106 	struct ufs1_dinode *dip = (struct ufs1_dinode *)bp->b_data;
    107 	struct ufs1_dinode *ldip, *fin;
    108 
    109 	ASSERT_NO_SEGLOCK(fs);
    110 	/*
    111 	 * Read the inode block backwards, since later versions of the
    112 	 * inode will supercede earlier ones.  Though it is unlikely, it is
    113 	 * possible that the same inode will appear in the same inode block.
    114 	 */
    115 	fin = dip + INOPB(fs);
    116 	for (ldip = fin - 1; ldip >= dip; --ldip)
    117 		if (ldip->di_inumber == ino)
    118 			return (ldip);
    119 
    120 	printf("searched %d entries\n", (int)(fin - dip));
    121 	printf("offset is 0x%x (seg %d)\n", fs->lfs_offset,
    122 	       dtosn(fs, fs->lfs_offset));
    123 	printf("block is 0x%llx (seg %lld)\n",
    124 	       (unsigned long long)dbtofsb(fs, bp->b_blkno),
    125 	       (long long)dtosn(fs, dbtofsb(fs, bp->b_blkno)));
    126 
    127 	return NULL;
    128 }
    129 
    130 int
    131 lfs_update(struct vnode *vp, const struct timespec *acc,
    132     const struct timespec *mod, int updflags)
    133 {
    134 	struct inode *ip;
    135 	struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
    136 	int s;
    137 	int flags;
    138 
    139 	ASSERT_NO_SEGLOCK(fs);
    140 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    141 		return (0);
    142 	ip = VTOI(vp);
    143 
    144 	/*
    145 	 * If we are called from vinvalbuf, and the file's blocks have
    146 	 * already been scheduled for writing, but the writes have not
    147 	 * yet completed, lfs_vflush will not be called, and vinvalbuf
    148 	 * will cause a panic.	So, we must wait until any pending write
    149 	 * for our inode completes, if we are called with UPDATE_WAIT set.
    150 	 */
    151 	s = splbio();
    152 	simple_lock(&vp->v_interlock);
    153 	while ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT &&
    154 	    WRITEINPROG(vp)) {
    155 		DLOG((DLOG_SEG, "lfs_update: sleeping on ino %d"
    156 		      " (in progress)\n", ip->i_number));
    157 		ltsleep(vp, (PRIBIO+1), "lfs_update", 0, &vp->v_interlock);
    158 	}
    159 	simple_unlock(&vp->v_interlock);
    160 	splx(s);
    161 	LFS_ITIMES(ip, acc, mod, NULL);
    162 	if (updflags & UPDATE_CLOSE)
    163 		flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING);
    164 	else
    165 		flags = ip->i_flag & (IN_MODIFIED | IN_CLEANING);
    166 	if (flags == 0)
    167 		return (0);
    168 
    169 	/* If sync, push back the vnode and any dirty blocks it may have. */
    170 	if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT) {
    171 		/* Avoid flushing VDIROP. */
    172 		simple_lock(&fs->lfs_interlock);
    173 		++fs->lfs_diropwait;
    174 		while (vp->v_flag & VDIROP) {
    175 			DLOG((DLOG_DIROP, "lfs_update: sleeping on inode %d"
    176 			      " (dirops)\n", ip->i_number));
    177 			DLOG((DLOG_DIROP, "lfs_update: vflags 0x%x, iflags"
    178 			      " 0x%x\n", vp->v_flag, ip->i_flag));
    179 			if (fs->lfs_dirops == 0)
    180 				lfs_flush_fs(fs, SEGM_SYNC);
    181 			else
    182 				ltsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync",
    183 					0, &fs->lfs_interlock);
    184 			/* XXX KS - by falling out here, are we writing the vn
    185 			twice? */
    186 		}
    187 		--fs->lfs_diropwait;
    188 		simple_unlock(&fs->lfs_interlock);
    189 		return lfs_vflush(vp);
    190 	}
    191 	return 0;
    192 }
    193 
    194 #define	SINGLE	0	/* index of single indirect block */
    195 #define	DOUBLE	1	/* index of double indirect block */
    196 #define	TRIPLE	2	/* index of triple indirect block */
    197 /*
    198  * Truncate the inode oip to at most length size, freeing the
    199  * disk blocks.
    200  */
    201 /* VOP_BWRITE 1 + NIADDR + UFS_BALLOC == 2 + 2*NIADDR times */
    202 
    203 int
    204 lfs_truncate(struct vnode *ovp, off_t length, int ioflag,
    205     struct ucred *cred, struct proc *p)
    206 {
    207 	struct genfs_node *gp = VTOG(ovp);
    208 	daddr_t lastblock;
    209 	struct inode *oip = VTOI(ovp);
    210 	daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
    211 	/* XXX ondisk32 */
    212 	int32_t newblks[NDADDR + NIADDR];
    213 	struct lfs *fs;
    214 	struct buf *bp;
    215 	int offset, size, level;
    216 	long count, rcount, blocksreleased = 0, real_released = 0;
    217 	int i, nblocks;
    218 	int aflags, error, allerror = 0;
    219 	off_t osize;
    220 	long lastseg;
    221 	size_t bc;
    222 	int obufsize, odb;
    223 	int usepc;
    224 	struct ufsmount *ump = oip->i_ump;
    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 		return (0);
    234 
    235 	if (ovp->v_type == VLNK &&
    236 	    (oip->i_size < ump->um_maxsymlinklen ||
    237 	     (ump->um_maxsymlinklen == 0 &&
    238 	      oip->i_ffs1_blocks == 0))) {
    239 #ifdef DIAGNOSTIC
    240 		if (length != 0)
    241 			panic("lfs_truncate: partial truncate of symlink");
    242 #endif
    243 		memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size);
    244 		oip->i_size = oip->i_ffs1_size = 0;
    245 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    246 		return (UFS_UPDATE(ovp, NULL, NULL, 0));
    247 	}
    248 	if (oip->i_size == length) {
    249 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    250 		return (UFS_UPDATE(ovp, NULL, NULL, 0));
    251 	}
    252 #ifdef QUOTA
    253 	if ((error = getinoquota(oip)) != 0)
    254 		return (error);
    255 #endif
    256 	fs = oip->i_lfs;
    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 > ump->um_maxfilesize)
    269 			return (EFBIG);
    270 		aflags = B_CLRBUF;
    271 		if (ioflag & IO_SYNC)
    272 			aflags |= B_SYNC;
    273 		if (usepc) {
    274 			if (lblkno(fs, osize) < NDADDR &&
    275 			    lblkno(fs, osize) != lblkno(fs, length) &&
    276 			    blkroundup(fs, osize) != osize) {
    277 				off_t eob;
    278 
    279 				eob = blkroundup(fs, osize);
    280 				error = ufs_balloc_range(ovp, osize,
    281 				    eob - osize, cred, aflags);
    282 				if (error)
    283 					return error;
    284 				if (ioflag & IO_SYNC) {
    285 					ovp->v_size = eob;
    286 					simple_lock(&ovp->v_interlock);
    287 					VOP_PUTPAGES(ovp,
    288 					    trunc_page(osize & fs->lfs_bmask),
    289 					    round_page(eob),
    290 					    PGO_CLEANIT | PGO_SYNCIO);
    291 				}
    292 			}
    293 			error = ufs_balloc_range(ovp, length - 1, 1, cred,
    294 						 aflags);
    295 			if (error) {
    296 				(void) UFS_TRUNCATE(ovp, osize,
    297 						    ioflag & IO_SYNC, cred, p);
    298 				return error;
    299 			}
    300 			uvm_vnp_setsize(ovp, length);
    301 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    302 			KASSERT(ovp->v_size == oip->i_size);
    303 			oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
    304 			return (UFS_UPDATE(ovp, NULL, NULL, 0));
    305 		} else {
    306 			error = lfs_reserve(fs, ovp, NULL,
    307 			    btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
    308 			if (error)
    309 				return (error);
    310 			error = UFS_BALLOC(ovp, length - 1, 1, cred,
    311 					   aflags, &bp);
    312 			lfs_reserve(fs, ovp, NULL,
    313 			    -btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
    314 			if (error)
    315 				return (error);
    316 			oip->i_ffs1_size = oip->i_size = length;
    317 			uvm_vnp_setsize(ovp, length);
    318 			(void) VOP_BWRITE(bp);
    319 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    320 			oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
    321 			return (UFS_UPDATE(ovp, NULL, NULL, 0));
    322 		}
    323 	}
    324 
    325 	if ((error = lfs_reserve(fs, ovp, NULL,
    326 	    btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift))) != 0)
    327 		return (error);
    328 
    329 	/*
    330 	 * Shorten the size of the file. If the file is not being
    331 	 * truncated to a block boundary, the contents of the
    332 	 * partial block following the end of the file must be
    333 	 * zero'ed in case it ever becomes accessible again because
    334 	 * of subsequent file growth. Directories however are not
    335 	 * zero'ed as they should grow back initialized to empty.
    336 	 */
    337 	offset = blkoff(fs, length);
    338 	lastseg = -1;
    339 	bc = 0;
    340 
    341 	if (ovp != fs->lfs_ivnode)
    342 		lfs_seglock(fs, SEGM_PROT);
    343 	if (offset == 0) {
    344 		oip->i_size = oip->i_ffs1_size = length;
    345 	} else if (!usepc) {
    346 		lbn = lblkno(fs, length);
    347 		aflags = B_CLRBUF;
    348 		if (ioflag & IO_SYNC)
    349 			aflags |= B_SYNC;
    350 		error = UFS_BALLOC(ovp, length - 1, 1, cred, aflags, &bp);
    351 		if (error) {
    352 			lfs_reserve(fs, ovp, NULL,
    353 			    -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
    354 			goto errout;
    355 		}
    356 		obufsize = bp->b_bufsize;
    357 		odb = btofsb(fs, bp->b_bcount);
    358 		oip->i_size = oip->i_ffs1_size = length;
    359 		size = blksize(fs, oip, lbn);
    360 		if (ovp->v_type != VDIR)
    361 			memset((char *)bp->b_data + offset, 0,
    362 			       (u_int)(size - offset));
    363 		allocbuf(bp, size, 1);
    364 		if ((bp->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED) {
    365 			simple_lock(&lfs_subsys_lock);
    366 			locked_queue_bytes -= obufsize - bp->b_bufsize;
    367 			simple_unlock(&lfs_subsys_lock);
    368 		}
    369 		if (bp->b_flags & B_DELWRI)
    370 			fs->lfs_avail += odb - btofsb(fs, size);
    371 		(void) VOP_BWRITE(bp);
    372 	} else { /* vp->v_type == VREG && length < osize && offset != 0 */
    373 		/*
    374 		 * When truncating a regular file down to a non-block-aligned
    375 		 * size, we must zero the part of last block which is past
    376 		 * the new EOF.  We must synchronously flush the zeroed pages
    377 		 * to disk since the new pages will be invalidated as soon
    378 		 * as we inform the VM system of the new, smaller size.
    379 		 * We must do this before acquiring the GLOCK, since fetching
    380 		 * the pages will acquire the GLOCK internally.
    381 		 * So there is a window where another thread could see a whole
    382 		 * zeroed page past EOF, but that's life.
    383 		 */
    384 		daddr_t xlbn;
    385 		voff_t eoz;
    386 
    387 		aflags = ioflag & IO_SYNC ? B_SYNC : 0;
    388 		error = ufs_balloc_range(ovp, length - 1, 1, cred, aflags);
    389 		if (error) {
    390 			lfs_reserve(fs, ovp, NULL,
    391 				    -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
    392 			goto errout;
    393 		}
    394 		xlbn = lblkno(fs, length);
    395 		size = blksize(fs, oip, xlbn);
    396 		eoz = MIN(lblktosize(fs, xlbn) + size, osize);
    397 		uvm_vnp_zerorange(ovp, length, eoz - length);
    398 		if (round_page(eoz) > round_page(length)) {
    399 			simple_lock(&ovp->v_interlock);
    400 			error = VOP_PUTPAGES(ovp, round_page(length),
    401 			    round_page(eoz),
    402 			    PGO_CLEANIT | PGO_DEACTIVATE |
    403 			    ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
    404 			if (error) {
    405 				lfs_reserve(fs, ovp, NULL,
    406 					    -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
    407 				goto errout;
    408 			}
    409 		}
    410 	}
    411 
    412 	lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    413 
    414 	oip->i_size = oip->i_ffs1_size = length;
    415 	uvm_vnp_setsize(ovp, length);
    416 	/*
    417 	 * Calculate index into inode's block list of
    418 	 * last direct and indirect blocks (if any)
    419 	 * which we want to keep.  Lastblock is -1 when
    420 	 * the file is truncated to 0.
    421 	 */
    422 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1;
    423 	lastiblock[SINGLE] = lastblock - NDADDR;
    424 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    425 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    426 	nblocks = btofsb(fs, fs->lfs_bsize);
    427 	/*
    428 	 * Record changed file and block pointers before we start
    429 	 * freeing blocks.  lastiblock values are also normalized to -1
    430 	 * for calls to lfs_indirtrunc below.
    431 	 */
    432 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs1_db[0], sizeof newblks);
    433 	for (level = TRIPLE; level >= SINGLE; level--)
    434 		if (lastiblock[level] < 0) {
    435 			newblks[NDADDR+level] = 0;
    436 			lastiblock[level] = -1;
    437 		}
    438 	for (i = NDADDR - 1; i > lastblock; i--)
    439 		newblks[i] = 0;
    440 
    441 	oip->i_size = oip->i_ffs1_size = osize;
    442 	error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0);
    443 	if (error && !allerror)
    444 		allerror = error;
    445 
    446 	/*
    447 	 * Indirect blocks first.
    448 	 */
    449 	indir_lbn[SINGLE] = -NDADDR;
    450 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    451 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    452 	for (level = TRIPLE; level >= SINGLE; level--) {
    453 		bn = oip->i_ffs1_ib[level];
    454 		if (bn != 0) {
    455 			error = lfs_indirtrunc(oip, indir_lbn[level],
    456 					       bn, lastiblock[level],
    457 					       level, &count, &rcount,
    458 					       &lastseg, &bc, p);
    459 			if (error)
    460 				allerror = error;
    461 			real_released += rcount;
    462 			blocksreleased += count;
    463 			if (lastiblock[level] < 0) {
    464 				if (oip->i_ffs1_ib[level] > 0)
    465 					real_released += nblocks;
    466 				blocksreleased += nblocks;
    467 				oip->i_ffs1_ib[level] = 0;
    468 				lfs_blkfree(fs, bn, fs->lfs_bsize, &lastseg, &bc);
    469         			lfs_deregister_block(ovp, bn);
    470 			}
    471 		}
    472 		if (lastiblock[level] >= 0)
    473 			goto done;
    474 	}
    475 
    476 	/*
    477 	 * All whole direct blocks or frags.
    478 	 */
    479 	for (i = NDADDR - 1; i > lastblock; i--) {
    480 		long bsize, obsize;
    481 
    482 		bn = oip->i_ffs1_db[i];
    483 		if (bn == 0)
    484 			continue;
    485 		bsize = blksize(fs, oip, i);
    486 		if (oip->i_ffs1_db[i] > 0) {
    487 			/* Check for fragment size changes */
    488 			obsize = oip->i_lfs_fragsize[i];
    489 			real_released += btofsb(fs, obsize);
    490 			oip->i_lfs_fragsize[i] = 0;
    491 		} else
    492 			obsize = 0;
    493 		blocksreleased += btofsb(fs, bsize);
    494 		oip->i_ffs1_db[i] = 0;
    495 		lfs_blkfree(fs, bn, obsize, &lastseg, &bc);
    496         	lfs_deregister_block(ovp, bn);
    497 	}
    498 	if (lastblock < 0)
    499 		goto done;
    500 
    501 	/*
    502 	 * Finally, look for a change in size of the
    503 	 * last direct block; release any frags.
    504 	 */
    505 	bn = oip->i_ffs1_db[lastblock];
    506 	if (bn != 0) {
    507 		long oldspace, newspace;
    508 #if 0
    509 		long olddspace;
    510 #endif
    511 
    512 		/*
    513 		 * Calculate amount of space we're giving
    514 		 * back as old block size minus new block size.
    515 		 */
    516 		oldspace = blksize(fs, oip, lastblock);
    517 #if 0
    518 		olddspace = oip->i_lfs_fragsize[lastblock];
    519 #endif
    520 
    521 		oip->i_size = oip->i_ffs1_size = length;
    522 		newspace = blksize(fs, oip, lastblock);
    523 		if (newspace == 0)
    524 			panic("itrunc: newspace");
    525 		if (oldspace - newspace > 0) {
    526 			blocksreleased += btofsb(fs, oldspace - newspace);
    527 		}
    528 #if 0
    529 		if (bn > 0 && olddspace - newspace > 0) {
    530 			/* No segment accounting here, just vnode */
    531 			real_released += btofsb(fs, olddspace - newspace);
    532 		}
    533 #endif
    534 	}
    535 
    536 done:
    537 	/* Finish segment accounting corrections */
    538 	lfs_update_seguse(fs, lastseg, bc);
    539 #ifdef DIAGNOSTIC
    540 	for (level = SINGLE; level <= TRIPLE; level++)
    541 		if ((newblks[NDADDR + level] == 0) !=
    542 		    (oip->i_ffs1_ib[level]) == 0) {
    543 			panic("lfs itrunc1");
    544 		}
    545 	for (i = 0; i < NDADDR; i++)
    546 		if ((newblks[i] == 0) != (oip->i_ffs1_db[i] == 0)) {
    547 			panic("lfs itrunc2");
    548 		}
    549 	if (length == 0 &&
    550 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    551 		panic("lfs itrunc3");
    552 #endif /* DIAGNOSTIC */
    553 	/*
    554 	 * Put back the real size.
    555 	 */
    556 	oip->i_size = oip->i_ffs1_size = length;
    557 	oip->i_lfs_effnblks -= blocksreleased;
    558 	oip->i_ffs1_blocks -= real_released;
    559 	simple_lock(&fs->lfs_interlock);
    560 	fs->lfs_bfree += blocksreleased;
    561 	simple_unlock(&fs->lfs_interlock);
    562 #ifdef DIAGNOSTIC
    563 	if (oip->i_size == 0 &&
    564 	    (oip->i_ffs1_blocks != 0 || oip->i_lfs_effnblks != 0)) {
    565 		printf("lfs_truncate: truncate to 0 but %d blks/%d effblks\n",
    566 		       oip->i_ffs1_blocks, oip->i_lfs_effnblks);
    567 		panic("lfs_truncate: persistent blocks");
    568 	}
    569 #endif
    570 	oip->i_flag |= IN_CHANGE;
    571 #ifdef QUOTA
    572 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    573 #endif
    574 	lfs_reserve(fs, ovp, NULL,
    575 	    -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
    576 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    577   errout:
    578 	oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
    579 	if (ovp != fs->lfs_ivnode)
    580 		lfs_segunlock(fs);
    581 	return (allerror ? allerror : error);
    582 }
    583 
    584 /* Update segment and avail usage information when removing a block. */
    585 static int
    586 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
    587 	    size_t *num)
    588 {
    589 	long seg;
    590 	int error = 0;
    591 
    592 	ASSERT_SEGLOCK(fs);
    593 	bsize = fragroundup(fs, bsize);
    594 	if (daddr > 0) {
    595 		if (*lastseg != (seg = dtosn(fs, daddr))) {
    596 			error = lfs_update_seguse(fs, *lastseg, *num);
    597 			*num = bsize;
    598 			*lastseg = seg;
    599 		} else
    600 			*num += bsize;
    601 	}
    602 
    603 	return error;
    604 }
    605 
    606 /* Finish the accounting updates for a segment. */
    607 static int
    608 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
    609 {
    610 	SEGUSE *sup;
    611 	struct buf *bp;
    612 
    613 	ASSERT_SEGLOCK(fs);
    614 	if (lastseg < 0 || num == 0)
    615 		return 0;
    616 
    617 	LFS_SEGENTRY(sup, fs, lastseg, bp);
    618 	if (num > sup->su_nbytes) {
    619 		printf("lfs_truncate: segment %ld short by %ld\n",
    620 		       lastseg, (long)num - sup->su_nbytes);
    621 		panic("lfs_truncate: negative bytes");
    622 		sup->su_nbytes = num;
    623 	}
    624 	sup->su_nbytes -= num;
    625 	LFS_WRITESEGENTRY(sup, fs, lastseg, bp);
    626 
    627 	return 0;
    628 }
    629 
    630 /*
    631  * Release blocks associated with the inode ip and stored in the indirect
    632  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    633  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    634  * and recursive calls to indirtrunc must be used to cleanse other indirect
    635  * blocks.
    636  *
    637  * NB: triple indirect blocks are untested.
    638  */
    639 static int
    640 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
    641 	       daddr_t lastbn, int level, long *countp,
    642 	       long *rcountp, long *lastsegp, size_t *bcp, struct proc *p)
    643 {
    644 	int i;
    645 	struct buf *bp;
    646 	struct lfs *fs = ip->i_lfs;
    647 	int32_t *bap;	/* XXX ondisk32 */
    648 	struct vnode *vp;
    649 	daddr_t nb, nlbn, last;
    650 	int32_t *copy = NULL;	/* XXX ondisk32 */
    651 	long blkcount, rblkcount, factor;
    652 	int nblocks, blocksreleased = 0, real_released = 0;
    653 	int error = 0, allerror = 0;
    654 
    655 	ASSERT_SEGLOCK(fs);
    656 	/*
    657 	 * Calculate index in current block of last
    658 	 * block to be kept.  -1 indicates the entire
    659 	 * block so we need not calculate the index.
    660 	 */
    661 	factor = 1;
    662 	for (i = SINGLE; i < level; i++)
    663 		factor *= NINDIR(fs);
    664 	last = lastbn;
    665 	if (lastbn > 0)
    666 		last /= factor;
    667 	nblocks = btofsb(fs, fs->lfs_bsize);
    668 	/*
    669 	 * Get buffer of block pointers, zero those entries corresponding
    670 	 * to blocks to be free'd, and update on disk copy first.  Since
    671 	 * double(triple) indirect before single(double) indirect, calls
    672 	 * to bmap on these blocks will fail.  However, we already have
    673 	 * the on disk address, so we have to set the b_blkno field
    674 	 * explicitly instead of letting bread do everything for us.
    675 	 */
    676 	vp = ITOV(ip);
    677 	bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
    678 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    679 		/* Braces must be here in case trace evaluates to nothing. */
    680 		trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
    681 	} else {
    682 		trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
    683 		p->p_stats->p_ru.ru_inblock++;	/* pay for read */
    684 		bp->b_flags |= B_READ;
    685 		if (bp->b_bcount > bp->b_bufsize)
    686 			panic("lfs_indirtrunc: bad buffer size");
    687 		bp->b_blkno = fsbtodb(fs, dbn);
    688 		VOP_STRATEGY(vp, bp);
    689 		error = biowait(bp);
    690 	}
    691 	if (error) {
    692 		brelse(bp);
    693 		*countp = *rcountp = 0;
    694 		return (error);
    695 	}
    696 
    697 	bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    698 	if (lastbn >= 0) {
    699 		copy = (int32_t *)lfs_malloc(fs, fs->lfs_bsize, LFS_NB_IBLOCK);
    700 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
    701 		memset((caddr_t)&bap[last + 1], 0,
    702 		/* XXX ondisk32 */
    703 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t));
    704 		error = VOP_BWRITE(bp);
    705 		if (error)
    706 			allerror = error;
    707 		bap = copy;
    708 	}
    709 
    710 	/*
    711 	 * Recursively free totally unused blocks.
    712 	 */
    713 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    714 	    i--, nlbn += factor) {
    715 		nb = bap[i];
    716 		if (nb == 0)
    717 			continue;
    718 		if (level > SINGLE) {
    719 			error = lfs_indirtrunc(ip, nlbn, nb,
    720 					       (daddr_t)-1, level - 1,
    721 					       &blkcount, &rblkcount,
    722 					       lastsegp, bcp, p);
    723 			if (error)
    724 				allerror = error;
    725 			blocksreleased += blkcount;
    726 			real_released += rblkcount;
    727 		}
    728 		lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
    729 		if (bap[i] > 0)
    730 			real_released += nblocks;
    731 		blocksreleased += nblocks;
    732 	}
    733 
    734 	/*
    735 	 * Recursively free last partial block.
    736 	 */
    737 	if (level > SINGLE && lastbn >= 0) {
    738 		last = lastbn % factor;
    739 		nb = bap[i];
    740 		if (nb != 0) {
    741 			error = lfs_indirtrunc(ip, nlbn, nb,
    742 					       last, level - 1, &blkcount,
    743 					       &rblkcount, lastsegp, bcp, p);
    744 			if (error)
    745 				allerror = error;
    746 			real_released += rblkcount;
    747 			blocksreleased += blkcount;
    748 		}
    749 	}
    750 
    751 	if (copy != NULL) {
    752 		lfs_free(fs, copy, LFS_NB_IBLOCK);
    753 	} else {
    754 		if (bp->b_flags & B_DELWRI) {
    755 			LFS_UNLOCK_BUF(bp);
    756 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    757 			wakeup(&fs->lfs_avail);
    758 		}
    759 		bp->b_flags |= B_INVAL;
    760 		brelse(bp);
    761 	}
    762 
    763 	*countp = blocksreleased;
    764 	*rcountp = real_released;
    765 	return (allerror);
    766 }
    767 
    768 /*
    769  * Destroy any in core blocks past the truncation length.
    770  * Inlined from vtruncbuf, so that lfs_avail could be updated.
    771  * We take the seglock to prevent cleaning from occurring while we are
    772  * invalidating blocks.
    773  */
    774 static int
    775 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo)
    776 {
    777 	struct buf *bp, *nbp;
    778 	int s, error;
    779 	struct lfs *fs;
    780 	voff_t off;
    781 
    782 	off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
    783 	simple_lock(&vp->v_interlock);
    784 	error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
    785 	if (error)
    786 		return error;
    787 
    788 	fs = VTOI(vp)->i_lfs;
    789 	s = splbio();
    790 
    791 	ASSERT_SEGLOCK(fs);
    792 restart:
    793 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
    794 		nbp = LIST_NEXT(bp, b_vnbufs);
    795 		if (bp->b_lblkno < lbn)
    796 			continue;
    797 		simple_lock(&bp->b_interlock);
    798 		if (bp->b_flags & B_BUSY) {
    799 			bp->b_flags |= B_WANTED;
    800 			error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
    801 			    "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
    802 			if (error) {
    803 				splx(s);
    804 				return (error);
    805 			}
    806 			goto restart;
    807 		}
    808 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
    809 		if (bp->b_flags & B_DELWRI) {
    810 			bp->b_flags &= ~B_DELWRI;
    811 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    812 			wakeup(&fs->lfs_avail);
    813 		}
    814 		LFS_UNLOCK_BUF(bp);
    815 		simple_unlock(&bp->b_interlock);
    816 		brelse(bp);
    817 	}
    818 
    819 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    820 		nbp = LIST_NEXT(bp, b_vnbufs);
    821 		if (bp->b_lblkno < lbn)
    822 			continue;
    823 		simple_lock(&bp->b_interlock);
    824 		if (bp->b_flags & B_BUSY) {
    825 			bp->b_flags |= B_WANTED;
    826 			error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
    827 			    "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
    828 			if (error) {
    829 				splx(s);
    830 				return (error);
    831 			}
    832 			goto restart;
    833 		}
    834 		bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
    835 		if (bp->b_flags & B_DELWRI) {
    836 			bp->b_flags &= ~B_DELWRI;
    837 			fs->lfs_avail += btofsb(fs, bp->b_bcount);
    838 			wakeup(&fs->lfs_avail);
    839 		}
    840 		LFS_UNLOCK_BUF(bp);
    841 		simple_unlock(&bp->b_interlock);
    842 		brelse(bp);
    843 	}
    844 
    845 	splx(s);
    846 
    847 	return (0);
    848 }
    849 
    850