Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.15
      1  1.15  christos /*	$NetBSD: ffs_balloc.c,v 1.15 2013/01/26 00:19:39 christos Exp $	*/
      2   1.1     lukem /* From NetBSD: ffs_balloc.c,v 1.25 2001/08/08 08:36:36 lukem Exp */
      3   1.1     lukem 
      4   1.1     lukem /*
      5   1.1     lukem  * Copyright (c) 1982, 1986, 1989, 1993
      6   1.1     lukem  *	The Regents of the University of California.  All rights reserved.
      7   1.1     lukem  *
      8   1.1     lukem  * Redistribution and use in source and binary forms, with or without
      9   1.1     lukem  * modification, are permitted provided that the following conditions
     10   1.1     lukem  * are met:
     11   1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     12   1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     13   1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     15   1.1     lukem  *    documentation and/or other materials provided with the distribution.
     16  1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     17   1.1     lukem  *    may be used to endorse or promote products derived from this software
     18   1.1     lukem  *    without specific prior written permission.
     19   1.1     lukem  *
     20   1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21   1.1     lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1     lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24   1.1     lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1     lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1     lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1     lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1     lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1     lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1     lukem  * SUCH DAMAGE.
     31   1.1     lukem  *
     32   1.1     lukem  *	@(#)ffs_balloc.c	8.8 (Berkeley) 6/16/95
     33   1.1     lukem  */
     34   1.2     lukem 
     35  1.13       jmc #if HAVE_NBTOOL_CONFIG_H
     36  1.13       jmc #include "nbtool_config.h"
     37  1.13       jmc #endif
     38  1.13       jmc 
     39   1.2     lukem #include <sys/cdefs.h>
     40   1.9        tv #if defined(__RCSID) && !defined(__lint)
     41  1.15  christos __RCSID("$NetBSD: ffs_balloc.c,v 1.15 2013/01/26 00:19:39 christos Exp $");
     42   1.2     lukem #endif	/* !__lint */
     43   1.1     lukem 
     44   1.1     lukem #include <sys/param.h>
     45   1.1     lukem #include <sys/time.h>
     46   1.1     lukem 
     47   1.1     lukem #include <assert.h>
     48   1.1     lukem #include <errno.h>
     49   1.1     lukem #include <stdio.h>
     50   1.4   thorpej #include <stdlib.h>
     51   1.1     lukem #include <string.h>
     52   1.5     lukem 
     53   1.5     lukem #include "makefs.h"
     54   1.1     lukem 
     55   1.8     lukem #include <ufs/ufs/dinode.h>
     56   1.6     lukem #include <ufs/ufs/ufs_bswap.h>
     57   1.6     lukem #include <ufs/ffs/fs.h>
     58   1.1     lukem 
     59   1.1     lukem #include "ffs/buf.h"
     60   1.7     lukem #include "ffs/ufs_inode.h"
     61   1.1     lukem #include "ffs/ffs_extern.h"
     62   1.1     lukem 
     63  1.11      fvdl static int ffs_balloc_ufs1(struct inode *, off_t, int, struct buf **);
     64  1.11      fvdl static int ffs_balloc_ufs2(struct inode *, off_t, int, struct buf **);
     65  1.11      fvdl 
     66   1.1     lukem /*
     67   1.1     lukem  * Balloc defines the structure of file system storage
     68   1.1     lukem  * by allocating the physical blocks on a device given
     69   1.1     lukem  * the inode and the logical block number in a file.
     70   1.1     lukem  *
     71   1.1     lukem  * Assume: flags == B_SYNC | B_CLRBUF
     72   1.1     lukem  */
     73  1.11      fvdl 
     74   1.1     lukem int
     75   1.1     lukem ffs_balloc(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
     76   1.1     lukem {
     77  1.11      fvdl 	if (ip->i_fs->fs_magic == FS_UFS2_MAGIC)
     78  1.11      fvdl 		return ffs_balloc_ufs2(ip, offset, bufsize, bpp);
     79  1.11      fvdl 	else
     80  1.11      fvdl 		return ffs_balloc_ufs1(ip, offset, bufsize, bpp);
     81  1.11      fvdl }
     82  1.11      fvdl 
     83  1.11      fvdl static int
     84  1.11      fvdl ffs_balloc_ufs1(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
     85  1.11      fvdl {
     86  1.11      fvdl 	daddr_t lbn, lastlbn;
     87   1.1     lukem 	int size;
     88  1.11      fvdl 	int32_t nb;
     89   1.1     lukem 	struct buf *bp, *nbp;
     90   1.1     lukem 	struct fs *fs = ip->i_fs;
     91  1.14  dholland 	struct indir indirs[UFS_NIADDR + 2];
     92  1.10      fvdl 	daddr_t newb, pref;
     93  1.11      fvdl 	int32_t *bap;
     94   1.1     lukem 	int osize, nsize, num, i, error;
     95  1.14  dholland 	int32_t *allocblk, allociblk[UFS_NIADDR + 1];
     96  1.11      fvdl 	int32_t *allocib;
     97   1.1     lukem 	const int needswap = UFS_FSNEEDSWAP(fs);
     98  1.15  christos 	struct vnode vp = { ip->i_fd, ip->i_fs, NULL, 0 };
     99   1.1     lukem 
    100   1.1     lukem 	lbn = lblkno(fs, offset);
    101   1.1     lukem 	size = blkoff(fs, offset) + bufsize;
    102   1.1     lukem 	if (bpp != NULL) {
    103   1.1     lukem 		*bpp = NULL;
    104   1.1     lukem 	}
    105   1.1     lukem 
    106   1.1     lukem 	assert(size <= fs->fs_bsize);
    107   1.1     lukem 	if (lbn < 0)
    108   1.1     lukem 		return (EFBIG);
    109   1.1     lukem 
    110   1.1     lukem 	/*
    111   1.1     lukem 	 * If the next write will extend the file into a new block,
    112   1.1     lukem 	 * and the file is currently composed of a fragment
    113   1.1     lukem 	 * this fragment has to be extended to be a full block.
    114   1.1     lukem 	 */
    115   1.1     lukem 
    116  1.11      fvdl 	lastlbn = lblkno(fs, ip->i_ffs1_size);
    117  1.14  dholland 	if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
    118  1.11      fvdl 		nb = lastlbn;
    119   1.1     lukem 		osize = blksize(fs, ip, nb);
    120   1.1     lukem 		if (osize < fs->fs_bsize && osize > 0) {
    121   1.1     lukem 			warnx("need to ffs_realloccg; not supported!");
    122   1.1     lukem 			abort();
    123   1.1     lukem 		}
    124   1.1     lukem 	}
    125   1.1     lukem 
    126   1.1     lukem 	/*
    127  1.14  dholland 	 * The first UFS_NDADDR blocks are direct blocks
    128   1.1     lukem 	 */
    129   1.1     lukem 
    130  1.14  dholland 	if (lbn < UFS_NDADDR) {
    131  1.11      fvdl 		nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
    132  1.11      fvdl 		if (nb != 0 && ip->i_ffs1_size >= lblktosize(fs, lbn + 1)) {
    133   1.1     lukem 
    134   1.1     lukem 			/*
    135   1.1     lukem 			 * The block is an already-allocated direct block
    136   1.1     lukem 			 * and the file already extends past this block,
    137   1.1     lukem 			 * thus this must be a whole block.
    138   1.1     lukem 			 * Just read the block (if requested).
    139   1.1     lukem 			 */
    140   1.1     lukem 
    141   1.1     lukem 			if (bpp != NULL) {
    142  1.15  christos 				error = bread(&vp, lbn, fs->fs_bsize, NULL, 0,
    143  1.15  christos 				    bpp);
    144   1.1     lukem 				if (error) {
    145  1.15  christos 					brelse(*bpp, 0);
    146   1.1     lukem 					return (error);
    147   1.1     lukem 				}
    148   1.1     lukem 			}
    149   1.1     lukem 			return (0);
    150   1.1     lukem 		}
    151   1.1     lukem 		if (nb != 0) {
    152   1.1     lukem 
    153   1.1     lukem 			/*
    154   1.1     lukem 			 * Consider need to reallocate a fragment.
    155   1.1     lukem 			 */
    156   1.1     lukem 
    157  1.11      fvdl 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size));
    158   1.1     lukem 			nsize = fragroundup(fs, size);
    159   1.1     lukem 			if (nsize <= osize) {
    160   1.1     lukem 
    161   1.1     lukem 				/*
    162   1.1     lukem 				 * The existing block is already
    163   1.1     lukem 				 * at least as big as we want.
    164   1.1     lukem 				 * Just read the block (if requested).
    165   1.1     lukem 				 */
    166   1.1     lukem 
    167   1.1     lukem 				if (bpp != NULL) {
    168  1.15  christos 					error = bread(&vp, lbn, osize, NULL, 0,
    169  1.15  christos 					    bpp);
    170   1.1     lukem 					if (error) {
    171  1.15  christos 						brelse(*bpp, 0);
    172   1.1     lukem 						return (error);
    173   1.1     lukem 					}
    174   1.1     lukem 				}
    175   1.1     lukem 				return 0;
    176   1.1     lukem 			} else {
    177   1.1     lukem 				warnx("need to ffs_realloccg; not supported!");
    178   1.1     lukem 				abort();
    179   1.1     lukem 			}
    180   1.1     lukem 		} else {
    181   1.1     lukem 
    182   1.1     lukem 			/*
    183   1.1     lukem 			 * the block was not previously allocated,
    184   1.1     lukem 			 * allocate a new block or fragment.
    185   1.1     lukem 			 */
    186   1.1     lukem 
    187  1.11      fvdl 			if (ip->i_ffs1_size < lblktosize(fs, lbn + 1))
    188   1.1     lukem 				nsize = fragroundup(fs, size);
    189   1.1     lukem 			else
    190   1.1     lukem 				nsize = fs->fs_bsize;
    191   1.1     lukem 			error = ffs_alloc(ip, lbn,
    192  1.11      fvdl 			    ffs_blkpref_ufs1(ip, lbn, (int)lbn,
    193  1.11      fvdl 				&ip->i_ffs1_db[0]),
    194   1.1     lukem 				nsize, &newb);
    195   1.1     lukem 			if (error)
    196   1.1     lukem 				return (error);
    197   1.1     lukem 			if (bpp != NULL) {
    198  1.15  christos 				bp = getblk(&vp, lbn, nsize, 0, 0);
    199   1.1     lukem 				bp->b_blkno = fsbtodb(fs, newb);
    200   1.1     lukem 				clrbuf(bp);
    201   1.1     lukem 				*bpp = bp;
    202   1.1     lukem 			}
    203   1.1     lukem 		}
    204  1.11      fvdl 		ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap);
    205   1.1     lukem 		return (0);
    206   1.1     lukem 	}
    207  1.11      fvdl 
    208   1.1     lukem 	/*
    209   1.1     lukem 	 * Determine the number of levels of indirection.
    210   1.1     lukem 	 */
    211  1.11      fvdl 
    212   1.1     lukem 	pref = 0;
    213   1.1     lukem 	if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
    214  1.11      fvdl 		return (error);
    215   1.1     lukem 
    216   1.1     lukem 	if (num < 1) {
    217   1.1     lukem 		warnx("ffs_balloc: ufs_getlbns returned indirect block");
    218   1.1     lukem 		abort();
    219   1.1     lukem 	}
    220  1.11      fvdl 
    221   1.1     lukem 	/*
    222   1.1     lukem 	 * Fetch the first indirect block allocating if necessary.
    223   1.1     lukem 	 */
    224  1.11      fvdl 
    225   1.1     lukem 	--num;
    226  1.11      fvdl 	nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
    227   1.1     lukem 	allocib = NULL;
    228   1.1     lukem 	allocblk = allociblk;
    229   1.1     lukem 	if (nb == 0) {
    230  1.11      fvdl 		pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
    231   1.1     lukem 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
    232   1.1     lukem 		if (error)
    233  1.11      fvdl 			return error;
    234   1.1     lukem 		nb = newb;
    235   1.1     lukem 		*allocblk++ = nb;
    236  1.15  christos 		bp = getblk(&vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    237   1.1     lukem 		bp->b_blkno = fsbtodb(fs, nb);
    238   1.1     lukem 		clrbuf(bp);
    239   1.1     lukem 		/*
    240   1.1     lukem 		 * Write synchronously so that indirect blocks
    241   1.1     lukem 		 * never point at garbage.
    242   1.1     lukem 		 */
    243   1.1     lukem 		if ((error = bwrite(bp)) != 0)
    244  1.11      fvdl 			return error;
    245  1.11      fvdl 		allocib = &ip->i_ffs1_ib[indirs[0].in_off];
    246  1.10      fvdl 		*allocib = ufs_rw32((int32_t)nb, needswap);
    247   1.1     lukem 	}
    248  1.11      fvdl 
    249   1.1     lukem 	/*
    250   1.1     lukem 	 * Fetch through the indirect blocks, allocating as necessary.
    251   1.1     lukem 	 */
    252  1.11      fvdl 
    253   1.1     lukem 	for (i = 1;;) {
    254  1.15  christos 		error = bread(&vp, indirs[i].in_lbn, fs->fs_bsize, NULL, 0,
    255  1.15  christos 		    &bp);
    256   1.1     lukem 		if (error) {
    257  1.15  christos 			brelse(bp, 0);
    258  1.11      fvdl 			return error;
    259   1.1     lukem 		}
    260  1.10      fvdl 		bap = (int32_t *)bp->b_data;
    261   1.1     lukem 		nb = ufs_rw32(bap[indirs[i].in_off], needswap);
    262   1.1     lukem 		if (i == num)
    263   1.1     lukem 			break;
    264   1.1     lukem 		i++;
    265   1.1     lukem 		if (nb != 0) {
    266  1.15  christos 			brelse(bp, 0);
    267   1.1     lukem 			continue;
    268   1.1     lukem 		}
    269   1.1     lukem 		if (pref == 0)
    270  1.11      fvdl 			pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
    271   1.1     lukem 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
    272   1.1     lukem 		if (error) {
    273  1.15  christos 			brelse(bp, 0);
    274  1.11      fvdl 			return error;
    275   1.1     lukem 		}
    276   1.1     lukem 		nb = newb;
    277   1.1     lukem 		*allocblk++ = nb;
    278  1.15  christos 		nbp = getblk(&vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    279   1.1     lukem 		nbp->b_blkno = fsbtodb(fs, nb);
    280   1.1     lukem 		clrbuf(nbp);
    281   1.1     lukem 		/*
    282   1.1     lukem 		 * Write synchronously so that indirect blocks
    283   1.1     lukem 		 * never point at garbage.
    284   1.1     lukem 		 */
    285  1.11      fvdl 
    286   1.1     lukem 		if ((error = bwrite(nbp)) != 0) {
    287  1.15  christos 			brelse(bp, 0);
    288  1.11      fvdl 			return error;
    289   1.1     lukem 		}
    290   1.1     lukem 		bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
    291  1.11      fvdl 
    292  1.11      fvdl 		bwrite(bp);
    293  1.11      fvdl 	}
    294  1.11      fvdl 
    295  1.11      fvdl 	/*
    296  1.11      fvdl 	 * Get the data block, allocating if necessary.
    297  1.11      fvdl 	 */
    298  1.11      fvdl 
    299  1.11      fvdl 	if (nb == 0) {
    300  1.11      fvdl 		pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
    301  1.11      fvdl 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
    302  1.11      fvdl 		if (error) {
    303  1.15  christos 			brelse(bp, 0);
    304  1.11      fvdl 			return error;
    305  1.11      fvdl 		}
    306  1.11      fvdl 		nb = newb;
    307  1.11      fvdl 		*allocblk++ = nb;
    308  1.11      fvdl 		if (bpp != NULL) {
    309  1.15  christos 			nbp = getblk(&vp, lbn, fs->fs_bsize, 0, 0);
    310  1.11      fvdl 			nbp->b_blkno = fsbtodb(fs, nb);
    311  1.11      fvdl 			clrbuf(nbp);
    312  1.11      fvdl 			*bpp = nbp;
    313  1.11      fvdl 		}
    314  1.11      fvdl 		bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
    315  1.11      fvdl 
    316   1.1     lukem 		/*
    317   1.1     lukem 		 * If required, write synchronously, otherwise use
    318   1.1     lukem 		 * delayed write.
    319   1.1     lukem 		 */
    320   1.1     lukem 		bwrite(bp);
    321  1.11      fvdl 		return (0);
    322  1.11      fvdl 	}
    323  1.15  christos 	brelse(bp, 0);
    324  1.11      fvdl 	if (bpp != NULL) {
    325  1.15  christos 		error = bread(&vp, lbn, (int)fs->fs_bsize, NULL, 0, &nbp);
    326  1.11      fvdl 		if (error) {
    327  1.15  christos 			brelse(nbp, 0);
    328  1.11      fvdl 			return error;
    329  1.11      fvdl 		}
    330  1.11      fvdl 		*bpp = nbp;
    331  1.11      fvdl 	}
    332  1.11      fvdl 	return (0);
    333  1.11      fvdl }
    334  1.11      fvdl 
    335  1.11      fvdl static int
    336  1.11      fvdl ffs_balloc_ufs2(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
    337  1.11      fvdl {
    338  1.11      fvdl 	daddr_t lbn, lastlbn;
    339  1.11      fvdl 	int size;
    340  1.11      fvdl 	struct buf *bp, *nbp;
    341  1.11      fvdl 	struct fs *fs = ip->i_fs;
    342  1.14  dholland 	struct indir indirs[UFS_NIADDR + 2];
    343  1.11      fvdl 	daddr_t newb, pref, nb;
    344  1.11      fvdl 	int64_t *bap;
    345  1.11      fvdl 	int osize, nsize, num, i, error;
    346  1.14  dholland 	int64_t *allocblk, allociblk[UFS_NIADDR + 1];
    347  1.11      fvdl 	int64_t *allocib;
    348  1.11      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
    349  1.15  christos 	struct vnode vp = { ip->i_fd, ip->i_fs, NULL, 0 };
    350  1.11      fvdl 
    351  1.11      fvdl 	lbn = lblkno(fs, offset);
    352  1.11      fvdl 	size = blkoff(fs, offset) + bufsize;
    353  1.11      fvdl 	if (bpp != NULL) {
    354  1.11      fvdl 		*bpp = NULL;
    355   1.1     lukem 	}
    356  1.11      fvdl 
    357  1.11      fvdl 	assert(size <= fs->fs_bsize);
    358  1.11      fvdl 	if (lbn < 0)
    359  1.11      fvdl 		return (EFBIG);
    360  1.11      fvdl 
    361  1.11      fvdl 	/*
    362  1.11      fvdl 	 * If the next write will extend the file into a new block,
    363  1.11      fvdl 	 * and the file is currently composed of a fragment
    364  1.11      fvdl 	 * this fragment has to be extended to be a full block.
    365  1.11      fvdl 	 */
    366  1.11      fvdl 
    367  1.11      fvdl 	lastlbn = lblkno(fs, ip->i_ffs2_size);
    368  1.14  dholland 	if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
    369  1.11      fvdl 		nb = lastlbn;
    370  1.11      fvdl 		osize = blksize(fs, ip, nb);
    371  1.11      fvdl 		if (osize < fs->fs_bsize && osize > 0) {
    372  1.11      fvdl 			warnx("need to ffs_realloccg; not supported!");
    373  1.11      fvdl 			abort();
    374  1.11      fvdl 		}
    375  1.11      fvdl 	}
    376  1.11      fvdl 
    377  1.11      fvdl 	/*
    378  1.14  dholland 	 * The first UFS_NDADDR blocks are direct blocks
    379  1.11      fvdl 	 */
    380  1.11      fvdl 
    381  1.14  dholland 	if (lbn < UFS_NDADDR) {
    382  1.11      fvdl 		nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
    383  1.11      fvdl 		if (nb != 0 && ip->i_ffs2_size >= lblktosize(fs, lbn + 1)) {
    384  1.11      fvdl 
    385  1.11      fvdl 			/*
    386  1.11      fvdl 			 * The block is an already-allocated direct block
    387  1.11      fvdl 			 * and the file already extends past this block,
    388  1.11      fvdl 			 * thus this must be a whole block.
    389  1.11      fvdl 			 * Just read the block (if requested).
    390  1.11      fvdl 			 */
    391  1.11      fvdl 
    392  1.11      fvdl 			if (bpp != NULL) {
    393  1.15  christos 				error = bread(&vp, lbn, fs->fs_bsize, NULL, 0,
    394  1.15  christos 				    bpp);
    395  1.11      fvdl 				if (error) {
    396  1.15  christos 					brelse(*bpp, 0);
    397  1.11      fvdl 					return (error);
    398  1.11      fvdl 				}
    399  1.11      fvdl 			}
    400  1.11      fvdl 			return (0);
    401  1.11      fvdl 		}
    402  1.11      fvdl 		if (nb != 0) {
    403  1.11      fvdl 
    404  1.11      fvdl 			/*
    405  1.11      fvdl 			 * Consider need to reallocate a fragment.
    406  1.11      fvdl 			 */
    407  1.11      fvdl 
    408  1.11      fvdl 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size));
    409  1.11      fvdl 			nsize = fragroundup(fs, size);
    410  1.11      fvdl 			if (nsize <= osize) {
    411  1.11      fvdl 
    412  1.11      fvdl 				/*
    413  1.11      fvdl 				 * The existing block is already
    414  1.11      fvdl 				 * at least as big as we want.
    415  1.11      fvdl 				 * Just read the block (if requested).
    416  1.11      fvdl 				 */
    417  1.11      fvdl 
    418  1.11      fvdl 				if (bpp != NULL) {
    419  1.15  christos 					error = bread(&vp, lbn, osize, NULL, 0,
    420  1.15  christos 					    bpp);
    421  1.11      fvdl 					if (error) {
    422  1.15  christos 						brelse(*bpp, 0);
    423  1.11      fvdl 						return (error);
    424  1.11      fvdl 					}
    425  1.11      fvdl 				}
    426  1.11      fvdl 				return 0;
    427  1.11      fvdl 			} else {
    428  1.11      fvdl 				warnx("need to ffs_realloccg; not supported!");
    429  1.11      fvdl 				abort();
    430  1.11      fvdl 			}
    431  1.11      fvdl 		} else {
    432  1.11      fvdl 
    433  1.11      fvdl 			/*
    434  1.11      fvdl 			 * the block was not previously allocated,
    435  1.11      fvdl 			 * allocate a new block or fragment.
    436  1.11      fvdl 			 */
    437  1.11      fvdl 
    438  1.11      fvdl 			if (ip->i_ffs2_size < lblktosize(fs, lbn + 1))
    439  1.11      fvdl 				nsize = fragroundup(fs, size);
    440  1.11      fvdl 			else
    441  1.11      fvdl 				nsize = fs->fs_bsize;
    442  1.11      fvdl 			error = ffs_alloc(ip, lbn,
    443  1.11      fvdl 			    ffs_blkpref_ufs2(ip, lbn, (int)lbn,
    444  1.11      fvdl 				&ip->i_ffs2_db[0]),
    445  1.11      fvdl 				nsize, &newb);
    446  1.11      fvdl 			if (error)
    447  1.11      fvdl 				return (error);
    448  1.11      fvdl 			if (bpp != NULL) {
    449  1.15  christos 				bp = getblk(&vp, lbn, nsize, 0, 0);
    450  1.11      fvdl 				bp->b_blkno = fsbtodb(fs, newb);
    451  1.11      fvdl 				clrbuf(bp);
    452  1.11      fvdl 				*bpp = bp;
    453  1.11      fvdl 			}
    454  1.11      fvdl 		}
    455  1.11      fvdl 		ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
    456  1.11      fvdl 		return (0);
    457  1.11      fvdl 	}
    458  1.11      fvdl 
    459  1.11      fvdl 	/*
    460  1.11      fvdl 	 * Determine the number of levels of indirection.
    461  1.11      fvdl 	 */
    462  1.11      fvdl 
    463  1.11      fvdl 	pref = 0;
    464  1.11      fvdl 	if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
    465  1.11      fvdl 		return (error);
    466  1.11      fvdl 
    467  1.11      fvdl 	if (num < 1) {
    468  1.11      fvdl 		warnx("ffs_balloc: ufs_getlbns returned indirect block");
    469  1.11      fvdl 		abort();
    470  1.11      fvdl 	}
    471  1.11      fvdl 
    472  1.11      fvdl 	/*
    473  1.11      fvdl 	 * Fetch the first indirect block allocating if necessary.
    474  1.11      fvdl 	 */
    475  1.11      fvdl 
    476  1.11      fvdl 	--num;
    477  1.11      fvdl 	nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
    478  1.11      fvdl 	allocib = NULL;
    479  1.11      fvdl 	allocblk = allociblk;
    480  1.11      fvdl 	if (nb == 0) {
    481  1.11      fvdl 		pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
    482  1.11      fvdl 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
    483  1.11      fvdl 		if (error)
    484  1.11      fvdl 			return error;
    485  1.11      fvdl 		nb = newb;
    486  1.11      fvdl 		*allocblk++ = nb;
    487  1.15  christos 		bp = getblk(&vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    488  1.11      fvdl 		bp->b_blkno = fsbtodb(fs, nb);
    489  1.11      fvdl 		clrbuf(bp);
    490  1.11      fvdl 		/*
    491  1.11      fvdl 		 * Write synchronously so that indirect blocks
    492  1.11      fvdl 		 * never point at garbage.
    493  1.11      fvdl 		 */
    494  1.11      fvdl 		if ((error = bwrite(bp)) != 0)
    495  1.11      fvdl 			return error;
    496  1.11      fvdl 		allocib = &ip->i_ffs2_ib[indirs[0].in_off];
    497  1.11      fvdl 		*allocib = ufs_rw64(nb, needswap);
    498  1.11      fvdl 	}
    499  1.11      fvdl 
    500  1.11      fvdl 	/*
    501  1.11      fvdl 	 * Fetch through the indirect blocks, allocating as necessary.
    502  1.11      fvdl 	 */
    503  1.11      fvdl 
    504  1.11      fvdl 	for (i = 1;;) {
    505  1.15  christos 		error = bread(&vp, indirs[i].in_lbn, fs->fs_bsize, NULL, 0,
    506  1.15  christos 		    &bp);
    507  1.11      fvdl 		if (error) {
    508  1.15  christos 			brelse(bp, 0);
    509  1.11      fvdl 			return error;
    510  1.11      fvdl 		}
    511  1.11      fvdl 		bap = (int64_t *)bp->b_data;
    512  1.11      fvdl 		nb = ufs_rw64(bap[indirs[i].in_off], needswap);
    513  1.11      fvdl 		if (i == num)
    514  1.11      fvdl 			break;
    515  1.11      fvdl 		i++;
    516  1.11      fvdl 		if (nb != 0) {
    517  1.15  christos 			brelse(bp, 0);
    518  1.11      fvdl 			continue;
    519  1.11      fvdl 		}
    520  1.11      fvdl 		if (pref == 0)
    521  1.11      fvdl 			pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
    522  1.11      fvdl 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
    523  1.11      fvdl 		if (error) {
    524  1.15  christos 			brelse(bp, 0);
    525  1.11      fvdl 			return error;
    526  1.11      fvdl 		}
    527  1.11      fvdl 		nb = newb;
    528  1.11      fvdl 		*allocblk++ = nb;
    529  1.15  christos 		nbp = getblk(&vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    530  1.11      fvdl 		nbp->b_blkno = fsbtodb(fs, nb);
    531  1.11      fvdl 		clrbuf(nbp);
    532  1.11      fvdl 		/*
    533  1.11      fvdl 		 * Write synchronously so that indirect blocks
    534  1.11      fvdl 		 * never point at garbage.
    535  1.11      fvdl 		 */
    536  1.11      fvdl 
    537  1.11      fvdl 		if ((error = bwrite(nbp)) != 0) {
    538  1.15  christos 			brelse(bp, 0);
    539  1.11      fvdl 			return error;
    540  1.11      fvdl 		}
    541  1.11      fvdl 		bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
    542  1.11      fvdl 
    543  1.11      fvdl 		bwrite(bp);
    544  1.11      fvdl 	}
    545  1.11      fvdl 
    546   1.1     lukem 	/*
    547   1.1     lukem 	 * Get the data block, allocating if necessary.
    548   1.1     lukem 	 */
    549  1.11      fvdl 
    550   1.1     lukem 	if (nb == 0) {
    551  1.11      fvdl 		pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
    552   1.1     lukem 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
    553   1.1     lukem 		if (error) {
    554  1.15  christos 			brelse(bp, 0);
    555  1.11      fvdl 			return error;
    556   1.1     lukem 		}
    557   1.1     lukem 		nb = newb;
    558   1.1     lukem 		*allocblk++ = nb;
    559   1.1     lukem 		if (bpp != NULL) {
    560  1.15  christos 			nbp = getblk(&vp, lbn, fs->fs_bsize, 0, 0);
    561   1.1     lukem 			nbp->b_blkno = fsbtodb(fs, nb);
    562   1.1     lukem 			clrbuf(nbp);
    563   1.1     lukem 			*bpp = nbp;
    564   1.1     lukem 		}
    565  1.11      fvdl 		bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
    566  1.11      fvdl 
    567   1.1     lukem 		/*
    568   1.1     lukem 		 * If required, write synchronously, otherwise use
    569   1.1     lukem 		 * delayed write.
    570   1.1     lukem 		 */
    571   1.1     lukem 		bwrite(bp);
    572   1.1     lukem 		return (0);
    573   1.1     lukem 	}
    574  1.15  christos 	brelse(bp, 0);
    575   1.1     lukem 	if (bpp != NULL) {
    576  1.15  christos 		error = bread(&vp, lbn, (int)fs->fs_bsize, NULL, 0, &nbp);
    577  1.11      fvdl 		if (error) {
    578  1.15  christos 			brelse(nbp, 0);
    579  1.11      fvdl 			return error;
    580  1.11      fvdl 		}
    581   1.1     lukem 		*bpp = nbp;
    582   1.1     lukem 	}
    583   1.1     lukem 	return (0);
    584   1.1     lukem }
    585