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