Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.53
      1  1.53  perseant /*	$NetBSD: lfs_balloc.c,v 1.53 2005/04/14 00:44:17 perseant 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.43       agc  * 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.53  perseant __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.53 2005/04/14 00:44:17 perseant 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.1   mycroft #include <sys/trace.h>
     84  1.49  perseant #include <sys/malloc.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.49  perseant u_int64_t locked_fakequeue_count;
    101  1.53  perseant extern int lfs_blist_hw;
    102  1.53  perseant extern int lfs_blist_total;    /* # entries in hash table */
    103  1.53  perseant extern int lfs_blist_maxdepth; /* Hash max depth */
    104  1.53  perseant extern LIST_HEAD(, lbnentry) *lfs_blist;
    105  1.49  perseant 
    106  1.16  perseant /*
    107  1.16  perseant  * Allocate a block, and to inode and filesystem block accounting for it
    108  1.16  perseant  * and for any indirect blocks the may need to be created in order for
    109  1.16  perseant  * this block to be created.
    110  1.16  perseant  *
    111  1.16  perseant  * Blocks which have never been accounted for (i.e., which "do not exist")
    112  1.16  perseant  * have disk address 0, which is translated by ufs_bmap to the special value
    113  1.16  perseant  * UNASSIGNED == -1, as in the historical UFS.
    114  1.50     perry  *
    115  1.16  perseant  * Blocks which have been accounted for but which have not yet been written
    116  1.16  perseant  * to disk are given the new special disk address UNWRITTEN == -2, so that
    117  1.16  perseant  * they can be differentiated from completely new blocks.
    118  1.16  perseant  */
    119  1.25  perseant /* VOP_BWRITE NIADDR+2 times */
    120   1.1   mycroft int
    121  1.29  perseant lfs_balloc(void *v)
    122  1.14      fvdl {
    123  1.14      fvdl 	struct vop_balloc_args /* {
    124  1.14      fvdl 		struct vnode *a_vp;
    125  1.14      fvdl 		off_t a_startoffset;
    126  1.14      fvdl 		int a_size;
    127  1.14      fvdl 		struct ucred *a_cred;
    128  1.14      fvdl 		int a_flags;
    129  1.14      fvdl 		struct buf *a_bpp;
    130  1.14      fvdl 	} */ *ap = v;
    131   1.1   mycroft 	struct vnode *vp;
    132   1.5      fvdl 	int offset;
    133   1.1   mycroft 	u_long iosize;
    134  1.46   mycroft 	daddr_t daddr, idaddr;
    135  1.36  perseant 	struct buf *ibp, *bp, **bpp;
    136   1.1   mycroft 	struct inode *ip;
    137   1.1   mycroft 	struct lfs *fs;
    138  1.16  perseant 	struct indir indirs[NIADDR+2], *idp;
    139  1.35      fvdl 	daddr_t	lbn, lastblock;
    140  1.19  perseant 	int bb, bcount;
    141  1.11  perseant 	int error, frags, i, nsize, osize, num;
    142  1.14      fvdl 
    143  1.50     perry 	vp = ap->a_vp;
    144   1.1   mycroft 	ip = VTOI(vp);
    145   1.1   mycroft 	fs = ip->i_lfs;
    146  1.14      fvdl 	offset = blkoff(fs, ap->a_startoffset);
    147  1.14      fvdl 	iosize = ap->a_size;
    148  1.41      yamt 	KASSERT(iosize <= fs->lfs_bsize);
    149  1.14      fvdl 	lbn = lblkno(fs, ap->a_startoffset);
    150  1.36  perseant 	/* (void)lfs_check(vp, lbn, 0); */
    151  1.36  perseant 	bpp = ap->a_bpp;
    152  1.36  perseant 
    153  1.52  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    154  1.52  perseant 
    155  1.50     perry 	/*
    156   1.1   mycroft 	 * Three cases: it's a block beyond the end of file, it's a block in
    157   1.1   mycroft 	 * the file that may or may not have been assigned a disk address or
    158  1.19  perseant 	 * we're writing an entire block.
    159  1.19  perseant 	 *
    160  1.19  perseant 	 * Note, if the daddr is UNWRITTEN, the block already exists in
    161  1.37  perseant 	 * the cache (it was read or written earlier).	If so, make sure
    162  1.19  perseant 	 * we don't count it as a new block or zero out its contents. If
    163  1.19  perseant 	 * it did not, make sure we allocate any necessary indirect
    164  1.19  perseant 	 * blocks.
    165  1.19  perseant 	 *
    166   1.5      fvdl 	 * If we are writing a block beyond the end of the file, we need to
    167  1.11  perseant 	 * check if the old last block was a fragment.	If it was, we need
    168   1.5      fvdl 	 * to rewrite it.
    169   1.1   mycroft 	 */
    170  1.50     perry 
    171  1.36  perseant 	if (bpp)
    172  1.36  perseant 		*bpp = NULL;
    173  1.50     perry 
    174   1.5      fvdl 	/* Check for block beyond end of file and fragment extension needed. */
    175  1.40      fvdl 	lastblock = lblkno(fs, ip->i_size);
    176   1.5      fvdl 	if (lastblock < NDADDR && lastblock < lbn) {
    177   1.5      fvdl 		osize = blksize(fs, ip, lastblock);
    178   1.5      fvdl 		if (osize < fs->lfs_bsize && osize > 0) {
    179   1.5      fvdl 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    180  1.36  perseant 						    lastblock,
    181  1.36  perseant 						    (bpp ? &bp : NULL),
    182  1.29  perseant 						    ap->a_cred)))
    183  1.31       chs 				return (error);
    184  1.40      fvdl 			ip->i_ffs1_size = ip->i_size =
    185  1.40      fvdl 			    (lastblock + 1) * fs->lfs_bsize;
    186  1.40      fvdl 			uvm_vnp_setsize(vp, ip->i_size);
    187   1.5      fvdl 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    188  1.36  perseant 			if (bpp)
    189  1.36  perseant 				(void) VOP_BWRITE(bp);
    190   1.5      fvdl 		}
    191   1.5      fvdl 	}
    192   1.5      fvdl 
    193   1.5      fvdl 	/*
    194   1.5      fvdl 	 * If the block we are writing is a direct block, it's the last
    195   1.5      fvdl 	 * block in the file, and offset + iosize is less than a full
    196   1.5      fvdl 	 * block, we can write one or more fragments.  There are two cases:
    197   1.5      fvdl 	 * the block is brand new and we should allocate it the correct
    198   1.5      fvdl 	 * size or it already exists and contains some fragments and
    199   1.5      fvdl 	 * may need to extend it.
    200   1.5      fvdl 	 */
    201  1.40      fvdl 	if (lbn < NDADDR && lblkno(fs, ip->i_size) <= lbn) {
    202  1.11  perseant 		osize = blksize(fs, ip, lbn);
    203   1.5      fvdl 		nsize = fragroundup(fs, offset + iosize);
    204  1.40      fvdl 		if (lblktosize(fs, lbn) >= ip->i_size) {
    205   1.5      fvdl 			/* Brand new block or fragment */
    206  1.19  perseant 			frags = numfrags(fs, nsize);
    207  1.29  perseant 			bb = fragstofsb(fs, frags);
    208  1.51  perseant 			if (!ISSPACE(fs, bb, ap->a_cred))
    209  1.51  perseant 				return ENOSPC;
    210  1.36  perseant 			if (bpp) {
    211  1.36  perseant 				*ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    212  1.36  perseant 				bp->b_blkno = UNWRITTEN;
    213  1.38  perseant 				if (ap->a_flags & B_CLRBUF)
    214  1.38  perseant 					clrbuf(bp);
    215  1.36  perseant 			}
    216  1.21  perseant 			ip->i_lfs_effnblks += bb;
    217  1.52  perseant 			simple_lock(&fs->lfs_interlock);
    218  1.53  perseant 			fs->lfs_bfree -= bb;
    219  1.52  perseant 			simple_unlock(&fs->lfs_interlock);
    220  1.40      fvdl 			ip->i_ffs1_db[lbn] = UNWRITTEN;
    221  1.19  perseant 		} else {
    222  1.11  perseant 			if (nsize <= osize) {
    223  1.11  perseant 				/* No need to extend */
    224  1.36  perseant 				if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
    225  1.11  perseant 					return error;
    226  1.11  perseant 			} else {
    227  1.11  perseant 				/* Extend existing block */
    228  1.11  perseant 				if ((error =
    229  1.36  perseant 				     lfs_fragextend(vp, osize, nsize, lbn,
    230  1.36  perseant 						    (bpp ? &bp : NULL),
    231  1.29  perseant 						    ap->a_cred)))
    232  1.19  perseant 					return error;
    233  1.11  perseant 			}
    234  1.36  perseant 			if (bpp)
    235  1.36  perseant 				*bpp = bp;
    236   1.5      fvdl 		}
    237  1.19  perseant 		return 0;
    238  1.19  perseant 	}
    239  1.19  perseant 
    240  1.42      yamt 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL, NULL);
    241  1.19  perseant 	if (error)
    242  1.19  perseant 		return (error);
    243  1.49  perseant 
    244  1.49  perseant 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
    245  1.49  perseant 	KASSERT(daddr <= LFS_MAX_DADDR);
    246  1.49  perseant 
    247  1.19  perseant 	/*
    248  1.19  perseant 	 * Do byte accounting all at once, so we can gracefully fail *before*
    249  1.19  perseant 	 * we start assigning blocks.
    250  1.19  perseant 	 */
    251  1.19  perseant 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    252  1.19  perseant 	bcount = 0;
    253  1.19  perseant 	if (daddr == UNASSIGNED) {
    254  1.19  perseant 		bcount = bb;
    255  1.19  perseant 	}
    256  1.19  perseant 	for (i = 1; i < num; ++i) {
    257  1.19  perseant 		if (!indirs[i].in_exists) {
    258  1.19  perseant 			bcount += bb;
    259  1.19  perseant 		}
    260  1.19  perseant 	}
    261  1.19  perseant 	if (ISSPACE(fs, bcount, ap->a_cred)) {
    262  1.52  perseant 		simple_lock(&fs->lfs_interlock);
    263  1.53  perseant 		fs->lfs_bfree -= bcount;
    264  1.52  perseant 		simple_unlock(&fs->lfs_interlock);
    265  1.21  perseant 		ip->i_lfs_effnblks += bcount;
    266   1.5      fvdl 	} else {
    267  1.19  perseant 		return ENOSPC;
    268  1.19  perseant 	}
    269  1.19  perseant 
    270  1.19  perseant 	if (daddr == UNASSIGNED) {
    271  1.40      fvdl 		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
    272  1.40      fvdl 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
    273  1.19  perseant 		}
    274  1.19  perseant 
    275   1.5      fvdl 		/*
    276  1.19  perseant 		 * Create new indirect blocks if necessary
    277   1.5      fvdl 		 */
    278  1.46   mycroft 		if (num > 1) {
    279  1.40      fvdl 			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
    280  1.46   mycroft 			for (i = 1; i < num; ++i) {
    281  1.46   mycroft 				ibp = getblk(vp, indirs[i].in_lbn,
    282  1.46   mycroft 				    fs->lfs_bsize, 0,0);
    283  1.46   mycroft 				if (!indirs[i].in_exists) {
    284  1.46   mycroft 					clrbuf(ibp);
    285  1.46   mycroft 					ibp->b_blkno = UNWRITTEN;
    286  1.46   mycroft 				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
    287  1.46   mycroft 					ibp->b_blkno = fsbtodb(fs, idaddr);
    288  1.46   mycroft 					ibp->b_flags |= B_READ;
    289  1.48   hannken 					VOP_STRATEGY(vp, ibp);
    290  1.46   mycroft 					biowait(ibp);
    291  1.46   mycroft 				}
    292  1.46   mycroft 				/*
    293  1.46   mycroft 				 * This block exists, but the next one may not.
    294  1.46   mycroft 				 * If that is the case mark it UNWRITTEN to keep
    295  1.46   mycroft 				 * the accounting straight.
    296  1.46   mycroft 				 */
    297  1.46   mycroft 				/* XXX ondisk32 */
    298  1.46   mycroft 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
    299  1.46   mycroft 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
    300  1.46   mycroft 						UNWRITTEN;
    301  1.46   mycroft 				/* XXX ondisk32 */
    302  1.46   mycroft 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
    303  1.52  perseant #ifdef DEBUG
    304  1.52  perseant 				if (vp == fs->lfs_ivnode) {
    305  1.52  perseant 					LFS_ENTER_LOG("balloc", __FILE__,
    306  1.52  perseant 						__LINE__, indirs[i].in_lbn,
    307  1.52  perseant 						ibp->b_flags, curproc->p_pid);
    308  1.52  perseant 				}
    309  1.52  perseant #endif
    310  1.46   mycroft 				if ((error = VOP_BWRITE(ibp)))
    311  1.46   mycroft 					return error;
    312  1.19  perseant 			}
    313  1.19  perseant 		}
    314  1.50     perry 	}
    315  1.19  perseant 
    316  1.19  perseant 
    317  1.19  perseant 	/*
    318  1.36  perseant 	 * Get the existing block from the cache, if requested.
    319  1.19  perseant 	 */
    320  1.29  perseant 	frags = fsbtofrags(fs, bb);
    321  1.36  perseant 	if (bpp)
    322  1.36  perseant 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    323  1.50     perry 
    324  1.49  perseant 	/*
    325  1.49  perseant 	 * Do accounting on blocks that represent pages.
    326  1.49  perseant 	 */
    327  1.49  perseant 	if (!bpp)
    328  1.49  perseant 		lfs_register_block(vp, lbn);
    329  1.49  perseant 
    330  1.50     perry 	/*
    331   1.5      fvdl 	 * The block we are writing may be a brand new block
    332  1.19  perseant 	 * in which case we need to do accounting.
    333  1.15  perseant 	 *
    334  1.19  perseant 	 * We can tell a truly new block because ufs_bmaparray will say
    335  1.19  perseant 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
    336  1.19  perseant 	 * disk address UNWRITTEN.
    337   1.5      fvdl 	 */
    338  1.16  perseant 	if (daddr == UNASSIGNED) {
    339  1.36  perseant 		if (bpp) {
    340  1.36  perseant 			if (ap->a_flags & B_CLRBUF)
    341  1.36  perseant 				clrbuf(bp);
    342  1.50     perry 
    343  1.36  perseant 			/* Note the new address */
    344  1.36  perseant 			bp->b_blkno = UNWRITTEN;
    345  1.36  perseant 		}
    346  1.50     perry 
    347  1.19  perseant 		switch (num) {
    348  1.19  perseant 		    case 0:
    349  1.40      fvdl 			ip->i_ffs1_db[lbn] = UNWRITTEN;
    350  1.19  perseant 			break;
    351  1.19  perseant 		    case 1:
    352  1.40      fvdl 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
    353  1.19  perseant 			break;
    354  1.19  perseant 		    default:
    355  1.19  perseant 			idp = &indirs[num - 1];
    356  1.19  perseant 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
    357  1.19  perseant 				  &ibp))
    358  1.35      fvdl 				panic("lfs_balloc: bread bno %lld",
    359  1.35      fvdl 				    (long long)idp->in_lbn);
    360  1.35      fvdl 			/* XXX ondisk32 */
    361  1.35      fvdl 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
    362  1.52  perseant #ifdef DEBUG
    363  1.52  perseant 			if (vp == fs->lfs_ivnode) {
    364  1.52  perseant 				LFS_ENTER_LOG("balloc", __FILE__,
    365  1.52  perseant 					__LINE__, idp->in_lbn,
    366  1.52  perseant 					ibp->b_flags, curproc->p_pid);
    367  1.52  perseant 			}
    368  1.52  perseant #endif
    369  1.19  perseant 			VOP_BWRITE(ibp);
    370  1.15  perseant 		}
    371  1.36  perseant 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
    372  1.15  perseant 		/*
    373  1.15  perseant 		 * Not a brand new block, also not in the cache;
    374  1.15  perseant 		 * read it in from disk.
    375  1.15  perseant 		 */
    376  1.15  perseant 		if (iosize == fs->lfs_bsize)
    377   1.5      fvdl 			/* Optimization: I/O is unnecessary. */
    378   1.5      fvdl 			bp->b_blkno = daddr;
    379  1.15  perseant 		else {
    380   1.5      fvdl 			/*
    381   1.5      fvdl 			 * We need to read the block to preserve the
    382   1.5      fvdl 			 * existing bytes.
    383   1.5      fvdl 			 */
    384   1.1   mycroft 			bp->b_blkno = daddr;
    385   1.1   mycroft 			bp->b_flags |= B_READ;
    386  1.48   hannken 			VOP_STRATEGY(vp, bp);
    387  1.31       chs 			return (biowait(bp));
    388   1.1   mycroft 		}
    389   1.1   mycroft 	}
    390  1.50     perry 
    391   1.5      fvdl 	return (0);
    392   1.5      fvdl }
    393   1.5      fvdl 
    394  1.25  perseant /* VOP_BWRITE 1 time */
    395   1.5      fvdl int
    396  1.35      fvdl lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
    397   1.5      fvdl {
    398   1.5      fvdl 	struct inode *ip;
    399   1.5      fvdl 	struct lfs *fs;
    400   1.5      fvdl 	long bb;
    401   1.5      fvdl 	int error;
    402  1.11  perseant 	extern long locked_queue_bytes;
    403  1.26  perseant 	size_t obufsize;
    404   1.5      fvdl 
    405   1.5      fvdl 	ip = VTOI(vp);
    406   1.5      fvdl 	fs = ip->i_lfs;
    407  1.29  perseant 	bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
    408  1.18  perseant 	error = 0;
    409  1.18  perseant 
    410  1.52  perseant 	ASSERT_DUNNO_SEGLOCK(fs);
    411  1.52  perseant 
    412  1.18  perseant 	/*
    413  1.36  perseant 	 * Get the seglock so we don't enlarge blocks while a segment
    414  1.36  perseant 	 * is being written.  If we're called with bpp==NULL, though,
    415  1.36  perseant 	 * we are only pretending to change a buffer, so we don't have to
    416  1.36  perseant 	 * lock.
    417  1.18  perseant 	 */
    418  1.26  perseant     top:
    419  1.36  perseant 	if (bpp) {
    420  1.36  perseant 		lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    421  1.44      yamt 		LFS_DEBUG_COUNTLOCKED("frag");
    422  1.36  perseant 	}
    423  1.36  perseant 
    424  1.29  perseant 	if (!ISSPACE(fs, bb, cred)) {
    425  1.18  perseant 		error = ENOSPC;
    426  1.18  perseant 		goto out;
    427   1.5      fvdl 	}
    428  1.36  perseant 
    429  1.36  perseant 	/*
    430  1.36  perseant 	 * If we are not asked to actually return the block, all we need
    431  1.36  perseant 	 * to do is allocate space for it.  UBC will handle dirtying the
    432  1.36  perseant 	 * appropriate things and making sure it all goes to disk.
    433  1.36  perseant 	 * Don't bother to read in that case.
    434  1.36  perseant 	 */
    435  1.36  perseant 	if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
    436   1.5      fvdl 		brelse(*bpp);
    437  1.18  perseant 		goto out;
    438   1.5      fvdl 	}
    439  1.19  perseant #ifdef QUOTA
    440  1.29  perseant 	if ((error = chkdq(ip, bb, cred, 0))) {
    441  1.36  perseant 		if (bpp)
    442  1.36  perseant 			brelse(*bpp);
    443  1.19  perseant 		goto out;
    444  1.19  perseant 	}
    445  1.19  perseant #endif
    446  1.13  perseant 	/*
    447  1.26  perseant 	 * Adjust accounting for lfs_avail.  If there's not enough room,
    448  1.26  perseant 	 * we will have to wait for the cleaner, which we can't do while
    449  1.26  perseant 	 * holding a block busy or while holding the seglock.  In that case,
    450  1.26  perseant 	 * release both and start over after waiting.
    451  1.26  perseant 	 */
    452  1.33  perseant 
    453  1.36  perseant 	if (bpp && ((*bpp)->b_flags & B_DELWRI)) {
    454  1.26  perseant 		if (!lfs_fits(fs, bb)) {
    455  1.36  perseant 			if (bpp)
    456  1.36  perseant 				brelse(*bpp);
    457  1.26  perseant #ifdef QUOTA
    458  1.29  perseant 			chkdq(ip, -bb, cred, 0);
    459  1.26  perseant #endif
    460  1.33  perseant 			lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    461  1.26  perseant 			lfs_availwait(fs, bb);
    462  1.26  perseant 			goto top;
    463  1.26  perseant 		}
    464  1.26  perseant 		fs->lfs_avail -= bb;
    465  1.26  perseant 	}
    466  1.26  perseant 
    467  1.52  perseant 	simple_lock(&fs->lfs_interlock);
    468  1.19  perseant 	fs->lfs_bfree -= bb;
    469  1.52  perseant 	simple_unlock(&fs->lfs_interlock);
    470  1.22  perseant 	ip->i_lfs_effnblks += bb;
    471   1.5      fvdl 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    472  1.26  perseant 
    473  1.36  perseant 	if (bpp) {
    474  1.36  perseant 		obufsize = (*bpp)->b_bufsize;
    475  1.47        pk 		allocbuf(*bpp, nsize, 1);
    476  1.26  perseant 
    477  1.36  perseant 		/* Adjust locked-list accounting */
    478  1.52  perseant 		if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED) {
    479  1.52  perseant 			simple_lock(&lfs_subsys_lock);
    480  1.36  perseant 			locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
    481  1.52  perseant 			simple_unlock(&lfs_subsys_lock);
    482  1.52  perseant 		}
    483  1.26  perseant 
    484  1.36  perseant 		bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    485  1.36  perseant 	}
    486  1.18  perseant 
    487  1.18  perseant     out:
    488  1.36  perseant 	if (bpp) {
    489  1.36  perseant 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    490  1.36  perseant 	}
    491  1.18  perseant 	return (error);
    492   1.1   mycroft }
    493  1.49  perseant 
    494  1.53  perseant static __inline unsigned int
    495  1.53  perseant lfs_blist_hash(struct lfs *fs, struct inode *ip, daddr_t lbn)
    496  1.53  perseant {
    497  1.53  perseant         return ((intptr_t)fs ^ ip->i_number ^ lbn) & (lfs_blist_hw - 1);
    498  1.53  perseant }
    499  1.53  perseant 
    500  1.53  perseant #define HASH_HEADER(ip, hash) &(lfs_blist[hash])
    501  1.53  perseant 
    502  1.49  perseant /*
    503  1.49  perseant  * Record this lbn as being "write pending".  We used to have this information
    504  1.49  perseant  * on the buffer headers, but since pages don't have buffer headers we
    505  1.49  perseant  * record it here instead.
    506  1.49  perseant  */
    507  1.49  perseant void
    508  1.49  perseant lfs_register_block(struct vnode *vp, daddr_t lbn)
    509  1.49  perseant {
    510  1.49  perseant 	struct lfs *fs;
    511  1.49  perseant 	struct inode *ip;
    512  1.49  perseant 	struct lbnentry *lbp;
    513  1.53  perseant 	unsigned int hash, depth;
    514  1.53  perseant 
    515  1.53  perseant 	ip = VTOI(vp);
    516  1.49  perseant 
    517  1.49  perseant 	/* Don't count metadata */
    518  1.53  perseant 	if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
    519  1.49  perseant 		return;
    520  1.49  perseant 
    521  1.49  perseant 	fs = ip->i_lfs;
    522  1.49  perseant 
    523  1.52  perseant 	ASSERT_NO_SEGLOCK(fs);
    524  1.52  perseant 
    525  1.49  perseant 	/* If no space, wait for the cleaner */
    526  1.49  perseant 	lfs_availwait(fs, btofsb(fs, 1 << fs->lfs_bshift));
    527  1.49  perseant 
    528  1.53  perseant 	hash = lfs_blist_hash(fs, ip, lbn);
    529  1.53  perseant 	depth = 0;
    530  1.53  perseant 	LIST_FOREACH(lbp, HASH_HEADER(ip, hash), entry) {
    531  1.53  perseant 		if (lbp->lbn == lbn && lbp->vp == vp && lbp->fs == fs)
    532  1.49  perseant 			return;
    533  1.53  perseant 		++depth;
    534  1.49  perseant 	}
    535  1.49  perseant 
    536  1.49  perseant 	lbp = (struct lbnentry *)pool_get(&lfs_lbnentry_pool, PR_WAITOK);
    537  1.49  perseant 	lbp->lbn = lbn;
    538  1.53  perseant 	lbp->vp = vp;
    539  1.53  perseant 	lbp->fs = fs;
    540  1.53  perseant 	if (depth > lfs_blist_maxdepth)
    541  1.53  perseant 		lfs_blist_maxdepth = depth;
    542  1.53  perseant 	++lfs_blist_total;
    543  1.53  perseant 	LIST_INSERT_HEAD(HASH_HEADER(ip, hash), lbp, entry);
    544  1.52  perseant 
    545  1.52  perseant 	simple_lock(&fs->lfs_interlock);
    546  1.49  perseant 	fs->lfs_favail += btofsb(fs, (1 << fs->lfs_bshift));
    547  1.49  perseant 	++locked_fakequeue_count;
    548  1.52  perseant 	simple_unlock(&fs->lfs_interlock);
    549  1.49  perseant }
    550  1.49  perseant 
    551  1.49  perseant static void
    552  1.53  perseant lfs_do_deregister(struct lfs *fs, struct inode *ip, struct lbnentry *lbp)
    553  1.49  perseant {
    554  1.52  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    555  1.52  perseant 
    556  1.49  perseant 	LIST_REMOVE(lbp, entry);
    557  1.53  perseant 	--lfs_blist_total;
    558  1.49  perseant 	pool_put(&lfs_lbnentry_pool, lbp);
    559  1.52  perseant 	simple_lock(&fs->lfs_interlock);
    560  1.49  perseant 	if (fs->lfs_favail > btofsb(fs, (1 << fs->lfs_bshift)))
    561  1.49  perseant 		fs->lfs_favail -= btofsb(fs, (1 << fs->lfs_bshift));
    562  1.52  perseant 	simple_lock(&lfs_subsys_lock);
    563  1.49  perseant 	if (locked_fakequeue_count > 0)
    564  1.49  perseant 		--locked_fakequeue_count;
    565  1.52  perseant 	simple_unlock(&lfs_subsys_lock);
    566  1.52  perseant 	simple_unlock(&fs->lfs_interlock);
    567  1.49  perseant }
    568  1.49  perseant 
    569  1.49  perseant void
    570  1.49  perseant lfs_deregister_block(struct vnode *vp, daddr_t lbn)
    571  1.49  perseant {
    572  1.49  perseant 	struct lfs *fs;
    573  1.49  perseant 	struct inode *ip;
    574  1.49  perseant 	struct lbnentry *lbp;
    575  1.53  perseant 	unsigned int hash;
    576  1.53  perseant 
    577  1.53  perseant 	ip = VTOI(vp);
    578  1.49  perseant 
    579  1.49  perseant 	/* Don't count metadata */
    580  1.53  perseant 	if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
    581  1.49  perseant 		return;
    582  1.49  perseant 
    583  1.49  perseant 	fs = ip->i_lfs;
    584  1.53  perseant 	hash = lfs_blist_hash(fs, ip, lbn);
    585  1.53  perseant 	LIST_FOREACH(lbp, HASH_HEADER(vp, hash), entry) {
    586  1.53  perseant 		if (lbp->lbn == lbn && lbp->vp == vp && lbp->fs == fs)
    587  1.49  perseant 			break;
    588  1.49  perseant 	}
    589  1.49  perseant 	if (lbp == NULL)
    590  1.49  perseant 		return;
    591  1.49  perseant 
    592  1.53  perseant 	lfs_do_deregister(fs, ip, lbp);
    593  1.49  perseant }
    594