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