Home | History | Annotate | Line # | Download | only in lfs
lfs_inode.c revision 1.33
      1  1.33  perseant /*	$NetBSD: lfs_inode.c,v 1.33 2000/04/23 21:10:27 perseant Exp $	*/
      2   1.2       cgd 
      3  1.18  perseant /*-
      4  1.18  perseant  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.18  perseant  * All rights reserved.
      6  1.18  perseant  *
      7  1.18  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.18  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  1.18  perseant  *
     10  1.18  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.18  perseant  * modification, are permitted provided that the following conditions
     12  1.18  perseant  * are met:
     13  1.18  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.18  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.18  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.18  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.18  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.18  perseant  * 3. All advertising materials mentioning features or use of this software
     19  1.18  perseant  *    must display the following acknowledgement:
     20  1.18  perseant  *      This product includes software developed by the NetBSD
     21  1.18  perseant  *      Foundation, Inc. and its contributors.
     22  1.18  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.18  perseant  *    contributors may be used to endorse or promote products derived
     24  1.18  perseant  *    from this software without specific prior written permission.
     25  1.18  perseant  *
     26  1.18  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.18  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.18  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.18  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.18  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.18  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.18  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.18  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.18  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.18  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.18  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37  1.18  perseant  */
     38   1.1   mycroft /*
     39   1.1   mycroft  * Copyright (c) 1986, 1989, 1991, 1993
     40   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     41   1.1   mycroft  *
     42   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     43   1.1   mycroft  * modification, are permitted provided that the following conditions
     44   1.1   mycroft  * are met:
     45   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     46   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     47   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     50   1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     51   1.1   mycroft  *    must display the following acknowledgement:
     52   1.1   mycroft  *	This product includes software developed by the University of
     53   1.1   mycroft  *	California, Berkeley and its contributors.
     54   1.1   mycroft  * 4. Neither the name of the University nor the names of its contributors
     55   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     56   1.1   mycroft  *    without specific prior written permission.
     57   1.1   mycroft  *
     58   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68   1.1   mycroft  * SUCH DAMAGE.
     69   1.1   mycroft  *
     70  1.12      fvdl  *	@(#)lfs_inode.c	8.9 (Berkeley) 5/8/95
     71   1.1   mycroft  */
     72  1.13    scottr 
     73  1.14    scottr #if defined(_KERNEL) && !defined(_LKM)
     74  1.13    scottr #include "opt_quota.h"
     75  1.14    scottr #endif
     76   1.1   mycroft 
     77   1.1   mycroft #include <sys/param.h>
     78   1.1   mycroft #include <sys/systm.h>
     79   1.1   mycroft #include <sys/mount.h>
     80   1.1   mycroft #include <sys/proc.h>
     81   1.1   mycroft #include <sys/file.h>
     82   1.1   mycroft #include <sys/buf.h>
     83   1.1   mycroft #include <sys/vnode.h>
     84   1.1   mycroft #include <sys/kernel.h>
     85   1.1   mycroft #include <sys/malloc.h>
     86   1.1   mycroft 
     87   1.1   mycroft #include <vm/vm.h>
     88   1.1   mycroft 
     89   1.1   mycroft #include <ufs/ufs/quota.h>
     90   1.1   mycroft #include <ufs/ufs/inode.h>
     91   1.1   mycroft #include <ufs/ufs/ufsmount.h>
     92   1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     93   1.1   mycroft 
     94   1.1   mycroft #include <ufs/lfs/lfs.h>
     95   1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     96   1.1   mycroft 
     97  1.21  perseant static int lfs_vinvalbuf __P((struct vnode *, struct ucred *, struct proc *, ufs_daddr_t));
     98  1.21  perseant 
     99   1.1   mycroft /* Search a block for a specific dinode. */
    100   1.1   mycroft struct dinode *
    101  1.30  perseant lfs_ifind(fs, ino, bp)
    102   1.1   mycroft 	struct lfs *fs;
    103   1.1   mycroft 	ino_t ino;
    104  1.30  perseant 	struct buf *bp;
    105   1.1   mycroft {
    106  1.32  augustss 	int cnt;
    107  1.32  augustss 	struct dinode *dip = (struct dinode *)bp->b_data;
    108  1.32  augustss 	struct dinode *ldip;
    109  1.18  perseant 
    110   1.1   mycroft 	for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
    111   1.1   mycroft 		if (ldip->di_inumber == ino)
    112   1.1   mycroft 			return (ldip);
    113  1.18  perseant 
    114  1.30  perseant 	printf("offset is %d (seg %d)\n", fs->lfs_offset, datosn(fs,fs->lfs_offset));
    115  1.30  perseant 	printf("block is %d (seg %d)\n", bp->b_blkno, datosn(fs,bp->b_blkno));
    116   1.1   mycroft 	panic("lfs_ifind: dinode %u not found", ino);
    117   1.1   mycroft 	/* NOTREACHED */
    118   1.1   mycroft }
    119   1.1   mycroft 
    120   1.1   mycroft int
    121   1.4  christos lfs_update(v)
    122   1.4  christos 	void *v;
    123   1.4  christos {
    124   1.1   mycroft 	struct vop_update_args /* {
    125  1.18  perseant 				  struct vnode *a_vp;
    126  1.18  perseant 				  struct timespec *a_access;
    127  1.18  perseant 				  struct timespec *a_modify;
    128  1.18  perseant 				  int a_waitfor;
    129  1.18  perseant 				  } */ *ap = v;
    130   1.1   mycroft 	struct inode *ip;
    131  1.12      fvdl 	struct vnode *vp = ap->a_vp;
    132  1.18  perseant 	int mod, oflag;
    133   1.6   mycroft 	struct timespec ts;
    134  1.25  perseant 	struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
    135  1.18  perseant 
    136  1.12      fvdl 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    137   1.1   mycroft 		return (0);
    138  1.12      fvdl 	ip = VTOI(vp);
    139  1.18  perseant 
    140  1.18  perseant 	/*
    141  1.18  perseant 	 * If we are called from vinvalbuf, and the file's blocks have
    142  1.18  perseant 	 * already been scheduled for writing, but the writes have not
    143  1.18  perseant 	 * yet completed, lfs_vflush will not be called, and vinvalbuf
    144  1.18  perseant 	 * will cause a panic.  So, we must wait until any pending write
    145  1.18  perseant 	 * for our inode completes, if we are called with LFS_SYNC set.
    146  1.18  perseant 	 */
    147  1.18  perseant 	while((ap->a_waitfor & LFS_SYNC) && WRITEINPROG(vp)) {
    148  1.23  perseant #ifdef DEBUG_LFS
    149  1.25  perseant 		printf("lfs_update: sleeping on inode %d (in-progress)\n",ip->i_number);
    150  1.23  perseant #endif
    151  1.18  perseant 		tsleep(vp, (PRIBIO+1), "lfs_update", 0);
    152  1.18  perseant 	}
    153   1.6   mycroft 	mod = ip->i_flag & IN_MODIFIED;
    154  1.18  perseant 	oflag = ip->i_flag;
    155   1.6   mycroft 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    156  1.18  perseant 	LFS_ITIMES(ip,
    157  1.18  perseant 		   ap->a_access ? ap->a_access : &ts,
    158  1.18  perseant 		   ap->a_modify ? ap->a_modify : &ts, &ts);
    159  1.18  perseant 	if (!mod && (ip->i_flag & IN_MODIFIED))
    160   1.6   mycroft 		ip->i_lfs->lfs_uinodes++;
    161  1.18  perseant 	if ((ip->i_flag & (IN_MODIFIED|IN_CLEANING)) == 0) {
    162   1.1   mycroft 		return (0);
    163  1.18  perseant 	}
    164  1.18  perseant 
    165   1.1   mycroft 	/* If sync, push back the vnode and any dirty blocks it may have. */
    166  1.25  perseant 	if(ap->a_waitfor & LFS_SYNC) {
    167  1.25  perseant 		/* Avoid flushing VDIROP. */
    168  1.25  perseant 		++fs->lfs_diropwait;
    169  1.25  perseant 		while(vp->v_flag & VDIROP) {
    170  1.25  perseant #ifdef DEBUG_LFS
    171  1.25  perseant 			printf("lfs_update: sleeping on inode %d (dirops)\n",ip->i_number);
    172  1.25  perseant #endif
    173  1.25  perseant 			if(fs->lfs_dirops==0)
    174  1.27  perseant 				lfs_flush_fs(vp->v_mount,SEGM_SYNC);
    175  1.25  perseant 			else
    176  1.25  perseant 				tsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync", 0);
    177  1.25  perseant 			/* XXX KS - by falling out here, are we writing the vn
    178  1.25  perseant 			twice? */
    179  1.25  perseant 		}
    180  1.25  perseant 		--fs->lfs_diropwait;
    181  1.25  perseant 		return lfs_vflush(vp);
    182  1.25  perseant         }
    183  1.25  perseant 	return 0;
    184   1.1   mycroft }
    185   1.1   mycroft 
    186   1.1   mycroft /* Update segment usage information when removing a block. */
    187   1.1   mycroft #define UPDATE_SEGUSE \
    188   1.1   mycroft 	if (lastseg != -1) { \
    189   1.1   mycroft 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
    190  1.18  perseant 		if (num > sup->su_nbytes) { \
    191  1.26  perseant 			printf("lfs_truncate: negative bytes: segment %d short by %d\n", \
    192  1.26  perseant 			      lastseg, num - sup->su_nbytes); \
    193  1.26  perseant 			panic("lfs_truncate: negative bytes"); \
    194  1.18  perseant 		      sup->su_nbytes = 0; \
    195  1.18  perseant 		} else \
    196  1.12      fvdl 		sup->su_nbytes -= num; \
    197   1.1   mycroft 		e1 = VOP_BWRITE(sup_bp); \
    198  1.12      fvdl 		fragsreleased += numfrags(fs, num); \
    199   1.1   mycroft 	}
    200   1.1   mycroft 
    201  1.12      fvdl #define SEGDEC(S) { \
    202   1.1   mycroft 	if (daddr != 0) { \
    203   1.1   mycroft 		if (lastseg != (seg = datosn(fs, daddr))) { \
    204   1.1   mycroft 			UPDATE_SEGUSE; \
    205  1.12      fvdl 			num = (S); \
    206   1.1   mycroft 			lastseg = seg; \
    207   1.1   mycroft 		} else \
    208  1.12      fvdl 			num += (S); \
    209   1.1   mycroft 	} \
    210   1.1   mycroft }
    211   1.1   mycroft 
    212   1.1   mycroft /*
    213   1.1   mycroft  * Truncate the inode ip to at most length size.  Update segment usage
    214   1.1   mycroft  * table information.
    215   1.1   mycroft  */
    216   1.1   mycroft /* ARGSUSED */
    217   1.1   mycroft int
    218   1.4  christos lfs_truncate(v)
    219   1.4  christos 	void *v;
    220   1.4  christos {
    221   1.1   mycroft 	struct vop_truncate_args /* {
    222   1.1   mycroft 		struct vnode *a_vp;
    223   1.1   mycroft 		off_t a_length;
    224   1.1   mycroft 		int a_flags;
    225   1.1   mycroft 		struct ucred *a_cred;
    226   1.1   mycroft 		struct proc *a_p;
    227   1.4  christos 	} */ *ap = v;
    228  1.32  augustss 	struct indir *inp;
    229  1.32  augustss 	int i;
    230  1.33  perseant         int error, aflags;
    231  1.32  augustss 	ufs_daddr_t *daddrp;
    232  1.32  augustss 	struct vnode *vp = ap->a_vp;
    233   1.1   mycroft 	off_t length = ap->a_length;
    234   1.1   mycroft 	struct buf *bp, *sup_bp;
    235   1.1   mycroft 	struct ifile *ifp;
    236   1.1   mycroft 	struct inode *ip;
    237   1.1   mycroft 	struct lfs *fs;
    238   1.1   mycroft 	struct indir a[NIADDR + 2], a_end[NIADDR + 2];
    239   1.1   mycroft 	SEGUSE *sup;
    240  1.12      fvdl 	ufs_daddr_t daddr, lastblock, lbn, olastblock;
    241  1.12      fvdl 	ufs_daddr_t oldsize_lastblock, oldsize_newlast, newsize;
    242  1.12      fvdl 	long off, a_released, fragsreleased, i_released;
    243  1.28      fvdl 	int e1, e2, depth, lastseg, num, offset, seg, freesize, s;
    244  1.18  perseant 
    245  1.33  perseant 	if (length < 0)
    246  1.33  perseant 		return (EINVAL);
    247  1.33  perseant 
    248  1.18  perseant 	ip = VTOI(vp);
    249  1.33  perseant 	if (length == ip->i_ffs_size) /* XXX don't update times */
    250  1.33  perseant 		return 0;
    251   1.1   mycroft 
    252  1.31    bouyer 	if (vp->v_type == VLNK &&
    253  1.31    bouyer 	   (ip->i_ffs_size < vp->v_mount->mnt_maxsymlinklen ||
    254  1.31    bouyer 	     (vp->v_mount->mnt_maxsymlinklen == 0 &&
    255  1.31    bouyer 	      ip->i_din.ffs_din.di_blocks == 0))) {
    256   1.1   mycroft #ifdef DIAGNOSTIC
    257   1.1   mycroft 		if (length != 0)
    258   1.1   mycroft 			panic("lfs_truncate: partial truncate of symlink");
    259   1.1   mycroft #endif
    260   1.9    bouyer 		bzero((char *)&ip->i_ffs_shortlink, (u_int)ip->i_ffs_size);
    261   1.9    bouyer 		ip->i_ffs_size = 0;
    262   1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    263  1.17   mycroft 		return (VOP_UPDATE(vp, NULL, NULL, 0));
    264   1.1   mycroft 	}
    265  1.18  perseant 
    266   1.1   mycroft 	fs = ip->i_lfs;
    267  1.18  perseant 	lfs_imtime(fs);
    268  1.18  perseant 
    269   1.1   mycroft 	/* If length is larger than the file, just update the times. */
    270  1.33  perseant 	if (ip->i_ffs_size < length) {
    271  1.33  perseant 		if (length > fs->lfs_maxfilesize)
    272  1.33  perseant 			return (EFBIG);
    273  1.33  perseant 		/*
    274  1.33  perseant 		 * Allocate the new last block to ensure that any previously
    275  1.33  perseant 		 * existing fragments get extended.  (XXX Adding the new
    276  1.33  perseant 		 * block is not really necessary.)
    277  1.33  perseant 		 */
    278  1.33  perseant 		error = VOP_BALLOC(vp, length - 1, 1, ap->a_cred, aflags, &bp);
    279  1.33  perseant 		if (error)
    280  1.33  perseant  			return (error);
    281  1.33  perseant 		VOP_BWRITE(bp);
    282  1.33  perseant 		ip->i_ffs_size = length;
    283  1.33  perseant 		uvm_vnp_setsize(vp, length);
    284  1.33  perseant 		(void) uvm_vnp_uncache(vp);
    285   1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    286  1.17   mycroft 		return (VOP_UPDATE(vp, NULL, NULL, 0));
    287   1.1   mycroft 	}
    288  1.33  perseant 	uvm_vnp_setsize(vp, length);
    289   1.1   mycroft 
    290  1.18  perseant 	/*
    291  1.20  perseant 	 * Make sure no writes happen while we're truncating.
    292  1.20  perseant 	 * Otherwise, blocks which are accounted for on the inode
    293  1.20  perseant 	 * *and* which have been created for cleaning can coexist,
    294  1.20  perseant 	 * and cause us to overcount, and panic below.
    295  1.20  perseant 	 *
    296  1.20  perseant 	 * XXX KS - too restrictive?  Maybe only when cleaning?
    297  1.18  perseant 	 */
    298  1.29  perseant 	while(fs->lfs_seglock && fs->lfs_lockpid != ap->a_p->p_pid) {
    299  1.18  perseant 		tsleep(&fs->lfs_seglock, (PRIBIO+1), "lfs_truncate", 0);
    300  1.18  perseant 	}
    301  1.18  perseant 
    302   1.1   mycroft 	/*
    303   1.1   mycroft 	 * Calculate index into inode's block list of last direct and indirect
    304   1.1   mycroft 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
    305   1.1   mycroft 	 * file is truncated to 0.
    306   1.1   mycroft 	 */
    307   1.1   mycroft 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
    308   1.9    bouyer 	olastblock = lblkno(fs, ip->i_ffs_size + fs->lfs_bsize - 1) - 1;
    309   1.1   mycroft 
    310   1.1   mycroft 	/*
    311   1.1   mycroft 	 * Update the size of the file. If the file is not being truncated to
    312   1.1   mycroft 	 * a block boundry, the contents of the partial block following the end
    313  1.22  perseant 	 * must be zero'd in case it ever becomes accessible again
    314  1.12      fvdl 	 * because of subsequent file growth.  For this part of the code,
    315  1.12      fvdl 	 * oldsize_newlast refers to the old size of the new last block in the
    316  1.12      fvdl 	 * file.
    317   1.1   mycroft 	 */
    318   1.1   mycroft 	offset = blkoff(fs, length);
    319  1.12      fvdl 	lbn = lblkno(fs, length);
    320  1.12      fvdl 	oldsize_newlast = blksize(fs, ip, lbn);
    321  1.12      fvdl 
    322  1.12      fvdl 	/* Now set oldsize to the current size of the current last block */
    323  1.12      fvdl 	oldsize_lastblock = blksize(fs, ip, olastblock);
    324   1.1   mycroft 	if (offset == 0)
    325   1.9    bouyer 		ip->i_ffs_size = length;
    326   1.1   mycroft 	else {
    327   1.1   mycroft #ifdef QUOTA
    328   1.4  christos 		if ((e1 = getinoquota(ip)) != 0)
    329   1.1   mycroft 			return (e1);
    330   1.1   mycroft #endif
    331  1.15    bouyer 		if ((e1 = bread(vp, lbn, oldsize_newlast, NOCRED, &bp)) != 0) {
    332  1.18  perseant 			printf("lfs_truncate: bread: %d\n",e1);
    333  1.15    bouyer 			brelse(bp);
    334   1.1   mycroft 			return (e1);
    335  1.15    bouyer 		}
    336   1.9    bouyer 		ip->i_ffs_size = length;
    337  1.11       chs 		(void)uvm_vnp_uncache(vp);
    338  1.12      fvdl 		newsize = blksize(fs, ip, lbn);
    339  1.12      fvdl 		bzero((char *)bp->b_data + offset, (u_int)(newsize - offset));
    340  1.18  perseant #ifdef DEBUG
    341  1.18  perseant 		if(bp->b_flags & B_CALL)
    342  1.18  perseant 		    panic("Can't allocbuf malloced buffer!");
    343  1.18  perseant 		else
    344  1.18  perseant #endif
    345  1.18  perseant 			allocbuf(bp, newsize);
    346  1.18  perseant 		if(oldsize_newlast > newsize)
    347  1.18  perseant 			ip->i_ffs_blocks -= btodb(oldsize_newlast - newsize);
    348  1.18  perseant 		if ((e1 = VOP_BWRITE(bp)) != 0) {
    349  1.18  perseant 			printf("lfs_truncate: bwrite: %d\n",e1);
    350   1.1   mycroft 			return (e1);
    351  1.18  perseant 		}
    352   1.1   mycroft 	}
    353   1.1   mycroft 	/*
    354   1.1   mycroft 	 * Modify sup->su_nbyte counters for each deleted block; keep track
    355   1.9    bouyer 	 * of number of blocks removed for ip->i_ffs_blocks.
    356   1.1   mycroft 	 */
    357  1.12      fvdl 	fragsreleased = 0;
    358   1.1   mycroft 	num = 0;
    359   1.1   mycroft 	lastseg = -1;
    360   1.1   mycroft 
    361   1.1   mycroft 	for (lbn = olastblock; lbn >= lastblock;) {
    362   1.1   mycroft 		/* XXX use run length from bmap array to make this faster */
    363   1.1   mycroft 		ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
    364  1.12      fvdl 		if (lbn == olastblock) {
    365   1.1   mycroft 			for (i = NIADDR + 2; i--;)
    366   1.1   mycroft 				a_end[i] = a[i];
    367  1.12      fvdl 			freesize = oldsize_lastblock;
    368  1.12      fvdl 		} else
    369  1.12      fvdl 			freesize = fs->lfs_bsize;
    370   1.1   mycroft 		switch (depth) {
    371  1.18  perseant 		case 0:		/* Direct block. */
    372   1.9    bouyer 			daddr = ip->i_ffs_db[lbn];
    373  1.12      fvdl 			SEGDEC(freesize);
    374   1.9    bouyer 			ip->i_ffs_db[lbn] = 0;
    375   1.1   mycroft 			--lbn;
    376   1.1   mycroft 			break;
    377   1.1   mycroft #ifdef DIAGNOSTIC
    378  1.18  perseant 		case 1:		/* An indirect block. */
    379   1.1   mycroft 			panic("lfs_truncate: ufs_bmaparray returned depth 1");
    380   1.1   mycroft 			/* NOTREACHED */
    381   1.1   mycroft #endif
    382  1.18  perseant 		default:	/* Chain of indirect blocks. */
    383   1.1   mycroft 			inp = a + --depth;
    384   1.1   mycroft 			if (inp->in_off > 0 && lbn != lastblock) {
    385   1.1   mycroft 				lbn -= inp->in_off < lbn - lastblock ?
    386  1.18  perseant 					inp->in_off : lbn - lastblock;
    387   1.1   mycroft 				break;
    388   1.1   mycroft 			}
    389   1.1   mycroft 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
    390  1.18  perseant 			     --inp, --depth) {
    391   1.1   mycroft 				if (bread(vp,
    392  1.18  perseant 					  inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
    393   1.1   mycroft 					panic("lfs_truncate: bread bno %d",
    394  1.18  perseant 					      inp->in_lbn);
    395  1.12      fvdl 				daddrp = (ufs_daddr_t *)bp->b_data + inp->in_off;
    396   1.1   mycroft 				for (i = inp->in_off;
    397  1.18  perseant 				     i++ <= a_end[depth].in_off;) {
    398   1.1   mycroft 					daddr = *daddrp++;
    399  1.12      fvdl 					SEGDEC(freesize);
    400   1.1   mycroft 				}
    401   1.1   mycroft 				a_end[depth].in_off = NINDIR(fs) - 1;
    402   1.1   mycroft 				if (inp->in_off == 0)
    403   1.1   mycroft 					brelse (bp);
    404   1.1   mycroft 				else {
    405  1.12      fvdl 					bzero((ufs_daddr_t *)bp->b_data +
    406  1.18  perseant 					      inp->in_off, fs->lfs_bsize -
    407  1.18  perseant 					      inp->in_off * sizeof(ufs_daddr_t));
    408  1.18  perseant 					if ((e1 = VOP_BWRITE(bp)) != 0) {
    409  1.18  perseant 						printf("lfs_truncate: indir bwrite: %d\n",e1);
    410   1.1   mycroft 						return (e1);
    411  1.18  perseant 					}
    412   1.1   mycroft 				}
    413   1.1   mycroft 			}
    414   1.1   mycroft 			if (depth == 0 && a[1].in_off == 0) {
    415   1.1   mycroft 				off = a[0].in_off;
    416   1.9    bouyer 				daddr = ip->i_ffs_ib[off];
    417  1.12      fvdl 				SEGDEC(freesize);
    418   1.9    bouyer 				ip->i_ffs_ib[off] = 0;
    419   1.1   mycroft 			}
    420   1.1   mycroft 			if (lbn == lastblock || lbn <= NDADDR)
    421   1.1   mycroft 				--lbn;
    422   1.1   mycroft 			else {
    423   1.1   mycroft 				lbn -= NINDIR(fs);
    424   1.1   mycroft 				if (lbn < lastblock)
    425   1.1   mycroft 					lbn = lastblock;
    426   1.1   mycroft 			}
    427   1.1   mycroft 		}
    428   1.1   mycroft 	}
    429   1.1   mycroft 	UPDATE_SEGUSE;
    430  1.18  perseant 
    431   1.1   mycroft 	/* If truncating the file to 0, update the version number. */
    432   1.1   mycroft 	if (length == 0) {
    433   1.1   mycroft 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
    434   1.1   mycroft 		++ifp->if_version;
    435   1.1   mycroft 		(void) VOP_BWRITE(bp);
    436   1.1   mycroft 	}
    437   1.1   mycroft #ifdef DIAGNOSTIC
    438  1.12      fvdl 	if (ip->i_ffs_blocks < fragstodb(fs, fragsreleased)) {
    439  1.18  perseant 		panic("lfs_truncate: frag count < 0 (%d<%ld), ino %d\n",
    440  1.18  perseant 			    ip->i_ffs_blocks, fragstodb(fs, fragsreleased),
    441  1.18  perseant 			    ip->i_number);
    442  1.12      fvdl 		fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
    443   1.1   mycroft 	}
    444   1.1   mycroft #endif
    445  1.12      fvdl 	ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
    446  1.12      fvdl 	fs->lfs_bfree +=  fragstodb(fs, fragsreleased);
    447   1.1   mycroft 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    448   1.1   mycroft 	/*
    449   1.1   mycroft 	 * Traverse dirty block list counting number of dirty buffers
    450   1.1   mycroft 	 * that are being deleted out of the cache, so that the lfs_avail
    451   1.1   mycroft 	 * field can be updated.
    452   1.1   mycroft 	 */
    453   1.1   mycroft 	a_released = 0;
    454   1.1   mycroft 	i_released = 0;
    455  1.28      fvdl 
    456  1.28      fvdl 	s = splbio();
    457  1.18  perseant 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
    458  1.24  perseant 
    459  1.24  perseant 		/* XXX KS - Don't miscount if we're not truncating to zero. */
    460  1.24  perseant 		if(length>0 && !(bp->b_lblkno >= 0 && bp->b_lblkno > lastblock)
    461  1.24  perseant 		   && !(bp->b_lblkno < 0 && bp->b_lblkno < -lastblock-NIADDR))
    462  1.24  perseant 			continue;
    463  1.24  perseant 
    464   1.1   mycroft 		if (bp->b_flags & B_LOCKED) {
    465  1.12      fvdl 			a_released += numfrags(fs, bp->b_bcount);
    466   1.1   mycroft 			/*
    467   1.1   mycroft 			 * XXX
    468   1.1   mycroft 			 * When buffers are created in the cache, their block
    469   1.1   mycroft 			 * number is set equal to their logical block number.
    470   1.1   mycroft 			 * If that is still true, we are assuming that the
    471   1.1   mycroft 			 * blocks are new (not yet on disk) and weren't
    472   1.1   mycroft 			 * counted above.  However, there is a slight chance
    473   1.1   mycroft 			 * that a block's disk address is equal to its logical
    474   1.1   mycroft 			 * block number in which case, we'll get an overcounting
    475   1.1   mycroft 			 * here.
    476   1.1   mycroft 			 */
    477  1.18  perseant 			if (bp->b_blkno == bp->b_lblkno) {
    478  1.12      fvdl 				i_released += numfrags(fs, bp->b_bcount);
    479  1.18  perseant 			}
    480   1.1   mycroft 		}
    481  1.18  perseant 	}
    482  1.28      fvdl 	splx(s);
    483  1.12      fvdl 	fragsreleased = i_released;
    484   1.1   mycroft #ifdef DIAGNOSTIC
    485  1.12      fvdl 	if (fragsreleased > dbtofrags(fs, ip->i_ffs_blocks)) {
    486  1.18  perseant 		printf("lfs_inode: %ld frags released > %d in inode %d\n",
    487  1.18  perseant 		       fragsreleased, dbtofrags(fs, ip->i_ffs_blocks),
    488  1.18  perseant 		       ip->i_number);
    489  1.12      fvdl 		fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
    490   1.1   mycroft 	}
    491   1.1   mycroft #endif
    492  1.12      fvdl 	fs->lfs_bfree += fragstodb(fs, fragsreleased);
    493  1.12      fvdl 	ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
    494   1.1   mycroft #ifdef DIAGNOSTIC
    495  1.12      fvdl 	if (length == 0 && ip->i_ffs_blocks != 0) {
    496  1.18  perseant 		printf("lfs_inode: trunc to zero, but %d blocks left on inode %d\n",
    497  1.18  perseant 		       ip->i_ffs_blocks, ip->i_number);
    498  1.18  perseant 		panic("lfs_inode\n");
    499  1.12      fvdl 	}
    500   1.1   mycroft #endif
    501  1.12      fvdl 	fs->lfs_avail += fragstodb(fs, a_released);
    502  1.21  perseant 	if(length>0)
    503  1.22  perseant 		e1 = lfs_vinvalbuf(vp, ap->a_cred, ap->a_p, lastblock-1);
    504  1.21  perseant 	else
    505  1.21  perseant 		e1 = vinvalbuf(vp, 0, ap->a_cred, ap->a_p, 0, 0);
    506  1.17   mycroft 	e2 = VOP_UPDATE(vp, NULL, NULL, 0);
    507  1.18  perseant 	if(e1)
    508  1.18  perseant 		printf("lfs_truncate: vinvalbuf: %d\n",e1);
    509  1.18  perseant 	if(e2)
    510  1.18  perseant 		printf("lfs_truncate: update: %d\n",e2);
    511  1.18  perseant 
    512   1.1   mycroft 	return (e1 ? e1 : e2 ? e2 : 0);
    513  1.21  perseant }
    514  1.21  perseant 
    515  1.21  perseant /*
    516  1.21  perseant  * Get rid of blocks a la vinvalbuf; but only blocks that are of a higher
    517  1.21  perseant  * lblkno than the file size allows.
    518  1.21  perseant  */
    519  1.21  perseant int
    520  1.21  perseant lfs_vinvalbuf(vp, cred, p, maxblk)
    521  1.32  augustss 	struct vnode *vp;
    522  1.21  perseant 	struct ucred *cred;
    523  1.21  perseant 	struct proc *p;
    524  1.21  perseant 	ufs_daddr_t maxblk;
    525  1.21  perseant {
    526  1.32  augustss 	struct buf *bp;
    527  1.21  perseant 	struct buf *nbp, *blist;
    528  1.22  perseant 	int i, s, error, dirty;
    529  1.21  perseant 
    530  1.22  perseant       top:
    531  1.22  perseant 	dirty=0;
    532  1.21  perseant 	for (i=0;i<2;i++) {
    533  1.21  perseant 		if(i==0)
    534  1.21  perseant 			blist = vp->v_cleanblkhd.lh_first;
    535  1.21  perseant 		else /* i == 1 */
    536  1.21  perseant 			blist = vp->v_dirtyblkhd.lh_first;
    537  1.21  perseant 
    538  1.28      fvdl 		s = splbio();
    539  1.21  perseant 		for (bp = blist; bp; bp = nbp) {
    540  1.21  perseant 			nbp = bp->b_vnbufs.le_next;
    541  1.22  perseant 
    542  1.22  perseant 			if (bp->b_flags & B_GATHERED) {
    543  1.22  perseant 				error = tsleep(vp, PRIBIO+1, "lfs_vin2", 0);
    544  1.22  perseant 				splx(s);
    545  1.22  perseant 				if(error)
    546  1.22  perseant 					return error;
    547  1.22  perseant 				goto top;
    548  1.22  perseant 			}
    549  1.21  perseant 			if (bp->b_flags & B_BUSY) {
    550  1.21  perseant 				bp->b_flags |= B_WANTED;
    551  1.21  perseant 				error = tsleep((caddr_t)bp,
    552  1.21  perseant 					(PRIBIO + 1), "lfs_vinval", 0);
    553  1.28      fvdl 				if (error) {
    554  1.28      fvdl 					splx(s);
    555  1.21  perseant 					return (error);
    556  1.28      fvdl 				}
    557  1.22  perseant 				goto top;
    558  1.21  perseant 			}
    559  1.22  perseant 
    560  1.21  perseant 			bp->b_flags |= B_BUSY;
    561  1.21  perseant 			if((bp->b_lblkno >= 0 && bp->b_lblkno > maxblk)
    562  1.22  perseant 			   || (bp->b_lblkno < 0 && bp->b_lblkno < -maxblk-(NIADDR-1)))
    563  1.21  perseant 			{
    564  1.22  perseant 				bp->b_flags |= B_INVAL | B_VFLUSH;
    565  1.22  perseant 				if(bp->b_flags & B_CALL) {
    566  1.22  perseant 					lfs_freebuf(bp);
    567  1.22  perseant 				} else {
    568  1.22  perseant 					brelse(bp);
    569  1.22  perseant 				}
    570  1.22  perseant 				++dirty;
    571  1.22  perseant 			} else {
    572  1.22  perseant 				/*
    573  1.22  perseant 				 * This buffer is still on its free list.
    574  1.22  perseant 				 * So don't brelse, but wake up any sleepers.
    575  1.22  perseant 				 */
    576  1.22  perseant 				bp->b_flags &= ~B_BUSY;
    577  1.22  perseant 				if(bp->b_flags & B_WANTED) {
    578  1.22  perseant 					bp->b_flags &= ~(B_WANTED|B_AGE);
    579  1.22  perseant 					wakeup(bp);
    580  1.22  perseant 				}
    581  1.21  perseant 			}
    582  1.21  perseant 		}
    583  1.28      fvdl 		splx(s);
    584  1.21  perseant 	}
    585  1.22  perseant 	if(dirty)
    586  1.22  perseant 		goto top;
    587  1.21  perseant 	return (0);
    588   1.1   mycroft }
    589