Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.42.2.5
      1  1.42.2.5     skrll /*	$NetBSD: lfs_balloc.c,v 1.42.2.5 2005/11/10 14:12:32 skrll Exp $	*/
      2       1.2       cgd 
      3      1.11  perseant /*-
      4      1.36  perseant  * Copyright (c) 1999, 2000, 2001, 2002, 2003 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.37  perseant  *	This product includes software developed by the NetBSD
     21      1.37  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.42.2.1     skrll  * 3. Neither the name of the University nor the names of its contributors
     51       1.1   mycroft  *    may be used to endorse or promote products derived from this software
     52       1.1   mycroft  *    without specific prior written permission.
     53       1.1   mycroft  *
     54       1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55       1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56       1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57       1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58       1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59       1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60       1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61       1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62       1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63       1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64       1.1   mycroft  * SUCH DAMAGE.
     65       1.1   mycroft  *
     66       1.5      fvdl  *	@(#)lfs_balloc.c	8.4 (Berkeley) 5/8/95
     67       1.1   mycroft  */
     68      1.30     lukem 
     69      1.30     lukem #include <sys/cdefs.h>
     70  1.42.2.5     skrll __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.42.2.5 2005/11/10 14:12:32 skrll Exp $");
     71       1.8    scottr 
     72      1.28       mrg #if defined(_KERNEL_OPT)
     73       1.8    scottr #include "opt_quota.h"
     74       1.9    scottr #endif
     75       1.8    scottr 
     76       1.1   mycroft #include <sys/param.h>
     77       1.3  christos #include <sys/systm.h>
     78       1.1   mycroft #include <sys/buf.h>
     79       1.1   mycroft #include <sys/proc.h>
     80       1.1   mycroft #include <sys/vnode.h>
     81       1.1   mycroft #include <sys/mount.h>
     82       1.1   mycroft #include <sys/resourcevar.h>
     83  1.42.2.5     skrll #include <sys/tree.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.5      fvdl 
     96      1.36  perseant #include <uvm/uvm.h>
     97      1.36  perseant 
     98      1.35      fvdl int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, struct ucred *);
     99       1.5      fvdl 
    100  1.42.2.4     skrll u_int64_t locked_fakequeue_count;
    101  1.42.2.4     skrll 
    102      1.16  perseant /*
    103      1.16  perseant  * Allocate a block, and to inode and filesystem block accounting for it
    104      1.16  perseant  * and for any indirect blocks the may need to be created in order for
    105      1.16  perseant  * this block to be created.
    106      1.16  perseant  *
    107      1.16  perseant  * Blocks which have never been accounted for (i.e., which "do not exist")
    108      1.16  perseant  * have disk address 0, which is translated by ufs_bmap to the special value
    109      1.16  perseant  * UNASSIGNED == -1, as in the historical UFS.
    110  1.42.2.4     skrll  *
    111      1.16  perseant  * Blocks which have been accounted for but which have not yet been written
    112      1.16  perseant  * to disk are given the new special disk address UNWRITTEN == -2, so that
    113      1.16  perseant  * they can be differentiated from completely new blocks.
    114      1.16  perseant  */
    115      1.25  perseant /* VOP_BWRITE NIADDR+2 times */
    116       1.1   mycroft int
    117  1.42.2.5     skrll lfs_balloc(struct vnode *vp, off_t startoffset, int iosize, struct ucred *cred,
    118  1.42.2.5     skrll     int flags, struct buf **bpp)
    119      1.14      fvdl {
    120       1.5      fvdl 	int offset;
    121      1.19  perseant 	daddr_t daddr, idaddr;
    122  1.42.2.5     skrll 	struct buf *ibp, *bp;
    123       1.1   mycroft 	struct inode *ip;
    124       1.1   mycroft 	struct lfs *fs;
    125      1.16  perseant 	struct indir indirs[NIADDR+2], *idp;
    126      1.35      fvdl 	daddr_t	lbn, lastblock;
    127      1.19  perseant 	int bb, bcount;
    128      1.11  perseant 	int error, frags, i, nsize, osize, num;
    129      1.14      fvdl 
    130       1.1   mycroft 	ip = VTOI(vp);
    131       1.1   mycroft 	fs = ip->i_lfs;
    132  1.42.2.5     skrll 	offset = blkoff(fs, startoffset);
    133      1.41      yamt 	KASSERT(iosize <= fs->lfs_bsize);
    134  1.42.2.5     skrll 	lbn = lblkno(fs, startoffset);
    135      1.36  perseant 	/* (void)lfs_check(vp, lbn, 0); */
    136  1.42.2.5     skrll 
    137  1.42.2.5     skrll 	ASSERT_MAYBE_SEGLOCK(fs);
    138      1.36  perseant 
    139  1.42.2.4     skrll 	/*
    140       1.1   mycroft 	 * Three cases: it's a block beyond the end of file, it's a block in
    141       1.1   mycroft 	 * the file that may or may not have been assigned a disk address or
    142      1.19  perseant 	 * we're writing an entire block.
    143      1.19  perseant 	 *
    144      1.19  perseant 	 * Note, if the daddr is UNWRITTEN, the block already exists in
    145      1.37  perseant 	 * the cache (it was read or written earlier).	If so, make sure
    146      1.19  perseant 	 * we don't count it as a new block or zero out its contents. If
    147      1.19  perseant 	 * it did not, make sure we allocate any necessary indirect
    148      1.19  perseant 	 * blocks.
    149      1.19  perseant 	 *
    150       1.5      fvdl 	 * If we are writing a block beyond the end of the file, we need to
    151      1.11  perseant 	 * check if the old last block was a fragment.	If it was, we need
    152       1.5      fvdl 	 * to rewrite it.
    153       1.1   mycroft 	 */
    154  1.42.2.4     skrll 
    155      1.36  perseant 	if (bpp)
    156      1.36  perseant 		*bpp = NULL;
    157  1.42.2.4     skrll 
    158       1.5      fvdl 	/* Check for block beyond end of file and fragment extension needed. */
    159      1.40      fvdl 	lastblock = lblkno(fs, ip->i_size);
    160       1.5      fvdl 	if (lastblock < NDADDR && lastblock < lbn) {
    161       1.5      fvdl 		osize = blksize(fs, ip, lastblock);
    162       1.5      fvdl 		if (osize < fs->lfs_bsize && osize > 0) {
    163       1.5      fvdl 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    164      1.36  perseant 						    lastblock,
    165  1.42.2.5     skrll 						    (bpp ? &bp : NULL), cred)))
    166      1.31       chs 				return (error);
    167      1.40      fvdl 			ip->i_ffs1_size = ip->i_size =
    168      1.40      fvdl 			    (lastblock + 1) * fs->lfs_bsize;
    169      1.40      fvdl 			uvm_vnp_setsize(vp, ip->i_size);
    170       1.5      fvdl 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    171      1.36  perseant 			if (bpp)
    172      1.36  perseant 				(void) VOP_BWRITE(bp);
    173       1.5      fvdl 		}
    174       1.5      fvdl 	}
    175       1.5      fvdl 
    176       1.5      fvdl 	/*
    177       1.5      fvdl 	 * If the block we are writing is a direct block, it's the last
    178       1.5      fvdl 	 * block in the file, and offset + iosize is less than a full
    179       1.5      fvdl 	 * block, we can write one or more fragments.  There are two cases:
    180       1.5      fvdl 	 * the block is brand new and we should allocate it the correct
    181       1.5      fvdl 	 * size or it already exists and contains some fragments and
    182       1.5      fvdl 	 * may need to extend it.
    183       1.5      fvdl 	 */
    184      1.40      fvdl 	if (lbn < NDADDR && lblkno(fs, ip->i_size) <= lbn) {
    185      1.11  perseant 		osize = blksize(fs, ip, lbn);
    186       1.5      fvdl 		nsize = fragroundup(fs, offset + iosize);
    187      1.40      fvdl 		if (lblktosize(fs, lbn) >= ip->i_size) {
    188       1.5      fvdl 			/* Brand new block or fragment */
    189      1.19  perseant 			frags = numfrags(fs, nsize);
    190      1.29  perseant 			bb = fragstofsb(fs, frags);
    191  1.42.2.5     skrll 			if (!ISSPACE(fs, bb, cred))
    192  1.42.2.4     skrll 				return ENOSPC;
    193      1.36  perseant 			if (bpp) {
    194  1.42.2.5     skrll 				*bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    195      1.36  perseant 				bp->b_blkno = UNWRITTEN;
    196  1.42.2.5     skrll 				if (flags & B_CLRBUF)
    197      1.38  perseant 					clrbuf(bp);
    198      1.36  perseant 			}
    199      1.21  perseant 			ip->i_lfs_effnblks += bb;
    200  1.42.2.5     skrll 			simple_lock(&fs->lfs_interlock);
    201  1.42.2.5     skrll 			fs->lfs_bfree -= bb;
    202  1.42.2.5     skrll 			simple_unlock(&fs->lfs_interlock);
    203      1.40      fvdl 			ip->i_ffs1_db[lbn] = UNWRITTEN;
    204      1.19  perseant 		} else {
    205      1.11  perseant 			if (nsize <= osize) {
    206      1.11  perseant 				/* No need to extend */
    207      1.36  perseant 				if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
    208      1.11  perseant 					return error;
    209      1.11  perseant 			} else {
    210      1.11  perseant 				/* Extend existing block */
    211      1.11  perseant 				if ((error =
    212      1.36  perseant 				     lfs_fragextend(vp, osize, nsize, lbn,
    213  1.42.2.5     skrll 						    (bpp ? &bp : NULL), cred)))
    214      1.19  perseant 					return error;
    215      1.11  perseant 			}
    216      1.36  perseant 			if (bpp)
    217      1.36  perseant 				*bpp = bp;
    218       1.5      fvdl 		}
    219      1.19  perseant 		return 0;
    220      1.19  perseant 	}
    221      1.19  perseant 
    222      1.42      yamt 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL, NULL);
    223      1.19  perseant 	if (error)
    224      1.19  perseant 		return (error);
    225  1.42.2.4     skrll 
    226  1.42.2.4     skrll 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
    227  1.42.2.4     skrll 	KASSERT(daddr <= LFS_MAX_DADDR);
    228  1.42.2.4     skrll 
    229      1.19  perseant 	/*
    230      1.19  perseant 	 * Do byte accounting all at once, so we can gracefully fail *before*
    231      1.19  perseant 	 * we start assigning blocks.
    232      1.19  perseant 	 */
    233      1.19  perseant 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    234      1.19  perseant 	bcount = 0;
    235      1.19  perseant 	if (daddr == UNASSIGNED) {
    236      1.19  perseant 		bcount = bb;
    237      1.19  perseant 	}
    238      1.19  perseant 	for (i = 1; i < num; ++i) {
    239      1.19  perseant 		if (!indirs[i].in_exists) {
    240      1.19  perseant 			bcount += bb;
    241      1.19  perseant 		}
    242      1.19  perseant 	}
    243  1.42.2.5     skrll 	if (ISSPACE(fs, bcount, cred)) {
    244  1.42.2.5     skrll 		simple_lock(&fs->lfs_interlock);
    245  1.42.2.5     skrll 		fs->lfs_bfree -= bcount;
    246  1.42.2.5     skrll 		simple_unlock(&fs->lfs_interlock);
    247      1.21  perseant 		ip->i_lfs_effnblks += bcount;
    248       1.5      fvdl 	} else {
    249      1.19  perseant 		return ENOSPC;
    250      1.19  perseant 	}
    251      1.19  perseant 
    252      1.19  perseant 	if (daddr == UNASSIGNED) {
    253      1.40      fvdl 		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
    254      1.40      fvdl 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
    255      1.19  perseant 		}
    256      1.19  perseant 
    257       1.5      fvdl 		/*
    258      1.19  perseant 		 * Create new indirect blocks if necessary
    259       1.5      fvdl 		 */
    260  1.42.2.1     skrll 		if (num > 1) {
    261      1.40      fvdl 			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
    262  1.42.2.1     skrll 			for (i = 1; i < num; ++i) {
    263  1.42.2.1     skrll 				ibp = getblk(vp, indirs[i].in_lbn,
    264  1.42.2.1     skrll 				    fs->lfs_bsize, 0,0);
    265  1.42.2.1     skrll 				if (!indirs[i].in_exists) {
    266  1.42.2.1     skrll 					clrbuf(ibp);
    267  1.42.2.1     skrll 					ibp->b_blkno = UNWRITTEN;
    268  1.42.2.1     skrll 				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
    269  1.42.2.1     skrll 					ibp->b_blkno = fsbtodb(fs, idaddr);
    270  1.42.2.1     skrll 					ibp->b_flags |= B_READ;
    271  1.42.2.1     skrll 					VOP_STRATEGY(vp, ibp);
    272  1.42.2.1     skrll 					biowait(ibp);
    273  1.42.2.1     skrll 				}
    274  1.42.2.1     skrll 				/*
    275  1.42.2.1     skrll 				 * This block exists, but the next one may not.
    276  1.42.2.1     skrll 				 * If that is the case mark it UNWRITTEN to keep
    277  1.42.2.1     skrll 				 * the accounting straight.
    278  1.42.2.1     skrll 				 */
    279  1.42.2.1     skrll 				/* XXX ondisk32 */
    280  1.42.2.1     skrll 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
    281  1.42.2.1     skrll 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
    282  1.42.2.1     skrll 						UNWRITTEN;
    283  1.42.2.1     skrll 				/* XXX ondisk32 */
    284  1.42.2.1     skrll 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
    285  1.42.2.5     skrll #ifdef DEBUG
    286  1.42.2.5     skrll 				if (vp == fs->lfs_ivnode) {
    287  1.42.2.5     skrll 					LFS_ENTER_LOG("balloc", __FILE__,
    288  1.42.2.5     skrll 						__LINE__, indirs[i].in_lbn,
    289  1.42.2.5     skrll 						ibp->b_flags, curproc->p_pid);
    290  1.42.2.5     skrll 				}
    291  1.42.2.5     skrll #endif
    292  1.42.2.1     skrll 				if ((error = VOP_BWRITE(ibp)))
    293  1.42.2.1     skrll 					return error;
    294      1.19  perseant 			}
    295      1.19  perseant 		}
    296  1.42.2.4     skrll 	}
    297      1.19  perseant 
    298      1.19  perseant 
    299      1.19  perseant 	/*
    300      1.36  perseant 	 * Get the existing block from the cache, if requested.
    301      1.19  perseant 	 */
    302      1.29  perseant 	frags = fsbtofrags(fs, bb);
    303      1.36  perseant 	if (bpp)
    304      1.36  perseant 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    305  1.42.2.4     skrll 
    306  1.42.2.4     skrll 	/*
    307  1.42.2.4     skrll 	 * Do accounting on blocks that represent pages.
    308  1.42.2.4     skrll 	 */
    309  1.42.2.4     skrll 	if (!bpp)
    310  1.42.2.4     skrll 		lfs_register_block(vp, lbn);
    311  1.42.2.4     skrll 
    312  1.42.2.4     skrll 	/*
    313       1.5      fvdl 	 * The block we are writing may be a brand new block
    314      1.19  perseant 	 * in which case we need to do accounting.
    315      1.15  perseant 	 *
    316      1.19  perseant 	 * We can tell a truly new block because ufs_bmaparray will say
    317      1.19  perseant 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
    318      1.19  perseant 	 * disk address UNWRITTEN.
    319       1.5      fvdl 	 */
    320      1.16  perseant 	if (daddr == UNASSIGNED) {
    321      1.36  perseant 		if (bpp) {
    322  1.42.2.5     skrll 			if (flags & B_CLRBUF)
    323      1.36  perseant 				clrbuf(bp);
    324  1.42.2.4     skrll 
    325      1.36  perseant 			/* Note the new address */
    326      1.36  perseant 			bp->b_blkno = UNWRITTEN;
    327      1.36  perseant 		}
    328  1.42.2.4     skrll 
    329      1.19  perseant 		switch (num) {
    330      1.19  perseant 		    case 0:
    331      1.40      fvdl 			ip->i_ffs1_db[lbn] = UNWRITTEN;
    332      1.19  perseant 			break;
    333      1.19  perseant 		    case 1:
    334      1.40      fvdl 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
    335      1.19  perseant 			break;
    336      1.19  perseant 		    default:
    337      1.19  perseant 			idp = &indirs[num - 1];
    338      1.19  perseant 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
    339      1.19  perseant 				  &ibp))
    340      1.35      fvdl 				panic("lfs_balloc: bread bno %lld",
    341      1.35      fvdl 				    (long long)idp->in_lbn);
    342      1.35      fvdl 			/* XXX ondisk32 */
    343      1.35      fvdl 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
    344  1.42.2.5     skrll #ifdef DEBUG
    345  1.42.2.5     skrll 			if (vp == fs->lfs_ivnode) {
    346  1.42.2.5     skrll 				LFS_ENTER_LOG("balloc", __FILE__,
    347  1.42.2.5     skrll 					__LINE__, idp->in_lbn,
    348  1.42.2.5     skrll 					ibp->b_flags, curproc->p_pid);
    349  1.42.2.5     skrll 			}
    350  1.42.2.5     skrll #endif
    351      1.19  perseant 			VOP_BWRITE(ibp);
    352      1.15  perseant 		}
    353      1.36  perseant 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
    354      1.15  perseant 		/*
    355      1.15  perseant 		 * Not a brand new block, also not in the cache;
    356      1.15  perseant 		 * read it in from disk.
    357      1.15  perseant 		 */
    358      1.15  perseant 		if (iosize == fs->lfs_bsize)
    359       1.5      fvdl 			/* Optimization: I/O is unnecessary. */
    360       1.5      fvdl 			bp->b_blkno = daddr;
    361      1.15  perseant 		else {
    362       1.5      fvdl 			/*
    363       1.5      fvdl 			 * We need to read the block to preserve the
    364       1.5      fvdl 			 * existing bytes.
    365       1.5      fvdl 			 */
    366       1.1   mycroft 			bp->b_blkno = daddr;
    367       1.1   mycroft 			bp->b_flags |= B_READ;
    368  1.42.2.1     skrll 			VOP_STRATEGY(vp, bp);
    369      1.31       chs 			return (biowait(bp));
    370       1.1   mycroft 		}
    371       1.1   mycroft 	}
    372  1.42.2.4     skrll 
    373       1.5      fvdl 	return (0);
    374       1.5      fvdl }
    375       1.5      fvdl 
    376      1.25  perseant /* VOP_BWRITE 1 time */
    377       1.5      fvdl int
    378      1.35      fvdl lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
    379       1.5      fvdl {
    380       1.5      fvdl 	struct inode *ip;
    381       1.5      fvdl 	struct lfs *fs;
    382       1.5      fvdl 	long bb;
    383       1.5      fvdl 	int error;
    384      1.11  perseant 	extern long locked_queue_bytes;
    385      1.26  perseant 	size_t obufsize;
    386       1.5      fvdl 
    387       1.5      fvdl 	ip = VTOI(vp);
    388       1.5      fvdl 	fs = ip->i_lfs;
    389      1.29  perseant 	bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
    390      1.18  perseant 	error = 0;
    391      1.18  perseant 
    392  1.42.2.5     skrll 	ASSERT_DUNNO_SEGLOCK(fs);
    393  1.42.2.5     skrll 
    394      1.18  perseant 	/*
    395      1.36  perseant 	 * Get the seglock so we don't enlarge blocks while a segment
    396      1.36  perseant 	 * is being written.  If we're called with bpp==NULL, though,
    397      1.36  perseant 	 * we are only pretending to change a buffer, so we don't have to
    398      1.36  perseant 	 * lock.
    399      1.18  perseant 	 */
    400      1.26  perseant     top:
    401      1.36  perseant 	if (bpp) {
    402      1.36  perseant 		lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    403  1.42.2.1     skrll 		LFS_DEBUG_COUNTLOCKED("frag");
    404      1.36  perseant 	}
    405      1.36  perseant 
    406      1.29  perseant 	if (!ISSPACE(fs, bb, cred)) {
    407      1.18  perseant 		error = ENOSPC;
    408      1.18  perseant 		goto out;
    409       1.5      fvdl 	}
    410      1.36  perseant 
    411      1.36  perseant 	/*
    412      1.36  perseant 	 * If we are not asked to actually return the block, all we need
    413      1.36  perseant 	 * to do is allocate space for it.  UBC will handle dirtying the
    414      1.36  perseant 	 * appropriate things and making sure it all goes to disk.
    415      1.36  perseant 	 * Don't bother to read in that case.
    416      1.36  perseant 	 */
    417      1.36  perseant 	if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
    418       1.5      fvdl 		brelse(*bpp);
    419      1.18  perseant 		goto out;
    420       1.5      fvdl 	}
    421      1.19  perseant #ifdef QUOTA
    422      1.29  perseant 	if ((error = chkdq(ip, bb, cred, 0))) {
    423      1.36  perseant 		if (bpp)
    424      1.36  perseant 			brelse(*bpp);
    425      1.19  perseant 		goto out;
    426      1.19  perseant 	}
    427      1.19  perseant #endif
    428      1.13  perseant 	/*
    429      1.26  perseant 	 * Adjust accounting for lfs_avail.  If there's not enough room,
    430      1.26  perseant 	 * we will have to wait for the cleaner, which we can't do while
    431      1.26  perseant 	 * holding a block busy or while holding the seglock.  In that case,
    432      1.26  perseant 	 * release both and start over after waiting.
    433      1.26  perseant 	 */
    434      1.33  perseant 
    435      1.36  perseant 	if (bpp && ((*bpp)->b_flags & B_DELWRI)) {
    436      1.26  perseant 		if (!lfs_fits(fs, bb)) {
    437      1.36  perseant 			if (bpp)
    438      1.36  perseant 				brelse(*bpp);
    439      1.26  perseant #ifdef QUOTA
    440      1.29  perseant 			chkdq(ip, -bb, cred, 0);
    441      1.26  perseant #endif
    442      1.33  perseant 			lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    443      1.26  perseant 			lfs_availwait(fs, bb);
    444      1.26  perseant 			goto top;
    445      1.26  perseant 		}
    446      1.26  perseant 		fs->lfs_avail -= bb;
    447      1.26  perseant 	}
    448      1.26  perseant 
    449  1.42.2.5     skrll 	simple_lock(&fs->lfs_interlock);
    450      1.19  perseant 	fs->lfs_bfree -= bb;
    451  1.42.2.5     skrll 	simple_unlock(&fs->lfs_interlock);
    452      1.22  perseant 	ip->i_lfs_effnblks += bb;
    453       1.5      fvdl 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    454      1.26  perseant 
    455      1.36  perseant 	if (bpp) {
    456      1.36  perseant 		obufsize = (*bpp)->b_bufsize;
    457  1.42.2.1     skrll 		allocbuf(*bpp, nsize, 1);
    458      1.26  perseant 
    459      1.36  perseant 		/* Adjust locked-list accounting */
    460  1.42.2.5     skrll 		if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED) {
    461  1.42.2.5     skrll 			simple_lock(&lfs_subsys_lock);
    462      1.36  perseant 			locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
    463  1.42.2.5     skrll 			simple_unlock(&lfs_subsys_lock);
    464  1.42.2.5     skrll 		}
    465      1.32  perseant 
    466      1.36  perseant 		bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    467      1.36  perseant 	}
    468      1.18  perseant 
    469      1.18  perseant     out:
    470      1.36  perseant 	if (bpp) {
    471      1.36  perseant 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    472      1.36  perseant 	}
    473      1.18  perseant 	return (error);
    474       1.1   mycroft }
    475  1.42.2.4     skrll 
    476  1.42.2.5     skrll static __inline int
    477  1.42.2.5     skrll lge(struct lbnentry *a, struct lbnentry *b)
    478  1.42.2.5     skrll {
    479  1.42.2.5     skrll 	return a->lbn - b->lbn;
    480  1.42.2.5     skrll }
    481  1.42.2.5     skrll 
    482  1.42.2.5     skrll SPLAY_PROTOTYPE(lfs_splay, lbnentry, entry, lge);
    483  1.42.2.5     skrll 
    484  1.42.2.5     skrll SPLAY_GENERATE(lfs_splay, lbnentry, entry, lge);
    485  1.42.2.5     skrll 
    486  1.42.2.4     skrll /*
    487  1.42.2.4     skrll  * Record this lbn as being "write pending".  We used to have this information
    488  1.42.2.4     skrll  * on the buffer headers, but since pages don't have buffer headers we
    489  1.42.2.4     skrll  * record it here instead.
    490  1.42.2.4     skrll  */
    491  1.42.2.4     skrll void
    492  1.42.2.4     skrll lfs_register_block(struct vnode *vp, daddr_t lbn)
    493  1.42.2.4     skrll {
    494  1.42.2.4     skrll 	struct lfs *fs;
    495  1.42.2.4     skrll 	struct inode *ip;
    496  1.42.2.4     skrll 	struct lbnentry *lbp;
    497  1.42.2.5     skrll 
    498  1.42.2.5     skrll 	ip = VTOI(vp);
    499  1.42.2.4     skrll 
    500  1.42.2.4     skrll 	/* Don't count metadata */
    501  1.42.2.5     skrll 	if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
    502  1.42.2.4     skrll 		return;
    503  1.42.2.4     skrll 
    504  1.42.2.4     skrll 	fs = ip->i_lfs;
    505  1.42.2.4     skrll 
    506  1.42.2.5     skrll 	ASSERT_NO_SEGLOCK(fs);
    507  1.42.2.5     skrll 
    508  1.42.2.4     skrll 	/* If no space, wait for the cleaner */
    509  1.42.2.4     skrll 	lfs_availwait(fs, btofsb(fs, 1 << fs->lfs_bshift));
    510  1.42.2.4     skrll 
    511  1.42.2.4     skrll 	lbp = (struct lbnentry *)pool_get(&lfs_lbnentry_pool, PR_WAITOK);
    512  1.42.2.4     skrll 	lbp->lbn = lbn;
    513  1.42.2.5     skrll 	if (SPLAY_INSERT(lfs_splay, &ip->i_lfs_lbtree, lbp) != NULL) {
    514  1.42.2.5     skrll 		/* Already there */
    515  1.42.2.5     skrll 		pool_put(&lfs_lbnentry_pool, lbp);
    516  1.42.2.5     skrll 		return;
    517  1.42.2.5     skrll 	}
    518  1.42.2.5     skrll 
    519  1.42.2.5     skrll 	++ip->i_lfs_nbtree;
    520  1.42.2.5     skrll 	simple_lock(&fs->lfs_interlock);
    521  1.42.2.4     skrll 	fs->lfs_favail += btofsb(fs, (1 << fs->lfs_bshift));
    522  1.42.2.5     skrll 	fs->lfs_pages += fs->lfs_bsize >> PAGE_SHIFT;
    523  1.42.2.5     skrll 	simple_lock(&lfs_subsys_lock);
    524  1.42.2.4     skrll 	++locked_fakequeue_count;
    525  1.42.2.5     skrll 	lfs_subsys_pages += fs->lfs_bsize >> PAGE_SHIFT;
    526  1.42.2.5     skrll 	simple_unlock(&lfs_subsys_lock);
    527  1.42.2.5     skrll 	simple_unlock(&fs->lfs_interlock);
    528  1.42.2.4     skrll }
    529  1.42.2.4     skrll 
    530  1.42.2.4     skrll static void
    531  1.42.2.5     skrll lfs_do_deregister(struct lfs *fs, struct inode *ip, struct lbnentry *lbp)
    532  1.42.2.4     skrll {
    533  1.42.2.5     skrll 	ASSERT_MAYBE_SEGLOCK(fs);
    534  1.42.2.5     skrll 
    535  1.42.2.5     skrll 	--ip->i_lfs_nbtree;
    536  1.42.2.5     skrll 	SPLAY_REMOVE(lfs_splay, &ip->i_lfs_lbtree, lbp);
    537  1.42.2.4     skrll 	pool_put(&lfs_lbnentry_pool, lbp);
    538  1.42.2.5     skrll 	simple_lock(&fs->lfs_interlock);
    539  1.42.2.4     skrll 	if (fs->lfs_favail > btofsb(fs, (1 << fs->lfs_bshift)))
    540  1.42.2.4     skrll 		fs->lfs_favail -= btofsb(fs, (1 << fs->lfs_bshift));
    541  1.42.2.5     skrll 	fs->lfs_pages -= fs->lfs_bsize >> PAGE_SHIFT;
    542  1.42.2.5     skrll 	simple_lock(&lfs_subsys_lock);
    543  1.42.2.4     skrll 	if (locked_fakequeue_count > 0)
    544  1.42.2.4     skrll 		--locked_fakequeue_count;
    545  1.42.2.5     skrll 	lfs_subsys_pages -= fs->lfs_bsize >> PAGE_SHIFT;
    546  1.42.2.5     skrll 	simple_unlock(&lfs_subsys_lock);
    547  1.42.2.5     skrll 	simple_unlock(&fs->lfs_interlock);
    548  1.42.2.4     skrll }
    549  1.42.2.4     skrll 
    550  1.42.2.4     skrll void
    551  1.42.2.4     skrll lfs_deregister_block(struct vnode *vp, daddr_t lbn)
    552  1.42.2.4     skrll {
    553  1.42.2.4     skrll 	struct lfs *fs;
    554  1.42.2.4     skrll 	struct inode *ip;
    555  1.42.2.4     skrll 	struct lbnentry *lbp;
    556  1.42.2.5     skrll 	struct lbnentry tmp;
    557  1.42.2.5     skrll 
    558  1.42.2.5     skrll 	ip = VTOI(vp);
    559  1.42.2.4     skrll 
    560  1.42.2.4     skrll 	/* Don't count metadata */
    561  1.42.2.5     skrll 	if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
    562  1.42.2.4     skrll 		return;
    563  1.42.2.4     skrll 
    564  1.42.2.4     skrll 	fs = ip->i_lfs;
    565  1.42.2.5     skrll 	tmp.lbn = lbn;
    566  1.42.2.5     skrll 	lbp = SPLAY_FIND(lfs_splay, &ip->i_lfs_lbtree, &tmp);
    567  1.42.2.4     skrll 	if (lbp == NULL)
    568  1.42.2.4     skrll 		return;
    569  1.42.2.4     skrll 
    570  1.42.2.5     skrll 	lfs_do_deregister(fs, ip, lbp);
    571  1.42.2.4     skrll }
    572  1.42.2.4     skrll 
    573  1.42.2.4     skrll void
    574  1.42.2.4     skrll lfs_deregister_all(struct vnode *vp)
    575  1.42.2.4     skrll {
    576  1.42.2.5     skrll 	struct lbnentry *lbp, *nlbp;
    577  1.42.2.5     skrll 	struct lfs_splay *hd;
    578  1.42.2.4     skrll 	struct lfs *fs;
    579  1.42.2.4     skrll 	struct inode *ip;
    580  1.42.2.4     skrll 
    581  1.42.2.4     skrll 	ip = VTOI(vp);
    582  1.42.2.4     skrll 	fs = ip->i_lfs;
    583  1.42.2.5     skrll 	hd = &ip->i_lfs_lbtree;
    584  1.42.2.5     skrll 
    585  1.42.2.5     skrll 	for (lbp = SPLAY_MIN(lfs_splay, hd); lbp != NULL; lbp = nlbp) {
    586  1.42.2.5     skrll 		nlbp = SPLAY_NEXT(lfs_splay, hd, lbp);
    587  1.42.2.5     skrll 		lfs_do_deregister(fs, ip, lbp);
    588  1.42.2.5     skrll 	}
    589  1.42.2.4     skrll }
    590