Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_balloc.c revision 1.5
      1 /*	$NetBSD: ext2fs_balloc.c,v 1.5 2000/05/28 08:44:32 mycroft 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 	struct inode *ip;
     64 	ufs_daddr_t bn;
     65 	int size;
     66 	struct ucred *cred;
     67 	struct buf **bpp;
     68 	int flags;
     69 {
     70 	struct m_ext2fs *fs;
     71 	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 	int unwindidx = -1;
     80 
     81 	*bpp = NULL;
     82 	if (bn < 0)
     83 		return (EFBIG);
     84 	fs = ip->i_e2fs;
     85 	lbn = bn;
     86 
     87 	/*
     88 	 * The first NDADDR blocks are direct blocks
     89 	 */
     90 	if (bn < NDADDR) {
     91 		nb = fs2h32(ip->i_e2fs_blocks[bn]);
     92 		if (nb != 0) {
     93 			error = bread(vp, bn, fs->e2fs_bsize, NOCRED, &bp);
     94 			if (error) {
     95 				brelse(bp);
     96 				return (error);
     97 			}
     98 			*bpp = bp;
     99 			return (0);
    100 		} else {
    101 			error = ext2fs_alloc(ip, bn,
    102 				ext2fs_blkpref(ip, bn, (int)bn, &ip->i_e2fs_blocks[0]),
    103 				cred, &newb);
    104 			if (error)
    105 				return (error);
    106 			ip->i_e2fs_last_lblk = lbn;
    107 			ip->i_e2fs_last_blk = newb;
    108 			bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
    109 			bp->b_blkno = fsbtodb(fs, newb);
    110 			if (flags & B_CLRBUF)
    111 				clrbuf(bp);
    112 		}
    113 		ip->i_e2fs_blocks[bn] = h2fs32(dbtofsb(fs, bp->b_blkno));
    114 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    115 		*bpp = bp;
    116 		return (0);
    117 	}
    118 	/*
    119 	 * Determine the number of levels of indirection.
    120 	 */
    121 	pref = 0;
    122 	if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
    123 		return(error);
    124 #ifdef DIAGNOSTIC
    125 	if (num < 1)
    126 		panic ("ext2fs_balloc: ufs_getlbns returned indirect block\n");
    127 #endif
    128 	/*
    129 	 * Fetch the first indirect block allocating if necessary.
    130 	 */
    131 	--num;
    132 	nb = fs2h32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
    133 	allocib = NULL;
    134 	allocblk = allociblk;
    135 	if (nb == 0) {
    136 		pref = ext2fs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    137 		error = ext2fs_alloc(ip, lbn, pref, 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 		unwindidx = 0;
    153 		allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
    154 		*allocib = h2fs32(newb);
    155 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    156 	}
    157 	/*
    158 	 * Fetch through the indirect blocks, allocating as necessary.
    159 	 */
    160 	for (i = 1;;) {
    161 		error = bread(vp,
    162 		    indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
    163 		if (error) {
    164 			brelse(bp);
    165 			goto fail;
    166 		}
    167 		bap = (ufs_daddr_t *)bp->b_data;
    168 		nb = fs2h32(bap[indirs[i].in_off]);
    169 		if (i == num)
    170 			break;
    171 		i++;
    172 		if (nb != 0) {
    173 			brelse(bp);
    174 			continue;
    175 		}
    176 		pref = ext2fs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    177 		error = ext2fs_alloc(ip, lbn, pref, cred, &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 		if (unwindidx < 0)
    197 			unwindidx = i - 1;
    198 		bap[indirs[i - 1].in_off] = h2fs32(nb);
    199 		/*
    200 		 * If required, write synchronously, otherwise use
    201 		 * delayed write.
    202 		 */
    203 		if (flags & B_SYNC) {
    204 			bwrite(bp);
    205 		} else {
    206 			bdwrite(bp);
    207 		}
    208 	}
    209 	/*
    210 	 * Get the data block, allocating if necessary.
    211 	 */
    212 	if (nb == 0) {
    213 		pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
    214 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
    215 		if (error) {
    216 			brelse(bp);
    217 			goto fail;
    218 		}
    219 		nb = newb;
    220 		*allocblk++ = nb;
    221 		ip->i_e2fs_last_lblk = lbn;
    222 		ip->i_e2fs_last_blk = newb;
    223 		nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
    224 		nbp->b_blkno = fsbtodb(fs, nb);
    225 		if (flags & B_CLRBUF)
    226 			clrbuf(nbp);
    227 		bap[indirs[num].in_off] = h2fs32(nb);
    228 		/*
    229 		 * If required, write synchronously, otherwise use
    230 		 * delayed write.
    231 		 */
    232 		if (flags & B_SYNC) {
    233 			bwrite(bp);
    234 		} else {
    235 			bdwrite(bp);
    236 		}
    237 		*bpp = nbp;
    238 		return (0);
    239 	}
    240 	brelse(bp);
    241 	if (flags & B_CLRBUF) {
    242 		error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
    243 		if (error) {
    244 			brelse(nbp);
    245 			goto fail;
    246 		}
    247 	} else {
    248 		nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
    249 		nbp->b_blkno = fsbtodb(fs, nb);
    250 	}
    251 	*bpp = nbp;
    252 	return (0);
    253 fail:
    254 	/*
    255 	 * If we have failed part way through block allocation, we
    256 	 * have to deallocate any indirect blocks that we have allocated.
    257 	 */
    258 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    259 		ext2fs_blkfree(ip, *blkp);
    260 		deallocated += fs->e2fs_bsize;
    261 	}
    262 	if (unwindidx >= 0) {
    263 		if (unwindidx == 0) {
    264 			*allocib = 0;
    265 		} else {
    266 			int r;
    267 
    268 			r = bread(vp, indirs[unwindidx].in_lbn,
    269 			    (int)fs->e2fs_bsize, NOCRED, &bp);
    270 			if (r) {
    271 				panic("Could not unwind indirect block, error %d", r);
    272 				brelse(bp);
    273 			} else {
    274 				bap = (ufs_daddr_t *)bp->b_data;
    275 				bap[indirs[unwindidx].in_off] = 0;
    276 				if (flags & B_SYNC)
    277 					bwrite(bp);
    278 				else
    279 					bdwrite(bp);
    280 			}
    281 		}
    282 		for (i = unwindidx + 1; i <= num; i++) {
    283 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
    284 			    0, 0);
    285 			bp->b_flags |= B_INVAL;
    286 			brelse(bp);
    287 		}
    288 	}
    289 	if (deallocated) {
    290 		ip->i_e2fs_nblock -= btodb(deallocated);
    291 		ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
    292 	}
    293 	return (error);
    294 }
    295