Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_balloc.c revision 1.3
      1 /*	$NetBSD: ext2fs_balloc.c,v 1.3 1998/03/01 02:23:45 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Manuel Bouyer.
      5  * Copyright (c) 1982, 1986, 1989, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
     37  * Modified for ext2fs by Manuel Bouyer.
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/buf.h>
     43 #include <sys/proc.h>
     44 #include <sys/file.h>
     45 #include <sys/vnode.h>
     46 
     47 #include <vm/vm.h>
     48 
     49 #include <ufs/ufs/quota.h>
     50 #include <ufs/ufs/inode.h>
     51 #include <ufs/ufs/ufs_extern.h>
     52 
     53 #include <ufs/ext2fs/ext2fs.h>
     54 #include <ufs/ext2fs/ext2fs_extern.h>
     55 
     56 /*
     57  * Balloc defines the structure of file system storage
     58  * by allocating the physical blocks on a device given
     59  * the inode and the logical block number in a file.
     60  */
     61 int
     62 ext2fs_balloc(ip, bn, size, cred, bpp, flags)
     63 	register struct inode *ip;
     64 	register ufs_daddr_t bn;
     65 	int size;
     66 	struct ucred *cred;
     67 	struct buf **bpp;
     68 	int flags;
     69 {
     70 	register struct m_ext2fs *fs;
     71 	register ufs_daddr_t nb;
     72 	struct buf *bp, *nbp;
     73 	struct vnode *vp = ITOV(ip);
     74 	struct indir indirs[NIADDR + 2];
     75 	ufs_daddr_t newb, lbn, *bap, pref;
     76 	int num, i, error;
     77 	u_int deallocated;
     78 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
     79 
     80 	*bpp = NULL;
     81 	if (bn < 0)
     82 		return (EFBIG);
     83 	fs = ip->i_e2fs;
     84 	lbn = bn;
     85 
     86 	/*
     87 	 * The first NDADDR blocks are direct blocks
     88 	 */
     89 	if (bn < NDADDR) {
     90 		nb = fs2h32(ip->i_e2fs_blocks[bn]);
     91 		if (nb != 0) {
     92 			error = bread(vp, bn, fs->e2fs_bsize, NOCRED, &bp);
     93 			if (error) {
     94 				brelse(bp);
     95 				return (error);
     96 			}
     97 			*bpp = bp;
     98 			return (0);
     99 		} else {
    100 			error = ext2fs_alloc(ip, bn,
    101 				ext2fs_blkpref(ip, bn, (int)bn, &ip->i_e2fs_blocks[0]),
    102 				cred, &newb);
    103 			if (error)
    104 				return (error);
    105 			ip->i_e2fs_last_lblk = lbn;
    106 			ip->i_e2fs_last_blk = newb;
    107 			bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
    108 			bp->b_blkno = fsbtodb(fs, newb);
    109 			if (flags & B_CLRBUF)
    110 				clrbuf(bp);
    111 		}
    112 		ip->i_e2fs_blocks[bn] = h2fs32(dbtofsb(fs, bp->b_blkno));
    113 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    114 		*bpp = bp;
    115 		return (0);
    116 	}
    117 	/*
    118 	 * Determine the number of levels of indirection.
    119 	 */
    120 	pref = 0;
    121 	if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
    122 		return(error);
    123 #ifdef DIAGNOSTIC
    124 	if (num < 1)
    125 		panic ("ext2fs_balloc: ufs_getlbns returned indirect block\n");
    126 #endif
    127 	/*
    128 	 * Fetch the first indirect block allocating if necessary.
    129 	 */
    130 	--num;
    131 	nb = fs2h32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
    132 	allocib = NULL;
    133 	allocblk = allociblk;
    134 	if (nb == 0) {
    135 		pref = ext2fs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    136 			error = ext2fs_alloc(ip, lbn, pref,
    137 				  cred, &newb);
    138 		if (error)
    139 			return (error);
    140 		nb = newb;
    141 		*allocblk++ = nb;
    142 		ip->i_e2fs_last_blk = newb;
    143 		bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
    144 		bp->b_blkno = fsbtodb(fs, newb);
    145 		clrbuf(bp);
    146 		/*
    147 		 * Write synchronously so that indirect blocks
    148 		 * never point at garbage.
    149 		 */
    150 		if ((error = bwrite(bp)) != 0)
    151 			goto fail;
    152 		allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
    153 		*allocib = h2fs32(newb);
    154 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    155 	}
    156 	/*
    157 	 * Fetch through the indirect blocks, allocating as necessary.
    158 	 */
    159 	for (i = 1;;) {
    160 		error = bread(vp,
    161 			indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
    162 		if (error) {
    163 			brelse(bp);
    164 			goto fail;
    165 		}
    166 		bap = (ufs_daddr_t *)bp->b_data;
    167 		nb = fs2h32(bap[indirs[i].in_off]);
    168 		if (i == num)
    169 			break;
    170 		i += 1;
    171 		if (nb != 0) {
    172 			brelse(bp);
    173 			continue;
    174 		}
    175 		pref = ext2fs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    176 		error = ext2fs_alloc(ip, lbn, pref, cred,
    177 				  &newb);
    178 		if (error) {
    179 			brelse(bp);
    180 			goto fail;
    181 		}
    182 		nb = newb;
    183 		*allocblk++ = nb;
    184 		ip->i_e2fs_last_blk = newb;
    185 		nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
    186 		nbp->b_blkno = fsbtodb(fs, nb);
    187 		clrbuf(nbp);
    188 		/*
    189 		 * Write synchronously so that indirect blocks
    190 		 * never point at garbage.
    191 		 */
    192 		if ((error = bwrite(nbp)) != 0) {
    193 			brelse(bp);
    194 			goto fail;
    195 		}
    196 		bap[indirs[i - 1].in_off] = h2fs32(nb);
    197 		/*
    198 		 * If required, write synchronously, otherwise use
    199 		 * delayed write.
    200 		 */
    201 		if (flags & B_SYNC) {
    202 			bwrite(bp);
    203 		} else {
    204 			bdwrite(bp);
    205 		}
    206 	}
    207 	/*
    208 	 * Get the data block, allocating if necessary.
    209 	 */
    210 	if (nb == 0) {
    211 		pref = ext2fs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
    212 		error = ext2fs_alloc(ip, lbn, pref, cred,
    213 				  &newb);
    214 		if (error) {
    215 			brelse(bp);
    216 			goto fail;
    217 		}
    218 		nb = newb;
    219 		*allocblk++ = nb;
    220 		ip->i_e2fs_last_lblk = lbn;
    221 		ip->i_e2fs_last_blk = newb;
    222 		nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
    223 		nbp->b_blkno = fsbtodb(fs, nb);
    224 		if (flags & B_CLRBUF)
    225 			clrbuf(nbp);
    226 		bap[indirs[i].in_off] = h2fs32(nb);
    227 		/*
    228 		 * If required, write synchronously, otherwise use
    229 		 * delayed write.
    230 		 */
    231 		if (flags & B_SYNC) {
    232 			bwrite(bp);
    233 		} else {
    234 			bdwrite(bp);
    235 		}
    236 		*bpp = nbp;
    237 		return (0);
    238 	}
    239 	brelse(bp);
    240 	if (flags & B_CLRBUF) {
    241 		error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
    242 		if (error) {
    243 			brelse(nbp);
    244 			return (error);
    245 		}
    246 	} else {
    247 		nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
    248 		nbp->b_blkno = fsbtodb(fs, nb);
    249 	}
    250 	*bpp = nbp;
    251 	return (0);
    252 fail:
    253 	/*
    254 	 * If we have failed part way through block allocation, we
    255 	 * have to deallocate any indirect blocks that we have allocated.
    256 	 */
    257 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    258 		ext2fs_blkfree(ip, *blkp);
    259 		deallocated += fs->e2fs_bsize;
    260 	}
    261 	if (allocib != NULL)
    262 		*allocib = 0;
    263 	if (deallocated) {
    264 		ip->i_e2fs_nblock -= btodb(deallocated);
    265 		ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
    266 	}
    267 	return error;
    268 }
    269