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