Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.5
      1 /*	$NetBSD: lfs_balloc.c,v 1.5 1998/03/01 02:23:24 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)lfs_balloc.c	8.4 (Berkeley) 5/8/95
     36  */
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/buf.h>
     40 #include <sys/proc.h>
     41 #include <sys/vnode.h>
     42 #include <sys/mount.h>
     43 #include <sys/resourcevar.h>
     44 #include <sys/trace.h>
     45 
     46 #include <miscfs/specfs/specdev.h>
     47 
     48 #include <ufs/ufs/quota.h>
     49 #include <ufs/ufs/inode.h>
     50 #include <ufs/ufs/ufsmount.h>
     51 #include <ufs/ufs/ufs_extern.h>
     52 
     53 #include <ufs/lfs/lfs.h>
     54 #include <ufs/lfs/lfs_extern.h>
     55 
     56 #include <vm/vm.h>
     57 #include <vm/vm_extern.h>
     58 
     59 int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
     60 
     61 int
     62 lfs_balloc(vp, offset, iosize, lbn, bpp)
     63 	struct vnode *vp;
     64 	int offset;
     65 	u_long iosize;
     66 	ufs_daddr_t lbn;
     67 	struct buf **bpp;
     68 {
     69 	struct buf *ibp, *bp;
     70 	struct inode *ip;
     71 	struct lfs *fs;
     72 	struct indir indirs[NIADDR+2];
     73 	ufs_daddr_t	daddr, lastblock;
     74  	int bb;		/* number of disk blocks in a block disk blocks */
     75  	int error, frags, i, nsize, osize, num;
     76 
     77 	ip = VTOI(vp);
     78 	fs = ip->i_lfs;
     79 
     80 	/*
     81 	 * Three cases: it's a block beyond the end of file, it's a block in
     82 	 * the file that may or may not have been assigned a disk address or
     83 	 * we're writing an entire block.  Note, if the daddr is unassigned,
     84 	 * the block might still have existed in the cache (if it was read
     85 	 * or written earlier).  If it did, make sure we don't count it as a
     86 	 * new block or zero out its contents.  If it did not, make sure
     87 	 * we allocate any necessary indirect blocks.
     88 	 * If we are writing a block beyond the end of the file, we need to
     89 	 * check if the old last block was a fragment.  If it was, we need
     90 	 * to rewrite it.
     91 	 */
     92 
     93 	*bpp = NULL;
     94 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
     95 	if (error)
     96 		return (error);
     97 
     98 	/* Check for block beyond end of file and fragment extension needed. */
     99 	lastblock = lblkno(fs, ip->i_ffs_size);
    100 	if (lastblock < NDADDR && lastblock < lbn) {
    101 		osize = blksize(fs, ip, lastblock);
    102 		if (osize < fs->lfs_bsize && osize > 0) {
    103 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    104 			    lastblock, &bp)))
    105 				return(error);
    106 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
    107 			vnode_pager_setsize(vp, (u_long)ip->i_ffs_size);
    108 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    109 			VOP_BWRITE(bp);
    110 		}
    111 	}
    112 
    113 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    114 	if (daddr == UNASSIGNED)
    115 		/* May need to allocate indirect blocks */
    116 		for (i = 1; i < num; ++i)
    117 			if (!indirs[i].in_exists) {
    118 				ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
    119 				    0, 0);
    120 				if ((ibp->b_flags & (B_DONE | B_DELWRI)))
    121 					panic ("Indirect block should not exist");
    122 
    123 				if (!ISSPACE(fs, bb, curproc->p_ucred)){
    124 					ibp->b_flags |= B_INVAL;
    125 					brelse(ibp);
    126 					return(ENOSPC);
    127 				} else {
    128 					ip->i_ffs_blocks += bb;
    129 					ip->i_lfs->lfs_bfree -= bb;
    130 					clrbuf(ibp);
    131 					if ((error = VOP_BWRITE(ibp)))
    132 						return(error);
    133 				}
    134 			}
    135 
    136 	/*
    137 	 * If the block we are writing is a direct block, it's the last
    138 	 * block in the file, and offset + iosize is less than a full
    139 	 * block, we can write one or more fragments.  There are two cases:
    140 	 * the block is brand new and we should allocate it the correct
    141 	 * size or it already exists and contains some fragments and
    142 	 * may need to extend it.
    143 	 */
    144 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) == lbn) {
    145 		nsize = fragroundup(fs, offset + iosize);
    146 		frags = numfrags(fs, nsize);
    147 		bb = fragstodb(fs, frags);
    148 		if (lblktosize(fs, lbn) == ip->i_ffs_size)
    149 			/* Brand new block or fragment */
    150 			*bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    151 		else {
    152 			/* Extend existing block */
    153 			if ((error =
    154 			    lfs_fragextend(vp, (int)blksize(fs, ip, lbn),
    155 			    nsize, lbn, &bp)))
    156 				return(error);
    157 			*bpp = bp;
    158 		}
    159 	} else {
    160 		/*
    161 		 * Get the existing block from the cache either because the
    162 		 * block is 1) not a direct block or because it's not the last
    163 		 * block in the file.
    164 		 */
    165 		frags = dbtofrags(fs, bb);
    166 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    167 	}
    168 
    169 	/*
    170 	 * The block we are writing may be a brand new block
    171 	 * in which case we need to do accounting (i.e. check
    172 	 * for free space and update the inode number of blocks.
    173 	 */
    174 	if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
    175 		if (daddr == UNASSIGNED)
    176 			if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    177 				bp->b_flags |= B_INVAL;
    178 				brelse(bp);
    179 				return(ENOSPC);
    180 			} else {
    181 				ip->i_ffs_blocks += bb;
    182 				ip->i_lfs->lfs_bfree -= bb;
    183 				if (iosize != fs->lfs_bsize)
    184 					clrbuf(bp);
    185 			}
    186 		else if (iosize == fs->lfs_bsize)
    187 			/* Optimization: I/O is unnecessary. */
    188 			bp->b_blkno = daddr;
    189 		else  {
    190 			/*
    191 			 * We need to read the block to preserve the
    192 			 * existing bytes.
    193 			 */
    194 			bp->b_blkno = daddr;
    195 			bp->b_flags |= B_READ;
    196 			VOP_STRATEGY(bp);
    197 			return(biowait(bp));
    198 		}
    199 	}
    200 	return (0);
    201 }
    202 
    203 int
    204 lfs_fragextend(vp, osize, nsize, lbn, bpp)
    205 	struct vnode *vp;
    206 	int osize;
    207 	int nsize;
    208 	ufs_daddr_t lbn;
    209 	struct buf **bpp;
    210 {
    211 	struct inode *ip;
    212 	struct lfs *fs;
    213 	long bb;
    214 	int error;
    215 
    216 	ip = VTOI(vp);
    217 	fs = ip->i_lfs;
    218 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
    219 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    220 		return(ENOSPC);
    221 	}
    222 
    223 	if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
    224 		brelse(*bpp);
    225 		return(error);
    226 	}
    227 #ifdef QUOTA
    228 	if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
    229 		brelse(*bpp);
    230 		return (error);
    231 	}
    232 #endif
    233 	ip->i_ffs_blocks += bb;
    234 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    235 	fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
    236 	allocbuf(*bpp, nsize);
    237 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    238 	return(0);
    239 }
    240