Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.9
      1 /*	$NetBSD: lfs_balloc.c,v 1.9 1998/06/09 07:46:33 scottr 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 
     38 #if defined(_KERNEL) && !defined(_LKM)
     39 #include "opt_quota.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/buf.h>
     45 #include <sys/proc.h>
     46 #include <sys/vnode.h>
     47 #include <sys/mount.h>
     48 #include <sys/resourcevar.h>
     49 #include <sys/trace.h>
     50 
     51 #include <miscfs/specfs/specdev.h>
     52 
     53 #include <ufs/ufs/quota.h>
     54 #include <ufs/ufs/inode.h>
     55 #include <ufs/ufs/ufsmount.h>
     56 #include <ufs/ufs/ufs_extern.h>
     57 
     58 #include <ufs/lfs/lfs.h>
     59 #include <ufs/lfs/lfs_extern.h>
     60 
     61 #include <vm/vm.h>
     62 #include <vm/vm_extern.h>
     63 
     64 int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
     65 
     66 int
     67 lfs_balloc(vp, offset, iosize, lbn, bpp)
     68 	struct vnode *vp;
     69 	int offset;
     70 	u_long iosize;
     71 	ufs_daddr_t lbn;
     72 	struct buf **bpp;
     73 {
     74 	struct buf *ibp, *bp;
     75 	struct inode *ip;
     76 	struct lfs *fs;
     77 	struct indir indirs[NIADDR+2];
     78 	ufs_daddr_t	daddr, lastblock;
     79  	int bb;		/* number of disk blocks in a block disk blocks */
     80  	int error, frags, i, nsize, osize, num;
     81 
     82 	ip = VTOI(vp);
     83 	fs = ip->i_lfs;
     84 
     85 	/*
     86 	 * Three cases: it's a block beyond the end of file, it's a block in
     87 	 * the file that may or may not have been assigned a disk address or
     88 	 * we're writing an entire block.  Note, if the daddr is unassigned,
     89 	 * the block might still have existed in the cache (if it was read
     90 	 * or written earlier).  If it did, make sure we don't count it as a
     91 	 * new block or zero out its contents.  If it did not, make sure
     92 	 * we allocate any necessary indirect blocks.
     93 	 * If we are writing a block beyond the end of the file, we need to
     94 	 * check if the old last block was a fragment.  If it was, we need
     95 	 * to rewrite it.
     96 	 */
     97 
     98 	*bpp = NULL;
     99 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
    100 	if (error)
    101 		return (error);
    102 
    103 	/* Check for block beyond end of file and fragment extension needed. */
    104 	lastblock = lblkno(fs, ip->i_ffs_size);
    105 	if (lastblock < NDADDR && lastblock < lbn) {
    106 		osize = blksize(fs, ip, lastblock);
    107 		if (osize < fs->lfs_bsize && osize > 0) {
    108 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    109 			    lastblock, &bp)))
    110 				return(error);
    111 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
    112 #if defined(UVM)
    113 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    114 #else
    115 			vnode_pager_setsize(vp, ip->i_ffs_size);
    116 #endif
    117 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    118 			VOP_BWRITE(bp);
    119 		}
    120 	}
    121 
    122 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    123 	if (daddr == UNASSIGNED)
    124 		/* May need to allocate indirect blocks */
    125 		for (i = 1; i < num; ++i)
    126 			if (!indirs[i].in_exists) {
    127 				ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
    128 				    0, 0);
    129 				if ((ibp->b_flags & (B_DONE | B_DELWRI)))
    130 					panic ("Indirect block should not exist");
    131 
    132 				if (!ISSPACE(fs, bb, curproc->p_ucred)){
    133 					ibp->b_flags |= B_INVAL;
    134 					brelse(ibp);
    135 					return(ENOSPC);
    136 				} else {
    137 					ip->i_ffs_blocks += bb;
    138 					ip->i_lfs->lfs_bfree -= bb;
    139 					clrbuf(ibp);
    140 					if ((error = VOP_BWRITE(ibp)))
    141 						return(error);
    142 				}
    143 			}
    144 
    145 	/*
    146 	 * If the block we are writing is a direct block, it's the last
    147 	 * block in the file, and offset + iosize is less than a full
    148 	 * block, we can write one or more fragments.  There are two cases:
    149 	 * the block is brand new and we should allocate it the correct
    150 	 * size or it already exists and contains some fragments and
    151 	 * may need to extend it.
    152 	 */
    153 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) == lbn) {
    154 		nsize = fragroundup(fs, offset + iosize);
    155 		frags = numfrags(fs, nsize);
    156 		bb = fragstodb(fs, frags);
    157 		if (lblktosize(fs, lbn) == ip->i_ffs_size)
    158 			/* Brand new block or fragment */
    159 			*bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    160 		else {
    161 			/* Extend existing block */
    162 			if ((error =
    163 			    lfs_fragextend(vp, (int)blksize(fs, ip, lbn),
    164 			    nsize, lbn, &bp)))
    165 				return(error);
    166 			*bpp = bp;
    167 		}
    168 	} else {
    169 		/*
    170 		 * Get the existing block from the cache either because the
    171 		 * block is 1) not a direct block or because it's not the last
    172 		 * block in the file.
    173 		 */
    174 		frags = dbtofrags(fs, bb);
    175 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    176 	}
    177 
    178 	/*
    179 	 * The block we are writing may be a brand new block
    180 	 * in which case we need to do accounting (i.e. check
    181 	 * for free space and update the inode number of blocks.
    182 	 */
    183 	if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
    184 		if (daddr == UNASSIGNED)
    185 			if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    186 				bp->b_flags |= B_INVAL;
    187 				brelse(bp);
    188 				return(ENOSPC);
    189 			} else {
    190 				ip->i_ffs_blocks += bb;
    191 				ip->i_lfs->lfs_bfree -= bb;
    192 				if (iosize != fs->lfs_bsize)
    193 					clrbuf(bp);
    194 			}
    195 		else if (iosize == fs->lfs_bsize)
    196 			/* Optimization: I/O is unnecessary. */
    197 			bp->b_blkno = daddr;
    198 		else  {
    199 			/*
    200 			 * We need to read the block to preserve the
    201 			 * existing bytes.
    202 			 */
    203 			bp->b_blkno = daddr;
    204 			bp->b_flags |= B_READ;
    205 			VOP_STRATEGY(bp);
    206 			return(biowait(bp));
    207 		}
    208 	}
    209 	return (0);
    210 }
    211 
    212 int
    213 lfs_fragextend(vp, osize, nsize, lbn, bpp)
    214 	struct vnode *vp;
    215 	int osize;
    216 	int nsize;
    217 	ufs_daddr_t lbn;
    218 	struct buf **bpp;
    219 {
    220 	struct inode *ip;
    221 	struct lfs *fs;
    222 	long bb;
    223 	int error;
    224 
    225 	ip = VTOI(vp);
    226 	fs = ip->i_lfs;
    227 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
    228 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    229 		return(ENOSPC);
    230 	}
    231 
    232 	if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
    233 		brelse(*bpp);
    234 		return(error);
    235 	}
    236 #ifdef QUOTA
    237 	if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
    238 		brelse(*bpp);
    239 		return (error);
    240 	}
    241 #endif
    242 	ip->i_ffs_blocks += bb;
    243 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    244 	fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
    245 	allocbuf(*bpp, nsize);
    246 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    247 	return(0);
    248 }
    249