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