Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.12.4.3
      1  1.12.4.3  perseant /*	$NetBSD: lfs_balloc.c,v 1.12.4.3 1999/08/31 21:03:45 perseant Exp $	*/
      2       1.2       cgd 
      3      1.11  perseant /*-
      4      1.11  perseant  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5      1.11  perseant  * All rights reserved.
      6      1.11  perseant  *
      7      1.11  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8      1.11  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9      1.11  perseant  *
     10      1.11  perseant  * Redistribution and use in source and binary forms, with or without
     11      1.11  perseant  * modification, are permitted provided that the following conditions
     12      1.11  perseant  * are met:
     13      1.11  perseant  * 1. Redistributions of source code must retain the above copyright
     14      1.11  perseant  *    notice, this list of conditions and the following disclaimer.
     15      1.11  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.11  perseant  *    notice, this list of conditions and the following disclaimer in the
     17      1.11  perseant  *    documentation and/or other materials provided with the distribution.
     18      1.11  perseant  * 3. All advertising materials mentioning features or use of this software
     19      1.11  perseant  *    must display the following acknowledgement:
     20      1.11  perseant  *      This product includes software developed by the NetBSD
     21      1.11  perseant  *      Foundation, Inc. and its contributors.
     22      1.11  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.11  perseant  *    contributors may be used to endorse or promote products derived
     24      1.11  perseant  *    from this software without specific prior written permission.
     25      1.11  perseant  *
     26      1.11  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.11  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.11  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.11  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.11  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.11  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.11  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.11  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.11  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.11  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.11  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37      1.11  perseant  */
     38       1.1   mycroft /*
     39       1.1   mycroft  * Copyright (c) 1989, 1991, 1993
     40       1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     41       1.1   mycroft  *
     42       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     43       1.1   mycroft  * modification, are permitted provided that the following conditions
     44       1.1   mycroft  * are met:
     45       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     46       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     47       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     50       1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     51       1.1   mycroft  *    must display the following acknowledgement:
     52       1.1   mycroft  *	This product includes software developed by the University of
     53       1.1   mycroft  *	California, Berkeley and its contributors.
     54       1.1   mycroft  * 4. Neither the name of the University nor the names of its contributors
     55       1.1   mycroft  *    may be used to endorse or promote products derived from this software
     56       1.1   mycroft  *    without specific prior written permission.
     57       1.1   mycroft  *
     58       1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59       1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60       1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61       1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62       1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63       1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64       1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65       1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66       1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67       1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68       1.1   mycroft  * SUCH DAMAGE.
     69       1.1   mycroft  *
     70       1.5      fvdl  *	@(#)lfs_balloc.c	8.4 (Berkeley) 5/8/95
     71       1.1   mycroft  */
     72       1.8    scottr 
     73       1.9    scottr #if defined(_KERNEL) && !defined(_LKM)
     74       1.8    scottr #include "opt_quota.h"
     75       1.9    scottr #endif
     76       1.8    scottr 
     77       1.1   mycroft #include <sys/param.h>
     78       1.3  christos #include <sys/systm.h>
     79       1.1   mycroft #include <sys/buf.h>
     80       1.1   mycroft #include <sys/proc.h>
     81       1.1   mycroft #include <sys/vnode.h>
     82       1.1   mycroft #include <sys/mount.h>
     83       1.1   mycroft #include <sys/resourcevar.h>
     84       1.1   mycroft #include <sys/trace.h>
     85       1.1   mycroft 
     86       1.1   mycroft #include <miscfs/specfs/specdev.h>
     87       1.1   mycroft 
     88       1.1   mycroft #include <ufs/ufs/quota.h>
     89       1.1   mycroft #include <ufs/ufs/inode.h>
     90       1.1   mycroft #include <ufs/ufs/ufsmount.h>
     91       1.3  christos #include <ufs/ufs/ufs_extern.h>
     92       1.1   mycroft 
     93       1.1   mycroft #include <ufs/lfs/lfs.h>
     94       1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     95       1.1   mycroft 
     96       1.5      fvdl #include <vm/vm.h>
     97      1.12       mrg #include <uvm/uvm_extern.h>
     98       1.5      fvdl 
     99       1.5      fvdl int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
    100       1.5      fvdl 
    101  1.12.4.3  perseant /*
    102  1.12.4.3  perseant  * Balloc defines the structure of file system storage
    103  1.12.4.3  perseant  * by allocating the physical blocks on a device given
    104  1.12.4.3  perseant  * the inode and the logical block number in a file.
    105  1.12.4.3  perseant  */
    106  1.12.4.3  perseant 
    107  1.12.4.3  perseant int
    108  1.12.4.3  perseant lfs_balloc(v)
    109  1.12.4.3  perseant 	void *v;
    110  1.12.4.3  perseant {
    111  1.12.4.3  perseant 	struct vop_balloc_args /* {
    112  1.12.4.3  perseant 		struct vnode *a_vp;
    113  1.12.4.3  perseant 		off_t a_offset;
    114  1.12.4.3  perseant 		off_t a_length;
    115  1.12.4.3  perseant 		struct ucred *a_cred;
    116  1.12.4.3  perseant 		int a_flags;
    117  1.12.4.3  perseant 	} */ *ap = v;
    118  1.12.4.3  perseant 
    119  1.12.4.3  perseant 	off_t off, len;
    120  1.12.4.3  perseant 	struct vnode *vp = ap->a_vp;
    121  1.12.4.3  perseant 	struct inode *ip = VTOI(vp);
    122  1.12.4.3  perseant 	struct lfs *fs = ip->i_lfs;
    123  1.12.4.3  perseant 	int error, delta, bshift, bsize;
    124  1.12.4.3  perseant 
    125  1.12.4.3  perseant 	bshift = fs->lfs_bshift;
    126  1.12.4.3  perseant 	bsize = 1 << bshift;
    127  1.12.4.3  perseant 
    128  1.12.4.3  perseant 	off = ap->a_offset;
    129  1.12.4.3  perseant 	len = ap->a_length;
    130  1.12.4.3  perseant 
    131  1.12.4.3  perseant 	delta = off & (bsize - 1);
    132  1.12.4.3  perseant 	off -= delta;
    133  1.12.4.3  perseant 	len += delta;
    134  1.12.4.3  perseant 
    135  1.12.4.3  perseant 	while (len > 0) {
    136  1.12.4.3  perseant 		bsize = min(bsize, len);
    137  1.12.4.3  perseant 
    138  1.12.4.3  perseant 		if ((error = lfs_balloc1(vp, blkoff(fs,off), bsize,
    139  1.12.4.3  perseant 					 lblkno(fs, off), NULL))) {
    140  1.12.4.3  perseant 			return error;
    141  1.12.4.3  perseant 		}
    142  1.12.4.3  perseant 
    143  1.12.4.3  perseant 		/*
    144  1.12.4.3  perseant 		 * increase file size now, VOP_BALLOC() requires that
    145  1.12.4.3  perseant 		 * EOF be up-to-date before each call.
    146  1.12.4.3  perseant 		 */
    147  1.12.4.3  perseant 
    148  1.12.4.3  perseant 		if (ip->i_ffs_size < off + bsize) {
    149  1.12.4.3  perseant 			ip->i_ffs_size = off + bsize;
    150  1.12.4.3  perseant 			if (vp->v_uvm.u_size < ip->i_ffs_size) {
    151  1.12.4.3  perseant 				uvm_vnp_setsize(vp, ip->i_ffs_size);
    152  1.12.4.3  perseant 			}
    153  1.12.4.3  perseant 		}
    154  1.12.4.3  perseant 
    155  1.12.4.3  perseant 		off += bsize;
    156  1.12.4.3  perseant 		len -= bsize;
    157  1.12.4.3  perseant 	}
    158  1.12.4.3  perseant 	return 0;
    159  1.12.4.3  perseant }
    160  1.12.4.3  perseant 
    161       1.1   mycroft int
    162  1.12.4.3  perseant lfs_balloc1(vp, offset, iosize, lbn, bpp)
    163       1.1   mycroft 	struct vnode *vp;
    164       1.5      fvdl 	int offset;
    165       1.1   mycroft 	u_long iosize;
    166       1.5      fvdl 	ufs_daddr_t lbn;
    167       1.1   mycroft 	struct buf **bpp;
    168       1.1   mycroft {
    169       1.1   mycroft 	struct buf *ibp, *bp;
    170       1.1   mycroft 	struct inode *ip;
    171       1.1   mycroft 	struct lfs *fs;
    172  1.12.4.3  perseant 	struct indir *ap, indirs[NIADDR+2];
    173       1.5      fvdl 	ufs_daddr_t	daddr, lastblock;
    174      1.11  perseant 	int bb;		/* number of disk blocks in a block disk blocks */
    175      1.11  perseant 	int error, frags, i, nsize, osize, num;
    176      1.11  perseant 
    177       1.1   mycroft 	ip = VTOI(vp);
    178       1.1   mycroft 	fs = ip->i_lfs;
    179  1.12.4.3  perseant 
    180  1.12.4.3  perseant 	/* XXX printf("lfs_balloc1(%p,%d,%ld,%x,%p)\n",
    181  1.12.4.3  perseant 			 vp, offset, iosize, lbn, bpp); */
    182      1.11  perseant 
    183      1.11  perseant #ifdef DEBUG
    184      1.11  perseant 	if(!VOP_ISLOCKED(vp)) {
    185      1.11  perseant 		printf("lfs_balloc: warning: ino %d not locked\n",ip->i_number);
    186      1.11  perseant 	}
    187      1.11  perseant #endif
    188      1.11  perseant 
    189       1.1   mycroft 	/*
    190       1.1   mycroft 	 * Three cases: it's a block beyond the end of file, it's a block in
    191       1.1   mycroft 	 * the file that may or may not have been assigned a disk address or
    192       1.1   mycroft 	 * we're writing an entire block.  Note, if the daddr is unassigned,
    193       1.1   mycroft 	 * the block might still have existed in the cache (if it was read
    194      1.11  perseant 	 * or written earlier).	 If it did, make sure we don't count it as a
    195      1.11  perseant 	 * new block or zero out its contents.	If it did not, make sure
    196       1.1   mycroft 	 * we allocate any necessary indirect blocks.
    197       1.5      fvdl 	 * If we are writing a block beyond the end of the file, we need to
    198      1.11  perseant 	 * check if the old last block was a fragment.	If it was, we need
    199       1.5      fvdl 	 * to rewrite it.
    200       1.1   mycroft 	 */
    201      1.11  perseant 
    202  1.12.4.2       chs 	if (bpp != NULL) {
    203  1.12.4.2       chs 		*bpp = NULL;
    204  1.12.4.2       chs 	}
    205       1.5      fvdl 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
    206       1.3  christos 	if (error)
    207       1.1   mycroft 		return (error);
    208      1.11  perseant 
    209       1.5      fvdl 	/* Check for block beyond end of file and fragment extension needed. */
    210       1.5      fvdl 	lastblock = lblkno(fs, ip->i_ffs_size);
    211       1.5      fvdl 	if (lastblock < NDADDR && lastblock < lbn) {
    212       1.5      fvdl 		osize = blksize(fs, ip, lastblock);
    213       1.5      fvdl 		if (osize < fs->lfs_bsize && osize > 0) {
    214       1.5      fvdl 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    215  1.12.4.3  perseant 						    lastblock, (bpp ? &bp : NULL))))
    216       1.5      fvdl 				return(error);
    217       1.5      fvdl 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
    218       1.7  drochner 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    219       1.5      fvdl 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    220       1.5      fvdl 			VOP_BWRITE(bp);
    221       1.5      fvdl 		}
    222       1.5      fvdl 	}
    223      1.11  perseant 
    224       1.1   mycroft 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    225  1.12.4.3  perseant 	if (daddr == UNASSIGNED) {
    226       1.1   mycroft 		/* May need to allocate indirect blocks */
    227  1.12.4.3  perseant 		for (i = 1; i < num; ++i) {
    228       1.1   mycroft 			if (!indirs[i].in_exists) {
    229       1.5      fvdl 				ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
    230      1.11  perseant 					     0, 0);
    231       1.5      fvdl 				if ((ibp->b_flags & (B_DONE | B_DELWRI)))
    232       1.1   mycroft 					panic ("Indirect block should not exist");
    233       1.5      fvdl 
    234       1.5      fvdl 				if (!ISSPACE(fs, bb, curproc->p_ucred)){
    235       1.5      fvdl 					ibp->b_flags |= B_INVAL;
    236       1.5      fvdl 					brelse(ibp);
    237       1.5      fvdl 					return(ENOSPC);
    238       1.5      fvdl 				} else {
    239       1.5      fvdl 					ip->i_ffs_blocks += bb;
    240       1.5      fvdl 					ip->i_lfs->lfs_bfree -= bb;
    241       1.5      fvdl 					clrbuf(ibp);
    242       1.5      fvdl 					if ((error = VOP_BWRITE(ibp)))
    243       1.5      fvdl 						return(error);
    244       1.5      fvdl 				}
    245       1.1   mycroft 			}
    246  1.12.4.3  perseant 		}
    247  1.12.4.3  perseant 	}
    248      1.11  perseant 
    249       1.5      fvdl 	/*
    250       1.5      fvdl 	 * If the block we are writing is a direct block, it's the last
    251       1.5      fvdl 	 * block in the file, and offset + iosize is less than a full
    252       1.5      fvdl 	 * block, we can write one or more fragments.  There are two cases:
    253       1.5      fvdl 	 * the block is brand new and we should allocate it the correct
    254       1.5      fvdl 	 * size or it already exists and contains some fragments and
    255       1.5      fvdl 	 * may need to extend it.
    256       1.5      fvdl 	 */
    257      1.11  perseant 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
    258      1.11  perseant 		osize = blksize(fs, ip, lbn);
    259       1.5      fvdl 		nsize = fragroundup(fs, offset + iosize);
    260       1.5      fvdl 		frags = numfrags(fs, nsize);
    261       1.5      fvdl 		bb = fragstodb(fs, frags);
    262  1.12.4.2       chs 		if (lblktosize(fs, lbn) >= ip->i_ffs_size) {
    263       1.5      fvdl 			/* Brand new block or fragment */
    264  1.12.4.2       chs 			if (bpp != NULL) {
    265  1.12.4.2       chs 				*bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    266  1.12.4.2       chs 			}
    267  1.12.4.3  perseant 		} else {
    268      1.11  perseant 			if (nsize <= osize) {
    269      1.11  perseant 				/* No need to extend */
    270      1.11  perseant 				/* XXX KS - Are we wasting space? */
    271  1.12.4.3  perseant 				if (bpp != NULL && (error = bread(vp, lbn, osize, NOCRED, &bp)))
    272      1.11  perseant 					return error;
    273      1.11  perseant 			} else {
    274      1.11  perseant 				/* Extend existing block */
    275  1.12.4.3  perseant 				if (bpp != NULL && (error =
    276      1.11  perseant 				     lfs_fragextend(vp, osize, nsize, lbn, &bp)))
    277      1.11  perseant 					return(error);
    278      1.11  perseant 			}
    279  1.12.4.2       chs 			if (bpp != NULL) {
    280  1.12.4.2       chs 				*bpp = bp;
    281  1.12.4.2       chs 			}
    282       1.5      fvdl 		}
    283       1.5      fvdl 	} else {
    284       1.5      fvdl 		/*
    285       1.5      fvdl 		 * Get the existing block from the cache either because the
    286      1.11  perseant 		 * block 1) is not a direct block or 2) is not the last
    287       1.5      fvdl 		 * block in the file.
    288       1.5      fvdl 		 */
    289       1.5      fvdl 		frags = dbtofrags(fs, bb);
    290  1.12.4.2       chs 		if (bpp != NULL) {
    291  1.12.4.2       chs 			*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    292  1.12.4.2       chs 		}
    293       1.1   mycroft 	}
    294      1.11  perseant 
    295       1.5      fvdl 	/*
    296       1.5      fvdl 	 * The block we are writing may be a brand new block
    297       1.5      fvdl 	 * in which case we need to do accounting (i.e. check
    298       1.5      fvdl 	 * for free space and update the inode number of blocks.
    299       1.5      fvdl 	 */
    300  1.12.4.3  perseant 	if (bpp == NULL && daddr == UNASSIGNED) {
    301  1.12.4.3  perseant 		/*
    302  1.12.4.3  perseant 		 * If bpp is NULL we are being called from UBC.  In this
    303  1.12.4.3  perseant 		 * case, all we have to do is block accounting for the
    304  1.12.4.3  perseant 		 * inode and the filesystem.
    305  1.12.4.3  perseant 		 */
    306  1.12.4.3  perseant 		if (!ISSPACE(fs, bb, curproc->p_ucred))
    307  1.12.4.3  perseant 			return(ENOSPC);
    308  1.12.4.3  perseant 
    309  1.12.4.3  perseant 		ip->i_ffs_blocks += bb;
    310  1.12.4.3  perseant 		ip->i_lfs->lfs_bfree -= bb;
    311  1.12.4.3  perseant 
    312  1.12.4.3  perseant 		/* Assign a daddr != UNASSIGNED, so we don't overcount */
    313  1.12.4.3  perseant                 switch (num) {
    314  1.12.4.3  perseant                 case 0:
    315  1.12.4.3  perseant                         ip->i_ffs_db[lbn] = UNWRITTEN;
    316  1.12.4.3  perseant                         break;
    317  1.12.4.3  perseant                 case 1:
    318  1.12.4.3  perseant                         ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    319  1.12.4.3  perseant                         break;
    320  1.12.4.3  perseant                 default:
    321  1.12.4.3  perseant                         ap = &indirs[num - 1];
    322  1.12.4.3  perseant                         if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
    323  1.12.4.3  perseant                                 panic("lfs_balloc: bread bno %d", ap->in_lbn);
    324  1.12.4.3  perseant 			/* The indirect block exists, no need to account it */
    325  1.12.4.3  perseant                         ((ufs_daddr_t *)bp->b_data)[ap->in_off] = UNWRITTEN;
    326  1.12.4.3  perseant                         VOP_BWRITE(bp);
    327  1.12.4.3  perseant                 }
    328  1.12.4.3  perseant 	} else if (bpp && !(bp->b_flags & (B_DONE | B_DELWRI))) {
    329  1.12.4.3  perseant 		if (daddr == UNASSIGNED) {
    330       1.1   mycroft 			if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    331       1.1   mycroft 				bp->b_flags |= B_INVAL;
    332       1.1   mycroft 				brelse(bp);
    333       1.1   mycroft 				return(ENOSPC);
    334       1.1   mycroft 			} else {
    335       1.4    bouyer 				ip->i_ffs_blocks += bb;
    336       1.1   mycroft 				ip->i_lfs->lfs_bfree -= bb;
    337       1.1   mycroft 				if (iosize != fs->lfs_bsize)
    338       1.1   mycroft 					clrbuf(bp);
    339       1.1   mycroft 			}
    340  1.12.4.3  perseant 		} else if (iosize == fs->lfs_bsize) {
    341       1.5      fvdl 			/* Optimization: I/O is unnecessary. */
    342       1.5      fvdl 			bp->b_blkno = daddr;
    343  1.12.4.3  perseant 		} else  {
    344       1.5      fvdl 			/*
    345       1.5      fvdl 			 * We need to read the block to preserve the
    346       1.5      fvdl 			 * existing bytes.
    347       1.5      fvdl 			 */
    348       1.1   mycroft 			bp->b_blkno = daddr;
    349       1.1   mycroft 			bp->b_flags |= B_READ;
    350       1.1   mycroft 			VOP_STRATEGY(bp);
    351       1.1   mycroft 			return(biowait(bp));
    352       1.1   mycroft 		}
    353       1.1   mycroft 	}
    354      1.11  perseant 
    355       1.5      fvdl 	return (0);
    356       1.5      fvdl }
    357       1.5      fvdl 
    358       1.5      fvdl int
    359       1.5      fvdl lfs_fragextend(vp, osize, nsize, lbn, bpp)
    360       1.5      fvdl 	struct vnode *vp;
    361       1.5      fvdl 	int osize;
    362       1.5      fvdl 	int nsize;
    363       1.5      fvdl 	ufs_daddr_t lbn;
    364       1.5      fvdl 	struct buf **bpp;
    365       1.5      fvdl {
    366       1.5      fvdl 	struct inode *ip;
    367       1.5      fvdl 	struct lfs *fs;
    368       1.5      fvdl 	long bb;
    369       1.5      fvdl 	int error;
    370      1.11  perseant 	extern long locked_queue_bytes;
    371  1.12.4.1   thorpej 	struct buf *ibp;
    372  1.12.4.1   thorpej 	SEGUSE *sup;
    373  1.12.4.3  perseant 	daddr_t daddr;
    374       1.5      fvdl 
    375       1.5      fvdl 	ip = VTOI(vp);
    376       1.5      fvdl 	fs = ip->i_lfs;
    377      1.11  perseant 
    378       1.5      fvdl 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
    379      1.11  perseant  top:
    380       1.5      fvdl 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    381       1.5      fvdl 		return(ENOSPC);
    382       1.5      fvdl 	}
    383  1.12.4.3  perseant 	if (bpp) {
    384  1.12.4.3  perseant 		if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
    385  1.12.4.3  perseant 			brelse(*bpp);
    386  1.12.4.3  perseant 			return(error);
    387  1.12.4.3  perseant 		}
    388  1.12.4.3  perseant 		daddr = (*bpp)->b_blkno;
    389  1.12.4.3  perseant 	} else {
    390  1.12.4.3  perseant 		/* We still need the daddr, just not contents. */
    391  1.12.4.3  perseant 		if((error = VOP_BMAP(vp, lbn, NULL, &daddr, NULL)))
    392  1.12.4.3  perseant 			return (error);
    393       1.5      fvdl 	}
    394  1.12.4.1   thorpej 
    395  1.12.4.1   thorpej 	/*
    396  1.12.4.1   thorpej  	 * Fix the allocation for this fragment so that it looks like the
    397  1.12.4.1   thorpej          * source segment contained a block of the new size.  This overcounts;
    398  1.12.4.1   thorpej 	 * but the overcount only lasts until the block in question
    399  1.12.4.1   thorpej 	 * is written, so the on-disk live bytes count is always correct.
    400  1.12.4.1   thorpej 	 */
    401  1.12.4.3  perseant 	LFS_SEGENTRY(sup, fs, datosn(fs,daddr), ibp);
    402  1.12.4.1   thorpej 	sup->su_nbytes += (nsize-osize);
    403  1.12.4.1   thorpej 	VOP_BWRITE(ibp);
    404  1.12.4.1   thorpej 
    405       1.5      fvdl #ifdef QUOTA
    406       1.5      fvdl 	if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
    407  1.12.4.3  perseant 		if (bpp)
    408  1.12.4.3  perseant 			brelse(*bpp);
    409       1.5      fvdl 		return (error);
    410       1.5      fvdl 	}
    411       1.5      fvdl #endif
    412      1.11  perseant 	/*
    413      1.11  perseant 	 * XXX - KS - Don't change size while we're gathered, as we could
    414      1.11  perseant 	 * then overlap another buffer in lfs_writeseg.
    415      1.11  perseant 	 */
    416  1.12.4.3  perseant 	if(bpp && (*bpp)->b_flags & B_GATHERED) {
    417      1.11  perseant 		(*bpp)->b_flags |= B_NEEDCOMMIT; /* XXX KS - what flag to use? */
    418      1.11  perseant 		brelse(*bpp);
    419      1.11  perseant 		tsleep(*bpp, (PRIBIO+1), "lfs_fragextend", 0);
    420      1.11  perseant 		goto top;
    421      1.11  perseant 	}
    422       1.5      fvdl 	ip->i_ffs_blocks += bb;
    423       1.5      fvdl 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    424       1.5      fvdl 	fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
    425  1.12.4.3  perseant 	if (bpp) {
    426  1.12.4.3  perseant 		if((*bpp)->b_flags & B_LOCKED)
    427  1.12.4.3  perseant 			locked_queue_bytes += (nsize - osize);
    428  1.12.4.3  perseant 		allocbuf(*bpp, nsize);
    429  1.12.4.3  perseant 		bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    430  1.12.4.3  perseant 	}
    431       1.5      fvdl 	return(0);
    432       1.1   mycroft }
    433