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