Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_balloc.c revision 1.28.8.1
      1 /*	$NetBSD: ext2fs_balloc.c,v 1.28.8.1 2006/05/24 10:59:24 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
     32  * Modified for ext2fs by Manuel Bouyer.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1997 Manuel Bouyer.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *	This product includes software developed by Manuel Bouyer.
     49  * 4. The name of the author may not be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62  *
     63  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
     64  * Modified for ext2fs by Manuel Bouyer.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_balloc.c,v 1.28.8.1 2006/05/24 10:59:24 yamt Exp $");
     69 
     70 #if defined(_KERNEL_OPT)
     71 #include "opt_uvmhist.h"
     72 #endif
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/buf.h>
     77 #include <sys/proc.h>
     78 #include <sys/file.h>
     79 #include <sys/vnode.h>
     80 #include <sys/mount.h>
     81 #include <sys/kauth.h>
     82 
     83 #include <uvm/uvm.h>
     84 
     85 #include <ufs/ufs/inode.h>
     86 #include <ufs/ufs/ufs_extern.h>
     87 
     88 #include <ufs/ext2fs/ext2fs.h>
     89 #include <ufs/ext2fs/ext2fs_extern.h>
     90 
     91 /*
     92  * Balloc defines the structure of file system storage
     93  * by allocating the physical blocks on a device given
     94  * the inode and the logical block number in a file.
     95  */
     96 int
     97 ext2fs_balloc(struct inode *ip, daddr_t bn, int size, kauth_cred_t cred,
     98 		struct buf **bpp, int flags)
     99 {
    100 	struct m_ext2fs *fs;
    101 	daddr_t nb;
    102 	struct buf *bp, *nbp;
    103 	struct vnode *vp = ITOV(ip);
    104 	struct indir indirs[NIADDR + 2];
    105 	daddr_t newb, lbn, pref;
    106 	int32_t *bap;	/* XXX ondisk32 */
    107 	int num, i, error;
    108 	u_int deallocated;
    109 	daddr_t *blkp, *allocblk, allociblk[NIADDR + 1];
    110 	int32_t *allocib;	/* XXX ondisk32 */
    111 	int unwindidx = -1;
    112 	UVMHIST_FUNC("ext2fs_balloc"); UVMHIST_CALLED(ubchist);
    113 
    114 	UVMHIST_LOG(ubchist, "bn 0x%x", bn,0,0,0);
    115 
    116 	if (bpp != NULL) {
    117 		*bpp = NULL;
    118 	}
    119 	if (bn < 0)
    120 		return (EFBIG);
    121 	fs = ip->i_e2fs;
    122 	lbn = bn;
    123 
    124 	/*
    125 	 * The first NDADDR blocks are direct blocks
    126 	 */
    127 	if (bn < NDADDR) {
    128 		/* XXX ondisk32 */
    129 		nb = fs2h32(ip->i_e2fs_blocks[bn]);
    130 		if (nb != 0) {
    131 
    132 			/*
    133 			 * the block is already allocated, just read it.
    134 			 */
    135 
    136 			if (bpp != NULL) {
    137 				error = bread(vp, bn, fs->e2fs_bsize, NOCRED,
    138 					      &bp);
    139 				if (error) {
    140 					brelse(bp);
    141 					return (error);
    142 				}
    143 				*bpp = bp;
    144 			}
    145 			return (0);
    146 		}
    147 
    148 		/*
    149 		 * allocate a new direct block.
    150 		 */
    151 
    152 		error = ext2fs_alloc(ip, bn,
    153 		    ext2fs_blkpref(ip, bn, bn, &ip->i_e2fs_blocks[0]),
    154 		    cred, &newb);
    155 		if (error)
    156 			return (error);
    157 		ip->i_e2fs_last_lblk = lbn;
    158 		ip->i_e2fs_last_blk = newb;
    159 		/* XXX ondisk32 */
    160 		ip->i_e2fs_blocks[bn] = h2fs32((int32_t)newb);
    161 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    162 		if (bpp != NULL) {
    163 			bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
    164 			bp->b_blkno = fsbtodb(fs, newb);
    165 			if (flags & B_CLRBUF)
    166 				clrbuf(bp);
    167 			*bpp = bp;
    168 		}
    169 		return (0);
    170 	}
    171 	/*
    172 	 * Determine the number of levels of indirection.
    173 	 */
    174 	pref = 0;
    175 	if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
    176 		return(error);
    177 #ifdef DIAGNOSTIC
    178 	if (num < 1)
    179 		panic ("ext2fs_balloc: ufs_getlbns returned indirect block\n");
    180 #endif
    181 	/*
    182 	 * Fetch the first indirect block allocating if necessary.
    183 	 */
    184 	--num;
    185 	/* XXX ondisk32 */
    186 	nb = fs2h32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
    187 	allocib = NULL;
    188 	allocblk = allociblk;
    189 	if (nb == 0) {
    190 		pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
    191 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
    192 		if (error)
    193 			return (error);
    194 		nb = newb;
    195 		*allocblk++ = nb;
    196 		ip->i_e2fs_last_blk = newb;
    197 		bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
    198 		bp->b_blkno = fsbtodb(fs, newb);
    199 		clrbuf(bp);
    200 		/*
    201 		 * Write synchronously so that indirect blocks
    202 		 * never point at garbage.
    203 		 */
    204 		if ((error = bwrite(bp)) != 0)
    205 			goto fail;
    206 		unwindidx = 0;
    207 		allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
    208 		/* XXX ondisk32 */
    209 		*allocib = h2fs32((int32_t)newb);
    210 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    211 	}
    212 	/*
    213 	 * Fetch through the indirect blocks, allocating as necessary.
    214 	 */
    215 	for (i = 1;;) {
    216 		error = bread(vp,
    217 		    indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
    218 		if (error) {
    219 			brelse(bp);
    220 			goto fail;
    221 		}
    222 		bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    223 		nb = fs2h32(bap[indirs[i].in_off]);
    224 		if (i == num)
    225 			break;
    226 		i++;
    227 		if (nb != 0) {
    228 			brelse(bp);
    229 			continue;
    230 		}
    231 		pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
    232 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
    233 		if (error) {
    234 			brelse(bp);
    235 			goto fail;
    236 		}
    237 		nb = newb;
    238 		*allocblk++ = nb;
    239 		ip->i_e2fs_last_blk = newb;
    240 		nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
    241 		nbp->b_blkno = fsbtodb(fs, nb);
    242 		clrbuf(nbp);
    243 		/*
    244 		 * Write synchronously so that indirect blocks
    245 		 * never point at garbage.
    246 		 */
    247 		if ((error = bwrite(nbp)) != 0) {
    248 			brelse(bp);
    249 			goto fail;
    250 		}
    251 		if (unwindidx < 0)
    252 			unwindidx = i - 1;
    253 		/* XXX ondisk32 */
    254 		bap[indirs[i - 1].in_off] = h2fs32((int32_t)nb);
    255 		/*
    256 		 * If required, write synchronously, otherwise use
    257 		 * delayed write.
    258 		 */
    259 		if (flags & B_SYNC) {
    260 			bwrite(bp);
    261 		} else {
    262 			bdwrite(bp);
    263 		}
    264 	}
    265 	/*
    266 	 * Get the data block, allocating if necessary.
    267 	 */
    268 	if (nb == 0) {
    269 		pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
    270 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
    271 		if (error) {
    272 			brelse(bp);
    273 			goto fail;
    274 		}
    275 		nb = newb;
    276 		*allocblk++ = nb;
    277 		ip->i_e2fs_last_lblk = lbn;
    278 		ip->i_e2fs_last_blk = newb;
    279 		/* XXX ondisk32 */
    280 		bap[indirs[num].in_off] = h2fs32((int32_t)nb);
    281 		/*
    282 		 * If required, write synchronously, otherwise use
    283 		 * delayed write.
    284 		 */
    285 		if (flags & B_SYNC) {
    286 			bwrite(bp);
    287 		} else {
    288 			bdwrite(bp);
    289 		}
    290 		if (bpp != NULL) {
    291 			nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
    292 			nbp->b_blkno = fsbtodb(fs, nb);
    293 			if (flags & B_CLRBUF)
    294 				clrbuf(nbp);
    295 			*bpp = nbp;
    296 		}
    297 		return (0);
    298 	}
    299 	brelse(bp);
    300 	if (bpp != NULL) {
    301 		if (flags & B_CLRBUF) {
    302 			error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED,
    303 				      &nbp);
    304 			if (error) {
    305 				brelse(nbp);
    306 				goto fail;
    307 			}
    308 		} else {
    309 			nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
    310 			nbp->b_blkno = fsbtodb(fs, nb);
    311 		}
    312 		*bpp = nbp;
    313 	}
    314 	return (0);
    315 fail:
    316 	/*
    317 	 * If we have failed part way through block allocation, we
    318 	 * have to deallocate any indirect blocks that we have allocated.
    319 	 */
    320 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    321 		ext2fs_blkfree(ip, *blkp);
    322 		deallocated += fs->e2fs_bsize;
    323 	}
    324 	if (unwindidx >= 0) {
    325 		if (unwindidx == 0) {
    326 			*allocib = 0;
    327 		} else {
    328 			int r;
    329 
    330 			r = bread(vp, indirs[unwindidx].in_lbn,
    331 			    (int)fs->e2fs_bsize, NOCRED, &bp);
    332 			if (r) {
    333 				panic("Could not unwind indirect block, error %d", r);
    334 				brelse(bp);
    335 			} else {
    336 				bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
    337 				bap[indirs[unwindidx].in_off] = 0;
    338 				if (flags & B_SYNC)
    339 					bwrite(bp);
    340 				else
    341 					bdwrite(bp);
    342 			}
    343 		}
    344 		for (i = unwindidx + 1; i <= num; i++) {
    345 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
    346 			    0, 0);
    347 			bp->b_flags |= B_INVAL;
    348 			brelse(bp);
    349 		}
    350 	}
    351 	if (deallocated) {
    352 		ip->i_e2fs_nblock -= btodb(deallocated);
    353 		ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
    354 	}
    355 	return error;
    356 }
    357 
    358 int
    359 ext2fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    360     kauth_cred_t cred)
    361 {
    362 	struct inode *ip = VTOI(vp);
    363 	struct m_ext2fs *fs = ip->i_e2fs;
    364 	int error, delta, bshift, bsize;
    365 	UVMHIST_FUNC("ext2fs_gop_alloc"); UVMHIST_CALLED(ubchist);
    366 
    367 	bshift = fs->e2fs_bshift;
    368 	bsize = 1 << bshift;
    369 
    370 	delta = off & (bsize - 1);
    371 	off -= delta;
    372 	len += delta;
    373 
    374 	while (len > 0) {
    375 		bsize = min(bsize, len);
    376 		UVMHIST_LOG(ubchist, "off 0x%x len 0x%x bsize 0x%x",
    377 			    off, len, bsize, 0);
    378 
    379 		error = ext2fs_balloc(ip, lblkno(fs, off), bsize, cred,
    380 		    NULL, flags);
    381 		if (error) {
    382 			UVMHIST_LOG(ubchist, "error %d", error, 0,0,0);
    383 			return error;
    384 		}
    385 
    386 		/*
    387 		 * increase file size now, ext2fs_balloc() requires that
    388 		 * EOF be up-to-date before each call.
    389 		 */
    390 
    391 		if (ext2fs_size(ip) < off + bsize) {
    392 			UVMHIST_LOG(ubchist, "old 0x%lx%8lx new 0x%lx%8lx",
    393 			    /* Note that arguments are always cast to u_long. */
    394 				    ext2fs_size(ip) >> 32,
    395 				    ext2fs_size(ip) & 0xffffffff,
    396 				    (off + bsize) >> 32,
    397 				    (off + bsize) & 0xffffffff);
    398 			error = ext2fs_setsize(ip, off + bsize);
    399 			if (error) {
    400 				UVMHIST_LOG(ubchist, "error %d", error, 0,0,0);
    401 				return error;
    402 			}
    403 		}
    404 
    405 		off += bsize;
    406 		len -= bsize;
    407 	}
    408 	return 0;
    409 }
    410