Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.1
      1 /*
      2  * Copyright (c) 1989, 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)lfs_balloc.c	8.1 (Berkeley) 6/11/93
     34  *	$Id: lfs_balloc.c,v 1.1 1994/06/08 11:42:26 mycroft Exp $
     35  */
     36 #include <sys/param.h>
     37 #include <sys/buf.h>
     38 #include <sys/proc.h>
     39 #include <sys/vnode.h>
     40 #include <sys/mount.h>
     41 #include <sys/resourcevar.h>
     42 #include <sys/trace.h>
     43 
     44 #include <miscfs/specfs/specdev.h>
     45 
     46 #include <ufs/ufs/quota.h>
     47 #include <ufs/ufs/inode.h>
     48 #include <ufs/ufs/ufsmount.h>
     49 
     50 #include <ufs/lfs/lfs.h>
     51 #include <ufs/lfs/lfs_extern.h>
     52 
     53 int
     54 lfs_balloc(vp, iosize, lbn, bpp)
     55 	struct vnode *vp;
     56 	u_long iosize;
     57 	daddr_t lbn;
     58 	struct buf **bpp;
     59 {
     60 	struct buf *ibp, *bp;
     61 	struct inode *ip;
     62 	struct lfs *fs;
     63 	struct indir indirs[NIADDR+2];
     64 	daddr_t daddr;
     65 	int bb, error, i, num;
     66 
     67 	ip = VTOI(vp);
     68 	fs = ip->i_lfs;
     69 
     70 	/*
     71 	 * Three cases: it's a block beyond the end of file, it's a block in
     72 	 * the file that may or may not have been assigned a disk address or
     73 	 * we're writing an entire block.  Note, if the daddr is unassigned,
     74 	 * the block might still have existed in the cache (if it was read
     75 	 * or written earlier).  If it did, make sure we don't count it as a
     76 	 * new block or zero out its contents.  If it did not, make sure
     77 	 * we allocate any necessary indirect blocks.
     78 	 */
     79 
     80 	*bpp = NULL;
     81 	if (error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL ))
     82 		return (error);
     83 
     84 	*bpp = bp = getblk(vp, lbn, fs->lfs_bsize, 0, 0);
     85 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
     86 	if (daddr == UNASSIGNED)
     87 		/* May need to allocate indirect blocks */
     88 		for (i = 1; i < num; ++i)
     89 			if (!indirs[i].in_exists) {
     90 				ibp =
     91 				    getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
     92 					0, 0);
     93 				if (!(ibp->b_flags & (B_DONE | B_DELWRI))) {
     94 					if (!ISSPACE(fs, bb, curproc->p_ucred)){
     95 						ibp->b_flags |= B_INVAL;
     96 						brelse(ibp);
     97 						error = ENOSPC;
     98 					} else {
     99 						ip->i_blocks += bb;
    100 						ip->i_lfs->lfs_bfree -= bb;
    101 						clrbuf(ibp);
    102 						error = VOP_BWRITE(ibp);
    103 					}
    104 				} else
    105 					panic ("Indirect block should not exist");
    106 			}
    107 	if (error) {
    108 		if (bp)
    109 			brelse(bp);
    110 		return(error);
    111 	}
    112 
    113 
    114 	/* Now, we may need to allocate the data block */
    115 	if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
    116 		if (daddr == UNASSIGNED)
    117 			if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    118 				bp->b_flags |= B_INVAL;
    119 				brelse(bp);
    120 				return(ENOSPC);
    121 			} else {
    122 				ip->i_blocks += bb;
    123 				ip->i_lfs->lfs_bfree -= bb;
    124 				if (iosize != fs->lfs_bsize)
    125 					clrbuf(bp);
    126 			}
    127 		else if (iosize == fs->lfs_bsize)
    128 			bp->b_blkno = daddr;		/* Skip the I/O */
    129 		else  {
    130 			bp->b_blkno = daddr;
    131 			bp->b_flags |= B_READ;
    132 			VOP_STRATEGY(bp);
    133 			return(biowait(bp));
    134 		}
    135 	}
    136 	return (error);
    137 }
    138