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