Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.13.2.1
      1  1.13.2.1    bouyer /*	$NetBSD: lfs_balloc.c,v 1.13.2.1 2000/11/20 18:11:48 bouyer Exp $	*/
      2       1.2       cgd 
      3      1.11  perseant /*-
      4  1.13.2.1    bouyer  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5      1.11  perseant  * All rights reserved.
      6      1.11  perseant  *
      7      1.11  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8      1.11  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9      1.11  perseant  *
     10      1.11  perseant  * Redistribution and use in source and binary forms, with or without
     11      1.11  perseant  * modification, are permitted provided that the following conditions
     12      1.11  perseant  * are met:
     13      1.11  perseant  * 1. Redistributions of source code must retain the above copyright
     14      1.11  perseant  *    notice, this list of conditions and the following disclaimer.
     15      1.11  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.11  perseant  *    notice, this list of conditions and the following disclaimer in the
     17      1.11  perseant  *    documentation and/or other materials provided with the distribution.
     18      1.11  perseant  * 3. All advertising materials mentioning features or use of this software
     19      1.11  perseant  *    must display the following acknowledgement:
     20      1.11  perseant  *      This product includes software developed by the NetBSD
     21      1.11  perseant  *      Foundation, Inc. and its contributors.
     22      1.11  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.11  perseant  *    contributors may be used to endorse or promote products derived
     24      1.11  perseant  *    from this software without specific prior written permission.
     25      1.11  perseant  *
     26      1.11  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.11  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.11  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.11  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.11  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.11  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.11  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.11  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.11  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.11  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.11  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37      1.11  perseant  */
     38       1.1   mycroft /*
     39       1.1   mycroft  * Copyright (c) 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.5      fvdl  *	@(#)lfs_balloc.c	8.4 (Berkeley) 5/8/95
     71       1.1   mycroft  */
     72       1.8    scottr 
     73       1.9    scottr #if defined(_KERNEL) && !defined(_LKM)
     74       1.8    scottr #include "opt_quota.h"
     75       1.9    scottr #endif
     76       1.8    scottr 
     77       1.1   mycroft #include <sys/param.h>
     78       1.3  christos #include <sys/systm.h>
     79       1.1   mycroft #include <sys/buf.h>
     80       1.1   mycroft #include <sys/proc.h>
     81       1.1   mycroft #include <sys/vnode.h>
     82       1.1   mycroft #include <sys/mount.h>
     83       1.1   mycroft #include <sys/resourcevar.h>
     84       1.1   mycroft #include <sys/trace.h>
     85       1.1   mycroft 
     86       1.1   mycroft #include <miscfs/specfs/specdev.h>
     87       1.1   mycroft 
     88       1.1   mycroft #include <ufs/ufs/quota.h>
     89       1.1   mycroft #include <ufs/ufs/inode.h>
     90       1.1   mycroft #include <ufs/ufs/ufsmount.h>
     91       1.3  christos #include <ufs/ufs/ufs_extern.h>
     92       1.1   mycroft 
     93       1.1   mycroft #include <ufs/lfs/lfs.h>
     94       1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     95       1.1   mycroft 
     96       1.5      fvdl int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
     97       1.5      fvdl 
     98  1.13.2.1    bouyer /*
     99  1.13.2.1    bouyer  * Allocate a block, and to inode and filesystem block accounting for it
    100  1.13.2.1    bouyer  * and for any indirect blocks the may need to be created in order for
    101  1.13.2.1    bouyer  * this block to be created.
    102  1.13.2.1    bouyer  *
    103  1.13.2.1    bouyer  * Blocks which have never been accounted for (i.e., which "do not exist")
    104  1.13.2.1    bouyer  * have disk address 0, which is translated by ufs_bmap to the special value
    105  1.13.2.1    bouyer  * UNASSIGNED == -1, as in the historical UFS.
    106  1.13.2.1    bouyer  *
    107  1.13.2.1    bouyer  * Blocks which have been accounted for but which have not yet been written
    108  1.13.2.1    bouyer  * to disk are given the new special disk address UNWRITTEN == -2, so that
    109  1.13.2.1    bouyer  * they can be differentiated from completely new blocks.
    110  1.13.2.1    bouyer  */
    111  1.13.2.1    bouyer /* VOP_BWRITE NIADDR+2 times */
    112       1.1   mycroft int
    113  1.13.2.1    bouyer lfs_balloc(v)
    114  1.13.2.1    bouyer 	void *v;
    115  1.13.2.1    bouyer {
    116  1.13.2.1    bouyer 	struct vop_balloc_args /* {
    117  1.13.2.1    bouyer 		struct vnode *a_vp;
    118  1.13.2.1    bouyer 		off_t a_startoffset;
    119  1.13.2.1    bouyer 		int a_size;
    120  1.13.2.1    bouyer 		struct ucred *a_cred;
    121  1.13.2.1    bouyer 		int a_flags;
    122  1.13.2.1    bouyer 		struct buf *a_bpp;
    123  1.13.2.1    bouyer 	} */ *ap = v;
    124       1.1   mycroft 	struct vnode *vp;
    125       1.5      fvdl 	int offset;
    126       1.1   mycroft 	u_long iosize;
    127  1.13.2.1    bouyer 	daddr_t daddr, idaddr;
    128       1.1   mycroft 	struct buf *ibp, *bp;
    129       1.1   mycroft 	struct inode *ip;
    130       1.1   mycroft 	struct lfs *fs;
    131  1.13.2.1    bouyer 	struct indir indirs[NIADDR+2], *idp;
    132  1.13.2.1    bouyer 	ufs_daddr_t	lbn, lastblock;
    133  1.13.2.1    bouyer 	int bb, bcount;
    134      1.11  perseant 	int error, frags, i, nsize, osize, num;
    135  1.13.2.1    bouyer 
    136  1.13.2.1    bouyer 	vp = ap->a_vp;
    137       1.1   mycroft 	ip = VTOI(vp);
    138       1.1   mycroft 	fs = ip->i_lfs;
    139  1.13.2.1    bouyer 	offset = blkoff(fs, ap->a_startoffset);
    140  1.13.2.1    bouyer 	iosize = ap->a_size;
    141  1.13.2.1    bouyer 	lbn = lblkno(fs, ap->a_startoffset);
    142  1.13.2.1    bouyer 	(void)lfs_check(vp, lbn, 0);
    143      1.11  perseant 
    144       1.1   mycroft 	/*
    145       1.1   mycroft 	 * Three cases: it's a block beyond the end of file, it's a block in
    146       1.1   mycroft 	 * the file that may or may not have been assigned a disk address or
    147  1.13.2.1    bouyer 	 * we're writing an entire block.
    148  1.13.2.1    bouyer 	 *
    149  1.13.2.1    bouyer 	 * Note, if the daddr is UNWRITTEN, the block already exists in
    150  1.13.2.1    bouyer 	 * the cache (it was read or written earlier).  If so, make sure
    151  1.13.2.1    bouyer 	 * we don't count it as a new block or zero out its contents. If
    152  1.13.2.1    bouyer 	 * it did not, make sure we allocate any necessary indirect
    153  1.13.2.1    bouyer 	 * blocks.
    154  1.13.2.1    bouyer 	 *
    155       1.5      fvdl 	 * If we are writing a block beyond the end of the file, we need to
    156      1.11  perseant 	 * check if the old last block was a fragment.	If it was, we need
    157       1.5      fvdl 	 * to rewrite it.
    158       1.1   mycroft 	 */
    159      1.11  perseant 
    160  1.13.2.1    bouyer 	*ap->a_bpp = NULL;
    161      1.11  perseant 
    162       1.5      fvdl 	/* Check for block beyond end of file and fragment extension needed. */
    163       1.5      fvdl 	lastblock = lblkno(fs, ip->i_ffs_size);
    164       1.5      fvdl 	if (lastblock < NDADDR && lastblock < lbn) {
    165       1.5      fvdl 		osize = blksize(fs, ip, lastblock);
    166       1.5      fvdl 		if (osize < fs->lfs_bsize && osize > 0) {
    167       1.5      fvdl 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    168      1.11  perseant 						    lastblock, &bp)))
    169       1.5      fvdl 				return(error);
    170       1.5      fvdl 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
    171       1.7  drochner 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    172       1.5      fvdl 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    173  1.13.2.1    bouyer 			(void) VOP_BWRITE(bp);
    174       1.5      fvdl 		}
    175       1.5      fvdl 	}
    176  1.13.2.1    bouyer 
    177       1.5      fvdl 	/*
    178       1.5      fvdl 	 * If the block we are writing is a direct block, it's the last
    179       1.5      fvdl 	 * block in the file, and offset + iosize is less than a full
    180       1.5      fvdl 	 * block, we can write one or more fragments.  There are two cases:
    181       1.5      fvdl 	 * the block is brand new and we should allocate it the correct
    182       1.5      fvdl 	 * size or it already exists and contains some fragments and
    183       1.5      fvdl 	 * may need to extend it.
    184       1.5      fvdl 	 */
    185      1.11  perseant 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
    186      1.11  perseant 		osize = blksize(fs, ip, lbn);
    187       1.5      fvdl 		nsize = fragroundup(fs, offset + iosize);
    188  1.13.2.1    bouyer 		if (lblktosize(fs, lbn) >= ip->i_ffs_size) {
    189       1.5      fvdl 			/* Brand new block or fragment */
    190  1.13.2.1    bouyer 			frags = numfrags(fs, nsize);
    191  1.13.2.1    bouyer 			bb = fragstodb(fs, frags);
    192  1.13.2.1    bouyer 			*ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    193  1.13.2.1    bouyer 			ip->i_lfs_effnblks += bb;
    194  1.13.2.1    bouyer 			ip->i_lfs->lfs_bfree -= bb;
    195  1.13.2.1    bouyer 			ip->i_ffs_db[lbn] = bp->b_blkno = UNWRITTEN;
    196  1.13.2.1    bouyer 		} else {
    197      1.11  perseant 			if (nsize <= osize) {
    198      1.11  perseant 				/* No need to extend */
    199      1.11  perseant 				if ((error = bread(vp, lbn, osize, NOCRED, &bp)))
    200      1.11  perseant 					return error;
    201      1.11  perseant 			} else {
    202      1.11  perseant 				/* Extend existing block */
    203      1.11  perseant 				if ((error =
    204      1.11  perseant 				     lfs_fragextend(vp, osize, nsize, lbn, &bp)))
    205  1.13.2.1    bouyer 					return error;
    206      1.11  perseant 			}
    207  1.13.2.1    bouyer 			*ap->a_bpp = bp;
    208       1.5      fvdl 		}
    209  1.13.2.1    bouyer 		return 0;
    210  1.13.2.1    bouyer 	}
    211  1.13.2.1    bouyer 
    212  1.13.2.1    bouyer 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
    213  1.13.2.1    bouyer 	if (error)
    214  1.13.2.1    bouyer 		return (error);
    215  1.13.2.1    bouyer 	/*
    216  1.13.2.1    bouyer 	 * Do byte accounting all at once, so we can gracefully fail *before*
    217  1.13.2.1    bouyer 	 * we start assigning blocks.
    218  1.13.2.1    bouyer 	 */
    219  1.13.2.1    bouyer 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    220  1.13.2.1    bouyer 	bcount = 0;
    221  1.13.2.1    bouyer 	if (daddr == UNASSIGNED) {
    222  1.13.2.1    bouyer 		bcount = bb;
    223  1.13.2.1    bouyer 	}
    224  1.13.2.1    bouyer 	for (i = 1; i < num; ++i) {
    225  1.13.2.1    bouyer 		if (!indirs[i].in_exists) {
    226  1.13.2.1    bouyer 			bcount += bb;
    227  1.13.2.1    bouyer 		}
    228  1.13.2.1    bouyer 	}
    229  1.13.2.1    bouyer 	if (ISSPACE(fs, bcount, ap->a_cred)) {
    230  1.13.2.1    bouyer 		ip->i_lfs->lfs_bfree -= bcount;
    231  1.13.2.1    bouyer 		ip->i_lfs_effnblks += bcount;
    232       1.5      fvdl 	} else {
    233  1.13.2.1    bouyer 		return ENOSPC;
    234  1.13.2.1    bouyer 	}
    235  1.13.2.1    bouyer 
    236  1.13.2.1    bouyer 	if (daddr == UNASSIGNED) {
    237  1.13.2.1    bouyer 		if (num > 0 && ip->i_ffs_ib[indirs[0].in_off] == 0) {
    238  1.13.2.1    bouyer 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    239  1.13.2.1    bouyer 		}
    240  1.13.2.1    bouyer 
    241       1.5      fvdl 		/*
    242  1.13.2.1    bouyer 		 * Create new indirect blocks if necessary
    243       1.5      fvdl 		 */
    244  1.13.2.1    bouyer 		if (num > 1)
    245  1.13.2.1    bouyer 			idaddr = ip->i_ffs_ib[indirs[0].in_off];
    246  1.13.2.1    bouyer 		for (i = 1; i < num; ++i) {
    247  1.13.2.1    bouyer 			ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, 0,0);
    248  1.13.2.1    bouyer 			if (!indirs[i].in_exists) {
    249  1.13.2.1    bouyer 				clrbuf(ibp);
    250  1.13.2.1    bouyer 				ibp->b_blkno = UNWRITTEN;
    251  1.13.2.1    bouyer 			} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
    252  1.13.2.1    bouyer 				ibp->b_blkno = idaddr;
    253  1.13.2.1    bouyer 				ibp->b_flags |= B_READ;
    254  1.13.2.1    bouyer 				VOP_STRATEGY(ibp);
    255  1.13.2.1    bouyer 				biowait(ibp);
    256  1.13.2.1    bouyer 			}
    257  1.13.2.1    bouyer 			/*
    258  1.13.2.1    bouyer 			 * This block exists, but the next one may not.
    259  1.13.2.1    bouyer 			 * If that is the case mark it UNWRITTEN to keep
    260  1.13.2.1    bouyer 			 * the accounting straight.
    261  1.13.2.1    bouyer 			 */
    262  1.13.2.1    bouyer 			if (((daddr_t *)ibp->b_data)[indirs[i].in_off]==0)
    263  1.13.2.1    bouyer 				((daddr_t *)ibp->b_data)[indirs[i].in_off] =
    264  1.13.2.1    bouyer 					UNWRITTEN;
    265  1.13.2.1    bouyer 			idaddr = ((daddr_t *)ibp->b_data)[indirs[i].in_off];
    266  1.13.2.1    bouyer 			if ((error = VOP_BWRITE(ibp))) {
    267  1.13.2.1    bouyer 				return error;
    268  1.13.2.1    bouyer 			}
    269  1.13.2.1    bouyer 		}
    270  1.13.2.1    bouyer 	}
    271  1.13.2.1    bouyer 
    272  1.13.2.1    bouyer 
    273  1.13.2.1    bouyer 	/*
    274  1.13.2.1    bouyer 	 * Get the existing block from the cache.
    275  1.13.2.1    bouyer 	 */
    276  1.13.2.1    bouyer 	frags = dbtofrags(fs, bb);
    277  1.13.2.1    bouyer 	*ap->a_bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    278      1.11  perseant 
    279       1.5      fvdl 	/*
    280       1.5      fvdl 	 * The block we are writing may be a brand new block
    281  1.13.2.1    bouyer 	 * in which case we need to do accounting.
    282  1.13.2.1    bouyer 	 *
    283  1.13.2.1    bouyer 	 * We can tell a truly new block because ufs_bmaparray will say
    284  1.13.2.1    bouyer 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
    285  1.13.2.1    bouyer 	 * disk address UNWRITTEN.
    286       1.5      fvdl 	 */
    287  1.13.2.1    bouyer 	if (daddr == UNASSIGNED) {
    288  1.13.2.1    bouyer 		if (iosize != fs->lfs_bsize)
    289  1.13.2.1    bouyer 			clrbuf(bp);
    290  1.13.2.1    bouyer 
    291  1.13.2.1    bouyer 		/* Note the new address */
    292  1.13.2.1    bouyer 		bp->b_blkno = UNWRITTEN;
    293  1.13.2.1    bouyer 
    294  1.13.2.1    bouyer 		switch (num) {
    295  1.13.2.1    bouyer 		    case 0:
    296  1.13.2.1    bouyer 			ip->i_ffs_db[lbn] = UNWRITTEN;
    297  1.13.2.1    bouyer 			break;
    298  1.13.2.1    bouyer 		    case 1:
    299  1.13.2.1    bouyer 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    300  1.13.2.1    bouyer 			break;
    301  1.13.2.1    bouyer 		    default:
    302  1.13.2.1    bouyer 			idp = &indirs[num - 1];
    303  1.13.2.1    bouyer 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
    304  1.13.2.1    bouyer 				  &ibp))
    305  1.13.2.1    bouyer 				panic("lfs_balloc: bread bno %d", idp->in_lbn);
    306  1.13.2.1    bouyer 			((ufs_daddr_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
    307  1.13.2.1    bouyer 			VOP_BWRITE(ibp);
    308  1.13.2.1    bouyer 		}
    309  1.13.2.1    bouyer 	} else if (!(bp->b_flags & (B_DONE|B_DELWRI))) {
    310  1.13.2.1    bouyer 		/*
    311  1.13.2.1    bouyer 		 * Not a brand new block, also not in the cache;
    312  1.13.2.1    bouyer 		 * read it in from disk.
    313  1.13.2.1    bouyer 		 */
    314  1.13.2.1    bouyer 		if (iosize == fs->lfs_bsize)
    315       1.5      fvdl 			/* Optimization: I/O is unnecessary. */
    316       1.5      fvdl 			bp->b_blkno = daddr;
    317  1.13.2.1    bouyer 		else {
    318       1.5      fvdl 			/*
    319       1.5      fvdl 			 * We need to read the block to preserve the
    320       1.5      fvdl 			 * existing bytes.
    321       1.5      fvdl 			 */
    322       1.1   mycroft 			bp->b_blkno = daddr;
    323       1.1   mycroft 			bp->b_flags |= B_READ;
    324       1.1   mycroft 			VOP_STRATEGY(bp);
    325       1.1   mycroft 			return(biowait(bp));
    326       1.1   mycroft 		}
    327       1.1   mycroft 	}
    328      1.11  perseant 
    329       1.5      fvdl 	return (0);
    330       1.5      fvdl }
    331       1.5      fvdl 
    332  1.13.2.1    bouyer /* VOP_BWRITE 1 time */
    333       1.5      fvdl int
    334       1.5      fvdl lfs_fragextend(vp, osize, nsize, lbn, bpp)
    335       1.5      fvdl 	struct vnode *vp;
    336       1.5      fvdl 	int osize;
    337       1.5      fvdl 	int nsize;
    338       1.5      fvdl 	ufs_daddr_t lbn;
    339       1.5      fvdl 	struct buf **bpp;
    340       1.5      fvdl {
    341       1.5      fvdl 	struct inode *ip;
    342       1.5      fvdl 	struct lfs *fs;
    343       1.5      fvdl 	long bb;
    344       1.5      fvdl 	int error;
    345      1.11  perseant 	extern long locked_queue_bytes;
    346      1.13  perseant 	struct buf *ibp;
    347      1.13  perseant 	SEGUSE *sup;
    348       1.5      fvdl 
    349       1.5      fvdl 	ip = VTOI(vp);
    350       1.5      fvdl 	fs = ip->i_lfs;
    351       1.5      fvdl 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
    352  1.13.2.1    bouyer 	error = 0;
    353  1.13.2.1    bouyer 
    354  1.13.2.1    bouyer 	/*
    355  1.13.2.1    bouyer 	 * Get the seglock so we don't enlarge blocks or change the segment
    356  1.13.2.1    bouyer 	 * accounting information while a segment is being written.
    357  1.13.2.1    bouyer 	 */
    358  1.13.2.1    bouyer 	lfs_seglock(fs, SEGM_PROT);
    359  1.13.2.1    bouyer 
    360       1.5      fvdl 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    361  1.13.2.1    bouyer 		error = ENOSPC;
    362  1.13.2.1    bouyer 		goto out;
    363       1.5      fvdl 	}
    364       1.5      fvdl 	if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
    365       1.5      fvdl 		brelse(*bpp);
    366  1.13.2.1    bouyer 		goto out;
    367       1.5      fvdl 	}
    368       1.5      fvdl #ifdef QUOTA
    369       1.5      fvdl 	if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
    370       1.5      fvdl 		brelse(*bpp);
    371  1.13.2.1    bouyer 		goto out;
    372       1.5      fvdl 	}
    373       1.5      fvdl #endif
    374      1.11  perseant 	/*
    375  1.13.2.1    bouyer  	 * Fix the allocation for this fragment so that it looks like the
    376  1.13.2.1    bouyer          * source segment contained a block of the new size.  This overcounts;
    377  1.13.2.1    bouyer 	 * but the overcount only lasts until the block in question
    378  1.13.2.1    bouyer 	 * is written, so the on-disk live bytes count is always correct.
    379      1.11  perseant 	 */
    380  1.13.2.1    bouyer 	if ((*bpp)->b_blkno > 0) {
    381  1.13.2.1    bouyer 		LFS_SEGENTRY(sup, fs, datosn(fs,(*bpp)->b_blkno), ibp);
    382  1.13.2.1    bouyer 		sup->su_nbytes += (nsize-osize);
    383  1.13.2.1    bouyer 		VOP_BWRITE(ibp);
    384  1.13.2.1    bouyer 		ip->i_ffs_blocks += bb;
    385      1.11  perseant 	}
    386  1.13.2.1    bouyer 	fs->lfs_bfree -= bb;
    387  1.13.2.1    bouyer 	ip->i_lfs_effnblks += bb;
    388       1.5      fvdl 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    389      1.11  perseant 	if((*bpp)->b_flags & B_LOCKED)
    390      1.11  perseant 		locked_queue_bytes += (nsize - osize);
    391       1.5      fvdl 	allocbuf(*bpp, nsize);
    392       1.5      fvdl 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    393  1.13.2.1    bouyer 
    394  1.13.2.1    bouyer     out:
    395  1.13.2.1    bouyer 	lfs_segunlock(fs);
    396  1.13.2.1    bouyer 	return (error);
    397       1.1   mycroft }
    398