Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.1.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  *	@(#)lfs_balloc.c	8.1 (Berkeley) 6/11/93
     34  */
     35 #include <sys/param.h>
     36 #include <sys/buf.h>
     37 #include <sys/proc.h>
     38 #include <sys/vnode.h>
     39 #include <sys/mount.h>
     40 #include <sys/resourcevar.h>
     41 #include <sys/trace.h>
     42 
     43 #include <miscfs/specfs/specdev.h>
     44 
     45 #include <ufs/ufs/quota.h>
     46 #include <ufs/ufs/inode.h>
     47 #include <ufs/ufs/ufsmount.h>
     48 
     49 #include <ufs/lfs/lfs.h>
     50 #include <ufs/lfs/lfs_extern.h>
     51 
     52 int
     53 lfs_balloc(vp, iosize, lbn, bpp)
     54 	struct vnode *vp;
     55 	u_long iosize;
     56 	daddr_t lbn;
     57 	struct buf **bpp;
     58 {
     59 	struct buf *ibp, *bp;
     60 	struct inode *ip;
     61 	struct lfs *fs;
     62 	struct indir indirs[NIADDR+2];
     63 	daddr_t daddr;
     64 	int bb, error, i, num;
     65 
     66 	ip = VTOI(vp);
     67 	fs = ip->i_lfs;
     68 
     69 	/*
     70 	 * Three cases: it's a block beyond the end of file, it's a block in
     71 	 * the file that may or may not have been assigned a disk address or
     72 	 * we're writing an entire block.  Note, if the daddr is unassigned,
     73 	 * the block might still have existed in the cache (if it was read
     74 	 * or written earlier).  If it did, make sure we don't count it as a
     75 	 * new block or zero out its contents.  If it did not, make sure
     76 	 * we allocate any necessary indirect blocks.
     77 	 */
     78 
     79 	*bpp = NULL;
     80 	if (error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL ))
     81 		return (error);
     82 
     83 	*bpp = bp = getblk(vp, lbn, fs->lfs_bsize, 0, 0);
     84 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
     85 	if (daddr == UNASSIGNED)
     86 		/* May need to allocate indirect blocks */
     87 		for (i = 1; i < num; ++i)
     88 			if (!indirs[i].in_exists) {
     89 				ibp =
     90 				    getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
     91 					0, 0);
     92 				if (!(ibp->b_flags & (B_DONE | B_DELWRI))) {
     93 					if (!ISSPACE(fs, bb, curproc->p_ucred)){
     94 						ibp->b_flags |= B_INVAL;
     95 						brelse(ibp);
     96 						error = ENOSPC;
     97 					} else {
     98 						ip->i_blocks += bb;
     99 						ip->i_lfs->lfs_bfree -= bb;
    100 						clrbuf(ibp);
    101 						error = VOP_BWRITE(ibp);
    102 					}
    103 				} else
    104 					panic ("Indirect block should not exist");
    105 			}
    106 	if (error) {
    107 		if (bp)
    108 			brelse(bp);
    109 		return(error);
    110 	}
    111 
    112 
    113 	/* Now, we may need to allocate the data block */
    114 	if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
    115 		if (daddr == UNASSIGNED)
    116 			if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    117 				bp->b_flags |= B_INVAL;
    118 				brelse(bp);
    119 				return(ENOSPC);
    120 			} else {
    121 				ip->i_blocks += bb;
    122 				ip->i_lfs->lfs_bfree -= bb;
    123 				if (iosize != fs->lfs_bsize)
    124 					clrbuf(bp);
    125 			}
    126 		else if (iosize == fs->lfs_bsize)
    127 			bp->b_blkno = daddr;		/* Skip the I/O */
    128 		else  {
    129 			bp->b_blkno = daddr;
    130 			bp->b_flags |= B_READ;
    131 			VOP_STRATEGY(bp);
    132 			return(biowait(bp));
    133 		}
    134 	}
    135 	return (error);
    136 }
    137