Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.35
      1  1.35      fvdl /*	$NetBSD: lfs_balloc.c,v 1.35 2003/01/24 21:55:26 fvdl Exp $	*/
      2   1.2       cgd 
      3  1.11  perseant /*-
      4  1.25  perseant  * 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.30     lukem 
     73  1.30     lukem #include <sys/cdefs.h>
     74  1.35      fvdl __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.35 2003/01/24 21:55:26 fvdl Exp $");
     75   1.8    scottr 
     76  1.28       mrg #if defined(_KERNEL_OPT)
     77   1.8    scottr #include "opt_quota.h"
     78   1.9    scottr #endif
     79   1.8    scottr 
     80   1.1   mycroft #include <sys/param.h>
     81   1.3  christos #include <sys/systm.h>
     82   1.1   mycroft #include <sys/buf.h>
     83   1.1   mycroft #include <sys/proc.h>
     84   1.1   mycroft #include <sys/vnode.h>
     85   1.1   mycroft #include <sys/mount.h>
     86   1.1   mycroft #include <sys/resourcevar.h>
     87   1.1   mycroft #include <sys/trace.h>
     88   1.1   mycroft 
     89   1.1   mycroft #include <miscfs/specfs/specdev.h>
     90   1.1   mycroft 
     91   1.1   mycroft #include <ufs/ufs/quota.h>
     92   1.1   mycroft #include <ufs/ufs/inode.h>
     93   1.1   mycroft #include <ufs/ufs/ufsmount.h>
     94   1.3  christos #include <ufs/ufs/ufs_extern.h>
     95   1.1   mycroft 
     96   1.1   mycroft #include <ufs/lfs/lfs.h>
     97   1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     98   1.5      fvdl 
     99  1.35      fvdl int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, struct ucred *);
    100   1.5      fvdl 
    101  1.16  perseant /*
    102  1.16  perseant  * Allocate a block, and to inode and filesystem block accounting for it
    103  1.16  perseant  * and for any indirect blocks the may need to be created in order for
    104  1.16  perseant  * this block to be created.
    105  1.16  perseant  *
    106  1.16  perseant  * Blocks which have never been accounted for (i.e., which "do not exist")
    107  1.16  perseant  * have disk address 0, which is translated by ufs_bmap to the special value
    108  1.16  perseant  * UNASSIGNED == -1, as in the historical UFS.
    109  1.16  perseant  *
    110  1.16  perseant  * Blocks which have been accounted for but which have not yet been written
    111  1.16  perseant  * to disk are given the new special disk address UNWRITTEN == -2, so that
    112  1.16  perseant  * they can be differentiated from completely new blocks.
    113  1.16  perseant  */
    114  1.25  perseant /* VOP_BWRITE NIADDR+2 times */
    115   1.1   mycroft int
    116  1.29  perseant lfs_balloc(void *v)
    117  1.14      fvdl {
    118  1.14      fvdl 	struct vop_balloc_args /* {
    119  1.14      fvdl 		struct vnode *a_vp;
    120  1.14      fvdl 		off_t a_startoffset;
    121  1.14      fvdl 		int a_size;
    122  1.14      fvdl 		struct ucred *a_cred;
    123  1.14      fvdl 		int a_flags;
    124  1.14      fvdl 		struct buf *a_bpp;
    125  1.14      fvdl 	} */ *ap = v;
    126   1.1   mycroft 	struct vnode *vp;
    127   1.5      fvdl 	int offset;
    128   1.1   mycroft 	u_long iosize;
    129  1.19  perseant 	daddr_t daddr, idaddr;
    130   1.1   mycroft 	struct buf *ibp, *bp;
    131   1.1   mycroft 	struct inode *ip;
    132   1.1   mycroft 	struct lfs *fs;
    133  1.16  perseant 	struct indir indirs[NIADDR+2], *idp;
    134  1.35      fvdl 	daddr_t	lbn, lastblock;
    135  1.19  perseant 	int bb, bcount;
    136  1.11  perseant 	int error, frags, i, nsize, osize, num;
    137  1.14      fvdl 
    138  1.14      fvdl 	vp = ap->a_vp;
    139   1.1   mycroft 	ip = VTOI(vp);
    140   1.1   mycroft 	fs = ip->i_lfs;
    141  1.14      fvdl 	offset = blkoff(fs, ap->a_startoffset);
    142  1.14      fvdl 	iosize = ap->a_size;
    143  1.14      fvdl 	lbn = lblkno(fs, ap->a_startoffset);
    144  1.14      fvdl 	(void)lfs_check(vp, lbn, 0);
    145  1.11  perseant 
    146   1.1   mycroft 	/*
    147   1.1   mycroft 	 * Three cases: it's a block beyond the end of file, it's a block in
    148   1.1   mycroft 	 * the file that may or may not have been assigned a disk address or
    149  1.19  perseant 	 * we're writing an entire block.
    150  1.19  perseant 	 *
    151  1.19  perseant 	 * Note, if the daddr is UNWRITTEN, the block already exists in
    152  1.19  perseant 	 * the cache (it was read or written earlier).  If so, make sure
    153  1.19  perseant 	 * we don't count it as a new block or zero out its contents. If
    154  1.19  perseant 	 * it did not, make sure we allocate any necessary indirect
    155  1.19  perseant 	 * blocks.
    156  1.19  perseant 	 *
    157   1.5      fvdl 	 * If we are writing a block beyond the end of the file, we need to
    158  1.11  perseant 	 * check if the old last block was a fragment.	If it was, we need
    159   1.5      fvdl 	 * to rewrite it.
    160   1.1   mycroft 	 */
    161  1.11  perseant 
    162  1.14      fvdl 	*ap->a_bpp = NULL;
    163  1.11  perseant 
    164   1.5      fvdl 	/* Check for block beyond end of file and fragment extension needed. */
    165   1.5      fvdl 	lastblock = lblkno(fs, ip->i_ffs_size);
    166   1.5      fvdl 	if (lastblock < NDADDR && lastblock < lbn) {
    167   1.5      fvdl 		osize = blksize(fs, ip, lastblock);
    168   1.5      fvdl 		if (osize < fs->lfs_bsize && osize > 0) {
    169   1.5      fvdl 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    170  1.29  perseant 						    lastblock, &bp,
    171  1.29  perseant 						    ap->a_cred)))
    172  1.31       chs 				return (error);
    173   1.5      fvdl 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
    174   1.7  drochner 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    175   1.5      fvdl 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    176  1.25  perseant 			(void) VOP_BWRITE(bp);
    177   1.5      fvdl 		}
    178   1.5      fvdl 	}
    179   1.5      fvdl 
    180   1.5      fvdl 	/*
    181   1.5      fvdl 	 * If the block we are writing is a direct block, it's the last
    182   1.5      fvdl 	 * block in the file, and offset + iosize is less than a full
    183   1.5      fvdl 	 * block, we can write one or more fragments.  There are two cases:
    184   1.5      fvdl 	 * the block is brand new and we should allocate it the correct
    185   1.5      fvdl 	 * size or it already exists and contains some fragments and
    186   1.5      fvdl 	 * may need to extend it.
    187   1.5      fvdl 	 */
    188  1.11  perseant 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
    189  1.11  perseant 		osize = blksize(fs, ip, lbn);
    190   1.5      fvdl 		nsize = fragroundup(fs, offset + iosize);
    191  1.19  perseant 		if (lblktosize(fs, lbn) >= ip->i_ffs_size) {
    192   1.5      fvdl 			/* Brand new block or fragment */
    193  1.19  perseant 			frags = numfrags(fs, nsize);
    194  1.29  perseant 			bb = fragstofsb(fs, frags);
    195  1.14      fvdl 			*ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    196  1.34      yamt 			if (ap->a_flags & B_CLRBUF)
    197  1.34      yamt 				clrbuf(bp);
    198  1.21  perseant 			ip->i_lfs_effnblks += bb;
    199  1.19  perseant 			ip->i_lfs->lfs_bfree -= bb;
    200  1.19  perseant 			ip->i_ffs_db[lbn] = bp->b_blkno = UNWRITTEN;
    201  1.19  perseant 		} else {
    202  1.11  perseant 			if (nsize <= osize) {
    203  1.11  perseant 				/* No need to extend */
    204  1.11  perseant 				if ((error = bread(vp, lbn, osize, NOCRED, &bp)))
    205  1.11  perseant 					return error;
    206  1.11  perseant 			} else {
    207  1.11  perseant 				/* Extend existing block */
    208  1.11  perseant 				if ((error =
    209  1.29  perseant 				     lfs_fragextend(vp, osize, nsize, lbn, &bp,
    210  1.29  perseant 						    ap->a_cred)))
    211  1.19  perseant 					return error;
    212  1.11  perseant 			}
    213  1.14      fvdl 			*ap->a_bpp = bp;
    214   1.5      fvdl 		}
    215  1.19  perseant 		return 0;
    216  1.19  perseant 	}
    217  1.19  perseant 
    218  1.19  perseant 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
    219  1.19  perseant 	if (error)
    220  1.19  perseant 		return (error);
    221  1.19  perseant 	/*
    222  1.19  perseant 	 * Do byte accounting all at once, so we can gracefully fail *before*
    223  1.19  perseant 	 * we start assigning blocks.
    224  1.19  perseant 	 */
    225  1.19  perseant 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    226  1.19  perseant 	bcount = 0;
    227  1.19  perseant 	if (daddr == UNASSIGNED) {
    228  1.19  perseant 		bcount = bb;
    229  1.19  perseant 	}
    230  1.19  perseant 	for (i = 1; i < num; ++i) {
    231  1.19  perseant 		if (!indirs[i].in_exists) {
    232  1.19  perseant 			bcount += bb;
    233  1.19  perseant 		}
    234  1.19  perseant 	}
    235  1.19  perseant 	if (ISSPACE(fs, bcount, ap->a_cred)) {
    236  1.19  perseant 		ip->i_lfs->lfs_bfree -= bcount;
    237  1.21  perseant 		ip->i_lfs_effnblks += bcount;
    238   1.5      fvdl 	} else {
    239  1.19  perseant 		return ENOSPC;
    240  1.19  perseant 	}
    241  1.19  perseant 
    242  1.19  perseant 	if (daddr == UNASSIGNED) {
    243  1.19  perseant 		if (num > 0 && ip->i_ffs_ib[indirs[0].in_off] == 0) {
    244  1.19  perseant 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    245  1.19  perseant 		}
    246  1.19  perseant 
    247   1.5      fvdl 		/*
    248  1.19  perseant 		 * Create new indirect blocks if necessary
    249   1.5      fvdl 		 */
    250  1.19  perseant 		if (num > 1)
    251  1.19  perseant 			idaddr = ip->i_ffs_ib[indirs[0].in_off];
    252  1.19  perseant 		for (i = 1; i < num; ++i) {
    253  1.19  perseant 			ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, 0,0);
    254  1.19  perseant 			if (!indirs[i].in_exists) {
    255  1.19  perseant 				clrbuf(ibp);
    256  1.19  perseant 				ibp->b_blkno = UNWRITTEN;
    257  1.19  perseant 			} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
    258  1.29  perseant 				ibp->b_blkno = fsbtodb(fs, idaddr);
    259  1.19  perseant 				ibp->b_flags |= B_READ;
    260  1.19  perseant 				VOP_STRATEGY(ibp);
    261  1.19  perseant 				biowait(ibp);
    262  1.19  perseant 			}
    263  1.19  perseant 			/*
    264  1.19  perseant 			 * This block exists, but the next one may not.
    265  1.19  perseant 			 * If that is the case mark it UNWRITTEN to keep
    266  1.19  perseant 			 * the accounting straight.
    267  1.19  perseant 			 */
    268  1.35      fvdl 			/* XXX ondisk32 */
    269  1.35      fvdl 			if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
    270  1.35      fvdl 				((int32_t *)ibp->b_data)[indirs[i].in_off] =
    271  1.19  perseant 					UNWRITTEN;
    272  1.35      fvdl 			/* XXX ondisk32 */
    273  1.35      fvdl 			idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
    274  1.19  perseant 			if ((error = VOP_BWRITE(ibp))) {
    275  1.19  perseant 				return error;
    276  1.19  perseant 			}
    277  1.19  perseant 		}
    278  1.19  perseant 	}
    279  1.19  perseant 
    280  1.19  perseant 
    281  1.19  perseant 	/*
    282  1.19  perseant 	 * Get the existing block from the cache.
    283  1.19  perseant 	 */
    284  1.29  perseant 	frags = fsbtofrags(fs, bb);
    285  1.19  perseant 	*ap->a_bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    286  1.11  perseant 
    287   1.5      fvdl 	/*
    288   1.5      fvdl 	 * The block we are writing may be a brand new block
    289  1.19  perseant 	 * in which case we need to do accounting.
    290  1.15  perseant 	 *
    291  1.19  perseant 	 * We can tell a truly new block because ufs_bmaparray will say
    292  1.19  perseant 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
    293  1.19  perseant 	 * disk address UNWRITTEN.
    294   1.5      fvdl 	 */
    295  1.16  perseant 	if (daddr == UNASSIGNED) {
    296  1.34      yamt 		if (ap->a_flags & B_CLRBUF)
    297  1.19  perseant 			clrbuf(bp);
    298  1.19  perseant 
    299  1.19  perseant 		/* Note the new address */
    300  1.19  perseant 		bp->b_blkno = UNWRITTEN;
    301  1.19  perseant 
    302  1.19  perseant 		switch (num) {
    303  1.19  perseant 		    case 0:
    304  1.19  perseant 			ip->i_ffs_db[lbn] = UNWRITTEN;
    305  1.19  perseant 			break;
    306  1.19  perseant 		    case 1:
    307  1.19  perseant 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    308  1.19  perseant 			break;
    309  1.19  perseant 		    default:
    310  1.19  perseant 			idp = &indirs[num - 1];
    311  1.19  perseant 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
    312  1.19  perseant 				  &ibp))
    313  1.35      fvdl 				panic("lfs_balloc: bread bno %lld",
    314  1.35      fvdl 				    (long long)idp->in_lbn);
    315  1.35      fvdl 			/* XXX ondisk32 */
    316  1.35      fvdl 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
    317  1.19  perseant 			VOP_BWRITE(ibp);
    318  1.15  perseant 		}
    319  1.15  perseant 	} else if (!(bp->b_flags & (B_DONE|B_DELWRI))) {
    320  1.15  perseant 		/*
    321  1.15  perseant 		 * Not a brand new block, also not in the cache;
    322  1.15  perseant 		 * read it in from disk.
    323  1.15  perseant 		 */
    324  1.15  perseant 		if (iosize == fs->lfs_bsize)
    325   1.5      fvdl 			/* Optimization: I/O is unnecessary. */
    326   1.5      fvdl 			bp->b_blkno = daddr;
    327  1.15  perseant 		else {
    328   1.5      fvdl 			/*
    329   1.5      fvdl 			 * We need to read the block to preserve the
    330   1.5      fvdl 			 * existing bytes.
    331   1.5      fvdl 			 */
    332   1.1   mycroft 			bp->b_blkno = daddr;
    333   1.1   mycroft 			bp->b_flags |= B_READ;
    334   1.1   mycroft 			VOP_STRATEGY(bp);
    335  1.31       chs 			return (biowait(bp));
    336   1.1   mycroft 		}
    337   1.1   mycroft 	}
    338  1.11  perseant 
    339   1.5      fvdl 	return (0);
    340   1.5      fvdl }
    341   1.5      fvdl 
    342  1.25  perseant /* VOP_BWRITE 1 time */
    343   1.5      fvdl int
    344  1.35      fvdl lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
    345   1.5      fvdl {
    346   1.5      fvdl 	struct inode *ip;
    347   1.5      fvdl 	struct lfs *fs;
    348   1.5      fvdl 	long bb;
    349   1.5      fvdl 	int error;
    350  1.11  perseant 	extern long locked_queue_bytes;
    351  1.26  perseant 	size_t obufsize;
    352   1.5      fvdl 
    353   1.5      fvdl 	ip = VTOI(vp);
    354   1.5      fvdl 	fs = ip->i_lfs;
    355  1.29  perseant 	bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
    356  1.18  perseant 	error = 0;
    357  1.18  perseant 
    358  1.18  perseant 	/*
    359  1.18  perseant 	 * Get the seglock so we don't enlarge blocks or change the segment
    360  1.18  perseant 	 * accounting information while a segment is being written.
    361  1.18  perseant 	 */
    362  1.26  perseant     top:
    363  1.33  perseant #ifdef LFS_MALLOC_SEGLOCK
    364  1.18  perseant 	lfs_seglock(fs, SEGM_PROT);
    365  1.33  perseant #else
    366  1.33  perseant 	lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    367  1.33  perseant #endif
    368  1.29  perseant 	if (!ISSPACE(fs, bb, cred)) {
    369  1.18  perseant 		error = ENOSPC;
    370  1.18  perseant 		goto out;
    371   1.5      fvdl 	}
    372   1.5      fvdl 	if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
    373   1.5      fvdl 		brelse(*bpp);
    374  1.18  perseant 		goto out;
    375   1.5      fvdl 	}
    376  1.19  perseant #ifdef QUOTA
    377  1.29  perseant 	if ((error = chkdq(ip, bb, cred, 0))) {
    378  1.19  perseant 		brelse(*bpp);
    379  1.19  perseant 		goto out;
    380  1.19  perseant 	}
    381  1.19  perseant #endif
    382  1.13  perseant 	/*
    383  1.26  perseant 	 * Adjust accounting for lfs_avail.  If there's not enough room,
    384  1.26  perseant 	 * we will have to wait for the cleaner, which we can't do while
    385  1.26  perseant 	 * holding a block busy or while holding the seglock.  In that case,
    386  1.26  perseant 	 * release both and start over after waiting.
    387  1.26  perseant 	 */
    388  1.33  perseant 
    389  1.26  perseant 	if ((*bpp)->b_flags & B_DELWRI) {
    390  1.26  perseant 		if (!lfs_fits(fs, bb)) {
    391  1.26  perseant 			brelse(*bpp);
    392  1.26  perseant #ifdef QUOTA
    393  1.29  perseant 			chkdq(ip, -bb, cred, 0);
    394  1.26  perseant #endif
    395  1.33  perseant #ifdef LFS_FRAGSIZE_SEGLOCK
    396  1.26  perseant 			lfs_segunlock(fs);
    397  1.33  perseant #else
    398  1.33  perseant 			lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    399  1.33  perseant #endif
    400  1.26  perseant 			lfs_availwait(fs, bb);
    401  1.26  perseant 			goto top;
    402  1.26  perseant 		}
    403  1.26  perseant 		fs->lfs_avail -= bb;
    404  1.26  perseant 	}
    405  1.26  perseant 
    406  1.19  perseant 	fs->lfs_bfree -= bb;
    407  1.22  perseant 	ip->i_lfs_effnblks += bb;
    408   1.5      fvdl 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    409  1.26  perseant 
    410  1.32  perseant 	LFS_DEBUG_COUNTLOCKED("frag1");
    411  1.32  perseant 
    412  1.26  perseant 	obufsize = (*bpp)->b_bufsize;
    413   1.5      fvdl 	allocbuf(*bpp, nsize);
    414  1.26  perseant 
    415  1.26  perseant 	/* Adjust locked-list accounting */
    416  1.26  perseant 	if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
    417  1.27  perseant 		locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
    418  1.32  perseant 
    419  1.32  perseant 	LFS_DEBUG_COUNTLOCKED("frag2");
    420  1.26  perseant 
    421   1.5      fvdl 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    422  1.18  perseant 
    423  1.18  perseant     out:
    424  1.33  perseant #ifdef LFS_FRAGSIZE_SEGLOCK
    425  1.18  perseant 	lfs_segunlock(fs);
    426  1.33  perseant #else
    427  1.33  perseant 	lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    428  1.33  perseant #endif
    429  1.18  perseant 	return (error);
    430   1.1   mycroft }
    431